<!-- 保税仓业务-计费管理-线上支付 -->
<template>
    <app-drawer :title="`款项明细【${selectedRow.trademonth}】`" :width="460" v-model:show="show" :loading="loading"
        :refresh="refresh">
        <app-table-details :label-width="120" :data="selectedRow" :cell-props="detailCells">
            <!-- 账户可用余额 -->
            <template #balance>
                <span :style="{ color: balanceIsInsufficient ? 'red' : '#409340' }">
                    {{ accountStore.avaiableMoney.toFixed(2) }}
                </span>
                <span style="color:red;display:block;" v-if="balanceIsInsufficient">*余额不足，请与平台联系。</span>
            </template>
        </app-table-details>
        <template #footer>
            <el-button @click="onCancel(false)" plain>取消</el-button>
            <el-button type="primary" :disabled="balanceIsInsufficient" @click="onSubmit">确认</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useAccountStore } from '@/stores'
import { useBscConfirmPay } from '@/business/bonded'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppTableDetails from '@pc/components/base/table-details/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.GzbscusermonthpayRsp>,
        required: true
    },
})

const { loading, formSubmit } = useBscConfirmPay(props.selectedRow.trademonth)
const accountStore = useAccountStore()
const show = shallowRef(true)
const refresh = shallowRef(false)

const detailCells = [
    { prop: 'servicefee', label: '分拣室服务费：', decimal: 2 },
    { prop: 'powerfee', label: '分拣室电费：', decimal: 2 },
    { prop: 'storagefee', label: '仓储费：', decimal: 2 },
    { prop: 'premium', label: '保险费：', decimal: 2 },
    { prop: 'infee', label: '进仓报关费：', decimal: 2 },
    { prop: 'outfee', label: '出仓报关费：', decimal: 2 },
    { prop: 'totalfee', label: '合计费用：', decimal: 2 },
    { prop: 'balance', label: '账户可用余额：' },
]

// 是否余额不足
const balanceIsInsufficient = computed(() => accountStore.avaiableMoney < props.selectedRow.totalfee)

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    formSubmit().then(() => {
        ElMessage.success('提交成功')
        onCancel(true)
    }).catch((err) => {
        ElMessage.error('提交失败：' + err)
    })
}
</script>

<style lang="less">
@import './index.less';
</style>