<!-- 市场运行管理-监控管理-账户风险监控 -->
<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.account.orderbytype')" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item prop="risk">
                    {{ t('marketrun.monitor.account.risk') }}<el-input-number v-model="queryParams.risk"
                        :placeholder="t('common.pleaseenter')" />%
                </el-form-item>
                <el-form-item :label="t('marketrun.monitor.account.searchtype')" prop="searchtype">
                    <el-select v-model="queryParams.searchtype">
                        <el-option :label="t('marketrun.monitor.account.searchtype1')" :value="5" />
                        <template v-if="userStore.userInfo.releType === 1">
                            <el-option :label="t('marketrun.monitor.account.searchtype2')" :value="0" />
                            <el-option :label="t('marketrun.monitor.account.searchtype3')" :value="2" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('marketrun.monitor.account.puserid')" prop="puserid">
                    <app-select-member v-model="queryParams.puserid" :params="{ usertype: '2,3' }"
                        :placeholder="t('marketrun.monitor.account.tips2')" />
                </el-form-item>
                <el-form-item>
                    {{ t('marketrun.monitor.account.tips1') }} {{ countDown.time }})
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="countDown.stop" v-if="countDown.status.value">
                        {{ t('marketrun.monitor.account.stop') }}
                    </el-button>
                    <el-button type="primary" @click="startCountDown" :disabled="loading" v-else>
                        {{ t('marketrun.monitor.account.start') }}
                    </el-button>
                </el-form-item>
            </el-form>
        </template>
        <app-table :data="dataList" :columns="tableColumns">
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { ref, computed, onUnmounted } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { useCountDown } from '@/hooks/countdown'
import { useRequest } from '@/hooks/request'
import { marketRunInitInvestor, marketRunInvestor } from '@/services/api/market'
import { i18n, useUserStore } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'

const { global: { t } } = i18n
const userStore = useUserStore()
const formRef = ref<FormInstance>()
const countDown = useCountDown(10)

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

const { data } = useRequest(marketRunInitInvestor)

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

const tableColumns = computed<Model.TableColumn[]>(() => {
    const { isshowrate, isshowsafe } = data.value ?? {}
    if (isshowrate === '1' || isshowsafe === '1') {
        return [
            { field: 'accountid', label: 'marketrun.monitor.account.accountid' },
            { field: 'accountname', label: 'marketrun.monitor.account.accountname' },
            { field: 'memberusername', label: 'marketrun.monitor.account.memberusername' },
            { field: 'parentusername', label: 'marketrun.monitor.account.parentusername' },
            { field: 'freezemargin', label: 'marketrun.monitor.account.freezemargin' },
            { field: 'usedmargin', label: 'marketrun.monitor.account.usedmargin', width: 140 },
            { field: 'netbalance', label: 'marketrun.monitor.account.netbalance' },
            { field: 'balance', label: 'marketrun.monitor.account.balance' },
            { field: 'availmargin', label: 'marketrun.monitor.account.availmargin' },
            { field: 'totalfrozen', label: 'marketrun.monitor.account.totalfrozen' },
            { field: 'totalfloatpl', label: 'marketrun.monitor.account.totalfloatpl' },
            { field: 'credit', label: 'marketrun.monitor.account.credit' },
            { field: 'curriskrate', label: 'marketrun.monitor.account.curriskrate' },
            { field: 'currisklv', label: 'marketrun.monitor.account.currisklv' },
            { field: 'operate', label: 'common.operate', fixed: 'right' }
        ]
    }
    return [
        { field: 'accountid', label: 'marketrun.monitor.account.accountid' },
        { field: 'accountname', label: 'marketrun.monitor.account.accountname' },
        { field: 'memberusername', label: 'marketrun.monitor.account.memberusername' },
        { field: 'parentusername', label: 'marketrun.monitor.account.parentusername' },
        { field: 'freezemargin', label: 'marketrun.monitor.account.freezemargin' },
        { field: 'usedmargin', label: 'marketrun.monitor.account.usedmargin', width: 140 },
        { field: 'netbalance', label: 'marketrun.monitor.account.netbalance' },
        { field: 'balance', label: 'marketrun.monitor.account.balance' },
        { field: 'availmargin', label: 'marketrun.monitor.account.availmargin' },
        { field: 'totalfrozen', label: 'marketrun.monitor.account.totalfrozen' },
        { field: 'totalfloatpl', label: 'marketrun.monitor.account.totalfloatpl1' },
        { field: 'curriskrate', label: 'marketrun.monitor.account.curriskrate' },
        { field: 'currisklv', label: 'marketrun.monitor.account.currisklv' },
        { field: 'currisksaferate', label: 'marketrun.monitor.account.currisksaferate' },
        { field: 'currisksafelv', label: 'marketrun.monitor.account.currisksafelv' },
        { field: 'operate', label: 'common.operate', fixed: 'right' }
    ]
})

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

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