<template>
  <ele-page>
    <login-record-search :where="defaultWhere" @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        :highlight-current-row="true"
        :export-config="{ fileName: '登录日志数据' }"
        :where="defaultWhere"
        cache-key="systemLoginRecordTable"
      >
        <template #loginType="{ row }">
          <el-tag
            v-if="row.loginType === 99"
            size="small"
            type="success"
            :disable-transitions="true"
          >
            登录成功
          </el-tag>
          <el-tag
            v-else-if="row.loginType === 98"
            size="small"
            type="info"
            :disable-transitions="true"
          >
            退出登录
          </el-tag>
          <el-tag
            v-else-if="[11, 12, 21, 31].indexOf(row.loginType) != -1"
            size="small"
            type="danger"
            :disable-transitions="true"
          >
            登录失败/<span v-if="row.loginType == 11">用户名错误</span>
            <span v-if="row.loginType == 12">密码错误</span>
            <span v-if="row.loginType == 21">用户已被禁用</span>
            <span v-if="row.loginType == 31">登录异常</span>
          </el-tag>
        </template>
      </ele-pro-table>
    </ele-card>
  </ele-page>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import type { EleProTable } from 'ele-admin-plus';
  import type {
    DatasourceFunction,
    Columns
  } from 'ele-admin-plus/es/ele-pro-table/types';
  import LoginRecordSearch from './components/login-record-search.vue';
  import { pageLoginRecords } from '@/api/system/login-record';
  import type { LoginRecordParam } from '@/api/system/login-record/model';

  defineOptions({ name: 'SystemLoginRecord' });

  /** 默认搜索条件 */
  const defaultWhere = reactive({
    username: '',
    nickname: ''
  });

  /** 表格实例 */
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  /** 表格列配置 */
  const columns = ref<Columns>([
    {
      type: 'index',
      columnKey: 'index',
      width: 50,
      align: 'center'
    },
    {
      prop: 'contextId',
      label: 'ContextId',
      width: 280
    },
    {
      prop: 'username',
      label: '账号',
      width: 160
    },
    {
      prop: 'ip',
      label: 'IP地址',
      width: 170
    },
    {
      prop: 'device',
      label: '设备型号',
      minWidth: 110
    },
    {
      prop: 'os',
      label: '操作系统',
      minWidth: 110
    },
    {
      prop: 'browser',
      label: '浏览器',
      minWidth: 110
    },
    {
      prop: 'loginType',
      label: '操作类型',
      width: 140,
      slot: 'loginType',
      filters: [
        {
          text: '用户名错误',
          value: '11'
        },
        {
          text: '密码错误',
          value: '12'
        },
        {
          text: '用户已禁用',
          value: '21'
        },
        {
          text: '登录异常',
          value: '31'
        },
        {
          text: '登录成功',
          value: '99'
        },
        {
          text: '退出登录',
          value: '98'
        }
      ],
      filterMultiple: false
    },
    {
      prop: 'comments',
      label: '备注',
      minWidth: 210,
      align: 'left'
    },
    {
      prop: 'createTime',
      label: '登录时间',
      sortable: 'custom',
      width: 180,
      align: 'center'
    }
  ]);

  /** 表格数据源 */
  const datasource: DatasourceFunction = ({
    pages,
    where,
    orders,
    filters
  }) => {
    return pageLoginRecords({ ...where, ...orders, ...filters, ...pages });
  };

  /** 刷新表格 */
  const reload = (where?: LoginRecordParam) => {
    tableRef.value?.reload?.({ page: 1, where });
  };
</script>
