<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.OptTypeList" clearable multiple :collapse-tags="true" style="width: 200px">
            <el-option v-for="item of $d.UserOptType" v-show="item.value !== 3 && item.value !== 10 && item.value !== 9"
              :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-select v-model="searchData.IdentityUserId" clearable style="width: 120px">
            <el-option v-for="item of trialUserList" :key="item.IdentityUserId" :value="item.IdentityUserId"
              :label="item.UserName" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:loginLog:table:optUserName')" prop="TargetIdentityUserId" v-if="!isMine">
          <el-select v-model="searchData.TargetIdentityUserId" clearable style="width: 120px">
            <el-option v-for="item of trialUserList" :key="item.IdentityUserId" :value="item.IdentityUserId"
              :label="item.UserName" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:loginLog:table:userType')" prop="OptType" v-if="!isMine">
          <el-select v-model="searchData.LoginUserType" 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.label" :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
          :label="$t('trials:loginLog:table:IPRegion')"
          prop="IPRegion"
          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="ActionUserName" min-width="90"
          show-overflow-tooltip sortable="custom" />
        <el-table-column :label="$t('trials:loginLog:table:Aetionlserlype')" prop="ActionUserType" min-width="100"
          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="TargetIdentityUserName"
          min-width="120" show-overflow-tooltip sortable="custom" />
        <!-- <el-table-column
          v-if="!isMine"
          :label="$t('trials:loginLog:table:optUserType')"
          prop="OptUserTypeEnum"
          min-width="160"
          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:detail')" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button v-if="scope.row.JsonObj" type="text" @click="handleOpenDialog(scope.row)">
              <span>{{ $t('trials:loginLog:message:detail') }}</span>
            </el-button>
          </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>
    <detail :config="config" :JsonObj="JsonObj" />
  </BaseContainer>
</template>
<script>
import { getUserLogList, getTrialUserList } from '@/api/user'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import detail from '@/views/system/login-log/detail.vue'
const searchDataDefault = () => {
  return {
    TrialId: '',
    OptType: null,
    OptTypeList: [],
    Ip: '',
    LoginFaildName: '',
    LoginUserName: '',
    LoginUserType: null,
    BeginDate: '',
    EndDate: '',
    Asc: false,
    SortField: 'CreateTime',
    PageIndex: 1,
    PageSize: 20,
    UserId: '',
    TargetIdentityUserId: ''
  }
}
export default {
  components: { BaseContainer, Pagination, detail },
  props: {
    isMine: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: [],
      trialUserList: [],
      config: {
        visible: false,
        title: this.$t('trials:loginLog:dialog:title'),
        width: '500px',
        top: '10vh',
      },
      JsonObj: null,
    }
  },
  mounted() {
    if (!this.isMine) {
      this.getTrialUserList()
    }
    this.getList()
  },
  methods: {
    handleOpenDialog(row) {
      if (!row.JsonObj)
        return this.$message.warning(this.$t('trials:loginLog:message:noJSON'))
      this.config.title = this.$t('trials:loginLog:dialog:title').replace(
        'xxx',
        this.$fd('UserOptType', row.OptType)
      )
      this.JsonObj = row.JsonObj
      this.config.visible = true
    },
    // 获取登录用户
    async getTrialUserList() {
      try {
        let params = {
          TrialId: this.$route.query.trialId,
        }
        let res = await getTrialUserList(params)
        if (res.IsSuccess) {
          this.trialUserList = res.Result
        }
      } catch (err) {
        console.log(err)
      }
    },
    getList() {
      this.loading = true
      this.searchData.TrialId = this.$route.query.trialId
      if (this.isMine) {
        this.searchData.IdentityUserId = this.id
      }
      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>
