<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="code">
                    <el-input v-model="formData.code" clearable placeholder="请输入编号" />
                </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="send_company">
                    <el-input v-model="formData.send_company" clearable placeholder="请输入发文单位" />
                </el-form-item>
                <el-form-item label="发文日期" prop="send_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.send_date" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="请选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="收文人" prop="accept_user">
                    <el-input v-model="formData.accept_user" clearable placeholder="请输入收文人" />
                </el-form-item>
                <el-form-item label="收文日期" prop="accept_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.accept_date" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="请选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="传阅人" prop="read_user">
                    <el-input v-model="formData.read_user" clearable placeholder="请输入传阅人" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <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="manage_project" />
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="manageAcceptDocEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiManageAcceptDocAdd, apiManageAcceptDocEdit, apiManageAcceptDocDetail } from '@/api/manage_accept_doc'
import { manage_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: "",
    code: '',
    abstract: '',
    send_company: '',
    send_date: '',
    accept_user: '',
    accept_date: '',
    read_user: '',
    remark: '',
    annex: [],
})

const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}
// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    abstract: [{
        required: true,
        message: '请输入内容摘要',
        trigger: ['blur']
    }],
    send_company: [{
        required: true,
        message: '请输入发文单位',
        trigger: ['blur']
    }],
    send_date: [{
        required: true,
        message: '请输入发文日期',
        trigger: ['blur']
    }],
    accept_user: [{
        required: true,
        message: '请输入收文人',
        trigger: ['blur']
    }],
    accept_date: [{
        required: true,
        message: '请输入收文日期',
        trigger: ['blur']
    }],
    read_user: [{
        required: true,
        message: '请输入传阅人',
        trigger: ['blur']
    }],
    remark: [{
        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 apiManageAcceptDocDetail({
        id: row.id
    })
    setFormData(data)
}


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