<!-- 交易服务-保税服务 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-auth-operation type="dropdown" :menus="handleOperateButtons(row)" :options="{ selectedRow: row }"
                    @closed="onRefresh" />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable'
import { GZBSStatus } from '@/constants/customs'
import { useBSFWOrderList } from '@/business/customs/bonded'
import { useComposeTable } from '@pc/components/base/table'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'

const { loading, dataList, total, pageIndex, pageSize, getBSFWOrderList } = useBSFWOrderList()
const { tableColumns } = useComposeTable<Ermcp.GZBSFWOrderRsp>({ rowKey: 'orderid', columnKey: 'customs_bsfw' })
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.GZBSFWOrderReq>()

const handleOperateButtons = (row: Ermcp.GZBSFWOrderRsp) => {
    const buttons = ['customs_bonded_details', 'customs_bonded_download']
    switch (row.gzbsstatus) {
        case GZBSStatus.UploadBill: {
            buttons.push('customs_bonded_upload_bill')
            break
        }
        case GZBSStatus.UploadSeal: {
            buttons.push('customs_bonded_upload_seal')
            break
        }
        case GZBSStatus.AdvancePaymentConfirm: {
            buttons.push('customs_bonded_advance_payment')
            break
        }
        case GZBSStatus.PaymentConfirm: {
            buttons.push('customs_bonded_payment')
            break
        }
    }
    return buttons
}

const onSearch = (clear = false) => {
    getQueryParams((qs) => {
        pageIndex.value = 1
        getBSFWOrderList(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => {
        getBSFWOrderList(qs).catch((err) => ElMessage.error(err))
    })
}

filterOptons.selectList = [
    {
        label: '单据状态',
        key: 'executestatus',
        options: [
            { label: '待处理', value: 1 },
            { label: '进行中', value: 2 },
            { label: '已结束', value: 3 },
        ],
    },
]

filterOptons.buttonList = [
    { lable: '重置', onClick: () => onSearch(true) },
    { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() }
]

onRefresh()
</script>