<template>
    <div class="monitor-logininfor">
        <el-card v-show="showSearch" class="search-card">
            <div class="form-wrap">
                <el-form @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
                    <el-form-item prop="ipaddr">
                        <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>
                    <el-form-item prop="status">
                        <el-select v-model="queryParams.status" :placeholder="$t('system.logininfor.890875-0')" clearable>
                            <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value" />
                        </el-select>
                    </el-form-item>

                    <template v-if="searchShow">
                        <el-form-item>
                            <el-date-picker
                                v-model="dateRange"
                                style="width: 240px"
                                value-format="yyyy-MM-dd"
                                type="daterange"
                                range-separator="-"
                                :start-placeholder="$t('system.dict.index.880996-3')"
                                :end-placeholder="$t('system.dict.index.880996-4')"
                            ></el-date-picker>
                        </el-form-item>
                    </template>
                </el-form>
                <div class="search-btn-group">
                    <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>
                    <el-button type="text" @click="searchChange">
                        <span style="color: #486ff2; margin-left: 14px">{{ searchShow ? $t('template.index.891112-113') : $t('template.index.891112-112') }}</span>
                        <i style="color: #486ff2; margin-left: 10px" :class="{ 'el-icon-arrow-down': !searchShow, 'el-icon-arrow-up': searchShow }"></i>
                    </el-button>
                </div>
            </div>
        </el-card>

        <el-card>
            <el-row :gutter="10" style="margin-bottom: 15px">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-unlock" size="small" :disabled="single" @click="handleUnlock" v-hasPermi="['monitor:logininfor:unlock']">
                        {{ $t('system.logininfor.890875-1') }}
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-document-delete" size="small" @click="handleClean" v-hasPermi="['monitor:logininfor:remove']">{{ $t('operlog.874509-7') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete" v-hasPermi="['monitor:logininfor:remove']">{{ $t('del') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-download" size="small" @click="handleExport" v-hasPermi="['monitor:logininfor:export']">{{ $t('export') }}</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table ref="tables" v-loading="loading" :data="list" :border="false" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="$t('system.logininfor.890875-2')" align="center" prop="infoId" width="90" />
                <el-table-column :label="$t('user.profile.index.894502-1')" align="left" prop="userName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']" min-width="200" />
                <el-table-column :label="$t('online.093480-7')" align="center" prop="ipaddr" :show-overflow-tooltip="true" width="150" />
                <el-table-column :label="$t('online.093480-8')" align="center" prop="loginLocation" :show-overflow-tooltip="true" min-width="200" />
                <el-table-column :label="$t('online.093480-9')" align="center" prop="browser" :show-overflow-tooltip="true" min-width="130" />
                <el-table-column :label="$t('online.093480-10')" align="center" prop="os" min-width="150" />
                <el-table-column :label="$t('system.logininfor.890875-9')" align="center" prop="status" width="100">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column :label="$t('system.logininfor.890875-3')" align="left" prop="msg" min-width="180" />
                <el-table-column :label="$t('online.093480-11')" align="center" prop="loginTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.loginTime) }}</span>
                    </template>
                </el-table-column>
            </el-table>

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

<script>
import { list, delLogininfor, cleanLogininfor, unlockLogininfor } from '@/api/monitor/logininfor';

export default {
    name: 'Logininfor',
    dicts: ['sys_common_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 选择用户名
            selectName: '',
            // 显示搜索条件
            showSearch: true,
            searchShow: false, //搜索展开
            // 总条数
            total: 0,
            // 表格数据
            list: [],
            // 日期范围
            dateRange: [],
            // 默认排序
            defaultSort: { prop: 'loginTime', order: 'descending' },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                ipaddr: undefined,
                userName: undefined,
                status: undefined,
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询登录日志列表 */
        getList() {
            this.loading = true;
            list(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
                this.list = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm('queryForm');
            this.queryParams.pageNum = 1;
            this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
        },
        /** 多选框选中数据 */
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.infoId);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
            this.selectName = selection.map((item) => item.userName);
        },
        /** 排序触发事件 */
        handleSortChange(column, prop, order) {
            this.queryParams.orderByColumn = column.prop;
            this.queryParams.isAsc = column.order;
            this.getList();
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const infoIds = row.infoId || this.ids;
            this.$modal
                .confirm(this.$t('system.logininfor.890875-4', [infoIds]))
                .then(function () {
                    return delLogininfor(infoIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('delSuccess'));
                })
                .catch(() => {});
        },
        /** 清空按钮操作 */
        handleClean() {
            this.$modal
                .confirm(this.$t('system.logininfor.890875-5'))
                .then(function () {
                    return cleanLogininfor();
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('operlog.874509-29'));
                })
                .catch(() => {});
        },
        /** 解锁按钮操作 */
        handleUnlock() {
            const username = this.selectName;
            this.$modal
                .confirm(this.$t('system.logininfor.890875-6', [username]))
                .then(function () {
                    return unlockLogininfor(username);
                })
                .then(() => {
                    this.$modal.msgSuccess(this.$t('system.logininfor.890875-8') + username + this.$t('system.logininfor.890875-7'));
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'monitor/logininfor/export',
                {
                    ...this.queryParams,
                },
                `logininfor_${new Date().getTime()}.xlsx`
            );
        },
        // 搜索展开隐藏
        searchChange() {
            this.searchShow = !this.searchShow;
        },
    },
};
</script>

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

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

        .form-wrap {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: flex-end;
            margin-bottom: -22.5px;

            .search-btn-group {
                display: flex;
                flex-direction: row;
                margin-bottom: 22px;
            }
        }
    }
}
</style>
