<!-- 市场运行管理-监控管理-在线用户查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['marketrun_monitor_user_alloffline'])"
                    @click="openComponent" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['marketrun_monitor_user_offline'])"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { useOperation } from '@/hooks/operation'
import { marketRunUserLogin } from '@/services/api/market'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

const clientTypeEnum = useEnum('clientType') // 终端类型

const { filterOptons, getQueryParams } = useDataFilter<Model.MarketRunUserLoginReq>()

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.MarketRunUserLoginRsp>({
    onClose: () => onSearch()
})

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'loginid', label: 'marketrun.monitor.user.loginid' },
    { field: 'username', label: 'marketrun.monitor.user.username' },
    { field: 'loginip', label: 'marketrun.monitor.user.loginip' },
    { field: 'loginport', label: 'marketrun.monitor.user.loginport' },
    { field: 'logintime', label: 'marketrun.monitor.user.logintime', formatValue: (val) => formatDate(val) },
    { field: 'clienttype', label: 'marketrun.monitor.user.clienttype', formatValue: (val) => clientTypeEnum.getEnumTypeName(val) },
    { field: 'softversion', label: 'marketrun.monitor.user.softversion' },
    { field: 'areaname', label: 'marketrun.monitor.user.areaname' },
    { field: 'membername', label: 'marketrun.monitor.user.membername' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

filterOptons.inputList = [
    { label: t('marketrun.monitor.user.loginid'), keys: ['loginid'], type: 'number' },
]

filterOptons.buttonList = [
    { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
    { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
]

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    run(qs)
}
</script>