<template>
  <div class="login-log-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item prop="ipaddr">
            <el-input
              v-model="queryForm.ipaddr"
              clearable
              placeholder="请输入登录地址"
              @keyup.enter.native="queryData" />
          </el-form-item>

          <el-form-item prop="loginLocation">
            <el-input
              v-model="queryForm.loginLocation"
              clearable
              placeholder="请输入登录地点"
              size="small"
              @keyup.enter.native="queryData" />
          </el-form-item>

          <el-form-item prop="userName">
            <el-input
              v-model="queryForm.userName"
              clearable
              placeholder="请输入用户名称"
              @keyup.enter.native="queryData" />
          </el-form-item>

          <el-form-item prop="status">
            <el-select v-model="queryForm.status" clearable placeholder="登录状态">
              <el-option label="成功" value="10" />
              <el-option label="失败" value="20" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            <!--            <el-button type="text" @click="handleFold">-->
            <!--              <span v-if="fold">展开</span>-->
            <!--              <span v-else>合并</span>-->
            <!--              <vab-icon-->
            <!--                class="vab-dropdown"-->
            <!--                :class="{ 'vab-dropdown-active': fold }"-->
            <!--                icon="arrow-up-s-line" />-->
            <!--            </el-button>-->
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-delete" size="mini" type="danger" @click="handleDelete">删除</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <table-tool :check-list.sync="checkList" :columns="columns" />
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :height="height" @selection-change="setSelectRows">
      <el-table-column align="center" type="selection" />
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable"
        :width="item.width">
        <template #default="{ row }">
          <span v-if="item.prop === 'status'">
            {{ row.status === '10' ? '成功' : '失败' }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
  </div>
</template>

<script>
  import loginLogApi from '@/api/system/loginLog'
  import TableTool from '@/components/table/TableTool'
  import VabQueryFormTopPanel from '@/vab/components/VabQueryForm/components/VabQueryFormTopPanel'

  export default {
    name: 'LoginLog',
    components: { VabQueryFormTopPanel, TableTool },
    data() {
      return {
        fold: false,
        height: this.$baseTableHeight(2),
        checkList: [],
        columns: [
          {
            label: '登录名称',
            width: 'auto',
            prop: 'userName',
            sortable: true,
            disableCheck: true,
          },
          {
            label: '登录地址',
            width: 'auto',
            prop: 'ipaddr',
          },
          {
            label: '登录地点',
            width: 'auto',
            prop: 'loginLocation',
          },
          {
            label: '浏览器',
            width: 'auto',
            prop: 'browser',
          },
          {
            label: '操作系统',
            width: 'auto',
            prop: 'os',
          },
          {
            label: '登录状态',
            width: 'auto',
            prop: 'status',
          },
          {
            label: '操作信息',
            width: 'auto',
            prop: 'msg',
          },
          {
            label: '登录时间',
            width: 'auto',
            prop: 'loginTime',
            sortable: true,
          },
        ],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
        statusOptions: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    created() {
      this.fetchData()
    },
    beforeMount() {
      window.addEventListener('resize', this.handleHeight)
    },
    beforeDestroy() {
      window.removeEventListener('resize', this.handleHeight)
    },
    methods: {
      handleFold() {
        this.fold = !this.fold
        this.handleHeight()
      },
      handleHeight() {
        if (this.fold) this.height = this.$baseTableHeight(2)
        else this.height = this.$baseTableHeight(2)
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleDelete(row) {
        if (row.infoId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await loginLogApi.doDelete({ ids: [row.infoId] })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.infoId)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await loginLogApi.doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await loginLogApi.getList(this.queryForm)
        const { list, total } = data
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
