<!-- 市场运行管理-监控管理-斩仓监控 -->
<template>
    <app-view>
        <app-table :data="dataList" :columns="tableColumns">
            <template #headerLeft>
                <div>{{ t('marketrun.monitor.liquidation.countDown') }}{{ countDown.time }}</div>
                <el-button type="primary" @click="countDown.stop" v-if="countDown.status.value">
                    {{ t('marketrun.monitor.liquidation.stop') }}
                </el-button>
                <el-button type="primary" @click="startCountDown" :disabled="loading" v-else>
                    {{ t('marketrun.monitor.liquidation.start') }}
                </el-button>
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="run" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useCountDown } from '@/hooks/countdown'
import { useRequest } from '@/hooks/request'
import { marketRunCutposition } from '@/services/api/market'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import { i18n } from '@/stores'

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

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountid', label: 'marketrun.monitor.liquidation.accountid' },
    { field: 'goodsname', label: 'marketrun.monitor.liquidation.goodsname' },
    { field: 'buyorsell', label: 'marketrun.monitor.liquidation.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'cuttime', label: 'marketrun.monitor.liquidation.cuttime', formatValue: (val) => formatDate(val) },
    { field: 'cuttradeqty', label: 'marketrun.monitor.liquidation.cuttradeqty' },
    { field: 'cutorderid', label: 'marketrun.monitor.liquidation.cutorderid' }
])

const startCountDown = () => {
    countDown.start(() => run())
}

onUnmounted(() => {
    countDown.stop()
})
</script>