<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="项目编号">
                            <el-input v-model="formData.project_code" clearable placeholder="系统自动生成" disabled />
                        </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.supervision_project_industry" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </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.supervision_project_nature" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="委托单位">
                            <el-input v-model="formData.company" 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="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目地址">
                            <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="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="立项日期">
                            <el-date-picker class="flex-1 !flex" v-model="formData.initiation_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择咨询结束日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="关联合同">
                            <el-input v-model="formData.contract" clearable placeholder="请输入关联合同" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同金额">
                            <el-input v-model="formData.contract_amount" clearable placeholder="请输入合同金额"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同服务内容">
                            <el-input v-model="formData.contract_content" clearable placeholder="请输入合同服务内容" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目规模及概况">
                            <el-input v-model="formData.project_overview" clearable placeholder="请输入项目规模及概况" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="咨询范围">
                            <el-input v-model="formData.consult_content" clearable placeholder="请输入咨询范围" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="咨询开始日期">
                            <el-date-picker class="flex-1 !flex" v-model="formData.consult_start_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择咨询开始日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="咨询结束日期">
                            <el-date-picker class="flex-1 !flex" v-model="formData.consult_end_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择咨询结束日期">
                            </el-date-picker>
                        </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="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="咨询类型" prop="consult_type">
                            <el-select class="flex-1" v-model="formData.consult_type" clearable placeholder="请选择建设区域">
                                <el-option v-for="(item, index) in dictData.consult_type" :key="index"
                                    :label="item.name" :value="item.value" />
                            </el-select>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系电话">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="委托联系人">
                            <el-input v-model="formData.consult_user" clearable placeholder="请输入委托联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="监管部门">
                            <el-input v-model="formData.supervision_department" clearable placeholder="请输入监管部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实施部门">
                            <el-input v-model="formData.implementation_department" clearable placeholder="请输入实施部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目部">
                            <el-input v-model="formData.project_department" clearable placeholder="请输入项目部" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目负责人员">
                            <el-input v-model="formData.project_manager" clearable placeholder="请输入项目负责人员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方单位">
                            <el-input v-model="formData.part_a_unit" clearable placeholder="请输入甲方单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目归档">
                            <el-checkbox-group v-model="formData.project_archiving">
                                <el-checkbox :label="item.value" :key="i"
                                    v-for="(item, i) in dictData.project_archiving">
                                    {{ item.name }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="归档目录">
                            <el-input v-model="formData.archiving_directory" clearable placeholder="请输入归档目录" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="创建人" prop="create_user">
                            <el-input v-model="formData.create_user" clearable placeholder="请输入创建人" />
                        </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" placeholder="选择创建时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                </el-row>

            </el-form>
        </popup>
    </div>
</template>

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

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: '',
    project_code: '',
    industry: '',
    nature: '',
    company: '',
    build_area: '',
    address: '',
    project_level: '',
    total_investment: '',
    initiation_date: '',
    contract: '',
    contract_amount: '',
    contract_content: '',
    project_overview: '',
    consult_content: '',
    remark: '',
    consult_start_date: '',
    consult_end_date: '',
    engineering_status: '',
    consult_type: '',
    telephone: '',
    consult_user: '',
    supervision_department: '',
    implementation_department: '',
    project_department: '',
    project_manager: '',
    part_a_unit: '',
    project_archiving: [],
    archiving_directory: '',
    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']
    }],
    industry: [{
        required: true,
        message: '请输入行业',
        trigger: ['blur']
    }],
    nature: [{
        required: true,
        message: '请输入性质',
        trigger: ['blur']
    }],
    company: [{
        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']
    }],
    initiation_date: [{
        required: true,
        message: '请输入立项日期',
        trigger: ['blur']
    }],
    contract: [{
        required: true,
        message: '请输入关联合同',
        trigger: ['blur']
    }],
    contract_amount: [{
        required: true,
        message: '请输入合同金额',
        trigger: ['blur']
    }],
    contract_content: [{
        required: true,
        message: '请输入合同服务内容',
        trigger: ['blur']
    }],
    project_overview: [{
        required: true,
        message: '请输入项目规模及概况',
        trigger: ['blur']
    }],
    consult_content: [{
        required: true,
        message: '请输入咨询范围',
        trigger: ['blur']
    }],
    remark: [{
        required: true,
        message: '请输入备注',
        trigger: ['blur']
    }],
    consult_start_date: [{
        required: true,
        message: '请输入咨询开始日期',
        trigger: ['blur']
    }],
    consult_end_date: [{
        required: true,
        message: '请输入咨询结束日期',
        trigger: ['blur']
    }],
    engineering_status: [{
        required: true,
        message: '请输入工程状态',
        trigger: ['blur']
    }],
    consult_type: [{
        required: true,
        message: '请输入咨询类型',
        trigger: ['blur']
    }],
    telephone: [{
        required: true,
        message: '请输入联系电话',
        trigger: ['blur']
    }],
    consult_user: [{
        required: true,
        message: '请输入委托联系人',
        trigger: ['blur']
    }],
    supervision_department: [{
        required: true,
        message: '请输入监管部门',
        trigger: ['blur']
    }],
    implementation_department: [{
        required: true,
        message: '请输入实施部门',
        trigger: ['blur']
    }],
    project_department: [{
        required: true,
        message: '请输入项目部',
        trigger: ['blur']
    }],
    project_manager: [{
        required: true,
        message: '请输入项目负责人员',
        trigger: ['blur']
    }],
    part_a_unit: [{
        required: true,
        message: '请输入甲方单位',
        trigger: ['blur']
    }],
    archiving_directory: [{
        required: true,
        message: '请输入归档目录',
        trigger: ['blur']
    }],
    create_user: [{
        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]
        }
    }

    let toStringList = ['industry', 'nature', 'build_area', 'project_level', 'engineering_status', 'consult_type']
    toStringList.forEach(item => {
        formData[item] = String(formData[item])
    })


}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiConsultProjectEdit(data)
        : await apiConsultProjectAdd(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>
