<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"
              v-show="item.value !== 3 && item.value !== 10"
              :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"
          v-if="!isMine"
        >
          <el-input
            v-model="searchData.LoginFaildName"
            size="small"
            clearable
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item
          :label="$t('trials:loginLog:table:userName')"
          prop="LoginUserName"
          v-if="!isMine"
        >
          <el-input
            v-model="searchData.LoginUserName"
            size="small"
            clearable
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item
          :label="$t('trials:loginLog:table:userType')"
          prop="OptType"
          v-if="!isMine"
        >
          <el-select
            v-model="searchData.LoginUserTypeEnum"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item of $d.UserType"
              v-show="item.value !== 7 && item.value !== 8"
              :key="'UserType' + item.label"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </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: isMine ? 80 : 60 }"
        height="100"
        :data="list"
        class="table"
        @sort-change="handleSortByColumn"
        :default-sort="{ prop: 'CreateTime', order: 'descending' }"
      >
        <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
          sortable="custom"
        />
        <el-table-column
          v-if="!isMine"
          :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
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $fd("UserType", scope.row.LoginUserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="!isMine"
          :label="$t('trials:loginLog:table:optUserName')"
          prop="OptUserName"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          v-if="!isMine"
          :label="$t('trials:loginLog:table:optUserType')"
          prop="OptUserTypeEnum"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        >
          <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
          sortable="custom"
        />
      </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: "",
    LoginUserName: "",
    LoginUserTypeEnum: null,
    BeginDate: "",
    EndDate: "",
    Asc: false,
    SortField: "CreateTime",
    PageIndex: 1,
    PageSize: 20,
  };
};
export default {
  components: { BaseContainer, Pagination },
  props: {
    isMine: {
      type: Boolean,
      default: false,
    },
    name: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: [],
    };
  },
  mounted() {
    console.log(this.$d.UserOptType);
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.searchData.TrialId = this.$route.query.trialId;
      if (this.isMine) {
        this.searchData.LoginUserName = this.name;
      }
      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.datetimerange = null;
      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>
