<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="110px" :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="abstract">
                    <el-input v-model="formData.abstract" clearable placeholder="请输入信息摘要" />
                </el-form-item>
                <el-form-item label="发生日期" prop="happen_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.happen_date" clearable
                        value-format="YYYY-MM-DD" placeholder="选择发生日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="严重程度" prop="severity">
                    <el-select class="flex-1" v-model="formData.severity" clearable placeholder="请选择严重程度">
                        <el-option v-for="(item, index) in dictData.severity" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="信息类别" prop="info_cate">
                    <el-checkbox-group v-model="formData.info_cate" placeholder="请选择信息类别">
                        <el-checkbox v-for="(item, index) in dictData.info_cate" :key="index" :label="(item.value)">
                            {{ item.name }}
                        </el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item label="信息内容" prop="info_content">
                    <el-input v-model="formData.info_content" clearable placeholder="请输入信息内容" />
                </el-form-item>
                <el-form-item label="项目部意见" prop="opinions">
                    <el-input v-model="formData.opinions" clearable placeholder="请输入项目部意见" />
                </el-form-item>
                <el-form-item label="主送人" prop="giver">
                    <el-input v-model="formData.giver" clearable placeholder="请输入主送人" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                        @delFile="delFileFn" />
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <supervision_project @customEvent="customEvent"></supervision_project>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionProjectInfoReportEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionProjectInfoReportAdd, apiSupervisionProjectInfoReportEdit, apiSupervisionProjectInfoReportDetail } from '@/api/supervision_project_info_report'
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 showDialog = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--项目重大信息上报' : '新增工程监理--项目重大信息上报'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    abstract: '',
    happen_date: '',
    severity: '',
    info_cate: [],
    info_content: '',
    opinions: '',
    giver: '',
    annex: [],
    create_user: '',
})
const checkList = ref([])

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']
    }],
    abstract: [{
        required: true,
        message: '请输入信息摘要',
        trigger: ['blur']
    }],
    happen_date: [{
        required: true,
        message: '请输入发生日期',
        trigger: ['blur']
    }],
    severity: [{
        required: true,
        message: '请输入严重程度',
        trigger: ['blur']
    }],
    info_content: [{
        required: true,
        message: '请输入信息内容',
        trigger: ['blur']
    }],
    opinions: [{
        required: true,
        message: '请输入项目部意见',
        trigger: ['blur']
    }],
    giver: [{
        required: true,
        message: '请输入主送人',
        trigger: ['blur']
    }],
    create_user: [{
        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 customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}


const getDetail = async (row: Record<string, any>) => {
    const data = await apiSupervisionProjectInfoReportDetail({
        id: row.id
    })
    setFormData(data)
}


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