<!-- 市场运行管理-监控管理-做市会员风险监控 -->
<template>
    <app-view>
        <template #header>
            <el-form ref="formRef" class="el-form--filter" :model="queryParams">
                <el-form-item prop="orderbytype">
                    <el-select v-model="queryParams.orderbytype">
                        <el-option :label="t('marketrun.monitor.marketer.orderbytype')" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item prop="risk">
                    {{ t('marketrun.monitor.marketer.risk') }}<el-input-number v-model="queryParams.risk" :placeholder="t('common.pleaseenter')" />%
                </el-form-item>
                <el-form-item>
                    {{ t('marketrun.monitor.marketer.tips1') }} {{ countDown.time }})
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="countDown.stop" v-if="countDown.status.value">{{ t('marketrun.monitor.marketer.stop') }}</el-button>
                    <el-button type="primary" @click="startCountDown" :disabled="loading" v-else>{{ t('marketrun.monitor.marketer.start') }}</el-button>
                </el-form-item>
            </el-form>
        </template>
        <app-table :data="dataList" :columns="tableColumns">
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons()"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref, onUnmounted } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { useCountDown } from '@/hooks/countdown'
import { useOperation } from '@/hooks/operation'
import { useRequest } from '@/hooks/request'
import { marketRunMemberRisk } from '@/services/api/market'
import AppTable from '@pc/components/base/table/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const formRef = ref<FormInstance>()
const countDown = useCountDown(10)

const { global: { t} } = i18n

const queryParams = ref<Model.MarketRunInvestorReq>({
    orderbytype: 0,
    risk: 50
})

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.MarketRunMemberRiskRsp>()

const { dataList, loading, run } = useRequest(marketRunMemberRisk, {
    params: queryParams.value,
    onError: (err) => {
        ElMessage.error(err)
    },
    onFinally: () => {
        startCountDown()
    }
})

const tableColumns: Model.TableColumn[] = [
    { field: 'accountid', label: 'marketrun.monitor.marketer.accountid' },
    { field: 'tradestatus', label: 'marketrun.monitor.marketer.tradestatus' },
    { field: 'accountname', label: 'marketrun.monitor.marketer.accountname' },
    { field: 'freezemargin', label: 'marketrun.monitor.marketer.freezemargin' },
    { field: 'usedmargin', label: 'marketrun.monitor.marketer.usedmargin', width: 140 },
    { field: 'balance', label: 'marketrun.monitor.marketer.balance' },
    { field: 'availmargin', label: 'marketrun.monitor.marketer.availmargin' },
    { field: 'totalfrozen', label: 'marketrun.monitor.marketer.totalfrozen' },
    { field: 'totalfloatpl', label: 'marketrun.monitor.marketer.totalfloatpl' },
    { field: 'curriskrate', label: 'marketrun.monitor.marketer.curriskrate' },
    { field: 'currisklv', label: 'marketrun.monitor.marketer.currisklv' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
]

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

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