<!-- 查询管理-内部订单查询-委托单查询 -->
<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.order.marketid')" prop="marketId">
                    <el-select v-model="queryParams.marketId" @change="onMarketChange">
                        <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.order.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.order.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.order.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.order.orderStatus')" prop="orderStatus">
                    <el-select v-model="queryParams.orderStatus" clearable>
                        <template v-for="item in orderstatusEnum.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.order.orderId')" prop="orderId">
                    <el-input type="number" v-model="queryParams.orderId" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.order.goodsId')" prop="goodsId">
                    <el-input type="number" v-model="queryParams.goodsId" :placeholder="t('query.internal.order.tips3')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.order.accountName')" prop="accountName">
                    <el-input type="number" v-model="queryParams.accountName" :placeholder="t('query.internal.order.tips2')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.order.excludeAccountIdsStr')" prop="excludeAccountIdsStr">
                    <el-input v-model="queryParams.excludeAccountIdsStr" :placeholder="t('query.internal.order.tips1')"
                        style="width: 260px;" />
                </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_order_export'])"
                    @click="openComponentOnClick" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_order_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 { 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 { queryCommissionSheet } 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, getMarketById } = useMarket()
// 委托类型
const buildtypeEnum = useEnum('buildtype')
// 委托状态
const orderstatusEnum = useEnum('orderstatus')

const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const dateValue = ref<string[] | null>([])

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

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'orderId', label: 'query.internal.order.orderId' },
    { field: 'accountNameId', label: 'query.internal.order.accountNameId' },
    { field: 'goodsCodeName', label: 'query.internal.order.goodsCodeName' },
    { field: 'buildType', label: 'query.internal.order.buildType', formatValue: (val) => buildtypeEnum.getEnumTypeName(val) },
    { field: 'buyOrSell', label: 'query.internal.order.buyOrSell', formatValue: (val) => getBuyOrSellName(val) },
    { field: 'orderPrice', label: 'query.internal.order.orderPrice' },
    { field: 'orderQty', label: 'query.internal.order.orderQty' },
    { field: 'tradeQty', label: 'query.internal.order.tradeQty' },
    { field: 'orderStatus', label: 'query.internal.order.orderStatus', formatValue: (val) => orderstatusEnum.getEnumTypeName(val) },
    { field: 'orderTime', label: 'query.internal.order.orderTime', formatValue: (val) => formatDate(val) },
    { field: 'tradeDate', label: 'query.internal.order.tradeDate' },
    { 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 onMarketChange = (value: number) => {
    const market = getMarketById(value)
    queryParams.value.tradeMode = market?.trademode
}

// 选择查询类型
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>