<!-- 查询管理-订单管理-交收单管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #startdate="{ item }">
                    <el-form-item :label="item.label" prop="startdate">
                        <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_order_delivery_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 { queryDeliveryOrderList } from '@/services/api/order'
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'
import { i18n } from '@/stores'

const buyOrSellEnum = useEnum('buyOrSell') // 方向
const deliveryOrderStatusSellEnum = useEnum('deliveryOrderStatus') // 单据状态

const { global: { t } } = i18n

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

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

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.DeliveryOrderListReq>({
    filters: [
        {
            field: 'deliveryorderstatus',
            label: t('query.order.delivery.deliveryorderstatus'),
            options: () => deliveryOrderStatusSellEnum.getEnumOptions()
        },
        {
            field: 'deliverygoodsname',
            label: t('query.order.delivery.deliverygoodsname'),
        },
        {
            field: 'accountname',
            label: t('query.order.delivery.accountname'),
        },
        {
            field: 'startdate',
            label: t('query.order.delivery.startdate'),
        },
        {
            field: 'buyorsell',
            label: t('query.order.delivery.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.DeliveryOrderListRsp) => {
    const buttons = ['query_order_delivery_details']

    if (row.orderstatus === 2) {
        buttons.push('query_order_delivery_confirm', 'query_order_delivery_cancel')
        if (row.buyorsell === BuyOrSell.Sell) {
            buttons.push('query_order_delivery_pay')
        }
    } else if (row.orderstatus === 9) {
        buttons.push('query_order_delivery_pay')
    }

    return getActionButtons(buttons)
}

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