<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="140px" :rules="formRules">
                <el-form-item label="所在目录" prop="directory">
                    <el-input v-model="formData.directory" clearable placeholder="请输入所在目录" />
                </el-form-item>
                <el-form-item label="单位名称" prop="unit_name">
                    <el-input v-model="formData.unit_name" clearable placeholder="请输入单位名称" />
                </el-form-item>
                <el-form-item label="时间" prop="time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.time" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="请选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="所属专业" prop="major">
                    <el-input v-model="formData.major" clearable placeholder="请输入所属专业" />
                </el-form-item>
                <el-form-item label="类别" prop="category">
                    <el-input v-model="formData.category" clearable placeholder="请输入类别" />
                </el-form-item>
                <el-form-item label="审核内容" prop="review_content">
                    <el-input v-model="formData.review_content" clearable placeholder="请输入审核内容" />
                </el-form-item>
                <el-form-item label="审核意见" prop="review_comments">
                    <el-input v-model="formData.review_comments" clearable placeholder="请输入审核意见" />
                </el-form-item>
                <el-form-item label="审核人" prop="reviewer">
                    <el-input v-model="formData.reviewer" clearable placeholder="点击选择" readonly @click="userclick" />
                </el-form-item>
                <el-form-item label="施工单位申报值" prop="declared_value">
                    <el-input v-model="formData.declared_value" clearable placeholder="请输入施工单位申报值" type="number" />
                </el-form-item>
                <el-form-item label="咨询单位测算值" prop="calculated_value">
                    <el-input v-model="formData.calculated_value" clearable placeholder="请输入咨询单位测算值" type="number" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <div v-if="showDialog3">
                    <personnelselector ref="personnel" @confirm="submituser" type="1">
                    </personnelselector>
                </div>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectChangeVisaContentEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectChangeVisaContentAdd, apiProjectChangeVisaContentEdit, apiProjectChangeVisaContentDetail } from '@/api/project_change_visa_content'
import { timeFormat } from '@/utils/util'
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 showDialog3 = ref(false)
const personnel = ref<any>()

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目变更签证内容' : '新增项目变更签证内容'
})

// 表单数据
const formData = reactive({
    id: '',
    project_change_visa_dataid: '',
    directory: '',
    unit_name: '',
    time: '',
    major: '',
    category: '',
    review_content: '',
    review_comments: '',
    reviewer: '',
    reviewer_id: '',
    declared_value: '',
    calculated_value: '',
    remark: '',

})


const userclick = async (e: any) => {
    showDialog3.value = true
    await nextTick()
    personnel.value.open()
}

//确认
const submituser = (e: any) => {
    formData.reviewer = e.name
    formData.reviewer_id = e.id
    showDialog3.value = false
}

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

})


// 获取详情
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 apiProjectChangeVisaContentDetail({
        id: row.id
    })
    setFormData(data)
}


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