<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="120px" :rules="formRules">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" @click="showDialog = true"
                        readonly />
                </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="dept">
                    <el-input v-model="formData.dept" clearable placeholder="请输入部门" />
                </el-form-item>
                <el-form-item label="项目调入时间" prop="start_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.start_time" clearable value-format="YYYY-MM-DD "
                        placeholder="选择项目调入时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="项目调出时间" prop="end_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.end_time" clearable value-format="YYYY-MM-DD "
                        placeholder="选择项目调出时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="负责岗位" prop="job">
                    <el-input v-model="formData.job" clearable placeholder="请输入负责岗位" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable @customEvent="customEvent" :config="supervision_project" />
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionProjectPersonnelEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionProjectPersonnelAdd, apiSupervisionProjectPersonnelEdit, apiSupervisionProjectPersonnelDetail } from '@/api/supervision_project_personnel'
import { supervision_project } 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 showDialog = ref(false)


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

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    name: '',
    dept: '',
    start_time: '',
    end_time: '',
    job: '',
    remark: '',
})


const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.end_time) < new Date(formData.start_time)) {
        callback(new Error('项目调出时间不能早于项目调入时间'))
    } else {
        callback()
    }
}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入姓名',
        trigger: ['blur']
    }],
    start_time: [{
        required: true,
        message: '请输入项目调入时间',
        trigger: ['blur']
    }],
    end_time: [{
        required: true,
        message: '请输入项目调出时间',
        trigger: ['blur']
    },
    {
        validator: chekcDate,
        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 customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

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


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