<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="cost_subject_id">
                    <el-input v-model="formData.cost_subject_id" clearable placeholder="请输入一级科目id" />
                </el-form-item>
                <el-form-item label="费用明细id" prop="gc_cost_budget_id">
                    <el-input v-model="formData.gc_cost_budget_id" clearable placeholder="请输入费用明细id" />
                </el-form-item>
                <el-form-item label="部门id" prop="dept_id">
                    <el-input v-model="formData.dept_id" clearable placeholder="请输入部门id" />
                </el-form-item>
                <el-form-item label="1月" prop="month1">
                    <el-input v-model="formData.month1" clearable placeholder="请输入1月" />
                </el-form-item>
                <el-form-item label="2月" prop="month2">
                    <el-input v-model="formData.month2" clearable placeholder="请输入2月" />
                </el-form-item>
                <el-form-item label="3月" prop="month3">
                    <el-input v-model="formData.month3" clearable placeholder="请输入3月" />
                </el-form-item>
                <el-form-item label="4月" prop="month4">
                    <el-input v-model="formData.month4" clearable placeholder="请输入4月" />
                </el-form-item>
                <el-form-item label="5月" prop="month5">
                    <el-input v-model="formData.month5" clearable placeholder="请输入5月" />
                </el-form-item>
                <el-form-item label="6月" prop="month6">
                    <el-input v-model="formData.month6" clearable placeholder="请输入6月" />
                </el-form-item>
                <el-form-item label="7月" prop="month7">
                    <el-input v-model="formData.month7" clearable placeholder="请输入7月" />
                </el-form-item>
                <el-form-item label="8月" prop="month8">
                    <el-input v-model="formData.month8" clearable placeholder="请输入8月" />
                </el-form-item>
                <el-form-item label="9月" prop="month9">
                    <el-input v-model="formData.month9" clearable placeholder="请输入9月" />
                </el-form-item>
                <el-form-item label="10月" prop="month10">
                    <el-input v-model="formData.month10" clearable placeholder="请输入10月" />
                </el-form-item>
                <el-form-item label="11月" prop="month11">
                    <el-input v-model="formData.month11" clearable placeholder="请输入11月" />
                </el-form-item>
                <el-form-item label="12月" prop="month12">
                    <el-input v-model="formData.month12" clearable placeholder="请输入12月" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="costBudgetDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCostBudgetDetailAdd, apiCostBudgetDetailEdit, apiCostBudgetDetailDetail } from '@/api/cost_budget_detail'
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: '',
    cost_subject_id: '',
    gc_cost_budget_id: '',
    dept_id: '',
    month1: '',
    month2: '',
    month3: '',
    month4: '',
    month5: '',
    month6: '',
    month7: '',
    month8: '',
    month9: '',
    month10: '',
    month11: '',
    month12: '',
})


// 表单验证
const formRules = reactive<any>({
    cost_subject_id: [{
        required: true,
        message: '请输入一级科目id',
        trigger: ['blur']
    }],
    gc_cost_budget_id: [{
        required: true,
        message: '请输入费用明细id',
        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 apiCostBudgetDetailDetail({
        id: row.id
    })
    setFormData(data)
}


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