<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="num">
                    <el-input v-model="formData.num" clearable placeholder="系统自动生成" disabled />
                </el-form-item>
                <el-form-item label="项目名称" prop="project">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="项目编号" prop="project_num">
                    <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
                </el-form-item>
                <el-form-item label="委托单位" prop="wt_unit">
                    <el-input v-model="formData.wt_unit" clearable placeholder="请输入委托单位" />
                </el-form-item>
                <el-form-item label="项目负责人" prop="xm_master">
                    <el-input v-model="formData.xm_master" clearable placeholder="请输入项目负责人" />
                </el-form-item>
                <el-form-item label="咨询类型" prop="zx_type">
                    <el-input v-model="formData.zx_type" clearable placeholder="请输入咨询类型" />
                </el-form-item>
                <el-form-item label="项目整改人" prop="zg_master">
                    <el-input v-model="formData.zg_master" clearable placeholder="点击选择"
                        @click="userclick('zg_master')" />
                </el-form-item>
                <el-form-item label="登记人" prop="dj_person">
                    <el-input v-model="formData.dj_person" clearable placeholder="请输入登记人" />
                </el-form-item>
                <el-form-item label="登记日期" prop="apptime">
                    <el-date-picker class="flex-1 !flex" v-model="formData.apptime" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="请选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="存在问题" prop="problem">
                    <el-input v-model="formData.problem" clearable placeholder="请输入存在问题" />
                </el-form-item>
                <el-form-item label="修改情况" prop="deal_desc">
                    <el-input v-model="formData.deal_desc" clearable placeholder="请输入修改情况" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入" />
                </el-form-item>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable :config="cost_project" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>
                <div v-if="showDialog3">
                    <personnelselector ref="personnel" @confirm="submituser" type="1">
                    </personnelselector>
                </div>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectRectificationEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectRectificationAdd, apiProjectRectificationEdit, } from '@/api/project_rectification'
import { cost_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 showDialog3 = ref(false)
const personnel = ref<any>()

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目整改' : '新增项目整改'
})

// 表单数据
const formData = reactive({
    id: '',
    dataid: '',
    num: '',
    project_id: '',
    project_name: '',
    project_num: '',
    wt_unit: '',
    xm_master: '',
    zx_type: '',
    zg_master: '',
    dj_person: '',
    apptime: '',
    problem: '',
    deal_desc: '',
    remark: '',
    annex: [],
    projectInfo: {}

})


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

})
const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    formData.project_num = e.project_num
    showDialog.value = false
}

const tableKey = ref('')
//打开弹窗
const userclick = async (key) => {
    tableKey.value = key
    showDialog3.value = true
    await nextTick()
    personnel.value.open()

}


//确认
const submituser = (e: any) => {
    formData[tableKey.value] = e.name
    showDialog3.value = false
}


// 获取详情
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]
        }
    }
    formData.project_id = formData.projectInfo.id
    formData.project_name = formData.projectInfo.project_name
    formData.project_num = formData.projectInfo.project_num

}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectRectificationEdit(data)
        : await apiProjectRectificationAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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



defineExpose({
    open,
    setFormData,
})
</script>
