<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParams.model.account"
        :placeholder="$t('table.loginLog.account')" class="filter-item search-item"
      />
      <el-input
        v-model="queryParams.model.location" :placeholder="$t('table.loginLog.location')"
        class="filter-item search-item"
      />
      <el-input
        v-model="queryParams.model.requestIp"
        :placeholder="$t('table.loginLog.requestIp')" class="filter-item search-item"
      />
      <el-date-picker
        v-model="queryParams.timeRange"
        :range-separator="null"
        :start-placeholder="$t('table.createTime')"
        class="filter-item search-item date-range-item"
        format="yyyy-MM-dd HH:mm:ss"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"
      />
      <el-button class="filter-item" plain type="primary" @click="search">
        {{ $t("table.search") }}
      </el-button>
      <el-button class="filter-item" plain type="warning" @click="reset">
        {{ $t("table.reset") }}
      </el-button>
      <el-dropdown
        v-has-any-permission="['loginLog:delete', 'loginLog:export']" class="filter-item"
        trigger="click"
      >
        <el-button>
          {{ $t("table.more") }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-has-permission="['loginLog:delete']" @click.native="batchDelete">
            {{ $t("table.delete") }}
          </el-dropdown-item>
          <el-dropdown-item v-has-permission="['loginLog:export']" @click.native="exportExcel">
            {{ $t("table.export") }}
          </el-dropdown-item>
          <el-dropdown-item v-has-permission="['loginLog:export']" @click.native="exportExcelPreview">
            {{ $t("table.exportPreview") }}
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown v-has-any-permission="['loginLog:delete']" class="filter-item" trigger="click">
        <el-button>
          清理日志
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="clear(1)">
            保留一个月
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(2)">
            保留三个月
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(3)">
            保留六个月
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(4)">
            保留一年
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(5)">
            保留一千条
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(6)">
            保留一万条
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(7)">
            保留三万条
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(8)">
            保留十万条
          </el-dropdown-item>
          <el-dropdown-item @click.native="clear(9)">
            清空所有
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="loading"
      :data="tableData.records"
      border
      fit
      row-key="id" style="width: 100%;" @filter-change="filterChange"
      @selection-change="onSelectChange"
      @sort-change="sortChange"
      @cell-click="cellClick"
    >
      <el-table-column align="center" type="selection" width="40px" :reserve-selection="true" />
      <el-table-column
        :label="$t('table.loginLog.userName')"
        :show-overflow-tooltip="true"
        align="center"
        min-width="80px"
        prop="userName"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.loginLog.requestIp')"
        :show-overflow-tooltip="true"
        align="center"
        min-width="80px"
        prop="requestIp"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.requestIp }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.loginLog.browser')"
        :show-overflow-tooltip="true"
        align="center"
        prop="browser"
        width="120px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.browser }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.loginLog.browserVersion')"
        :show-overflow-tooltip="true"
        align="center"
        prop="browserVersion"
        width="120px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.browserVersion }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.loginLog.operatingSystem')"
        :show-overflow-tooltip="true"
        align="center"
        prop="operatingSystem"
        width="170px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.operatingSystem }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.loginLog.location')"
        :show-overflow-tooltip="true"
        align="center"
        min-width="150px"
        prop="location"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.location }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.loginLog.loginDate')"
        :show-overflow-tooltip="true"
        align="center"
        prop="createTime"
        sortable="custom"
        width="170px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.loginLog.description')"
        :show-overflow-tooltip="true"
        align="left"
        column-key="description"
        prop="description"
      >
        <template slot-scope="scope">
          <span>
            <el-badge
              :type="scope.row.description && scope.row.description == '登录成功' ? 'success' : 'danger'"
              class="item" is-dot
            />
            {{ scope.row.description }}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('table.operation')"
        column-key="operation"
        align="center"
        class-name="small-padding fixed-width"
        width="100px"
      >
        <template slot-scope="{ row }">
          <i
            v-has-permission="['loginLog:delete']"
            class="el-icon-delete table-operation"
            style="color: #f50;"
            @click="singleDelete(row)"
          ></i>

          <el-link v-has-no-permission="['loginLog:delete']" class="no-perm">{{
            $t("tips.noPermission")
          }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.total > 0"
      :limit.sync="queryParams.size"
      :page.sync="queryParams.current"
      :total="Number(tableData.total)"
      @pagination="fetch"
    />
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      title="预览"
      width="80%"
      top="50px"
      :visible.sync="preview.isVisible"
    >
      <el-scrollbar>
        <div v-html="preview.context"></div>
      </el-scrollbar>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from "@/components/Pagination"
import loginLogApi from "@/api/LoginLog.js"
import elDragDialog from '@/directive/el-drag-dialog'
import { downloadFile, initQueryParams } from '@/utils/commons'

export default {
  name: "LoginLog",
  directives: { elDragDialog },
  components: { Pagination },
  filters: {},
  data() {
    return {
      preview: {
        isVisible: false,
        context: ''
      },
      tableKey: 0,
      loading: false,
      queryParams: initQueryParams(),
      selection: [],
      tableData: {
        total: 0
      },
      enums: {},
      dicts: {}
    }
  },
  computed: {},
  mounted() {
    this.fetch()
  },
  methods: {
    onSelectChange(selection) {
      this.selection = selection
    },
    exportExcelPreview() {
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }
      this.queryParams.map.fileName = '导出登录日志数据'
      loginLogApi.preview(this.queryParams).then(response => {
        const res = response.data
        this.preview.isVisible = true
        this.preview.context = res.data
      })
    },
    exportExcel() {
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }
      this.queryParams.map.fileName = '导出登录日志数据'
      loginLogApi.export(this.queryParams).then(response => {
        downloadFile(response)
      })
    },
    fetch(params = {}) {
      this.loading = true
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }

      this.queryParams.current = params.current ? params.current : this.queryParams.current
      this.queryParams.size = params.size ? params.size : this.queryParams.size

      loginLogApi.page(this.queryParams).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.tableData = res.data
        }
      }).finally(() => this.loading = false)
    },
    singleDelete(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDelete()
    },
    clear(type) {
      this.$confirm('确认要清除日志吗？', this.$t("common.tips"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      })
        .then(() => {
          loginLogApi.clear({ type: type }).then(response => {
            const res = response.data
            if (res.isSuccess) {
              this.$message({
                message: this.$t("tips.deleteSuccess"),
                type: "success"
              })
            }

            this.search()
          })
        })
        .catch(() => {
        })
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t("tips.noDataSelected"),
          type: "warning"
        })
        return
      }
      this.$confirm(this.$t("tips.confirmDelete"), this.$t("common.tips"), {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning"
      })
        .then(() => {
          const logIds = this.selection.map(item => item.id)
          this.delete(logIds)
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    delete(logIds) {
      loginLogApi.delete({ ids: logIds }).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.$message({
            message: this.$t("tips.deleteSuccess"),
            type: "success"
          })
        }

        this.search()
      })
    },
    search() {
      this.fetch({
        ...this.queryParams
      })
    },
    reset() {
      this.queryParams = initQueryParams()
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    sortChange(val) {
      this.queryParams.sort = val.prop
      this.queryParams.order = val.order
      if (this.queryParams.sort) {
        this.search()
      }
    },
    filterChange(filters) {
      for (const key in filters) {
        if (key.includes('.')) {
          const val = {}
          val[key.split('.')[1]] = filters[key][0]
          this.queryParams.model[key.split('.')[0]] = val
        } else {
          this.queryParams.model[key] = filters[key][0]
        }
      }
      this.search()
    },
    cellClick (row, column) {
      if (column['columnKey'] === "operation") {
        return
      }
      let flag = false
      this.selection.forEach((item) => {
        if (item.id === row.id) {
          flag = true
          this.$refs.table.toggleRowSelection(row)
        }
      })

      if (!flag) {
        this.$refs.table.toggleRowSelection(row, true)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .item {
    margin-top: 7px;
  }
</style>
