<template>
  <div class="log">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item
          :label="$t('system:loginLog:label:OptType')"
          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="$t('system:loginLog:label:LoginFaildName')"
          prop="LoginFaildName"
        >
          <el-input
            v-model="searchData.LoginFaildName"
            size="small"
            clearable
            style="width:120px;"
          />
        </el-form-item>
        <el-form-item
          :label="$t('system:loginLog:label:CreateTime')"
        >
          <el-date-picker
            v-model="datetimerange"
            type="datetimerange"
            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"
          >
            {{ $t('common:button:search') }}
          </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="$t('system:loginLog:table:OptType')"
          prop="OptType"
          min-width="150"
          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="150"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('system:loginLog:table:LoginFaildName')"
          prop="LoginFaildName"
          min-width="180"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('system:loginLog:table:LoginUserName')"
          prop="LoginUserName"
          min-width="140"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('system:loginLog:table:LoginUserType')"
          prop="LoginUserTypeEnum"
          min-width="120"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType',scope.row.LoginUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:loginLog:table:OptUserName')"
          prop="OptUserName"
          min-width="200"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('system:loginLog:table:OptUserType')"
          prop="OptUserTypeEnum"
          min-width="200"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType',scope.row.OptUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:loginLog:table:CreateTime')"
          prop="CreateTime"
          min-width="180"
          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: false,
    SortField: 'CreateTime',
    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>
