<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="90px" :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="theme">
                    <el-input v-model="formData.theme" clearable placeholder="请输入会议主题" />
                </el-form-item>
                <el-form-item label="会议类型">
                    <el-input v-model="formData.type" clearable placeholder="请输入会议类型" />
                </el-form-item>
                <el-form-item label="会议时间">
                    <el-date-picker class="flex-1 !flex" v-model="formData.time" clearable value-format="YYYY-MM-DD"
                        placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="主持人">
                    <el-input v-model="formData.emcee" clearable placeholder="请输入主持人" />
                </el-form-item>
                <el-form-item label="记录人">
                    <el-input v-model="formData.recorder" clearable placeholder="请输入记录人" />
                </el-form-item>
                <el-form-item label="会议地点">
                    <el-input v-model="formData.location" clearable placeholder="请输入会议地点" />
                </el-form-item>
                <el-form-item label="与会人员">
                    <el-input v-model="formData.attendees" clearable placeholder="请输入与会人员" />
                </el-form-item>
                <el-form-item label="会议内容">
                    <el-input v-model="formData.content" clearable placeholder="请输入会议内容" />
                </el-form-item>
                <createUserLable :formData="formData"></createUserLable>

                <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="supervisionMeetingMinutesEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionMeetingMinutesAdd, apiSupervisionMeetingMinutesEdit, apiSupervisionMeetingMinutesDetail } from '@/api/supervision_meeting_minutes'
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: "",
    theme: '',
    type: '',
    time: '',
    emcee: '',
    recorder: '',
    location: '',
    attendees: '',
    content: '',
    annex: [],
    create_user: "",
    create_time: "",
})
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 customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}


// 表单验证
const formRules = reactive<any>({
    theme: [{
        required: true,
        message: '请输入会议主题',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入会议类型',
        trigger: ['blur']
    }],
    time: [{
        required: true,
        message: '请输入会议时间',
        trigger: ['blur']
    }],
    emcee: [{
        required: true,
        message: '请输入主持人',
        trigger: ['blur']
    }],

    location: [{
        required: true,
        message: '请输入会议地点',
        trigger: ['blur']
    }],
    attendees: [{
        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 apiSupervisionMeetingMinutesDetail({
        id: row.id
    })
    setFormData(data)
}


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