<!-- 查询管理-内部订单查询-商品成交汇总 -->
<template>
    <app-view>
        <template #header>
            <el-form ref="formRef" class="el-form--filter" :model="queryParams" :rules="formRules"
                :show-message="false">
                <el-form-item :label="t('query.internal.goodssumm.marketid')" prop="marketid">
                    <el-select v-model="queryParams.marketid">
                        <el-option v-for="item in getMarketOptions()" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.goodssumm.date')" prop="date">
                    <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                        :start-placeholder="t('common.startdate')" :end-placeholder="t('common.enddate')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.goodssumm.summode')" prop="summode">
                    <el-select v-model="queryParams.summode">
                        <el-option :label="t('query.internal.goodssumm.summode1')" :value="1" />
                        <el-option :label="t('query.internal.goodssumm.summode2')" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.goodssumm.goodsid')" prop="goodsid">
                    <app-select-goods v-model="queryParams.goodsid" :marketid="queryParams.marketid" status="3,6,7" />
                </el-form-item>
                <el-form-item :label="t('query.internal.goodssumm.excludeaccountidsstr')" prop="excludeaccountidsstr">
                    <el-input v-model="queryParams.excludeaccountidsstr" :placeholder="t('query.internal.goodssumm.tips1')" style="width: 260px;" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch">{{ t('operation.search') }}</el-button>
                </el-form-item>
            </el-form>
        </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 { reactive, shallowRef } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
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 AppSelectGoods from '@pc/components/modules/select-goods/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n
const formRef = shallowRef<FormInstance>()

const { getMarketOptions } = useMarket()
const dateValue = shallowRef<string[]>([])
const queryParams = reactive<Model.GoodsTradeReq>({
    pageNum: 1,
    pageSize: 20,
    summode: 1
})

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'tradeDate', label: 'query.internal.goodssumm.tradeDate' },
    { field: 'goodsfullname', 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 formRules: FormRules = {
    marketid: [{ required: true }],
    date: [{
        required: true,
        validator: (rule, value, callback) => {
            if (dateValue.value.length === 2) {
                callback()
            } else {
                callback(new Error(t('common.pleasechoicedate')))
            }
        },
    }],
}

const onSearch = (clear = false) => {
    processRequiredParams((qs) => run(qs), clear)
}

const openComponentOnClick = (code: string) => {
    processRequiredParams(() => openComponent(code))
}

// 处理请求参数
const processRequiredParams = (callback: (params: Model.InvestorOrderReq) => void, clear = false) => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const [startDate, endDate] = dateValue.value
            queryParams.startdate = startDate
            queryParams.enddate = endDate
            callback(queryParams)
        }
    })
}
</script>