<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="项目id" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                </el-form-item>
                <el-form-item label="工程地点" prop="address">
                    <el-input v-model="formData.address" clearable placeholder="请输入工程地点" />
                </el-form-item>
                <el-form-item label="服务范围" prop="service_scope">
                    <el-input v-model="formData.service_scope" clearable placeholder="请输入服务范围" />
                </el-form-item>
                <el-form-item label="建筑面积" prop="area">
                    <el-input v-model="formData.area" clearable placeholder="请输入建筑面积" />
                </el-form-item>
                <el-form-item label="投资(万元)" prop="invest">
                    <el-input v-model="formData.invest" clearable placeholder="请输入投资(万元)" />
                </el-form-item>
                <el-form-item label="基础类型" prop="basic_type">
                    <el-input v-model="formData.basic_type" clearable placeholder="请输入基础类型" />
                </el-form-item>
                <el-form-item label="工期" prop="duration">
                    <el-input v-model="formData.duration" clearable placeholder="请输入工期" />
                </el-form-item>
                <el-form-item label="结构类型" prop="structure_type">
                    <el-input v-model="formData.structure_type" clearable placeholder="请输入结构类型" />
                </el-form-item>
                <el-form-item label="地上层数" prop="land_floors">
                    <el-input v-model="formData.land_floors" clearable placeholder="请输入地上层数" />
                </el-form-item>
                <el-form-item label="地下层数" prop="underground_floors">
                    <el-input v-model="formData.underground_floors" clearable placeholder="请输入地下层数" />
                </el-form-item>
                <el-form-item label="业主单位" prop="owner_unit">
                    <el-input v-model="formData.owner_unit" clearable placeholder="请输入业主单位" />
                </el-form-item>
                <el-form-item label="业主单位联系人及电话" prop="owner_unit_contact">
                    <el-input v-model="formData.owner_unit_contact" clearable placeholder="请输入业主单位联系人及电话" />
                </el-form-item>
                <el-form-item label="设计单位" prop="design_unit">
                    <el-input v-model="formData.design_unit" clearable placeholder="请输入设计单位" />
                </el-form-item>
                <el-form-item label="设计单位联系人及电话" prop="design_unit_contact">
                    <el-input v-model="formData.design_unit_contact" clearable placeholder="请输入设计单位联系人及电话" />
                </el-form-item>
                <el-form-item label="勘察单位" prop="survey_unit">
                    <el-input v-model="formData.survey_unit" clearable placeholder="请输入勘察单位" />
                </el-form-item>
                <el-form-item label="勘察单位联系人及电话" prop="survey_unit_contact">
                    <el-input v-model="formData.survey_unit_contact" clearable placeholder="请输入勘察单位联系人及电话" />
                </el-form-item>
                <el-form-item label="施工单位" prop="build_unit">
                    <el-input v-model="formData.build_unit" clearable placeholder="请输入施工单位" />
                </el-form-item>
                <el-form-item label="施工单位联系人及电话" prop="build_unit_contact">
                    <el-input v-model="formData.build_unit_contact" clearable placeholder="请输入施工单位联系人及电话" />
                </el-form-item>
                <el-form-item label="项目经理" prop="project_manager">
                    <el-input v-model="formData.project_manager" clearable placeholder="请输入项目经理" />
                </el-form-item>
                <el-form-item label="项目副经理" prop="deputy_project_manager">
                    <el-input v-model="formData.deputy_project_manager" clearable placeholder="请输入项目副经理" />
                </el-form-item>
                <el-form-item label="专业工程师" prop="engineer">
                    <el-input v-model="formData.engineer" clearable placeholder="请输入专业工程师" />
                </el-form-item>
                <el-form-item label="其他事项情况说明" prop="content">
                    <el-input v-model="formData.content" clearable placeholder="请输入其他事项情况说明" />
                </el-form-item>
                <el-form-item label="填表人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入填表人" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="consultNewStartProjectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiConsultNewStartProjectAdd, apiConsultNewStartProjectEdit, apiConsultNewStartProjectDetail } from '@/api/consult_new_start_project'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目咨询--新开工项目管理' : '新增项目咨询--新开工项目管理'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    address: '',
    service_scope: '',
    area: '',
    invest: '',
    basic_type: '',
    duration: '',
    structure_type: '',
    land_floors: '',
    underground_floors: '',
    owner_unit: '',
    owner_unit_contact: '',
    design_unit: '',
    design_unit_contact: '',
    survey_unit: '',
    survey_unit_contact: '',
    build_unit: '',
    build_unit_contact: '',
    project_manager: '',
    deputy_project_manager: '',
    engineer: '',
    content: '',
    create_user: '',
})


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    address: [{
        required: true,
        message: '请输入工程地点',
        trigger: ['blur']
    }],
    service_scope: [{
        required: true,
        message: '请输入服务范围',
        trigger: ['blur']
    }],
    area: [{
        required: true,
        message: '请输入建筑面积',
        trigger: ['blur']
    }],
    invest: [{
        required: true,
        message: '请输入投资(万元)',
        trigger: ['blur']
    }],
    basic_type: [{
        required: true,
        message: '请输入基础类型',
        trigger: ['blur']
    }],
    duration: [{
        required: true,
        message: '请输入工期',
        trigger: ['blur']
    }],
    structure_type: [{
        required: true,
        message: '请输入结构类型',
        trigger: ['blur']
    }],
    land_floors: [{
        required: true,
        message: '请输入地上层数',
        trigger: ['blur']
    }],
    underground_floors: [{
        required: true,
        message: '请输入地下层数',
        trigger: ['blur']
    }],
    owner_unit: [{
        required: true,
        message: '请输入业主单位',
        trigger: ['blur']
    }],
    owner_unit_contact: [{
        required: true,
        message: '请输入业主单位联系人及电话',
        trigger: ['blur']
    }],
    design_unit: [{
        required: true,
        message: '请输入设计单位',
        trigger: ['blur']
    }],
    design_unit_contact: [{
        required: true,
        message: '请输入设计单位联系人及电话',
        trigger: ['blur']
    }],
    survey_unit: [{
        required: true,
        message: '请输入勘察单位',
        trigger: ['blur']
    }],
    survey_unit_contact: [{
        required: true,
        message: '请输入勘察单位联系人及电话',
        trigger: ['blur']
    }],
    build_unit: [{
        required: true,
        message: '请输入施工单位',
        trigger: ['blur']
    }],
    build_unit_contact: [{
        required: true,
        message: '请输入施工单位联系人及电话',
        trigger: ['blur']
    }],
    project_manager: [{
        required: true,
        message: '请输入项目经理',
        trigger: ['blur']
    }],
    deputy_project_manager: [{
        required: true,
        message: '请输入项目副经理',
        trigger: ['blur']
    }],
    engineer: [{
        required: true,
        message: '请输入专业工程师',
        trigger: ['blur']
    }],
    content: [{
        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]
        }
    }
    
    
}

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


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