<!-- 查询管理-订单管理-终止单查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <template #ishis="{ item }">
                    <el-form-item :label="item.label" prop="ishis">
                        <el-select v-model="item.value">
                            <el-option v-for="option in item.options?.()" :key="option.value" :label="option.label"
                                :value="option.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('query.order.close.date')" prop="date" v-if="item.value === 1">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('query.order.close.startdate')"
                            :end-placeholder="t('query.order.close.enddate')" />
                    </el-form-item>
                </template>
                <template #goodsid="{ item }">
                    <el-form-item :label="item.label" prop="goodsid">
                        <app-select-goods v-model="item.value"
                            :params="{ trademodels: '52', goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
                <template #areauserid="{ item }">
                    <el-form-item :label="item.label" prop="areauserid">
                        <app-select-member 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_deliverytrade_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_deliverytrade_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 { shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryTerOrder } 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 AppSelectMember from '@pc/components/modules/select-member/index.vue'
import { i18n } from '@/stores'

const buyOrSellEnum = useEnum('buyOrSell') // 方向

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

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'opentradeid', label: 'query.order.close.opentradeid' },
    { field: 'loginid', label: 'query.order.close.loginid' },
    { field: 'currentname', label: 'query.order.close.currentname' },
    { field: 'goodscode', label: 'query.order.close.goodsfullname' },
    { field: 'buyorsell', label: 'query.order.close.buyorselldisplay', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'opennum', label: 'query.order.close.opennum' },
    { field: 'openprice', label: 'query.order.close.openprice' },
    { field: 'closenum', label: 'query.order.close.closenum' },
    { field: 'closeprice', label: 'query.order.close.closeprice' },
    { field: 'closepl', label: 'query.order.close.closepl' },
    { field: 'repayeddeposit', label: 'query.order.close.repayeddeposit' },
    { field: 'tradetypedisplay', label: 'query.order.close.tradetypedisplay' },
    { field: 'tradetime', label: 'query.order.close.tradetime' },
    { field: 'closetime', label: 'query.order.close.closetime' },
    { field: 'closeid', label: 'query.order.close.closeid' }
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.TerOrderReq>({
    filters: [
        {
            field: 'account',
            label: t('query.order.close.account'),
        },
        {
            field: 'goodsid',
            label: t('query.order.close.goodsid'),
        },
        {
            field: 'buyorsell',
            label: t('query.order.close.buyorsell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'tradeid',
            label: t('query.order.close.tradeid'),
        },
        {
            field: 'areauserid',
            label: t('query.order.close.areauserid'),
        },
        {
            field: 'tradetype',
            label: t('query.order.close.tradetype'),
        },
        {
            field: 'ishis',
            label: t('query.order.close.isHis'),
            value: 0,
            options: () => [
                { label: t('common.current'), value: 0 },
                { label: t('common.history'), value: 1 }
            ]
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    const [startDate, endDate] = dateValue.value || []
    qs.startdate = startDate
    qs.enddate = endDate
    return qs
}

const onSearch = () => {
    const qs = processRequiredParams()
    run(qs)
}

const onReset = () => {
    dateValue.value = []
    resetFilters()
}
</script>