<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="130px" :rules="formRules">
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目"
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="项目总监意见" prop="opinion">
                    <el-input v-model="formData.opinion" clearable placeholder="请输入项目总监意见" type="textarea" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                </el-form-item>
            </el-form>
            <div style="margin-bottom: 30px;">资料归档目录</div>
            <div style="margin-bottom: 30px;">
                <el-table :data="formData.detail">
                    <el-table-column label="序号" width="150px">
                        <template #default="{ row }">
                            <el-button @click="handleAdd()">+</el-button>
                            <el-button @click="handleDelete(row)">-</el-button>
                        </template>
                    </el-table-column>

                    <el-table-column label="监理项目竣工资料归档目录" prop="data_catalog">

                        <template #default="{ row }">
                            <el-input v-model="row.data_catalog" />
                        </template>
                    </el-table-column>

                    <el-table-column label="项目部提交资料(份数)" prop="submit_by_project_dept">

                        <template #default="{ row }">
                            <el-input v-model="row.submit_by_project_dept" type="number" />
                        </template>
                    </el-table-column>
                    <el-table-column label="生产管理部核实情况(份数)" prop="submit_by_production_dept">

                        <template #default="{ row }">
                            <el-input v-model="row.submit_by_production_dept" type="number" />
                        </template>
                    </el-table-column>

                </el-table>
            </div>

            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <supervision_project @customEvent="customEvent"></supervision_project>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionProjectDataArchivingEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionProjectDataArchivingAdd, apiSupervisionProjectDataArchivingEdit, apiSupervisionProjectDataArchivingDetail, apisupervision_project_data_archiving_detail, apisupervision_project_data_archiving_detail_delete } from '@/api/supervision_project_data_archiving'
import { timeFormat } from '@/utils/util'
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: "",
    opinion: '',
    remark: '',
    "detail": [
        {
            "data_catalog": "",
            "submit_by_project_dept": "",
            "submit_by_production_dept": ""
        }
    ]
})

const handleAdd = () => {
    formData.detail.push({
        "data_catalog": "",
        "submit_by_project_dept": "",
        "submit_by_production_dept": ""
    })
}


const handleDelete = async (row: any) => {

    if (row.id) {
        await apisupervision_project_data_archiving_detail_delete({ id: row.id })
    }
    const index = formData.detail.indexOf(row);
    formData.detail.splice(index, 1);
}


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: '请输入项目id',
        trigger: ['blur']
    }],
    opinion: [{
        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]
        }
    }
    let res = await apisupervision_project_data_archiving_detail({ data_archiving_id: formData.id })
    formData.detail = res.lists

}

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


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