<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.name') }}</label>
              <div class="col-8">
                <input class="form-control" type="text" v-model="userName" @keydown.enter="getList()" />
              </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">
                <div class="form-check form-check-inline">
                  <input
                    class="form-check-input"
                    v-model="datePickerUnit"
                    type="radio"
                    name="datePickerUnit"
                    id="rdoMonth"
                    value="month"
                  />
                  <label class="form-check-label" for="rdoMonth">{{ $t('sdl.common.label.month') }}</label>
                </div>
                <div class="form-check form-check-inline">
                  <input
                    class="form-check-input"
                    v-model="datePickerUnit"
                    type="radio"
                    name="datePickerUnit"
                    id="rdoDay"
                    value="day"
                  />
                  <label class="form-check-label" for="rdoDay">{{ $t('sdl.common.label.day') }}</label>
                </div>
                <sdl-datepicker
                  v-model="searchPeriod"
                  :period="searchPeriod"
                  :minimum-view="datePickerUnit"
                ></sdl-datepicker>
              </div>
            </div>
          </div>
        </div>
        <!-- //row1 -->
      </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 class="float-end">
        <sdl-exceldownloadbtn :excelInfo="getExcelInfo()" class="btn btn-secondary btn-sm" />
      </div>
    </div>
    <!-- TABLE  Area-->
    <table class="table table-bordered ui--table">
      <colgroup>
        <col style="width:17%" />
        <col style="width:25%" />
        <col style="width:17%" />
        <col style="" />
        <col style="width:10%" />
      </colgroup>
      <thead>
        <tr>
          <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.common.label.fileName') }}</th>
          <th scope="col">{{ $t('sdl.common.label.useDate') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="log in logList" :key="log.no">
          <td>{{ log.user.compName }}</td>
          <td>{{ log.user.deptName }}</td>
          <td>{{ log.user.userName }}</td>
          <td>{{ log.description }}</td>
          <td v-if="displayDateUnit === 'month'" class="text-center">
            {{ $filters.dateFormat(log.useFromDate, $t('data-format.date.ym')) }}
          </td>
          <td v-if="displayDateUnit === 'day'" class="text-center">
            {{ $filters.dateFormat(log.useFromDate, $t('data-format.date.ymd')) }}
          </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 { DateUtil, SDLUtil, StringUtil } from '@/utils';

export default {
  data() {
    return {
      userName: '',
      reqType: '',
      datePickerUnit: 'day',
      displayDateUnit: '',
      searchPeriod: {},
      logList: [],
      records: 0, // total number of data
      currentPage: 1, // current page number
      pageSize: 10, // number of lists per page
    };
  },
  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')
      let searchPeriod = DateUtil.stdFormat(this.searchPeriod);

      // If the period is set to Monthly, set by changing the start date and the end date to Month.
      if (this.datePickerUnit !== 'day') {
        searchPeriod = DateUtil.stdFormat(searchPeriod, 'YYYY-MM');
      }

      const params = {
        pageIndex,
        pageSize: this.pageSize,
        userName: this.userName,
        periodFlag: this.datePickerUnit === 'day' ? 2 : 1,
        ...searchPeriod,
      };
      SDLUtil.showLoadingBar(true);
      axios
        .get(`${SDLUtil.API_URL}/history/file-download-logs?${StringUtil.queryStringfy(params)}`)
        .then(({ data }) => {
          this.logList = data.rows;
          this.currentPage = pageIndex;
          this.records = data.records;
          this.displayDateUnit = this.datePickerUnit;
          SDLUtil.showLoadingBar(false);
        })
        .catch(err => {
          console.error(err);
          SDLUtil.showLoadingBar(false);
        });
    },
    /**
     * param:
     * return:
     * exception:
     * description : excel file download
     */
    getExcelInfo() {
      let searchPeriod = DateUtil.stdFormat(this.searchPeriod);

      // If the period is set to Monthly, set by changing the start date and the end date to Month.
      if (this.datePickerUnit !== 'day') {
        searchPeriod = DateUtil.stdFormat(searchPeriod, 'YYYY-MM');
      }

      return {
        columnInfoFile: 'fileDownloadLogExcel',
        fileName: SDLUtil.getMsgProp('sdl.excel.fileDownLoadLog.title'),
        queryId: 'selectFileDownloadLogList',
        sheetName: SDLUtil.getMsgProp('sdl.excel.fileDownLoadLog.title'),
        useNumberField: 'Y',
        param: {
          userName: this.userName,
          reqType: this.reqType,
          periodFlag: this.datePickerUnit === 'day' ? 2 : 1,
          ...searchPeriod,
        },
      };
    },
    /**
     * param: pageIndex -> Int
     * return:
     * exception:
     * description : Pagination move
     */
    movePage(pageIndex) {
      this.getList(pageIndex);
    },
  },
  mounted() {
    this.$nextTick(() => {
      console.info('initialize FileDownload History Page');
      this.init();
    });
  },
};
</script>

<style scoped></style>
