<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="580px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="整改回复时间" prop="rectification_reply_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.rectification_reply_time" clearable
                        value-format="YYYY-MM-DD" placeholder="选择时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="回复人" prop="rectification_reply_user">
                    <el-input v-model="formData.rectification_reply_user" clearable placeholder="请输入回复人" />
                </el-form-item>
                <el-form-item label="整改回复意见" prop="rectification_reply_opinions">
                    <el-input v-model="formData.rectification_reply_opinions" clearable placeholder="整改回复意见"
                        type="textarea" />
                </el-form-item>
                <el-form-item label="附件" prop="rectification_reply_annex">
                    <uploadAnnex :formData="formData" value="rectification_reply_annex"></uploadAnnex>

                </el-form-item>

            </el-form>

        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionNoticeEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apisupervision_notice_problem_reply } from '@/api/supervision_notice'
import type { PropType } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return '监理回复'
})

// 表单数据
const formData = reactive({
    id: '',
    "rectification_reply_time": "",
    "rectification_reply_user": "",
    "rectification_reply_opinions": "",
    "rectification_reply_annex": []
})

// 表单验证
const formRules = reactive<any>(
    {
        rectification_reply_time: [{
            required: true,
            message: '请输入',
            trigger: ['blur']
        }],
        rectification_reply_user: [{
            required: true,
            message: '请输入',
            trigger: ['blur']
        }],
        rectification_reply_opinions: [{
            required: true,
            message: '请输入',
            trigger: ['blur']
        }],
        rectification_reply_annex: [{
            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 handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apisupervision_notice_problem_reply(data)
        : await apisupervision_notice_problem_reply(data)
    popupRef.value?.close()
    emit('success')
}

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

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



defineExpose({
    open,
    setFormData,

})
</script>
