<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="60%"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :inline="true"  :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="项目编号" prop="project_num">
                    <el-input v-model="formData.project_num" clearable placeholder="请输入项目编号" />
                </el-form-item>
                <el-form-item label="项目名称" prop="project_name">
                    <el-input v-model="formData.project_name" clearable placeholder="请输入项目名称" />
                </el-form-item>
                <el-form-item label="关联合同" prop="contract_id">
                    <el-input v-model="formData.contract_id" clearable placeholder="请输入关联合同" />
                </el-form-item>
                <el-form-item label="咨询类型" prop="types">
                    <el-input v-model="formData.types" clearable placeholder="请输入咨询类型" />
                </el-form-item>
                <el-form-item label="行业" prop="industry">
                    <el-input v-model="formData.industry" clearable placeholder="请输入行业" />
                </el-form-item>
                <el-form-item label="所在省份" prop="province">
                    <el-input v-model="formData.province" clearable placeholder="请输入所在省份" />
                </el-form-item>
                <el-form-item label="所在市区" prop="city">
                    <el-input v-model="formData.city" clearable placeholder="请输入所在市区" />
                </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="starting">
                    <el-input v-model="formData.starting" clearable placeholder="请输入计划开始日期" />
                </el-form-item>
                <el-form-item label="计划结束日期" prop="endtime">
                    <el-input v-model="formData.endtime" clearable placeholder="请输入计划结束日期" />
                </el-form-item>
                <el-form-item label="计划工期" prop="jhgq">
                    <el-input v-model="formData.jhgq" clearable placeholder="请输入计划工期（天）" />
                </el-form-item>
                <el-form-item label="项目所属部门" prop="depar">
                    <el-input v-model="formData.depar" clearable placeholder="请输入项目所属部门" />
                </el-form-item>
                <el-form-item label="项目负责人" prop="principal">
                    <el-input v-model="formData.principal" clearable placeholder="请输入项目负责人" />
                </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="invest">
                    <el-input v-model="formData.invest" clearable placeholder="请输入项目总投资（万元）" />
                </el-form-item>
                <el-form-item label="预算造价" prop="budget">
                    <el-input v-model="formData.budget" clearable placeholder="请输入预算造价（万元）" />
                </el-form-item>
                <el-form-item label="送审造价" prop="cost">
                    <el-input v-model="formData.cost" clearable placeholder="请输入送审造价（万元）" />
                </el-form-item>
                <el-form-item label="审定造价" prop="approval">
                    <el-input v-model="formData.approval" clearable placeholder="请输入审定造价（万元）" />
                </el-form-item>
                <el-form-item label="委托单位" prop="aunit">
                    <el-input v-model="formData.aunit" clearable placeholder="请输入委托单位" />
                </el-form-item>
                <el-form-item label="委托方联系人" prop="Acontact">
                    <el-input v-model="formData.Acontact" clearable placeholder="请输入委托方联系人" />
                </el-form-item>
                <el-form-item label="联系电话" prop="acontactnum">
                    <el-input v-model="formData.acontactnum" clearable placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="登记日期" prop="date">
                    <el-input v-model="formData.date" clearable placeholder="请输入登记日期" />
                </el-form-item>
                <el-form-item label="项目规模及概括" prop="generalize">
                    <el-input v-model="formData.generalize" clearable placeholder="请输入项目规模及概括" />
                </el-form-item>
                <el-form-item label="注意事项" prop="note">
                    <el-input v-model="formData.note" clearable placeholder="请输入注意事项" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="costProjectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCostProjectAdd, apiCostProjectEdit, apiCostProjectDetail } from '@/api/cost_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_num: '',
    project_name: '',
    contract_id: '',
    types: '',
    industry: '',
    province: '',
    city: '',
    address: '',
    starting: '',
    endtime: '',
    jhgq: '',
    depar: '',
    principal: '',
    person: '',
    invest: '',
    budget: '',
    cost: '',
    approval: '',
    aunit: '',
    Acontact: '',
    acontactnum: '',
    date: '',
    generalize: '',
    note: '',
    remark: '',
    annex: '',
})


// 表单验证
const formRules = reactive<any>({
    project_num: [{
        required: true,
        message: '请输入项目编号',
        trigger: ['blur']
    }],
    project_name: [{
        required: true,
        message: '请输入项目名称',
        trigger: ['blur']
    }],
    contract_id: [{
        required: true,
        message: '请输入关联合同',
        trigger: ['blur']
    }],
    starting: [{
        required: true,
        message: '请输入计划开始日期',
        trigger: ['blur']
    }],
    endtime: [{
        required: true,
        message: '请输入计划结束日期',
        trigger: ['blur']
    }],
    jhgq: [{
        required: true,
        message: '请输入计划工期（天）',
        trigger: ['blur']
    }],
    principal: [{
        required: true,
        message: '请输入项目负责人',
        trigger: ['blur']
    }],
    invest: [{
        required: true,
        message: '请输入项目总投资（万元）',
        trigger: ['blur']
    }],
    aunit: [{
        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 apiCostProjectDetail({
        id: row.id
    })
    setFormData(data)
}


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