<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="请输入项目名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="性质" prop="nature">
                            <el-select class="flex-1" v-model="formData.nature" clearable placeholder="请选择性质">
                                <el-option v-for="(item, index) in dictData.cost_consultation_industry_nature"
                                    :key="index" :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="行业" prop="industry">
                            <el-select class="flex-1" v-model="formData.industry" clearable placeholder="请选择行业">
                                <el-option v-for="(item, index) in dictData.industry" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="建设单位" prop="build_unit">
                            <el-input v-model="formData.build_unit" clearable placeholder="请输入建设单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="建设区域" prop="build_area">
                            <el-select class="flex-1" v-model="formData.build_area" clearable placeholder="请选择建设区域">
                                <el-option v-for="(item, index) in dictData.const_area" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目地址" prop="address">
                            <el-input v-model="formData.address" clearable placeholder="请输入项目地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目级别" prop="project_level">
                            <el-select class="flex-1" v-model="formData.project_level" clearable placeholder="请选择项目级别">
                                <el-option v-for="(item, index) in dictData.supervision_project_level" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="总投资(万元)" prop="total_investment">
                            <el-input v-model="formData.total_investment" clearable placeholder="请输入总投资(万元)"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工程状态" prop="engineering_status">
                            <el-select class="flex-1" v-model="formData.engineering_status" clearable
                                placeholder="请选择工程状态">
                                <el-option v-for="(item, index) in dictData.engineering_status" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同服务内容" prop="contract_content">
                            <el-input v-model="formData.contract_content" clearable placeholder="请输入合同服务内容"
                                type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目概况" prop="project_overview">
                            <el-input v-model="formData.project_overview" clearable placeholder="请输入项目概况" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目要求" prop="project_requirements">
                            <el-input v-model="formData.project_requirements" clearable placeholder="请输入项目要求" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计划开工日期" prop="planned_start_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.planned_start_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="计划竣工日期" prop="planned_end_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.planned_end_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实际开工日期" prop="actual_start_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.actual_start_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实际竣工日期" prop="actual_end_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.actual_end_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实施部门" prop="implementation_department">
                            <el-input v-model="formData.implementation_department" clearable placeholder="请输入实施部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="监管部门" prop="supervision_department">
                            <el-input v-model="formData.supervision_department" clearable placeholder="请输入监管部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目经理" prop="project_manager">
                            <el-input v-model="formData.project_manager" clearable placeholder="请输入项目经理" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="立项日期" prop="initiation_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.initiation_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目负责人" prop="project_leader">
                            <el-input v-model="formData.project_leader" clearable placeholder="请输入项目负责人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目部" prop="project_department">
                            <el-input v-model="formData.project_department" clearable placeholder="请输入项目部" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="关联合同" prop="contract">
                            <el-input v-model="formData.contract" clearable placeholder="请输入关联合同" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方单位" prop="part_a_unit">
                            <el-input v-model="formData.part_a_unit" clearable placeholder="请输入甲方单位" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="创建人" prop="create_user">
                            <el-input v-model="formData.create_user" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="创建时间" prop="create_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.create_time" clearable
                                type="datetime" value-format="YYYY-MM-DD HH:mm:ss">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <uploadAnnex :formData="formData"></uploadAnnex>

                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="manageProjectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiManageProjectAdd, apiManageProjectEdit, apiManageProjectDetail } from '@/api/manage_project'
import type { PropType } from 'vue'
import { timeFormat } from '@/utils/util'
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore().userInfo;

// create_user: userStore.account,
//     create_time: timeFormat('', 'yyyy-mm-dd hh:MM:ss'),


defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')



// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目管理--项目信息' : '新增项目管理--项目信息'
})

// 表单数据
const formData = reactive({
    id: '',
    project_name: '',
    nature: '',
    industry: '',
    build_unit: '',
    build_area: '',
    address: '',
    project_level: '',
    total_investment: '',
    engineering_status: '',
    contract_content: '',
    project_overview: '',
    project_requirements: '',
    planned_start_date: '',
    planned_end_date: '',
    actual_start_date: '',
    actual_end_date: '',
    implementation_department: '',
    supervision_department: '',
    project_manager: '',
    initiation_date: '',
    project_leader: '',
    project_department: '',
    remark: '',
    contract: '',
    part_a_unit: '',
    annex: [],
    create_user: userStore.account,
    create_time: timeFormat('', 'yyyy-mm-dd hh:MM:ss'),
})


// 表单验证
const formRules = reactive<any>({
    project_name: [{
        required: true,
        message: '请输入项目名称',
        trigger: ['blur']
    }],
    project_code: [{
        required: true,
        message: '请输入项目编号',
        trigger: ['blur']
    }],
    nature: [{
        required: true,
        message: '请输入性质',
        trigger: ['blur']
    }],
    industry: [{
        required: true,
        message: '请输入行业',
        trigger: ['blur']
    }],
    build_unit: [{
        required: true,
        message: '请输入建设单位',
        trigger: ['blur']
    }],
    build_area: [{
        required: true,
        message: '请输入建设区域',
        trigger: ['blur']
    }],
    address: [{
        required: true,
        message: '请输入项目地址',
        trigger: ['blur']
    }],
    project_level: [{
        required: true,
        message: '请输入项目级别',
        trigger: ['blur']
    }],
    total_investment: [{
        required: true,
        message: '请输入总投资(万元)',
        trigger: ['blur']
    }],
    engineering_status: [{
        required: true,
        message: '请输入工程状态',
        trigger: ['blur']
    }],
    contract_content: [{
        required: true,
        message: '请输入合同服务内容',
        trigger: ['blur']
    }],
    project_overview: [{
        required: true,
        message: '请输入项目概况',
        trigger: ['blur']
    }],
    project_requirements: [{
        required: true,
        message: '请输入项目要求',
        trigger: ['blur']
    }],
    planned_start_date: [{
        required: true,
        message: '请输入计划开工日期',
        trigger: ['blur']
    }],
    planned_end_date: [{
        required: true,
        message: '请输入计划竣工日期',
        trigger: ['blur']
    }],
    actual_start_date: [{
        required: true,
        message: '请输入实际开工日期',
        trigger: ['blur']
    }],
    actual_end_date: [{
        required: true,
        message: '请输入实际竣工日期',
        trigger: ['blur']
    }],
    implementation_department: [{
        required: true,
        message: '请输入实施部门',
        trigger: ['blur']
    }],
    supervision_department: [{
        required: true,
        message: '请输入监管部门',
        trigger: ['blur']
    }],
    project_manager: [{
        required: true,
        message: '请输入项目经理',
        trigger: ['blur']
    }],
    initiation_date: [{
        required: true,
        message: '请输入立项日期',
        trigger: ['blur']
    }],
    project_leader: [{
        required: true,
        message: '请输入项目负责人',
        trigger: ['blur']
    }],
    project_department: [{
        required: true,
        message: '请输入项目部',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }],
    contract: [{
        required: true,
        message: '请输入关联合同',
        trigger: ['blur']
    }],
    part_a_unit: [{
        required: true,
        message: '请输入甲方单位',
        trigger: ['blur']
    }],
    create_user: [{
        required: true,
        message: '请输入创建人',
        trigger: ['blur']
    }],
    create_time: [{
        required: true,
        message: '请输入创建时间',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiManageProjectDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiManageProjectEdit(data)
        : await apiManageProjectAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
