<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="700px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="项目名称" prop="cost_budget_id">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择" @click="showDialog = true"
                        readonly />
                </el-form-item>
                <el-form-item label="项目费用模板" prop="project_cost_temp_id" label-width="120px">
                    <el-input v-model="formData.first_level_subject" clearable placeholder="点击选择"
                        @click="showDialog1 = true" readonly />
                </el-form-item>

                <el-form-item label="单位" prop="unit">
                    <el-input v-model="formData.unit" clearable placeholder="请输入单位" />
                </el-form-item>
                <el-form-item label="预算金额" prop="amount">
                    <el-input v-model="formData.amount" clearable placeholder="请输入预算金额" />
                </el-form-item>
                <el-form-item label="编制标准" prop="standard">
                    <el-input v-model="formData.standard" clearable placeholder="请输入编制标准" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入" />
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <projectDialog @customEvent="customEvent"></projectDialog>
            </el-dialog>
            <el-dialog v-model="showDialog1" title="选择科目" width="70%">
                <projectcostTemLists @customEvent="customEvent1"></projectcostTemLists>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectCostBudgetDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectCostBudgetDetailAdd, apiProjectCostBudgetDetailEdit, apiProjectCostBudgetDetailDetail } from '@/api/project_cost_budget_detail'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import projectDialog from '@/components/projectCostBudget/index.vue'
import projectcostTemLists from '@/components/projectcostTemLists/index.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 showDialog1 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑费用预算明细' : '新增费用预算明细'
})

// 表单数据
const formData = reactive({
    id: '',
    "cost_budget_id": '',
    cost_budget_name: "",
    "project_cost_temp_id": '',
    first_level_subject: "",
    project_name: "",
    "unit": "",
    "amount": '',
    "standard": "",
    "remark": ""
})

const customEvent = (e: any) => {
    formData.cost_budget_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

const customEvent1 = (e: any) => {
    formData.first_level_subject = e.first_level_subject
    formData.project_cost_temp_id = e.id
    showDialog1.value = false
}


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    cost_budget_id: [{
        required: true,
        message: '请输入费用预算id',
        trigger: ['blur']
    }],
    project_cost_temp_id: [{
        required: true,
        message: '请输入项目费用模板id',
        trigger: ['blur']
    }],
    unit: [{
        required: true,
        message: '请输入单位',
        trigger: ['blur']
    }],
    amount: [{
        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 apiProjectCostBudgetDetailDetail({
        id: row.id
    })
    setFormData(data)
}


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