<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="190px" :rules="formRules">

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目"
                                @click="showDialog = true" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工程地点" prop="address">
                            <el-input v-model="formData.address" clearable placeholder="请输入工程地点" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="服务范围" prop="service_scope">
                            <el-input v-model="formData.service_scope" clearable placeholder="请输入服务范围" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="建筑面积" prop="area">
                            <el-input v-model="formData.area" clearable placeholder="请输入建筑面积" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="投资(万元)" prop="invest">
                            <el-input v-model="formData.invest" clearable placeholder="请输入投资(万元)" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="基础类型">
                            <el-input v-model="formData.basic_type" clearable placeholder="请输入基础类型" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="工期">
                            <el-input v-model="formData.duration" clearable placeholder="请输入工期" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="结构类型">
                            <el-input v-model="formData.structure_type" clearable placeholder="请输入结构类型" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="地上层数">
                            <el-input v-model="formData.land_floors" clearable placeholder="请输入地上层数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="地下层数">
                            <el-input v-model="formData.underground_floors" clearable placeholder="请输入地下层数"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业主单位">
                            <el-input v-model="formData.owner_unit" clearable placeholder="请输入业主单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="业主单位联系人及电话">
                            <el-input v-model="formData.owner_unit_contact" clearable placeholder="请输入业主单位联系人及电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="设计单位">
                            <el-input v-model="formData.design_unit" clearable placeholder="请输入设计单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="设计单位联系人及电话">
                            <el-input v-model="formData.design_unit_contact" clearable placeholder="请输入设计单位联系人及电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="勘察单位">
                            <el-input v-model="formData.survey_unit" clearable placeholder="请输入勘察单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="勘察单位联系人及电话">
                            <el-input v-model="formData.survey_unit_contact" clearable placeholder="请输入勘察单位联系人及电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="施工单位">
                            <el-input v-model="formData.build_unit" clearable placeholder="请输入施工单位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="施工单位联系人及电话">
                            <el-input v-model="formData.build_unit_contact" clearable placeholder="请输入施工单位联系人及电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目经理">
                            <el-input v-model="formData.project_manager" clearable placeholder="请输入项目经理" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目副经理">
                            <el-input v-model="formData.deputy_project_manager" clearable placeholder="请输入项目副经理" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="专业工程师">
                            <el-input v-model="formData.engineer" clearable placeholder="请输入专业工程师" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="其他事项情况说明">
                            <el-input v-model="formData.content" clearable placeholder="请输入其他事项情况说明" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="填表人" prop="create_user">
                            <el-input v-model="formData.create_user" clearable placeholder="请输入填表人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="填表时间" prop="create_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.create_time" clearable
                                type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择创建时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <formTable :formData="formData.evolve" :config="tableConfig"></formTable>
            <formTable :formData="formData.preparation" :config="tableConfig1"></formTable>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable @customEvent="customEvent" :config="consult_project" />
            </el-dialog>
        </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, apiconsult_project_evolve, apiconsult_project_evolve_delete, apiconsult_project_preparation, apiconsult_project_preparation_delete } from '@/api/consult_new_start_project'
import type { PropType } from 'vue'
import { consult_project } from "@/components/dialogTable/dialogTableConfig"
import { timeFormat } from '@/utils/util'
import useUserStore from "@/stores/modules/user";

const userStore = useUserStore().userInfo;
const showDialog = ref(false)
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: '',
    project_name: '',
    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: userStore.account,
    create_time: timeFormat('', 'yyyy-mm-dd hh:MM:ss'),
    "evolve": [
        {
            "name": "",
            "progress_status": "",
            "resp_user": ""
        },

    ],
    "preparation": [
        {
            "name": "",
            "progress_status": "",
            "resp_user": ""
        },

    ]

})

const tableConfig = reactive(
    {
        title: "项目组工作开展情况",
        tableConfig: [
            {
                label: "名称",
                value: 'name',

            },
            {
                label: "进展状态",
                value: 'progress_status'
            },
            {
                label: "项目组跟踪责任人",
                value: 'resp_user',
            },

        ],
        deleteApi: apiconsult_project_evolve_delete,
    }
)

const tableConfig1 = reactive(
    {
        title: "项目组后勤准备工作",
        tableConfig: [
            {
                label: "名称",
                value: 'name',

            },
            {
                label: "进展状态",
                value: 'progress_status'
            },
            {
                label: "项目组跟踪责任人",
                value: 'resp_user',
            },

        ],
        deleteApi: apiconsult_project_preparation_delete,
    }
)


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: '请选择项目',
        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]
        }
    }

    let res1 = await apiconsult_project_evolve({ new_start_project_id: formData.id })
    formData.evolve = res1.lists
    let res2 = await apiconsult_project_preparation({ new_start_project_id: formData.id })
    formData.preparation = res2.lists
}

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>
