<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="100px">
          <vab-query-form>
            <el-form-item width="100" prop="userName" :label="$t('staff.yhm')">
              <el-input
                v-model="queryForm.userName"
                size="small"
                placeholder="请输入用户名"
              ></el-input>
            </el-form-item>
            <el-form-item width="100" prop="time" :label="$t('defalult.rq')">
              <el-date-picker
                v-model="queryForm.time"
                size="small"
                type="daterange"
                align="right"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                @change="changeTime"
              ></el-date-picker>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">
          {{ $t('defalult.chongz') }}
        </el-button>
        <el-button type="primary" size="small" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
        <el-button type="primary" size="small" @click="exportData">
          {{ $t('defalult.dc') }}
        </el-button>
      </div>
    </div>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        fixed
        type="selection"
        width="40"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="userName"
        :label="$t('staff.yhm')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="loginIp"
        label="登录IP"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="loginLocation"
        label="登录地址"
        width="130"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="browser"
        label="浏览器"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="os"
        label="系统"
        width="280"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="msg"
        :label="$t('dataEnquiry.zt')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="loginTime"
        label="登录时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed="right"
        prop="details"
        :label="$t('defalult.cz')"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
import { loginLogData, removeLoginData, exportLoginLog } from '@/api/log';
import { getDate, getPastTime } from '@/common/times';
import ajax from '@/api/download';
export default {
  data() {
    return {
      tableData: [],
      dialogTableVisible: false,
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      ids: [],
      queryForm: {
        page: 1,
        size: 20,
        userName: '',
        start: '',
        end: '',
        time: '',
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    changeTime() {
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    setDefaultTime() {
      this.queryForm.time = getPastTime(1);
      this.queryForm.start = getDate(this.queryForm.time[0]);
      this.queryForm.end = getDate(this.queryForm.time[1]);
    },
    async fetchData() {
      const { data } = await loginLogData(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
    },
    //搜索
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    //导出
    async exportData() {
      const url = await exportLoginLog(this.queryForm);
      ajax.downloadPostFile(url, { fileName: '登录日志.xls' });
    },
    deletes() {
      const ids = this.ids;
      if (ids.length == 0) {
        this.$message({
          type: 'info',
          message: '没有选中任何项',
        });
        return false;
      }
      this.deleteData(ids);
    },
    handleSelectionChange(val) {
      const ids = [];
      val.forEach(row => {
        ids.push(row.id);
      });
      this.ids = ids;
    },
    //删除
    deleteData(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = removeLoginData(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      that.deleteData([row.id]);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-wrap {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .el-input {
    width: 200px !important;
  }
  .el-select {
    width: 200px !important;
  }
  .manage-button {
    padding-left: 12px;
    margin-bottom: 30px;
  }
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
.el-dialog {
  .el-dialog__header {
    background-color: #1890ff !important;
    padding: 15px 20px;
    text-align: left !important;
    .el-dialog__title {
      color: #e8f4ff !important;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #e8f4ff !important;
    }
  }
  .el-dialog__body {
    padding: 20px !important;
    .el-form-item {
      margin-bottom: 10px !important;
      .el-input {
        width: 100% !important;
      }
      .el-select {
        width: 100% !important;
      }
      .el-cascader {
        width: 100% !important;
      }
    }
    .el-form-item:last-child {
      margin-bottom: 20px !important;
    }
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
