<!-- 查询管理-订单管理-订单监控 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #risk="{ item }">
                    <el-form-item :label="item.label" prop="risk">
                        ><el-input-number v-model="item.value" :laceholder="t('common.pleaseenter')" />{{
                            t('query.order.monitor.tips1') }} {{ countDown.time }})
                    </el-form-item>
                </template>
                <el-form-item>
                    <el-button type="primary" @click="countDown.stop" v-if="countDown.status.value">{{
                        t('query.order.monitor.start')
                    }}</el-button>
                    <el-button type="primary" @click="startCountDown" v-else>{{ t('query.order.monitor.stop')
                    }}</el-button>
                </el-form-item>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <ul style="display: flex;gap: 20px;" v-if="data">
                    <li>
                        <span>{{ t('query.order.monitor.buyBackCount') }}</span>
                        <span>{{ data.buyBackCount }}</span>
                    </li>
                    <li>
                        <span>{{ t('query.order.monitor.sellCount') }}</span>
                        <span>{{ data.sellCount }}</span>
                    </li>
                    <li>
                        <span>{{ t('query.order.monitor.netcurpositionqty') }}</span>
                        <span>{{ data.netcurpositionqty }}</span>
                    </li>
                    <li>
                        <span>{{ t('query.order.monitor.avgholderprice') }}</span>
                        <span>{{ data.avgholderprice }}</span>
                    </li>
                    <li>
                        <span>{{ t('query.order.monitor.totalfloatpl') }}</span>
                        <span>{{ data.floatpl }}</span>
                    </li>
                </ul>
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useCountDown } from '@/hooks/countdown'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { monitorddjk, monitorddjkCount } from '@/services/api/order'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import { i18n } from '@/stores'

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

const countDown = useCountDown(10)
const { global: { t } } = i18n

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'userfullname', label: 'query.order.monitor.userfullname' },
    { field: 'goodsCode', label: 'query.order.monitor.goodsfullname' },
    { field: 'buyorsell', label: 'query.order.monitor.buyorselldisplay', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'tradetimestr', label: 'query.order.monitor.tradetimestr' },
    { field: 'holderprice', label: 'query.order.monitor.holderprice' },
    { field: 'lastprice', label: 'query.order.monitor.lastprice' },
    { field: 'depositratedisplay', label: 'query.order.monitor.depositratedisplay' },
    { field: 'floatpl', label: 'query.order.monitor.floatpl' },
    { field: 'holdernum', label: 'query.order.monitor.holdernum' },
    { field: 'freezenum', label: 'query.order.monitor.freezenum' },
    { field: 'holderamount', label: 'query.order.monitor.holderamount' },
    { field: 'payeddeposit', label: 'query.order.monitor.payeddeposit' },
    { field: 'tradeid', label: 'query.order.monitor.tradeid' }
])

const { queryParams, filterOption, getQueryParams } = useDataFilter<Model.MonitorddjkReq>({
    filters: [
        {
            field: 'risk',
            label: t('query.order.monitor.risk'),
            value: 50
        },
        {
            field: 'goodsfullname',
            label: t('query.order.monitor.goodsfullname1')
        },
        {
            field: 'buyorsell',
            label: t('query.order.monitor.buyorsell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'userfullname',
            label: t('query.order.monitor.userfullname')
        },
        {
            field: 'parentusername',
            label: t('query.order.monitor.parentusername')
        }
    ]
})

const { dataList, total, pageSize, pageIndex, loading, run: getMonitorddjk } = useRequest(monitorddjk, {
    params: {
        pageNum: 1,
        pageSize: 20,
        risk: queryParams.value.risk
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const { data, run: getMonitorddjkCount } = useRequest(monitorddjkCount, {
    params: {
        risk: queryParams.value.risk
    }
})

const onSearch = () => {
    const qs = getQueryParams()
    getMonitorddjk(qs)
    getMonitorddjkCount(qs)
}

const startCountDown = () => {
    countDown.start(() => onSearch())
}
</script>