<!-- 查询管理-内部订单查询-成交单查询 -->
<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.trade.marketid')" prop="marketId">
                    <el-select v-model="queryParams.marketId">
                        <template v-for="item in getMarketOptions([15, 97, 99], true)" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.isHis')" prop="isHis">
                    <el-select v-model="queryParams.isHis" @change="onTypeChange">
                        <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.trade.date')" prop="date" v-if="queryParams.isHis === 1">
                    <el-date-picker type="daterange" v-model="dateValue" value-format="YYYY-MM-DD"
                    :start-placeholder="t('common.start')" :end-placeholder="t('common.end')" @change="onDateChange" />
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.buyOrSell')" prop="buyOrSell">
                    <el-select v-model="queryParams.buyOrSell" clearable>
                        <template v-for="item in getBuyOrSellList()" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.buildType')" prop="buildType">
                    <el-select v-model="queryParams.buildType" clearable>
                        <template v-for="item in buildtypeEnum.getEnumOptions()" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.tradeTypes')" prop="tradeTypes">
                    <el-select v-model="queryParams.tradeTypes" clearable>
                        <el-option :label="t('query.internal.trade.tradeTypes1')" value="1,2,10" />
                        <el-option :label="t('query.internal.trade.tradeTypes2')" value="3,4" />
                        <el-option :label="t('query.internal.trade.tradeTypes3')" value="6" />
                        <el-option :label="t('query.internal.trade.tradeTypes4')" value="7,9,11" />
                        <el-option :label="t('query.internal.trade.tradeTypes5')" value="29" />
                        <el-option :label="t('query.internal.trade.tradeTypes6')" value="12" />
                        <el-option :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" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.feeSearchType')" prop="feeSearchType">
                    <el-select v-model="queryParams.feeSearchType" clearable>
                        <el-option :label="t('query.internal.trade.feeSearchType1')" :value="1" />
                        <el-option :label="t('query.internal.trade.feeSearchType2')" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.tradeId')" prop="tradeId">
                    <el-input type="number" v-model="queryParams.tradeId" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.goodsId')" prop="goodsId">
                    <el-input type="number" v-model="queryParams.goodsId" :placeholder="t('query.internal.trade.tips1')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.accountId')" prop="accountId">
                    <el-input type="number" v-model="queryParams.accountId" :placeholder="t('query.internal.trade.tips2')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.trade.areaUserName')" prop="areaUserName">
                    <el-input v-model="queryParams.areaUserName" :placeholder="t('query.internal.trade.tips3')" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch(false)">{{ t('operation.search') }}</el-button>
                    <el-button type="primary" @click="onSearch(true)">{{ t('operation.reset') }}</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_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, FormInstance, FormRules } from 'element-plus'
import { formatDate } from '@/filters'
import { getBuyOrSellList, getBuyOrSellName } from '@/constants/order'
import { useEnum } from '@/hooks/enum'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
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 { i18n } from '@/stores'

const { getMarketOptions } = useMarket()
const buildtypeEnum = useEnum('buildtype') // 单据类型
const tradetypeEnum = useEnum('tradetype') // 成交类别

const { global: { t } } = i18n

const formRef = ref<FormInstance>()
const dateValue = ref<string[] | null>([])

const queryParams = ref<Partial<Model.ContractSheetReq>>({
    isHis: 0
})

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: 'goodsCodeName', label: 'query.internal.trade.goodsCodeName' },
    { field: 'buyorsell', label: 'query.internal.trade.buyorsell', formatValue: (val) => getBuyOrSellName(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 formRules: FormRules = {
    marketId: [{ required: true }],
    date: [{
        required: true,
        validator: (rule, value, callback) => {
            if (dateValue.value && dateValue.value.length === 2) {
                callback()
            } else {
                callback(new Error(t('common.pleasechoicedate')))
            }
        },
    }],
}

// 选择查询类型
const onTypeChange = (value: number) => {
    if (!value) {
        dateValue.value = []
        onDateChange()
    }
}

// 选择日期
const onDateChange = () => {
    const [startDate, endDate] = dateValue.value || []
    queryParams.value.startDateHis = startDate
    queryParams.value.endDateHis = endDate
}

const onSearch = (clear = false) => {
    if (clear) {
        queryParams.value = {
            isHis: 0,
        }
        dateValue.value = []
    } else {
        formRef.value?.validate((valid) => {
            if (valid) {
                run(queryParams.value)
            }
        })
    }
}

const openComponentOnClick = (code: string) => {
    formRef.value?.validate((valid) => {
        if (valid) {
            openComponent(code)
        }
    })
}
</script>