<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="num">
                    <el-input v-model="formData.num" clearable placeholder="系统自动填写" disabled />
                </el-form-item>
                <el-form-item label="项目名称" prop="project">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="项目编号" prop="project_num">
                    <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
                </el-form-item>
                <el-form-item label="任务名称" prop="task">
                    <el-input v-model="formData.task" clearable placeholder="请输入任务名称" />
                </el-form-item>
                <el-form-item label="工序" prop="processes">
                    <el-input v-model="formData.processes" clearable placeholder="请输入工序" />
                </el-form-item>
                <el-form-item label="资料份数" prop="number">
                    <el-input v-model="formData.number" clearable placeholder="请输入资料份数" />
                </el-form-item>
                <el-form-item label="登记日期" prop="apptime">
                    <el-date-picker class="flex-1 !flex" v-model="formData.apptime" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="请选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="登记人" prop="person">
                    <el-input v-model="formData.person" clearable placeholder="请输入登记人" />
                </el-form-item>
                <el-form-item label="备注" prop="tips">
                    <el-input v-model="formData.tips" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
                <el-form-item label="多行文本框1" prop="fd_1">
                    <el-input class="flex-1" v-model="formData.fd_1" type="textarea" clearable
                        placeholder="请输入多行文本框1" />
                </el-form-item>
                <el-form-item label="多行文本框2" prop="fd_2">
                    <el-input class="flex-1" v-model="formData.fd_2" type="textarea" clearable
                        placeholder="请输入多行文本框2" />
                </el-form-item>
                <el-form-item label="多行文本框3" prop="fd_3">
                    <el-input class="flex-1" v-model="formData.fd_3" type="textarea" clearable
                        placeholder="请输入多行文本框3" />
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable :config="cost_project" :query="{ task_allocation: 1 }" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="approvalIssuanceAchievementDocumentsEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiApprovalIssuanceAchievementDocumentsAdd, apiApprovalIssuanceAchievementDocumentsEdit, apiApprovalIssuanceAchievementDocumentsDetail } from '@/api/approval_issuance_achievement_documents'
import { cost_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: '',
    dataid: '',
    num: '',
    project_id: '',
    project_name: "",
    project_num: '',
    task: '',
    processes: '',
    number: '',
    apptime: '',
    person: '',
    tips: '',
    fd_1: '',
    fd_2: '',
    fd_3: '',
    projectInfo: {}

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

// 表单验证
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]
        }
    }
    formData.project_name = formData.projectInfo?.project_name
    formData.project_num = formData.projectInfo?.project_num
    formData.project = formData.projectInfo?.id


}

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


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