<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-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>
    <!-- TABLE  Area-->
    <table class="table table-bordered ui--table">
      <colgroup>
        <col style="width:10%" />
        <col style="" />
        <col style="width:12%" />
        <col style="width:10%" />
        <col style="width:10%" />
        <col style="width:10%" />
        <col style="width:10%" />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">{{ $t('sdl.common.label.useDate') }}</th>
          <th scope="col">{{ $t('sdl.menu.label.menuPath') }}</th>
          <th scope="col">{{ $t('sdl.menu.menuType') }}</th>
          <th scope="col">{{ $t('sdl.menu.menuUsePeriod') }}</th>
          <th scope="col">{{ $t('sdl.menu.menuUserCount') }}</th>
          <th scope="col">Hits</th>
          <th scope="col">{{ $t('sdl.menu.menuUtilization') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(log, idx) in logList" :key="idx">
          <td v-if="displayDateUnit === 'month'" class="text-center">
            {{ $filters.dateFormat(log.useDate, $t('data-format.date.ym')) }}
          </td>
          <td v-if="displayDateUnit === 'day'" class="text-center">
            {{ $filters.dateFormat(log.useDate, $t('data-format.date.ymd')) }}
          </td>
          <td>{{ log.menuPathName }}</td>
          <td>{{ getLabel(log) }}</td>
          <td class="text-center">{{ log.menuUsePeriodName }}</td>
          <td class="text-end">{{ $filters.numberFormat(log.userNum) }}</td>
          <td class="text-center">{{ $filters.numberFormat(log.hitCount) }}</td>
          <td class="text-end">{{ $filters.numberFormat(log.utilizePercent) }}%</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';
import SDLLocale from '@/mixin/SDLLocale';

export default {
  mixins: [SDLLocale],
  data() {
    return {
      datePickerUnit: 'month',
      searchPeriod: {},
      displayDateUnit: '',
      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) {
      let { searchPeriod } = this;

      // 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(this.searchPeriod, 'YYYY-MM');
      } else {
        searchPeriod = DateUtil.stdFormat(this.searchPeriod);
      }

      const params = {
        pageIndex,
        pageSize: this.pageSize,
        periodFlag: this.datePickerUnit === 'day' ? 2 : 1,
        ...searchPeriod,
      };
      SDLUtil.showLoadingBar(true);
      axios
        .get(`${SDLUtil.API_URL}/menuutilization/menu-utilizations?${StringUtil.queryStringfy(params)}`)
        .then(({ data }) => {
          this.logList = data;
          this.currentPage = pageIndex;
          this.records = data.length;
          this.displayDateUnit = this.datePickerUnit;
          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 Menu Utilization History Page');
      this.init();
    });
  },
};
</script>

<style scoped></style>
