<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="单据编号">
                            <el-input v-model="formData.code" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审批部门" prop="approve_dept">
                            <el-select v-model="formData.approve_dept" placeholder="请选择部门" class="flex-1">
                                <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="费用类别" prop="cost_type">
                            <el-select v-model="formData.cost_type" placeholder="请选择费用类别" class="flex-1">
                                <el-option :label="item.name" :value="parseInt(item.value)"
                                    v-for="item in dictData.cost_type">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="支付方式" prop="pay_type">
                            <el-select v-model="formData.pay_type" placeholder="请选择支付方式" class="flex-1">
                                <el-option :label="item.name" :value="parseInt(item.value)"
                                    v-for="item in dictData.financial_pay_type">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="可抵扣税额" prop="tax_deductible_amount">
                            <el-input v-model="formData.tax_deductible_amount" clearable placeholder="请输入可抵扣税额"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附单据张数" prop="bill_num">
                            <el-input v-model="formData.bill_num" clearable placeholder="请输入附单据张数" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="关联费用申请" prop="fee_application_theme">
                            <el-input v-model="formData.fee_application_theme" clearable placeholder="点击选择费用申请单"
                                @click="showDialog1 = true" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="事由">
                            <el-input v-model="formData.content" clearable placeholder="请输入事由" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <create-user-lable flag name="申请" :form-data="formData"></create-user-lable>
                </el-row>
                <FormTable :form-data="formData.detail" :config="tableConfig" @getSummaries="getSummaries">
                    <el-table-column label="部门">
                        <template #default="{ row }">
                            <el-form-item label-width="0">
                                <el-select v-model="row.dept_id" placeholder="请选择部门">
                                    <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </FormTable>
            </el-form>
            <el-dialog v-model="showDialog1" title="选择费用申请单" width="70%">
                <dialogTable :config="financial_fee_application" @customEvent="customEvent1">
                </dialogTable>
            </el-dialog>
        </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, apiFinancialTravelReimbursementDetailLists, apiFinancialTravelReimbursementDetailDelete } from '@/api/financial_travel_reimbursement'
import type { PropType } from 'vue'
import { deptLists } from "@/api/org/department"
import { financial_fee_application } from "@/components/dialogTable/dialogTableConfig";
import { watch } 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 showDialog1 = ref(false)


// 弹窗标题
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: '',
    fee_application_theme: "",
    content: '',
    create_user: '',
    create_time: '',
    "detail": [
        {
            "dept_id": '',
            "date": "",
            "address": "",
            "traffic_amount": 0,
            "stay_amount": 0,
            "stay_days": 0,
            "stay_price": 0,
            "subsidy_days": 0,
            "subsidy_price": 0,
            "subsidy_amount": 0,
            "subsidy_deduction": 0,
            "abstract": "",
            subtotal: 0
        }
    ]
})

const tableConfig = reactive(
    {
        title: "报销明细",
        tableConfig: [
            {
                label: "起止日期",
                value: 'date',
                date: true

            },
            {
                label: "起止地点",
                value: 'address',
            },
            {
                label: "交通费",
                value: 'traffic_amount',
                type: 'number'

            },
            {
                label: "住宿费",
                value: 'stay_amount',
                type: 'number'

            },
            {
                label: "住宿天数",
                value: 'stay_days',
                type: 'number'

            },
            {
                label: "住宿单价",
                value: 'stay_price',
                type: 'number'

            },
            {
                label: "补助天数",
                value: 'subsidy_days',
                type: 'number'

            },
            {
                label: "补助金额",
                value: 'subsidy_amount',
                type: 'number'

            },
            {
                label: "补贴减扣",
                value: 'subsidy_deduction',
                type: 'number'

            },
            {
                label: "补助标准",
                value: 'subsidy_price',
                type: 'number'

            },
            {
                label: "摘要",
                value: 'abstract',
            },

        ],
        deleteApi: apiFinancialTravelReimbursementDetailDelete,
    }
)


// 小计
const getSummaries = (value: any) => {


}

const customEvent1 = (e) => {
    formData.fee_application_id = e.id
    formData.fee_application_theme = e.theme
    showDialog1.value = false
}


// 表单验证
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]
        }
    }
    let res = await apiFinancialTravelReimbursementDetailLists({ travel_reimbursement_id: data.id })
    formData.detail = res.lists

}

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')
}


const deptList = ref([])
const getDeptList = async () => {
    let res = await deptLists()
    deptList.value = res.lists
}

getDeptList()
defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
