<template>
 <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <el-form-item
          :label="$t('trials:loginLog:table: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('trials:loginLog:table:incorrectUserName')"
          prop="LoginFaildName"
        >
          <el-input
            v-model="searchData.LoginFaildName"
            size="small"
            clearable
            style="width:120px;"
          />
        </el-form-item>
        <el-form-item
          :label="$t('trials:loginLog:table:createTime')"
        >
          <el-date-picker
            v-model="datetimerange"
            type="datetimerange"
            :default-time="['00:00:00', '23:59:59']"
            :start-placeholder="$t('trials:loginLog:table:beginTime')"
            :end-placeholder="$t('trials:loginLog:table:endTime')"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="handleDatetimeChange"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <template slot="main-container">
      <el-table
        v-loading="loading"
        v-adaptive="{bottomOffset:60}"
        height="100"
        :data="list"
        class="table"
        @sort-change="handleSortByColumn"
      >
        <el-table-column
          type="index"
          width="50"
        />
        <el-table-column
          :label="$t('trials:loginLog:table:optType')"
          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"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('trials:loginLog:table:incorrectUserName')"
          prop="LoginFaildName"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('trials:loginLog:table:userName')"
          prop="LoginUserName"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('trials:loginLog:table:userType')"
          prop="LoginUserTypeEnum"
          min-width="90"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType',scope.row.LoginUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('trials:loginLog:table:optUserName')"
          prop="optUserName"
          min-width="90"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('trials:loginLog:table:optUserType')"
          prop="OptUserTypeEnum"
          min-width="90"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType',scope.row.OptUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('trials:loginLog:table:createTime')"
          prop="CreateTime"
          min-width="90"
          show-overflow-tooltip
        />
      </el-table>
       <!-- 分页组件 -->
       <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    </template>
  </BaseContainer>
</template>
<script>
import { getUserLogList } from '@/api/user'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    TrialId:'',
    OptType: null,
    Ip: '',
    LoginFaildName: '',
    BeginDate: '',
    EndDate: '',
    Asc: true,
    SortField: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  components: { BaseContainer,Pagination },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.searchData.TrialId = this.$route.query.trialId
      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 = ''
      }
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置列表查询
    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>
