<template>
  <!-- SUB Contents -->
  <div class="ui--content-wrapper">
    <sdl-breadcrumb></sdl-breadcrumb>
    <!-- SEARCH Area -->
    <div class="ui--search-container clearfix">
      <div class="col-10 float-start">
        <div class="row">
          <div class="col-6">
            <!-- form -->
            <div class="row">
              <label class="col-4 col-form-label text-end">{{ $t('sdl.common.label.type') }}</label>
              <div class="col-8">
                <!-- dropdown -->
                <select class="form-select" v-model="reqType">
                  <option :value="connectType.code" v-for="connectType in connectTypeList" :key="connectType.codeId">{{
                    getLabel(connectType)
                  }}</option>
                </select>
              </div>
            </div>
          </div>
          <div class="col-6">
            <!-- form -->
            <div class="row">
              <label class="col-4 col-form-label text-end">{{ $t('sdl.common.label.searchCondition') }}</label>
              <div class="col-8">
                <div class="input-group">
                  <select class="form-select" v-model="searchCondition">
                    <option :value="loginType.code" v-for="loginType in loginTypeList" :key="loginType.codeId">
                      {{ getLabel(loginType) }}
                    </option>
                  </select>
                  <input class="form-control" type="text" v-model="searchKeyword" @keydown.enter="getList()" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="row mt-2">
          <div class="col-12">
            <!-- form -->
            <div class="row">
              <label class="col-2 col-form-label text-end">{{ $t('sdl.common.label.period') }}</label>
              <div class="col-10 form-inline">
                <sdl-datepicker v-model="searchPeriod" :period="searchPeriod"></sdl-datepicker>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- search button -->
      <div class="col-2 float-end text-end">
        <button class="btn btn-primary btn-search" type="submit" @click="getList()">
          {{ $t('sdl.common.label.search') }}
        </button>
      </div>
    </div>
    <!-- LIST Header & buttons -->
    <div class="ui--list-heading clearfix">
      <span class="ui--text-total"><strong>Total</strong> {{ $filters.numberFormat(records) }}</span>
    </div>
    <!-- TABLE  Area-->
    <table class="table table-bordered ui--table">
      <colgroup>
        <col style="width:7%" />
        <col style="width:17%" />
        <col style="width:20%" />
        <col style="width:13%" />
        <col style="width:15%" />
        <col style="width:15%" />
        <col />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">{{ $t('sdl.common.label.type') }}</th>
          <th scope="col">{{ $t('sdl.common.label.compName') }}</th>
          <th scope="col">{{ $t('sdl.common.label.department') }}</th>
          <th scope="col">{{ $t('sdl.common.label.name') }}</th>
          <th scope="col">{{ $t('sdl.identity.log.label.loginTime') }}</th>
          <th scope="col">{{ $t('sdl.identity.log.label.logoutTime') }}</th>
          <th scope="col">IP</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="log in logList" :key="log.no">
          <td class="text-center">{{ log.reqType }}</td>
          <td>{{ log.compName }}</td>
          <td>{{ log.deptName }}</td>
          <td class="text-left">{{ log.userName }}</td>
          <td class="text-center">{{ $filters.dateFormat(log.loginTime, $t('data-format.datetime.ymdhms')) }}</td>
          <td class="text-center">{{ $filters.dateFormat(log.logoutTime, $t('data-format.datetime.ymdhms')) }}</td>
          <td class="text-center">{{ log.useIp }}</td>
        </tr>
      </tbody>
    </table>
    <!-- // TABLE  Area-->
    <!-- Pagination Area-->
    <sdl-pagination
      :current-page="currentPage"
      :total-rows="records"
      :per-page="pageSize"
      @change="movePage"
      align="center"
    />
    <!-- // Pagination Area-->
  </div>
</template>

<script>
import axios from 'axios';
import { SDLUtil, DateUtil, StringUtil } from '@/utils';
import SDLLocale from '@/mixin/SDLLocale';

export default {
  data() {
    return {
      searchCondition: 'userName',
      searchKeyword: '',
      minimumView: '',
      searchPeriod: {},
      reqType: '',
      connectTypeList: [],
      loginTypeList: [],
      logList: [],
      records: 0, // total number of data
      currentPage: 1, // current page number
      pageSize: 10, // number of lists per page
    };
  },
  mixins: [SDLLocale],
  methods: {
    /**
     * param:
     * return:
     * exception:
     * description : Initialize Page
     */
    init() {
      this.searchPeriod.startDate = DateUtil.subDate(1, 'M');
      this.searchPeriod.endDate = DateUtil.now();
      this.getList();
    },
    /**
     * param: pageIndex -> Int (default: 1)
     * return:
     * exception:
     * description : Search HistoryList
     */
    getList(pageIndex = 1) {
      // Change to date format standard at API request ('YYYY-MM-DD')
      const searchPeriod = DateUtil.stdFormat(this.searchPeriod);
      const params = {
        pageIndex,
        pageSize: this.pageSize,
        searchCondition: this.searchCondition,
        searchKeyword: this.searchKeyword,
        reqType: this.reqType === 'ALL' ? '' : this.reqType,
        ...searchPeriod,
      };
      SDLUtil.showLoadingBar(true);
      axios
        .get(`${SDLUtil.API_URL}/history/login-out-logs?${StringUtil.queryStringfy(params)}`)
        .then(({ data }) => {
          this.logList = data.rows;
          this.currentPage = pageIndex;
          this.records = data.records;
          SDLUtil.showLoadingBar(false);
        })
        .catch(err => {
          console.error(err);
          SDLUtil.showLoadingBar(false);
        });
    },
    /**
     * param: pageIndex -> Int
     * return:
     * exception:
     * description : Pagination move
     */
    movePage(pageIndex) {
      this.getList(pageIndex);
    },
  },
  mounted() {
    this.$nextTick(() => {
      console.info('initialize Login History Page');

      // connection type
      axios.get(`${SDLUtil.API_URL}/commcode/groupcodes/CONNECT_TYPE/commcodes`).then(({ data }) => {
        this.connectTypeList = data;
      });

      // connection type
      axios.get(`${SDLUtil.API_URL}/commcode/groupcodes/LOGIN_SEARCH_TYPE/commcodes`).then(({ data }) => {
        this.loginTypeList = data;
      });

      // connection type - all set
      this.reqType = 'ALL';

      this.init();
    });
  },
};
</script>

<style scoped></style>
