<!-- 查询管理-内部订单查询-线下交收单查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #startdate="{ item }">
                    <el-form-item :label="item.label" prop="cycletime">
                        <el-date-picker v-model="item.value" value-format="YYYYMMDD"
                            :placeholder="t('common.pleasechoice')" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_offlinedelivery_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="handleOperateButtons(row)"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { BuyOrSell } from '@/constants/order'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryOffline } from '@/services/api/order'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'

const { global: { t } } = i18n
const buyOrSellEnum = useEnum('buyOrSell') // 方向
const deliveryOrderStatusEnum = useEnum('deliveryOrderStatus') // 单据状态

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.OfflineRsp>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryOffline, {
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'deliveryorderid', label: 'query.internal.offlinedelivery.deliveryorderid' },
    { field: 'username', label: 'query.internal.offlinedelivery.username' },
    { field: 'goodscode', label: 'query.internal.offlinedelivery.goodsdisplay' },
    { field: 'buyorsell', label: 'query.internal.offlinedelivery.buyorselldisplay', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'deliveryqty', label: 'query.internal.offlinedelivery.deliveryqty' },
    { field: 'sucdeliveryqty', label: 'query.internal.offlinedelivery.sucdeliveryqty' },
    { field: 'deliveryprice', label: 'query.internal.offlinedelivery.deliveryprice' },
    { field: 'deliveryamount', label: 'query.internal.offlinedelivery.deliveryamount' },
    { field: 'matchname', label: 'query.internal.offlinedelivery.matchname' },
    { field: 'reqtime', label: 'query.internal.offlinedelivery.reqtime' },
    { field: 'orderstatus', label: 'query.internal.offlinedelivery.orderstatusdisplay', formatValue: (val) => deliveryOrderStatusEnum.getEnumTypeName(val) },
    { field: 'operate', label: 'common.operate', width: 180, fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.OfflineReq>({
    filters: [
        {
            field: 'deliveryorderstatus',
            label: t('query.internal.offlinedelivery.deliveryorderstatus'),
            options: () => deliveryOrderStatusEnum.getEnumOptions()
        },
        {
            field: 'deliverygoodsname',
            label: t('query.internal.offlinedelivery.deliverygoodsname'),
        },
        {
            field: 'accountname',
            label: t('query.internal.offlinedelivery.accountname'),
        },
        {
            field: 'startdate',
            label: t('query.internal.offlinedelivery.startdate'),
        },
        {
            field: 'buyorsell',
            label: t('query.internal.offlinedelivery.buyorsell'),
            options: () => buyOrSellEnum.getEnumOptions()
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 处理操作按钮
const handleOperateButtons = (row: Model.OfflineRsp) => {
    const buttons = ['query_internal_offlinedelivery_details']

    if (row.orderstatus === 2) {
        buttons.push('query_internal_offlinedelivery_confirm', 'query_internal_offlinedelivery_cancel')
        if (row.buyorsell === BuyOrSell.Sell && row.trademode === 50) {
            buttons.push('query_internal_offlinedelivery_pay')
        }
    } else if (row.orderstatus === 9 && row.trademode === 50) {
        buttons.push('query_internal_offlinedelivery_pay')
    }

    return getActionButtons(buttons)
}

const onSearch = () => {
    const qs = getQueryParams()
    run(qs)
}
</script>