<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules" :disabled="mode == 'detail'">

                <el-form-item label="所属单位" prop="unit_qualification_id">
                    <el-input v-model="formData.company_name" clearable placeholder="请输入所属单位" readonly
                        @click="showDialog1 = true" />
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入姓名" />
                </el-form-item>

                <el-form-item label="工种" prop="work_type">
                    <el-input v-model="formData.work_type" clearable placeholder="请输入工种" />
                </el-form-item>
                <el-form-item label="身份证" prop="id_card">
                    <el-input v-model="formData.id_card" clearable placeholder="请输入身份证" />
                </el-form-item>
                <el-form-item label="资格有效期" prop="validity_period" label-width="100px">
                    <el-date-picker class="flex-1 !flex" v-model="formData.validity_period" clearable
                        value-format="YYYY-MM-DD " placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="进场时间" prop="enter_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.enter_time" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择时间" />
                </el-form-item>
                <el-form-item label="离场时间" prop="leave_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.leave_time" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择时间" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select class="flex-1" v-model="formData.status" clearable placeholder="请选择">
                        <el-option label="有效" :value="0" />
                        <el-option label="无效" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <uploadAnnex :formData="formData"></uploadAnnex>

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

<script lang="ts" setup name="supervisionSpecialOperationPersonnelEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionSpecialOperationPersonnelAdd, apiSupervisionSpecialOperationPersonnelEdit, } from '@/api/supervision_special_operation_personnel'
import { supervision_participating_units_qualifications } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'
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: '',
    name: '',
    unit_qualification_id: '',
    company_name: "",
    work_type: '',
    id_card: '',
    validity_period: '',
    enter_time: '',
    leave_time: '',
    status: '',
    remark: '',
    annex: '',
    create_user: "",
    create_time: "",
})


const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.leave_time) < new Date(formData.enter_time)) {
        callback(new Error('离场时间不能早于进场时间'))
    } else {
        callback()
    }
}


const chekcDate1 = (rule: any, value: any, callback: any) => {
    if (new Date(formData.enter_time) > new Date(formData.validity_period)) {
        callback(new Error('请在资格有效期内进场'))
    } else {
        callback()
    }
}



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


// 表单验证
const formRules = reactive<any>({

    name: [{
        required: true,
        message: '请输入姓名',
        trigger: ['blur']
    }],
    unit_qualification_id: [{
        required: true,
        message: '请输入所属单位',
        trigger: ['blur']
    }],
    work_type: [{
        required: true,
        message: '请输入工种',
        trigger: ['blur']
    }],
    id_card: [{
        required: true,
        message: '请输入身份证',
        trigger: ['blur']
    }],
    validity_period: [{
        required: true,
        message: '请输入资格有效期',
        trigger: ['blur']
    }],
    enter_time: [{
        required: true,
        message: '请输入进场时间',
        trigger: ['blur']
    },
    {
        validator: chekcDate1,
        trigger: ['blur']
    }],
    leave_time: [{
        required: true,
        message: '请输入离场时间',
        trigger: ['blur']
    },
    {
        validator: chekcDate,
        trigger: ['blur']
    }],
    status: [{
        required: true,
        message: '请选择状态',
        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 handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiSupervisionSpecialOperationPersonnelEdit(data)
        : await apiSupervisionSpecialOperationPersonnelAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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



defineExpose({
    open,
    setFormData,

})
</script>
