<!-- 查询管理-内部订单查询-商品成交汇总 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <!-- 商品 -->
                <template #goodsid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-goods ref="goodsRef" v-model="item.value"
                            :params="{ marketId: queryParams.marketid ?? -1, goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
                <!-- 日期 -->
                <template #startdate="{ item }">
                    <el-form-item :label="item.label" prop="startdate">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('common.startdate')" :end-placeholder="t('common.enddate')" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_goodssumm_export'])"
                    @click="openComponentOnClick" />
            </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,  FormRules } from 'element-plus'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryGoodsTrade } 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 AppSelectGoods from '@pc/components/modules/select-goods/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

const goodsRef = shallowRef() // 商品组件实例
const dateValue = shallowRef<string[] | null>([])

const { getMarketOptions } = useMarket()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'tradeDate', label: 'query.internal.goodssumm.tradeDate' },
    { field: 'goodscode', label: 'query.internal.goodssumm.goodsfullname' },
    { field: 'tradeqtysum', label: 'query.internal.goodssumm.tradeqtysum' },
    { field: 'tradeamountsum', label: 'query.internal.goodssumm.tradeamountsum' },
    { field: 'closeplsum', label: 'query.internal.goodssumm.closeplsum' },
    { field: 'chargesum', label: 'query.internal.goodssumm.chargesum' },
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.GoodsTradeReq>({
    filters: [
        {
            field: 'marketid',
            label: t('query.internal.goodssumm.marketid'),
            required: true,
            options: () => getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsid',
            label: t('query.internal.goodssumm.goodsid')
        },
        {
            field: 'summode',
            label: t('query.internal.goodssumm.summode'),
            value: 1,
            options: () => [
                { label: t('query.internal.goodssumm.summode1'), value: 1 },
                { label: t('query.internal.goodssumm.summode2'), value: 2 },
            ]
        },
        {
            field: 'startdate',
            label: t('query.internal.goodssumm.date'),
            required: true
        },
        {
            field: 'excludeaccountidsstr',
            label: t('query.internal.goodssumm.excludeaccountidsstr'),
            placeholder: t('query.internal.goodssumm.tips1'),
            width: 260
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 表单验证规则
const filterRules: FormRules = {
    startdate: [{
        required: true,
        validator: () => dateValue.value?.length === 2
    }]
}

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    const [startDate, endDate] = dateValue.value || []
    qs.startdate = startDate
    qs.enddate = endDate
    run(qs)
}

const onReset = () => {
    resetFilters()
    dateValue.value = []
    goodsRef.value.onReset()
}

const openComponentOnClick = (code: string) => {
    //openComponent(code)
}
</script>