<!--
 Program: RoleListPopup.vue
 Author: SDL
 Description: popup page where you can view and select a list of roles
-->
<template>
  <div class="modal-dialog modal-dialog-centered  modal-dialog-scrollable modal-lg">
    <div class="modal-content" style="height:744px;">
      <!-- contents -->
      <div class="modal-header">
        <h5 class="modal-title h4">{{ $t('sdl.role.label.heading') }} {{ $t('sdl.common.label.search') }}</h5>
        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close" @click="$emit('close')"></button>
      </div>
      <div class="modal-body">
        <!-- SEARCH Area -->
        <div class="ui--search-container clearfix">
          <div class="col-11 float-start">
            <div class="row">
              <div class="col">
                <div class="row">
                  <label class="col-4 col-form-label text-end">{{
                    $t('sdl.role.label.heading')
                  }}</label>
                  <div class="col-8">
                    <input
                      type="text"
                      class="form-control"
                      v-model="searchParam.sesearchRoleName"
                      @keyup.enter="getRoleList()"
                      @keyup.esc="searchParam.sesearchRoleName = ''"
                    />
                  </div>
                </div>
              </div>
              <div class="col">
                <div class="row">
                  <label class="col-4 col-form-label text-end">{{
                    $t('sdl.role.label.roleName')
                  }}</label>
                  <div class="col-8">
                    <input
                      type="text"
                      class="form-control"
                      v-model="searchParam.searchLabel"
                      @keyup.enter="getRoleList()"
                      @keyup.esc="searchParam.searchLabel = ''"
                    />
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- search button -->
          <div class="col-1 float-end text-end">
            <button class="btn btn-primary btn-search" type="submit" @click="getRoleList()">
              {{ $t('sdl.common.label.search') }}
            </button>
          </div>
        </div>
        <!-- TABLE  Area-->
        <table class="table table-bordered ui--table">
          <colgroup>
            <col style="width:3%" />
            <col style="width:27%" />
            <col style="width:30%" />
            <col style="width:40%" />
          </colgroup>
          <thead>
            <tr>
              <th scope="col">
                <input type="checkbox" @click.stop v-model="selectAll" @change="toggleSelectAll" />
              </th>
              <th scope="col">{{ $t('sdl.role.label.heading') }}</th>
              <th scope="col">{{ $t('sdl.role.label.roleName') }}</th>
              <th scope="col">{{ $t('sdl.common.label.description') }}</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="role in roleList" :key="role.roleId">
              <td class="text-center">
                <input type="checkbox" v-model="role.selected" @change="checkBoxClick(role)" />
              </td>
              <td class="text-left">{{ role.roleName }}</td>
              <td class="text-left">{{ getLabel(role) }}</td>
              <td class="text-left">{{ role.description }}</td>
            </tr>
          </tbody>
        </table>
        <!-- TABLE  Area-->
        <!-- Pagination  Area-->
        <sdl-pagination
          :current-page="current_page"
          :total-rows="total"
          :per-page="per_page"
          @change="handleCurrentChange"
          align="center"
        />
        <!-- Pagination  Area-->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal" @click="$emit('close')">
          {{ $t('sdl.common.label.cancel') }}
        </button>
        <button type="button" class="btn btn-primary" @click="rtnData">{{ $t('sdl.common.label.confirm') }}</button>
      </div>
      <!-- // contents -->
    </div>
  </div>
</template>

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

export default {
  mixins: [SDLLocale],
  data() {
    return {
      searchParam: {
        sesearchRoleName: '',
        searchLabel: '',
      },
      selectAll: false,
      per_page: null,
      current_page: null,
      total: null,
      roleList: [],
    };
  },
  props: {
    sesearchRoleName: {
      required: false,
      type: String,
    },
    searchLabel: {
      required: false,
      type: String,
    },
    rtnFunc: {
      type: Function,
    },
  },
  methods: {
    /**
     * param: object
     * return: nothing
     * exception: nothing
     * description: Select check box in the list of roles
     */
    checkBoxClick(item) {
      if (!item.selected) {
        if (this.selectAll) {
          this.selectAll = false;
        }
      }
    },
    /**
     * param: nothing
     * return: nothing
     * exception: nothing
     * description: Select All
     */
    toggleSelectAll() {
      if (this.roleList.length > 0) {
        this.roleList.forEach(roleItem => {
          roleItem.selected = this.selectAll;
        });
      }
    },
    /**
     * param: number
     * return: nothing
     * exception: Call error alert
     * description: Look up the list of roles
     */
    getRoleList(pageIndex = 1) {
      // Set the page number sent to the current page number
      this.current_page = pageIndex;

      // Launch Loading bar
      SDLUtil.showLoadingBar(true);

      /**
       * Set parameters to query the list of roles
       */
      const params = {
        orderBy: 'ROLE_NAME',
        roleName: this.searchParam.sesearchRoleName,
        label: this.searchParam.searchLabel,
        pageIndex: this.current_page,
        pageSize: this.per_page,
      };

      /**
       * Call role list lookup API
       */
      axios
        .get(`${SDLUtil.API_URL}/auth/roles-with-paging?${StringUtil.queryStringfy(params)}`)
        .then(({ data }) => {
          this.roleList = data.rows;
          // Apply the page number that was retrieved to the current_page
          this.current_page = data.currentPage;
          // Full Row Count
          this.total = data.records;

          /**
           * If the paging result is 1 and the query result is one, return and close
           */
          if (this.roleList.length === 1 && this.current_page === 1) {
            this.rtnFunc(this.roleList);
            this.$emit('close');
          }

          // Remove Loading bar
          SDLUtil.showLoadingBar(false);
        })
        .catch(err => {
          // Remove Loading bar
          SDLUtil.showLoadingBar(false);
          // Call error alert
          SDLUtil.errorAlert(err);
        });

      // Unselect All
      this.selectAll = false;
    },
    /**
     * param: number
     * return: nothing
     * exception: nothing
     * description: paging
     */
    handleCurrentChange(currentPage) {
      // Calling up a function that queries the list of roles
      this.getRoleList(currentPage);
    },
    /**
     * param: nothing
     * return: nothing
     * exception: nothing
     * description: Forward selected list to call page via function
     */
    rtnData() {
      const selectRoleData = this.roleList.filter(role => role.selected);

      this.rtnFunc(selectRoleData);
      this.$emit('close');
    },
  },
  created() {
    // Page size - 10 settings
    this.per_page = 10;

    if (this.sesearchRoleName !== null && this.sesearchRoleName !== undefined) {
      this.searchParam.sesearchRoleName = this.sesearchRoleName;
    }

    if (this.searchLabel !== null && this.searchLabel !== undefined) {
      this.searchParam.searchLabel = this.searchLabel;
    }

    // Calling up a function that queries the list of roles
    this.getRoleList();
  },
};
</script>
<style></style>
