<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="所属单位" prop="company_name">
                            <el-input v-model="formData.company_name" clearable placeholder="请输入所属单位"
                                @click="showDialog1 = true" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="姓名" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入姓名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="身份证" prop="id_card">
                            <el-input v-model="formData.id_card" clearable placeholder="请输入身份证" />
                        </el-form-item>
                    </el-col>

                    <createUserLable :formData="formData" flag></createUserLable>
                    <el-col :span="8">
                        <el-form-item label="技术职称">
                            <el-input v-model="formData.technical_title" clearable placeholder="请输入技术职称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="岗位">
                            <el-input v-model="formData.job" clearable placeholder="请输入岗位" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="资质名称1" prop="qualification_name_one">
                            <el-input v-model="formData.qualification_name_one" clearable placeholder="请输入资质名称1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="认证机构1" prop="certification_body_one">
                            <el-input v-model="formData.certification_body_one" clearable placeholder="请输入认证机构1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质编号1" prop="qualification_number_one">
                            <el-input v-model="formData.qualification_number_one" clearable placeholder="请输入资质编号1"
                                v-type="'code'" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="发证日期1">
                            <el-date-picker class="flex-1 !flex" v-model="formData.get_date_one" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="有效日期1" prop="effective_date_one">
                            <el-date-picker class="flex-1 !flex" v-model="formData.effective_date_one" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质1状态" prop="qualification_one_status">
                            <el-select class="flex-1" v-model="formData.qualification_one_status" clearable
                                placeholder="请选择">
                                <el-option label="有效" :value="0" />
                                <el-option label="无效" :value="1" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="资质名称2" prop="qualification_name_two">
                            <el-input v-model="formData.qualification_name_two" clearable placeholder="请输入资质名称2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="认证机构2" prop="certification_body_two">
                            <el-input v-model="formData.certification_body_two" clearable placeholder="请输入认证机构2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质编号2" prop="qualification_number_two">
                            <el-input v-model="formData.qualification_number_two" clearable placeholder="请输入资质编号2"
                                v-type="'code'" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="发证日期2" prop="get_date_two">
                            <el-date-picker class="flex-1 !flex" v-model="formData.get_date_two" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="有效日期2" prop="effective_date_two">
                            <el-date-picker class="flex-1 !flex" v-model="formData.effective_date_two" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质2状态">
                            <el-select class="flex-1" v-model="formData.qualification_two_status" clearable
                                placeholder="请选择">
                                <el-option label="有效" :value="0" />
                                <el-option label="无效" :value="1" />
                            </el-select>
                        </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="附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>

                <div v-if="showDialog1">
                    <el-dialog v-model="showDialog1" title="选择单位" width="70%">
                        <dialogTable :config="supervision_participating_units_qualifications" @customEvent="customEvent1">
                        </dialogTable>
                    </el-dialog>
                </div>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionConstructionManagementPersonnelEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionSafetyManagementPersonnelAdd, apiSupervisionSafetyManagementPersonnelEdit, } from '@/api/supervision_safety_management_personnel'
import { supervision_participating_units_qualifications } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'
import { checkIdcard, checkPhone } from "@/utils/validate"


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 showDialog1 = ref(false)




// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--安全管理人员' : '新增工程监理--安全管理人员'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    name: '',
    unit_qualification_id: '',
    company_name: '',
    id_card: '',
    technical_title: '',
    job: '',
    qualification_name_one: '',
    certification_body_one: '',
    qualification_number_one: '',
    get_date_one: '',
    effective_date_one: '',
    qualification_one_status: '',
    qualification_name_two: '',
    certification_body_two: '',
    qualification_number_two: '',
    get_date_two: '',
    effective_date_two: '',
    qualification_two_status: '',
    remark: '',
    annex: [],
    create_user: "",
    create_time: "",

})

const customEvent1 = (e) => {
    formData.unit_qualification_id = e.id
    formData.company_name = e.company_name
    showDialog1.value = false
}


const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.effective_date_one) < new Date(formData.get_date_one)) {
        callback(new Error('有效日期不能早于发证时间'))
    } else {
        callback()
    }
}
const chekcDate1 = (rule: any, value: any, callback: any) => {
    if (new Date(formData.effective_date_two) < new Date(formData.get_date_two)) {
        callback(new Error('有效日期不能早于发证时间'))
    } else {
        callback()
    }
}




// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入姓名',
        trigger: ['blur']
    }],
    unit_qualification_id: [{
        required: true,
        message: '请输入所属单位',
        trigger: ['blur']
    }],
    id_card: [
        {
            required: true,
            message: '请输入身份证',
            trigger: ['blur']
        },
        {
            validator: checkIdcard,
            trigger: ['blur']
        }
    ],
    technical_title: [{
        required: true,
        message: '请输入技术职称',
        trigger: ['blur']
    }],
    job: [{
        required: true,
        message: '请输入岗位',
        trigger: ['blur']
    }],
    qualification_name_one: [{
        required: true,
        message: '请输入资质名称1',
        trigger: ['blur']
    }],
    certification_body_one: [{
        required: true,
        message: '请输入认证机构1',
        trigger: ['blur']
    }],
    qualification_number_one: [{
        required: true,
        message: '请输入资质编号1',
        trigger: ['blur']
    }],
    get_date_one: [{
        required: true,
        message: '请输入发证日期1',
        trigger: ['blur']
    }],
    effective_date_one: [{
        required: true,
        message: '请输入有效日期1',
        trigger: ['blur']
    },
    {
        validator: chekcDate,
        trigger: ['blur']
    }],
    qualification_one_status: [{
        required: true,
        message: '请输入资质1状态 0-有效 1-无效',
        trigger: ['blur']
    }],
    qualification_name_two: [{
        required: true,
        message: '请输入资质名称2',
        trigger: ['blur']
    }],
    certification_body_two: [{
        required: true,
        message: '请输入认证机构2',
        trigger: ['blur']
    }],
    qualification_number_two: [{
        required: true,
        message: '请输入资质编号2',
        trigger: ['blur']
    },
    {
        validator: chekcDate1,
        trigger: ['blur']
    }],
    get_date_two: [{
        required: true,
        message: '请输入发证日期2',
        trigger: ['blur']
    }],
    effective_date_two: [{
        required: true,
        message: '请输入有效日期2',
        trigger: ['blur']
    }],
    qualification_two_status: [{
        required: true,
        message: '请输入资质2状态 0-有效 1-无效',
        trigger: ['blur']
    }],
    remark: [{
        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]
        }
    }


}

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


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