<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input
          maxlength="128"
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="userName">
        <el-input
          maxlength="50"
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="登录状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择登录状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          style="display:none"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          v-hasPermi="['auditManager:logininfor:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          style="display:none"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['auditManager:logininfor:remove']"
        >清空</el-button>
      </el-col>
    </el-row>

    <div style="padding-top: 10px">
      <vxe-table
        ref="tables"
        align="center"
        :loading="loading"
        class="mytable-style"
        auto-resize
        border
        resizable
        export-config
        highlight-current-row
        show-overflow
        max-height="700"
        :data="list"
        :radio-config="{trigger: 'row'}"
        @sort-change="sortChangeEvent"
        :sort-config="{remote:'true'}"
      >
<!--        <vxe-column type="radio" width="60"/>-->
        <vxe-table-column field="infoId" title="访问编号"/>
        <vxe-table-column field="userName" title="用户账号" :sortable="true"/>
        <vxe-table-column field="ipaddr" title="登录地址" width="130"/>
        <vxe-table-column field="loginLocation" title="登录地点"/>
        <vxe-table-column field="browser" title="浏览器"/>
        <vxe-table-column field="os" title="操作系统"/>
        <vxe-table-column field="status" title="登录状态" :formatter="statusFormat"/>
        <vxe-table-column field="msg" title="操作信息"/>
        <vxe-table-column field="loginTime" title="登录日期" :sortable="true"/>
      </vxe-table>
      <vxe-pager
        v-show="showTable"
        perfect
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :total="total"
        :page-sizes="[10,50,100]"
        :layouts="['PrevJump', 'PrevPage','JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
        @page-change="handlePageChange"
      >
      </vxe-pager>
    </div>
  </div>
</template>

<script>

import {debounce} from "lodash";

export default {
  name: "Logininfor",
  data() {
    return {
      showTable: true,
      currentPage: 1,
      pageSize: 10,
      // 总条数
      total: 0,
      statusOptions: [
        {value: '0', label: '成功'},
        {value: '1', label: '失败'}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选择用户名
      selectName: "",
      // 显示搜索条件
      showSearch: true,
      // 表格数据
      list: [],
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: {prop: 'loginTime', order: 'descending'},
      // 查询参数
      queryParams: {
        ipaddr: undefined,
        userName: undefined,
        status: undefined
      },
      sortOrder: 'loginTime&desc',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    sortChangeEvent({column, property, order}) {
      if (order == null) {
        order = 'desc'
      }
      this.currentPage = 1
      this.sortOrder = property+'&'+order
      this.getList()
    },
    handlePageChange({currentPage, pageSize}) {
      this.currentPage = currentPage
      this.pageSize = pageSize
      this.getList()
    },
    // 列表状态格式化
    statusFormat({cellValue}) {
      let belongTo = '未知的类型'
      for (let i = 0; i < this.statusOptions.length; i++) {
        if (cellValue == "0") {
          belongTo = "成功"
        } else if (cellValue == "1") {
          belongTo = "失败"
        }
      }
      return belongTo
    },
    /** 查询登录日志列表 */
    getList:debounce(async function(){
      let sysTime
      let lk
      await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
        sysTime = res.data.sysTime
        lk = res.data.lk
      }).catch((error) => {
      })

      this.loading = true;
      let _startOperTime;
      let _endOperTime;
      if (this.dateRange == null) {
        _startOperTime = ''
        _endOperTime = ''
      } else {
        _startOperTime = this.dateRange[0]
        _endOperTime = this.dateRange[1]
      }
      var searchParams = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ipaddr: this.queryParams.ipaddr,
        userName: this.queryParams.userName,
        status: this.queryParams.status,
        startLoginTime:_startOperTime,
        endLoginTime:_endOperTime,
        sortOrder: this.sortOrder,
        sysTime: sysTime,
        lk: lk
      }
      await this.$axios.get('/sysLogininforController/getAll',
        {params: searchParams}).then((res) => {
        this.list = res.data.records
        this.total = res.data.total

        if (res.data.records == '') {
          this.showTable = false
        } else {
          this.showTable = true
        }
        this.loading = false
      }).catch((error) => {
        this.loading = false;
        // this.$message.error(error)
      })
    },1000),
    /** 搜索按钮操作 */
    handleQuery() {
      this.currentPage = 1
      this.pageSize = 10
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
    },
    /** 删除按钮操作 */
    handleDelete() {
      const _selectData = this.$refs.tables.getRadioRecord(true)
      if (_selectData == null) {
        this.$message({
          type: 'warning',
          message: '请选择记录!'
        });
        return
      }
      this.$confirm('是否确认删除用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDelete(_selectData)
      }).catch(() => {
      });
    },
    /**
     * 删除提交
     */
    doDelete:debounce(function(_selectData){
      const param = {
        infoId: _selectData.infoId
      }
      this.$axios.post('/sysLogininforController/delLoginInfo', param).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        } else {
          this.$message({
            type: 'error',
            message: res.data
          });
        }
      }).catch((error) => {
        this.$message({
          type: 'error',
          message: '删除失败!'
        });
        this.loading = false
      })
    },1000),
    /** 清空按钮操作 */
    handleClean() {
      if (this.list=='') {
        this.$message({
          type: 'warning',
          message: '没有记录不能清空!'
        });
        return
      }
      this.$confirm('是否确认清空所有登录日志数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.cleanLogininfor();
      }).catch(() => {
      })
    },
    /**
     * 清空提交
     */
    cleanLogininfor:debounce(function(){
      this.$axios.post('/sysLogininforController/cleanLogininfor',{}).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '清空成功!'
          });
          this.getList();
        } else {
          this.$message({
            type: 'error',
            message: res.data
          });
        }
      }).catch((error) => {
        this.$message({
          type: 'error',
          message: '清空失败!'
        });
        this.loading = false
      })
    },1000)
  }
};
</script>

