<template>
  <div class="log">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item
          label="操作类型"
          prop="OptType"
        >
          <el-select v-model="searchData.OptType" clearable style="width:120px;">
            <el-option
              v-for="item of $d.UserOptType"
              :key="'UserOptType' + item.label"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="IP"
          prop="IP"
        >
          <el-input
            v-model="searchData.IP"
            size="small"
            clearable
            style="width:120px;"
          />
        </el-form-item>
        <el-form-item
          label="错误用户名"
          prop="LoginFaildName"
        >
          <el-input
            v-model="searchData.LoginFaildName"
            size="small"
            clearable
            style="width:120px;"
          />
        </el-form-item>
        <el-form-item
          label="创建时间"
        >
          <el-date-picker
            v-model="datetimerange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            @change="handleDatetimeChange"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getList"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        v-adaptive="{bottomOffset:45}"
        height="100"
        :data="list"
        class="table"
        @sort-change="handleSortByColumn"
      >
        <el-table-column
          type="index"
          width="50"
        />
        <el-table-column
          label="操作类型"
          prop="OptType"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $fd('UserOptType',scope.row.OptType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="IP"
          prop="IP"
          min-width="90"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          label="错误用户名"
          prop="LoginFaildName"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="用户名"
          prop="LoginUserName"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="用户类型"
          prop="LoginUserTypeEnum"
          min-width="90"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType',scope.row.LoginUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作用户名"
          prop="OptUserName"
          min-width="90"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作用户类型"
          prop="OptUserTypeEnum"
          min-width="90"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType',scope.row.OptUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="CreateTime"
          min-width="90"
          sortable="custom"
          show-overflow-tooltip
        />
      </el-table>
      <div class="pagination" style="text-align: right;margin-top:5px;">
        <pagination
          :total="total"
          :page.sync="searchData.PageIndex"
          :limit.sync="searchData.PageSize"
          @pagination="getList"
        />
      </div>

    </div>
  </div>
</template>
<script>
import { getUserLogList } from '@/api/user'
import Pagination from '@/components/Pagination'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    OptType: null,
    Ip: '',
    LoginFaildName: '',
    BeginDate: '',
    EndDate: '',
    Asc: true,
    SortField: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  components: { Pagination },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getUserLogList(this.searchData).then((res) => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => {
        this.loading = false
      })
    },
    handleDatetimeChange(val) {
      if (val) {
        this.searchData.BeginDate = val[0]
        this.searchData.EndDate = val[1]
      } else {
        this.searchData.BeginDate = ''
        this.searchData.EndDate = ''
      }
    },
    // 重置列表查询
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    }
  }
}
</script>
<style lang="scss">
  .log {
    height: 100%;
    box-sizing: border-box;
    display: flex;
    padding: 10px;
    border-radius: 5px;
    .left {
      display: flex;
      flex-direction: column;
      width: 0;
      flex-grow: 4;
      // border-right: 1px solid #ccc;
      .filter-container {
        display: flex;
        align-items: center;
        margin: 5px;
      }
      .data-table {
        flex: 1;
        padding: 5px 0px;
      }
      .pagination-container {
        text-align: right;
      }
    }
  }
</style>
