<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="点击选择项目" 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="date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable value-format="YYYY-MM-DD"
                        placeholder="选择检查日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="检查人" prop="user">
                    <el-input v-model="formData.user" clearable placeholder="请输入检查人" />
                </el-form-item>
                <el-form-item label="问题名称" prop="problem_name">
                    <el-input v-model="formData.problem_name" clearable placeholder="请输入问题名称" />
                </el-form-item>
                <el-form-item label="问题类型" prop="problem_type">
                    <el-input v-model="formData.problem_type" clearable placeholder="请输入问题类型" />
                </el-form-item>
                <el-form-item label="问题说明" prop="problem_desc">
                    <el-input v-model="formData.problem_desc" clearable placeholder="请输入问题说明" />
                </el-form-item>
                <!-- <el-form-item label="整改回复人" prop="reply_user">
                    <el-input v-model="formData.reply_user" clearable placeholder="请输入整改回复人" />
                </el-form-item> -->
                <!-- <el-form-item label="整改回复时间" prop="reply_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.reply_date" clearable
                        value-format="YYYY-MM-DD" placeholder="选择检查日期">
                    </el-date-picker>
                </el-form-item> -->
                <!-- <el-form-item label="整改回复说明" prop="reply_content">
                    <el-input v-model="formData.reply_content" clearable placeholder="请输入整改回复说明" />
                </el-form-item>
                <el-form-item label="复核意见" prop="review_opinions">
                    <el-input v-model="formData.review_opinions" clearable placeholder="请输入复核意见" />
                </el-form-item> -->
                <el-form-item label="附件" prop="annex">
                    <uploadAnnex :formData="formData"></uploadAnnex>

                </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="supervisionCompanyCheckEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionCompanyCheckAdd, apiSupervisionCompanyCheckEdit, apiSupervisionCompanyCheckDetail } from '@/api/supervision_company_check'
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: '',
    date: '',
    user: '',
    problem_name: '',
    problem_type: '',
    problem_desc: '',
    reply_user: '',
    reply_date: '',
    reply_content: '',
    review_opinions: '',
    annex: [],
})

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}
const handleAvatarSuccess_four = (response: any) => {
    Array.isArray(formData.annex) ? '' : formData.annex = []
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};

// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入检查名称',
        trigger: ['blur']
    }],
    data: [{
        required: true,
        message: '请输入检查日期',
        trigger: ['blur']
    }],
    user: [{
        required: true,
        message: '请输入检查人',
        trigger: ['blur']
    }],
    problem_name: [{
        required: true,
        message: '请输入问题名称',
        trigger: ['blur']
    }],
    problem_type: [{
        required: true,
        message: '请输入问题类型',
        trigger: ['blur']
    }],
    problem_desc: [{
        required: true,
        message: '请输入问题说明',
        trigger: ['blur']
    }],
    reply_user: [{
        required: true,
        message: '请输入整改回复人',
        trigger: ['blur']
    }],
    reply_date: [{
        required: true,
        message: '请输入整改回复时间',
        trigger: ['blur']
    }],
    reply_content: [{
        required: true,
        message: '请输入整改回复说明',
        trigger: ['blur']
    }],
    review_opinions: [{
        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 apiSupervisionCompanyCheckDetail({
        id: row.id
    })
    setFormData(data)
}


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