<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="rectification_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.rectification_time" clearable
                        value-format="YYYY-MM-DD" placeholder="选择时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="整改回复人" prop="rectification_user">
                    <el-input v-model="formData.rectification_user" clearable placeholder="请输入整改回复人" />
                </el-form-item>
                <el-form-item label="检查结果" prop="rectification_result">
                    <el-radio-group v-model="formData.rectification_result">
                        <el-radio :label="0">
                            通过
                        </el-radio>
                        <el-radio :label="1">
                            不通过
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="整改回复意见" prop="rectification_opinion">
                    <el-input v-model="formData.rectification_opinion" clearable placeholder="请输入整改回复意见"
                        type="textarea" />
                </el-form-item>
                <el-form-item label="附件" prop="rectification_annex">
                    <uploadAnnex :formData="formData" value="rectification_annex"></uploadAnnex>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionProblemEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionProblemDetail, apisupervision_problem } from '@/api/supervision_problem'
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 popupTitle = computed(() => {
    return "检查验收"
})

// 表单数据
const formData = reactive({
    "id": 1,
    "rectification_time": "2024-03-01",
    "rectification_user": "张三",
    "rectification_result": 1,
    "rectification_opinion": "aa",
    "rectification_annex": []
})


// 表单验证
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 apiSupervisionProblemDetail({
        id: row.id
    })
    setFormData(data)
}


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