<!--
Program: Approval Sample List
Author: SDL
Description: Approval Sample List
-->
<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">
                <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="button" @click="onSearch()">
          {{ $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">
        <button type="button" class="btn btn-primary btn-sm" v-authorization="'update'" @click="onApprWrite">
          {{ $t('sdl.approval.label.APPROVE') }}
        </button>
      </div>
    </div>
    <!-- TABLE  Area-->
    <table class="table table-bordered ui--table">
      <colgroup>
        <col />
        <col style="width:10%" />
        <col style="width:8%" />
        <col style="width:20%" />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">{{ $t('sdl.approval.label.title') }}</th>
          <th scope="col">{{ $t('sdl.approval.label.approvalStatus') }}</th>
          <th scope="col">{{ $t('sdl.approval.label.requester') }}</th>
          <th scope="col">{{ $t('sdl.common.label.createdDate') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="grid in gridList"
          :key="grid.approvalStepSequence"
          @click="rowClick(grid.docId)"
          class="cursor-pointer"
        >
          <td class="text-left">{{ grid.title }}</td>
          <td class="text-center">
            <span v-if="grid.docStatus === 'SAVED'" class="badge ui--status-primary">{{
              $t('sdl.approval.docStatus.label.SAVED')
            }}</span>
            <span v-else-if="grid.docStatus === 'INPROCESS'" class="badge ui--status-processing">{{
              $t('sdl.approval.docStatus.label.INPROCESS')
            }}</span>
            <span v-else-if="grid.docStatus === 'CANCELLED'" class="badge ui--status-secondary">{{
              $t('sdl.approval.label.CANCELLED')
            }}</span>
            <span v-else-if="grid.docStatus === 'REJECTED'" class="badge ui--status-rejected">{{
              $t('sdl.approval.label.REJECTED')
            }}</span>
            <span v-else-if="grid.docStatus === 'COMPLETED'" class="badge ui--status-success">{{
              $t('sdl.approval.docStatus.label.COMPLETED')
            }}</span>
          </td>
          <td>{{ grid.creator }}</td>
          <td class="text-center">{{ $filters.dateFormat(grid.lastModDatetime, $t('data-format.datetime.ymdhms')) }}</td>
        </tr>
      </tbody>
    </table>
    <!-- // TABLE  Area-->
    <!-- Pagination Area-->
    <sdl-pagination
      :current-page="currentPage"
      :total-rows="records"
      :per-page="pageSize"
      @change="onSearch"
      text-align="center"
    />
    <!--  // Pagination Area-->
  </div>
</template>

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

export default {
  data() {
    return {
      gridList: [],
      records: 0,
      currentPage: 1,
      pageSize: 10,
      searchPeriod: {},
      internalApproval: false,
    };
  },
  mixins: [StoreParams],
  methods: {
    /**
     * param:
     * return:
     * exception:
     * Description : page initialization
     */
    init() {
      const storeParams = this.getStoreParameter();
      if (storeParams) {
        this.searchPeriod.startDate = storeParams.startDate;
        this.searchPeriod.endDate = storeParams.endDate;
        this.onSearch(storeParams.pageIndex);
      } else {
        this.searchPeriod.startDate = DateUtil.subDate(1, 'M');
        this.searchPeriod.endDate = DateUtil.now();
        this.onSearch();
      }
      // console.log(this.$route);
    },
    /**
     * param: pageIndex -> int
     * return:
     * exception:
     * Description : search
     */
    onSearch(pageIndex = 1) {
      this.currentPage = pageIndex;
      // eslint-disable-next-line no-underscore-dangle
      const _searchPeriod = DateUtil.stdFormat(this.searchPeriod);
      const params = {
        ..._searchPeriod,
        pageIndex: this.currentPage,
        pageSize: this.pageSize,
        internalApproval: this.internalApproval,
      };
      this.setStoreParameter(params);
      let listApi = `${SDLUtil.API_URL}/sample/approval/knox/sample-document-with-paging?${StringUtil.queryStringfy(params)}`;
      // show Loading bar
      SDLUtil.showLoadingBar(true);
      axios
        .get(listApi)
        .then(response => {
          this.gridList = response.data.rows;
          this.records = response.data.records;

          // hide Loading bar
          SDLUtil.showLoadingBar(false);
        })
        .catch(error => {
          SDLUtil.showLoadingBar(false);
          SDLUtil.errorAlert(error);
          console.log(error);
        });
    },
    /**
     * param:
     * return:
     * exception:
     * Description : go sample registration
     */
    onApprWrite() {
      if (SDLUtil.getLoginedUserInfo().serverLocation !== null) {
        this.$router.push({ path: `${this.$CONTEXT}/admin/sample/approval/knox/apprSampleWrite` });
      } else {
        SDLUtil.alert('sdl.approval.message.noInformation');
      }
    },
    /**
     * param:
     * return:
     * exception:
     * Description : go sample detail
     */
    rowClick(docId) {
      this.$router.push({ path: `${this.$CONTEXT}/admin/sample/approval/knox/apprSampleWrite/${docId}` });
    },
  },
  created() {
    this.init();
  },
};
</script>

<style scoped></style>
