<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.OptTypeList" clearable style="width: 200px" multiple :collapse-tags="true">
            <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:table:LoginUserName')" prop="LoginFaildName">
          <el-input v-model="searchData.LoginUserName" size="small" clearable style="width: 120px" />
        </el-form-item>
        <!-- <el-form-item :label="$t('system:loginLog:table:LoginUserType')" prop="LoginUserTypeEnum">
          <el-select v-model="searchData.LoginUserTypeEnum" clearable style="width: 120px">
            <el-option v-for="item of $d.UserType" :key="'UserType' + item.label" :value="item.value"
              :label="item.label" />
          </el-select>
        </el-form-item> -->
        <el-form-item :label="$t('system:loginLog:label:IsLoginUncommonly')" prop="IsLoginUncommonly ">
          <el-select v-model="searchData.IsLoginUncommonly" clearable style="width: 120px">
            <el-option v-for="item of $d.YesOrNo" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </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" style="width: 380px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">
            {{ $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>
      <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">
          <template slot-scope="scope">
            <span style="
                display: flex;
                align-items: center;
                justify-content: flex-end;
              ">
              <el-tooltip class="item" effect="dark" :content="$t('system:loginLog:label:IsLoginUncommonly')"
                placement="top" style="margin-right: 3px" v-if="scope.row.IsIgnoreUncommonly">
                <i v-if="scope.row.IsLoginUncommonly" class="el-icon-warning icon-i icon-success"></i>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" :content="$t('system:loginLog:label:IsLoginUncommonly')"
                placement="top" style="margin-right: 3px" v-else-if="scope.row.IsLoginUncommonly">
                <i v-if="scope.row.IsLoginUncommonly" class="el-icon-warning icon-i"
                  @click.stop="setIsIgnoreUncommonly(scope.row)"></i>
              </el-tooltip>
              <span>{{ scope.$index + 1 }}</span>
            </span>
          </template>
        </el-table-column>
        <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('trials:loginLog:table:IPRegion')" prop="IPRegion" min-width="120"
          show-overflow-tooltip sortable="custom" />
        <!-- <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="ActionUserName" min-width="140"
          show-overflow-tooltip sortable="custom" />
        <el-table-column :label="$t('system:loginLog:table:Aetionlserlype')" prop="ActionUserType" min-width="100"
          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="TargetIdentityUserName" 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:detail')" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button v-if="scope.row.JsonObj" type="text" @click="handleOpenDialog(scope.row)">
              <span>{{ $t('system:loginLog:message:detail') }}</span>
            </el-button>
          </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>
    <detail :config="config" :JsonObj="JsonObj" />
  </div>
</template>
<script>
import { getUserLogList, setIsIgnoreUncommonly } from '@/api/user'
import Pagination from '@/components/Pagination'
import detail from './detail.vue'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    OptType: null,
    OptTypeList: [],
    Ip: '',
    LoginFaildName: '',
    IsLoginUncommonly: null,
    LoginUserName: '',
    LoginUserTypeEnum: null,
    BeginDate: '',
    EndDate: '',
    Asc: false,
    SortField: 'CreateTime',
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  components: { Pagination, detail },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: [],
      config: {
        visible: false,
        title: this.$t('system:loginLog:dialog:title'),
        width: '500px',
        top: '10vh',
      },
      JsonObj: null,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 忽略异地登录
    async setIsIgnoreUncommonly(row) {
      try {
        let confirm = await this.$confirm(this.$t("system:loginLog:confirmMessage:setIsIgnoreUncommonly"))
        if (!confirm) return false
        let data = {
          IsIgnoreUncommonly: true,
          Id: row.Id
        }
        this.loading = true
        let res = await setIsIgnoreUncommonly(data)
        this.loading = false
        if (res.IsSuccess) {
          this.getList()
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    handleOpenDialog(row) {
      if (!row.JsonObj)
        return this.$message.warning(this.$t('system:loginLog:message:noJSON'))
      this.config.title = this.$t('system:loginLog:dialog:title').replace(
        'xxx',
        this.$fd('UserOptType', row.OptType)
      )
      this.JsonObj = row.JsonObj
      this.config.visible = true
    },
    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.datetimerange = []
      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;
    }
  }

  .icon-i {
    color: #f56c6c;
    cursor: pointer;
  }

  .icon-success {
    color: #67C23A
  }
}
</style>
