<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="contract_id">
                    <el-input v-model="formData.contract_id" clearable placeholder="请输入合同id" />
                </el-form-item>
                <el-form-item label="费用申请id" prop="fee_application_id">
                    <el-input v-model="formData.fee_application_id" clearable placeholder="请输入费用申请id" />
                </el-form-item>
                <el-form-item label="单据主题" prop="theme">
                    <el-input v-model="formData.theme" clearable placeholder="请输入单据主题" />
                </el-form-item>
                <el-form-item label="单据编号" prop="code">
                    <el-input v-model="formData.code" 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="pay_type">
                    <el-input v-model="formData.pay_type" 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="annex">
                    <el-input v-model="formData.annex" 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="financialExpenseReimbursementEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFinancialExpenseReimbursementAdd, apiFinancialExpenseReimbursementEdit, apiFinancialExpenseReimbursementDetail } from '@/api/financial_expense_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: '',
    contract_id: '',
    fee_application_id: '',
    theme: '',
    code: '',
    bill_num: '',
    pay_type: '',
    content: '',
    annex: '',
    create_user: '',
    create_time: '',
})


// 表单验证
const formRules = reactive<any>({
    contract_id: [{
        required: true,
        message: '请输入合同id',
        trigger: ['blur']
    }],
    fee_application_id: [{
        required: true,
        message: '请输入费用申请id',
        trigger: ['blur']
    }],
    theme: [{
        required: true,
        message: '请输入单据主题',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入单据编号',
        trigger: ['blur']
    }],
    bill_num: [{
        required: true,
        message: '请输入单据张数',
        trigger: ['blur']
    }],
    pay_type: [{
        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 apiFinancialExpenseReimbursementDetail({
        id: row.id
    })
    setFormData(data)
}


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