<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">
                <el-form-item label="单位名称" prop="unit_name">
                    <el-input v-model="formData.unit_name" clearable placeholder="点击选择单位" readonly
                        @click="showDialog = 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="duties">
                    <el-input v-model="formData.duties" clearable placeholder="请输入职务" />
                </el-form-item>
                <el-form-item label="办公电话" prop="telephone">
                    <el-input v-model="formData.telephone" clearable placeholder="请输入办公电话" />
                </el-form-item>
                <el-form-item label="手机号码" prop="mobile">
                    <el-input v-model="formData.mobile" clearable placeholder="请输入手机号码" />
                </el-form-item>
                <el-form-item label="电子邮箱" prop="email">
                    <el-input v-model="formData.email" clearable placeholder="请输入电子邮箱" />
                </el-form-item>
                <el-form-item label="传真" prop="fax">
                    <el-input v-model="formData.fax" clearable placeholder="请输入传真" />
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择单位" width="70%">
                    <dialogTable :config="supervision_participating_units" @customEvent="customEvent"></dialogTable>
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionParticipatingUnitsContactsEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionParticipatingUnitsContactsAdd, apiSupervisionParticipatingUnitsContactsEdit, apiSupervisionParticipatingUnitsContactsDetail } from '@/api/supervision_participating_units_contacts'
import type { PropType } from 'vue'
import { supervision_participating_units } from "@/components/dialogTable/dialogTableConfig"

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: '',
    unit_id: '',
    unit_name: '',
    name: '',
    duties: '',
    telephone: '',
    mobile: '',
    email: '',
    fax: '',
})

const customEvent = (e) => {
    formData.unit_id = e.id
    formData.unit_name = e.unit_name
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    unit_id: [{
        required: true,
        message: '请输入单位id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入姓名',
        trigger: ['blur']
    }],
    duties: [{
        required: true,
        message: '请输入职务',
        trigger: ['blur']
    }],
    telephone: [{
        required: true,
        message: '请输入办公电话',
        trigger: ['blur']
    }],
    mobile: [{
        required: true,
        message: '请输入手机号码',
        trigger: ['blur']
    }],
    email: [{
        required: true,
        message: '请输入电子邮箱',
        trigger: ['blur']
    }],
    fax: [{
        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 apiSupervisionParticipatingUnitsContactsDetail({
        id: row.id
    })
    setFormData(data)
}


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