<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_id">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="专项审批类型" prop="approval_type">
                    <el-select class="flex-1" v-model="formData.approval_type" clearable placeholder="请选择专项审批类型">
                        <el-option v-for="(item, index) in dictData.supervision_planning_approval_type" :key="index"
                            :label="item.name" :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="评审人员" prop="approval_user">
                    <el-input v-model="formData.approval_user" clearable placeholder="请输入评审人员" />
                </el-form-item>
                <el-form-item label="方案名称" prop="scheme_name">
                    <el-input v-model="formData.scheme_name" clearable placeholder="请输入方案名称" />
                </el-form-item>
                <el-form-item label="评审内容" prop="approval_content">
                    <el-input v-model="formData.approval_content" clearable placeholder="请输入评审内容" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                        @delFile="delFileFn" />
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <!-- <supervision_project @customEvent="customEvent"></supervision_project> -->
                    <dialogTable @customEvent="customEvent" :config="configA" />

                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionPlanningEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionPlanningAdd, apiSupervisionPlanningEdit, apiSupervisionPlanningDetail } from '@/api/supervision_planning'
import { timeFormat } from '@/utils/util'
import { configA } 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 handleAvatarSuccess_four = (response: any) => {
    // @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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--监理规划' : '新增工程监理--监理规划'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    approval_type: '',
    approval_user: '',
    scheme_name: '',
    approval_content: '',
    remark: '',
    annex: [],
    create_user: '',
})

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    approval_type: [{
        required: true,
        message: '请输入专项审批类型',
        trigger: ['blur']
    }],
    approval_user: [{
        required: true,
        message: '请输入评审人员',
        trigger: ['blur']
    }],
    scheme_name: [{
        required: true,
        message: '请输入方案名称',
        trigger: ['blur']
    }],
    approval_content: [{
        required: true,
        message: '请输入评审内容',
        trigger: ['blur']
    }],
    remark: [{
        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 getDetail = async (row: Record<string, any>) => {
    const data = await apiSupervisionPlanningDetail({
        id: row.id
    })
    setFormData(data)
}


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