<!--
  Program: Common code
  Author: SDL
  Brief: Common code list
-->
<template>
  <!-- SUB Contents -->
  <div class="ui--content-wrapper">
    <sdl-breadcrumb></sdl-breadcrumb>

    <div class="ui--search-container clearfix">
      <div class="col-10 float-start">
        <!-- row1 -->
        <div class="row">
          <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">
                  <!-- dropdown -->
                  <select class="form-select" v-model="searchCondition">
                    <option value="commCodeTypeCode">{{ $t('sdl.commonCode.label.code') }}</option>
                    <option value="label">{{ $t('sdl.commonCode.label.codeName') }}</option>
                    <option value="description">{{ $t('sdl.common.label.description') }}</option>
                  </select>
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="searchKeyword"
                    @keyup.esc="searchKeyword = ''"
                    @keyup.enter="getList()"
                  />
                </div>
              </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">
        <button type="button" class="btn btn-primary btn-sm" @click="newGroupCode()">
          {{ $t('sdl.common.label.register') }}
        </button>
      </div>
    </div>
    <!-- TABLE  Area-->
    <table class="table table-bordered ui--table">
      <colgroup>
        <col style="width:10%" />
        <col style="width:10%" />
        <col style="width:30%" />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">{{ $t('sdl.commonCode.label.code') }}</th>
          <th scope="col">{{ $t('sdl.commonCode.label.codeNameDefault') }}</th>
          <th scope="col">{{ $t('sdl.common.label.description') }}</th>
          <th scope="col" v-for="(locale, index) in $i18n.availableLocales" :key="'th' + index">{{ $t('sdl.commonCode.label.codeName') }}({{ locale }})</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in codeList" :key="item.commCodeTypeCode" class="cursor-pointer" @click="moveDetail(item)">
          <td>{{ item.commCodeTypeCode }}</td>
          <td>{{ item.label }}</td>
          <td>{{ item.description }}</td>
          <td v-for="(locale, index) in $i18n.availableLocales" :key="'td' + index">{{ item.labelJson[locale] }}</td>
        </tr>
      </tbody>
    </table>
    <sdl-pagination
      :current-page="currentPage"
      :total-rows="records"
      :per-page="pageSize"
      @change="movePage"
      align="center"
    />
  </div>
</template>

<script>
import axios from 'axios';
import SDLUtil from '@/utils/SDLUtil';
import SDLLocale from '@/mixin/SDLLocale';
import StoreParams from '@/mixin/StoreParams';

export default {
  mixins: [SDLLocale, StoreParams],
  data() {
    return {
      searchKeyword: '',
      searchCondition: 'commCodeTypeCode',
      codeList: [],
      records: 0, // total number of data
      currentPage: 1, // current page number
      pageSize: 10, // number of lists per page
      pageUnit: 5,
    };
  },
  created() {
    const storeParams = this.getStoreParameter();
    // Restore if there are stored parameters
    if (storeParams) {
      this.searchCondition = storeParams.searchCondition;
      this.searchKeyword = storeParams.searchKeyword;
      this.pageIndex = storeParams.pageIndex;
      this.pageSize = storeParams.pageSize;

      this.getList(this.pageIndex);
    } else {
      // Logic when entered without stored parameters
      this.getList();
    }
  },
  methods: {
    getList(pageIndex = 1) {
      const params = {
        pageIndex,
        pageSize: this.pageSize,
        searchCondition: this.searchCondition,
        searchKeyword: this.searchKeyword,
      };
      this.setStoreParameter(params);
      // Loading bar execution
      SDLUtil.showLoadingBar(true);
      axios.get(`${SDLUtil.API_URL}/commcode/groupcodes-with-paging`, { params }).then(({ data }) => {
        this.currentPage = pageIndex;
        this.codeList = data.rows;
        this.records = data.records;
        // Loading bar hidden
        SDLUtil.showLoadingBar(false);
      });
    },
    movePage(pageIndex) {
      this.getList(pageIndex);
    },
    moveDetail(item) {
      this.$router.push({ path: `${this.$CONTEXT}/admin/etc/groupcodes/detail/${item.commCodeTypeCode}` });
    },
    newGroupCode() {
      this.$router.push({ path: `${this.$CONTEXT}/admin/etc/groupcodes/new` });
    },
  },
};
</script>
