<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="40vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="报销单号" prop="expense_reimbursement_id">
                    <el-input v-model="formData.expense_reimbursement_code" clearable placeholder="请输入费用报销单id"
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="项目费用模板" prop="project_cost_temp_id">
                    <el-input v-model="formData.first_level_subject" clearable placeholder="请选额项目费用模板" readonly
                        @click="showDialog3 = true" />
                </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="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择报销单" width="70%">
                <projectExpenseReimbursement @customEvent="customEvent"></projectExpenseReimbursement>
            </el-dialog>

            <el-dialog v-model="showDialog3" title="选择科目" width="70%">
                <projectcostTemLists @customEvent="customEvent3"></projectcostTemLists>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectExpenseReimbursementDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectExpenseReimbursementDetailAdd, apiProjectExpenseReimbursementDetailEdit, apiProjectExpenseReimbursementDetailDetail } from '@/api/project_expense_reimbursement_detail'
import { timeFormat } from '@/utils/util'
import projectcostTemLists from '@/components/projectcostTemLists/index.vue'
import projectExpenseReimbursement from '@/components/project_expense_reimbursement/index.vue'

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 showDialog3 = ref(false)
const showDialog = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目财务-费用报销-报销明细' : '新增项目财务-费用报销-报销明细'
})

// 表单数据
const formData = reactive({
    id: '',
    expense_reimbursement_id: '',
    expense_reimbursement_code: "",
    project_cost_temp_id: '',
    first_level_subject: "",
    amount: '',
    remark: '',
})


// 表单验证
const formRules = reactive<any>({
    expense_reimbursement_id: [{
        required: true,
        message: '请输入费用报销单id',
        trigger: ['blur']
    }],
    project_cost_temp_id: [{
        required: true,
        message: '请输入项目费用模板id',
        trigger: ['blur']
    }],
    amount: [{
        required: true,
        message: '请输入金额',
        trigger: ['blur']
    }]
})

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

const customEvent3 = (e: any) => {
    formData.first_level_subject = e.first_level_subject
    formData.project_cost_temp_id = e.id
    showDialog3.value = false
}
// 获取详情
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 apiProjectExpenseReimbursementDetailDetail({
        id: row.id
    })
    setFormData(data)
}


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