<!-- 查询管理-内部订单查询-成交单查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <!-- 查询类型 -->
                <template #isHis="{ item }">
                    <el-form-item :label="item.label">
                        <el-select v-model="item.value">
                            <el-option :label="t('common.current')" :value="0" />
                            <el-option :label="t('common.history')" :value="1" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('query.internal.order.date')" prop="date" v-if="item.value === 1">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYY-MM-DD"
                            :start-placeholder="t('common.start')" :end-placeholder="t('common.end')" />
                    </el-form-item>
                </template>
                <!-- 商品 -->
                <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 #accountId="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-account v-model="item.value" />
                    </el-form-item>
                </template>
                <!-- 所属机构 -->
                <template #areaUserName="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member2 v-model="item.value" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_trade_export'])"
                    @click="openComponentOnClick" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_trade_details'])"
                    @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 { ref } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryContractSheet } 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 AppSelectAccount from '@pc/components/modules/select-account/index.vue'
import AppSelectMember2 from '@pc/components/modules/select-member2/index.vue'
import { i18n } from '@/stores'

const { getMarketOptions } = useMarket()

const buyOrSellEnum = useEnum('buyOrSell') // 方向
const buildtypeEnum = useEnum('buildtype') // 单据类型
const tradetypeEnum = useEnum('tradetype') // 成交类别

const { global: { t } } = i18n

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

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradeid', label: 'query.internal.trade.tradeid' },
    { field: 'loginid', label: 'query.internal.trade.loginid' },
    { field: 'accountid', label: 'query.internal.trade.accountid' },
    { field: 'currentname', label: 'query.internal.trade.currentname' },
    { field: 'membername', label: 'query.internal.trade.membername' },
    { field: 'goodscode', label: 'query.internal.trade.goodsCodeName' },
    { field: 'buyorsell', label: 'query.internal.trade.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'buildtype', label: 'query.internal.trade.buildtype', formatValue: (val) => buildtypeEnum.getEnumTypeName(val) },
    { field: 'tradetype', label: 'query.internal.trade.tradetype', formatValue: (val) => tradetypeEnum.getEnumTypeName(val) },
    { field: 'tradeprice', label: 'query.internal.trade.tradeprice' },
    { field: 'tradeqty', label: 'query.internal.trade.tradeqty' },
    { field: 'tradeamount', label: 'query.internal.trade.tradeamount' },
    { field: 'chargevalue', label: 'query.internal.trade.chargevalue' },
    { field: 'closepl', label: 'query.internal.trade.closepl' },
    { field: 'tradetime', label: 'query.internal.trade.tradetime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.ContractSheetReq>({
    filters: [
        {
            field: 'marketId',
            label: t('query.internal.trade.marketid'),
            required: true,
            options: () => getMarketOptions([15, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsId',
            label: t('query.internal.trade.goodsId')
        },
        {
            field: 'buyOrSell',
            label: t('query.internal.trade.buyOrSell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'isHis',
            label: t('query.internal.order.isHis'),
            value: 0
        },
        {
            field: 'buildType',
            label: t('query.internal.trade.buildType'),
            options: () => buildtypeEnum.getEnumOptions()
        },
        {
            field: 'tradeTypes',
            label: t('query.internal.trade.tradeTypes'),
            options: () => [
                { label: t('query.internal.trade.tradeTypes1'), value: '1,2,10' },
                { label: t('query.internal.trade.tradeTypes2'), value: '3,4' },
                { label: t('query.internal.trade.tradeTypes3'), value: '6' },
                { label: t('query.internal.trade.tradeTypes4'), value: '7,9,11' },
                { label: t('query.internal.trade.tradeTypes5'), value: '29' },
                { label: t('query.internal.trade.tradeTypes6'), value: '12' },
                { label: t('query.internal.trade.tradeTypes7'), value: '5,8,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28' },
            ]
        },
        {
            field: 'feeSearchType',
            label: t('query.internal.trade.feeSearchType'),
            options: () => [
                { label: t('query.internal.trade.feeSearchType1'), value: 1 },
                { label: t('query.internal.trade.feeSearchType2'), value: 2 },
            ]
        },
        {
            field: 'tradeId',
            label: t('query.internal.trade.tradeId')
        },
        {
            field: 'accountId',
            label: t('query.internal.trade.accountId')
        },
        {
            field: 'areaUserName',
            label: t('query.internal.trade.areaUserName')
        }
    ],
    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 = {
    date: [{
        required: true,
        validator: () => dateValue.value?.length === 2
    }]
}

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

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

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