<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="单据编号" prop="code">
                    <el-input v-model="formData.code" clearable placeholder="请输入单据编号" />
                </el-form-item>
                <el-form-item label="审批部门" prop="approve_dept">
                    <el-input v-model="formData.approve_dept" clearable placeholder="请输入审批部门" />
                </el-form-item>
                <el-form-item label="费用类别" prop="cost_type">
                    <el-input v-model="formData.cost_type" clearable placeholder="请输入费用类别" />
                </el-form-item>
                <el-form-item label="支付方式" prop="pay_type">
                    <el-input v-model="formData.pay_type" clearable placeholder="请输入支付方式" />
                </el-form-item>
                <el-form-item label="可抵扣税额" prop="tax_deductible_amount">
                    <el-input v-model="formData.tax_deductible_amount" clearable placeholder="请输入可抵扣税额" />
                </el-form-item>
                <el-form-item label="附单据张数" prop="bill_num">
                    <el-input v-model="formData.bill_num" clearable placeholder="请输入附单据张数" />
                </el-form-item>
                <el-form-item label="关联费用申请" prop="fee_application_id">
                    <el-input v-model="formData.fee_application_id" clearable placeholder="请输入关联费用申请" />
                </el-form-item>
                <el-form-item label="事由" prop="content">
                    <el-input v-model="formData.content" clearable placeholder="请输入事由" />
                </el-form-item>
                <el-form-item label="申请人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入申请人" />
                </el-form-item>
                <el-form-item label="申请日期" prop="create_time">
                    <el-input v-model="formData.create_time" clearable placeholder="请输入申请日期" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="financialTravelReimbursementEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFinancialTravelReimbursementAdd, apiFinancialTravelReimbursementEdit, apiFinancialTravelReimbursementDetail } from '@/api/financial_travel_reimbursement'
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: '',
    code: '',
    approve_dept: '',
    cost_type: '',
    pay_type: '',
    tax_deductible_amount: '',
    bill_num: '',
    fee_application_id: '',
    content: '',
    create_user: '',
    create_time: '',
})


// 表单验证
const formRules = reactive<any>({
    code: [{
        required: true,
        message: '请输入单据编号',
        trigger: ['blur']
    }],
    approve_dept: [{
        required: true,
        message: '请输入审批部门',
        trigger: ['blur']
    }],
    cost_type: [{
        required: true,
        message: '请输入费用类别',
        trigger: ['blur']
    }],
    pay_type: [{
        required: true,
        message: '请输入支付方式',
        trigger: ['blur']
    }],
    tax_deductible_amount: [{
        required: true,
        message: '请输入可抵扣税额',
        trigger: ['blur']
    }],
    bill_num: [{
        required: true,
        message: '请输入附单据张数',
        trigger: ['blur']
    }],
    fee_application_id: [{
        required: true,
        message: '请输入关联费用申请',
        trigger: ['blur']
    }],
    content: [{
        required: true,
        message: '请输入事由',
        trigger: ['blur']
    }],
    create_user: [{
        required: true,
        message: '请输入申请人',
        trigger: ['blur']
    }],
    create_time: [{
        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 apiFinancialTravelReimbursementDetail({
        id: row.id
    })
    setFormData(data)
}


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