<template>
    <div class="monitor-online">
        <el-card v-show="showSearch" class="search-card">
            <el-form @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="search-form">
                <el-form-item prop="loginLocation">
                    <el-input v-model="queryParams.ipaddr" :placeholder="$t('online.093480-1')" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item prop="userName">
                    <el-input v-model="queryParams.userName" :placeholder="$t('online.093480-2')" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
                <div style="float: right">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('search') }}</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('reset') }}</el-button>
                </div>
            </el-form>
        </el-card>

        <el-card>
            <el-row :gutter="10" style="margin-bottom: 15px">
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="list.slice((pageNum - 1) * pageSize, pageNum * pageSize)" :border="false" style="width: 100%">
                <el-table-column :label="$t('online.093480-3')" type="index" align="center">
                    <template slot-scope="scope">
                        <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('online.093480-4')" align="center" prop="tokenId" :show-overflow-tooltip="true" width="310" />
                <el-table-column :label="$t('online.093480-5')" align="left" prop="userName" :show-overflow-tooltip="true" min-width="200" />
                <el-table-column :label="$t('online.093480-6')" align="left" prop="deptName" min-width="180" />
                <el-table-column :label="$t('online.093480-7')" align="center" prop="ipaddr" :show-overflow-tooltip="true" min-width="150" />
                <el-table-column :label="$t('online.093480-8')" align="center" prop="loginLocation" :show-overflow-tooltip="true" min-width="180" />
                <el-table-column :label="$t('online.093480-9')" align="center" prop="browser" min-width="120" />
                <el-table-column :label="$t('online.093480-10')" align="center" prop="os" min-width="150" />
                <el-table-column :label="$t('online.093480-11')" align="center" prop="loginTime" min-width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.loginTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" :label="$t('opation')" align="center" width="80">
                    <template slot-scope="scope">
                        <el-button type="text" style="color: #ed2525" size="small" @click="handleForceLogout(scope.row)" v-hasPermi="['monitor:online:forceLogout']">
                            <svg-icon icon-class="logout" />
                            <div style="display: inline-block; margin-left: 5px">{{ $t('online.093480-12') }}</div>
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination style="margin-bottom: 20px" v-show="total > 0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />
        </el-card>
    </div>
</template>

<script>
import { list, forceLogout } from '@/api/monitor/online';

export default {
    name: 'Online',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            list: [],
            pageNum: 1,
            pageSize: 10,
            // 查询参数
            queryParams: {
                ipaddr: undefined,
                userName: undefined,
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询登录日志列表 */
        getList() {
            this.loading = true;
            list(this.queryParams).then((response) => {
                this.list = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 强退按钮操作
        handleForceLogout(row) {
            this.$modal
                .confirm(this.$t('online.093480-13', [row.userName]))
                .then(function () {
                    return forceLogout(row.tokenId);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('online.093480-14'));
                })
                .catch(() => {});
        },
    },
};
</script>

<style lang="scss" scoped>
.monitor-online {
    padding: 20px;

    .search-card {
        margin-bottom: 15px;
        padding: 3px 0;
    }

    .search-form {
        margin-bottom: -22.5px;
    }
}
</style>
