<!-- 市场运行管理-监控管理-做市会员风险监控 -->
<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="风险率" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item prop="risk">
                    监控当前风险率><el-input-number v-model="queryParams.risk" placeholder="请输入" />%
                </el-form-item>
                <el-form-item>
                    (默认风险率>50%;每10秒刷新一次,下次刷新: {{ countDown.time }})
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="countDown.stop" v-if="countDown.status.value">停止监控</el-button>
                    <el-button type="primary" @click="startCountDown" :disabled="loading" v-else>开始监控</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'

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

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: '账户' },
    { field: 'tradestatus', label: '账户状态' },
    { field: 'accountname', label: '会员名称' },
    { field: 'freezemargin', label: '当前冻结资金' },
    { field: 'usedmargin', label: '当前占用保证金', width: 140 },
    { field: 'balance', label: '当前净值' },
    { field: 'availmargin', label: '可用资金' },
    { field: 'totalfrozen', label: '总冻结' },
    { field: 'totalfloatpl', label: '浮动损益' },
    { field: 'curriskrate', label: '当前风险率(%)' },
    { field: 'currisklv', label: '当前风险等级' },
    { field: 'operate', label: '操作', fixed: 'right' }
]

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

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