<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="120px" :rules="formRules">
                <el-form-item label="报销单号" prop="travel_reimbursement_id">
                    <el-input v-model="formData.trip_reimbursement_code" clearable placeholder="请输入报销单号"
                        @click="showDialog = true" readonly />
                </el-form-item>
                <el-form-item label="项目费用模板" prop="project_cost_temp_id">
                    <el-input v-model="formData.first_level_subject" clearable placeholder="请输入项目费用模板id"
                        @click="showDialog1 = true" readonly />
                </el-form-item>
                <el-form-item label="交通" prop="traffic_fee">
                    <el-input v-model="formData.traffic_fee" clearable placeholder="请输入交通" />
                </el-form-item>
                <el-form-item label="住宿" prop="stay_fee">
                    <el-input v-model="formData.stay_fee" clearable placeholder="请输入住宿" />
                </el-form-item>
                <el-form-item label="餐饮" prop="restaurant_fee">
                    <el-input v-model="formData.restaurant_fee" clearable placeholder="请输入餐饮" />
                </el-form-item>
                <el-form-item label="补助" prop="subsidy_fee">
                    <el-input v-model="formData.subsidy_fee" clearable placeholder="请输入补助" />
                </el-form-item>
                <el-form-item label="其它" prop="other_fee">
                    <el-input v-model="formData.other_fee" 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%">
                <projectTravelReimbursement @customEvent="customEvent"></projectTravelReimbursement>
            </el-dialog>

            <el-dialog v-model="showDialog1" title="选择项目" width="70%">
                <projectcostTemLists @customEvent="customEvent1"></projectcostTemLists>
            </el-dialog>

        </popup>
    </div>
</template>

<script lang="ts" setup name="projectTravelReimbursementDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectTravelReimbursementDetailAdd, apiProjectTravelReimbursementDetailEdit, apiProjectTravelReimbursementDetailDetail } from '@/api/project_travel_reimbursement_detail'
// project_travel_reimbursement

import projectTravelReimbursement from "@/components/project_travel_reimbursement/index.vue"
import projectcostTemLists from "@/components/projectcostTemLists/index.vue"


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


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

// 表单数据
const formData = reactive({
    id: '',
    "travel_reimbursement_id": "",
    trip_reimbursement_code: "",
    "project_cost_temp_id": "",
    first_level_subject: "",
    "traffic_fee": "",
    "stay_fee": "",
    "restaurant_fee": "",
    "subsidy_fee": "",
    "other_fee": "",
    "remark": ""
})

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

}


// 表单验证
const formRules = reactive<any>({
    travel_reimbursement_id: [{
        required: true,
        message: '请输入差旅报销单id',
        trigger: ['blur']
    }],
    project_cost_temp_id: [{
        required: true,
        message: '请输入项目费用模板id',
        trigger: ['blur']
    }],
    traffic_fee: [{
        required: true,
        message: '请输入交通',
        trigger: ['blur']
    }],
    stay_fee: [{
        required: true,
        message: '请输入住宿',
        trigger: ['blur']
    }],
    restaurant_fee: [{
        required: true,
        message: '请输入餐饮',
        trigger: ['blur']
    }],
    subsidy_fee: [{
        required: true,
        message: '请输入补助',
        trigger: ['blur']
    }],
    other_fee: [{
        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 apiProjectTravelReimbursementDetailDetail({
        id: row.id
    })
    setFormData(data)
}


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