<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="单据编号" prop="num">
                            <el-input v-model="formData.num" clearable placeholder="系统自动填写" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编号" prop="project_num">
                            <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <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-col>
                    <el-col :span="8">
                        <el-form-item label="登记人" prop="person">
                            <el-input v-model="formData.person" clearable placeholder="请输入登记人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资料份数" prop="number">
                            <el-input v-model="formData.number" clearable placeholder="请输入资料份数" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="存放位置" prop="position">
                            <el-input v-model="formData.position" clearable placeholder="请输入存放位置" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="说明" prop="tips">
                            <el-input v-model="formData.tips" clearable placeholder="请输入说明" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="投标文件" prop="bidding_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="bidding_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="招标控制价文件" prop="zbkzj_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="zbkzj_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工程造价合同" prop="gczjht_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="gczjht_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目变更、签证资料" prop="xmbgqzzl_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="xmbgqzzl_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="送审概算、预算、结算资料" prop="ssgsysjs_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="ssgsysjs_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="往来函件" prop="wlhj_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="wlhj_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="其他资料" prop="other_file" label-width="200px">
                            <uploadAnnex :formData="formData" value="other_file"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <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="dataReceptionEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiDataReceptionAdd, apiDataReceptionEdit, } from '@/api/data_reception'
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: "",
    apptime: '',
    person: '',
    number: '',
    position: '',
    tips: '',
    bidding_file: [],
    zbkzj_file: [],
    gczjht_file: [],
    xmbgqzzl_file: [],
    ssgsysjs_file: [],
    wlhj_file: [],
    other_file: [],
    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_id = formData.projectInfo?.id
    formData.project_num = formData.projectInfo?.project_num

}

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


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