<!--
Program: Management Batch Job
Author: SDL
Description: Management Batch Job
-->
<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.type') }}</label>
              <div class="col-8">
                <!-- dropdown -->
                <select class="form-select" v-model="searchParam.commCodeId">
                  <option value="">{{ $t('sdl.common.label.all') }}</option>
                  <option v-for="code in batchTypeList" :key="code.codeId" :value="code.codeId">{{
                    getLabel(code)
                  }}</option>
                </select>
              </div>
            </div>
          </div>
        </div>
        <!-- //row1 -->
      </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-secondary btn-sm" @click="delEvent" v-authorization="'update'">
          {{ $t('sdl.common.label.delete') }}
        </button>
        <!--
        <button type="button" class="btn btn-secondary btn-sm" @click="modifyEvent" v-if="false">
          {{ $t('sdl.common.label.edit') }}
        </button>
        -->
        <button type="button" class="btn btn-secondary btn-sm" @click="addEvent" v-authorization="'update'">
          {{ $t('sdl.common.label.add') }}
        </button>
      </div>
    </div>
    <!-- TABLE  Area-->
    <table class="table table-bordered ui--table">
      <colgroup>
        <col style="width:3%" />
        <col style="width:10%" />
        <col style="width:15%" />
        <col style="width:15%" />
        <col style="width:8%" />
        <col style="width:9%" />
        <col />
        <col style="width:5%" />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">
            <input type="checkbox" @click.stop v-model="selectAll" @change="toggleSelectAll" />
          </th>
          <th scope="col">{{ $t('sdl.common.label.type') }}</th>
          <th scope="col">{{ $t('sdl.batchLogger.label.batchJobName') }}</th>
          <th scope="col">{{ $t('sdl.batchLogger.label.batchJobClass') }}</th>
          <th scope="col">{{ $t('sdl.common.label.createdBy') }}</th>
          <th scope="col">{{ $t('sdl.common.label.createdDate') }}</th>
          <th scope="col">URL</th>
          <th scope="col">{{ $t('sdl.gcm.label.update') }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="grid in gridList" :key="grid.sysResourceId">
          <td class="text-center">
            <input type="checkbox" v-model="grid.selected" @change="selectRow(grid)" />
          </td>
          <td class="text-center">{{ getLabel(grid.sysResourceType) }}</td>
          <td class="text-left">{{ $filters.cutString(grid.description, 50) }}</td>
          <td class="text-left">{{ grid.sysResourceName }}</td>
          <td class="text-left">{{ grid.firstRegrName }}</td>
          <td class="text-center">{{ $filters.dateFormat(grid.lastModDatetime) }}</td>
          <td class="text-left">{{ grid.sysResourceUrl }}</td>
          <td class="text-center">
            <button class="btn btn-outline-secondary ui--btn-xs" @click="modifyEvent(grid)" v-authorization="'update'">
              {{ $t('sdl.common.label.edit') }}
            </button>
          </td>
        </tr>
      </tbody>
    </table>
    <!-- // TABLE  Area-->
    <!-- Pagination Area-->
    <sdl-pagination
      :current-page="currentPage"
      :total-rows="records"
      :per-page="pageSize"
      @change="onSearch"
      align="center"
    />
    <!--  // Pagination Area-->
  </div>
</template>

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

export default {
  mixins: [SDLLocale],
  data() {
    return {
      batchTypeList: SDLUtil.getCommCodeList({ commCodeTypeId: 'BATCHGUBUN' }),
      batchTypeId: '',
      gridList: [],
      records: 0, // total number of data
      currentPage: 1, // current page number
      pageSize: 10, // Number of lists per page
      selectAll: false,
      searchParam: { commCodeId: '' },
    };
  },
  methods: {
    /**
     * param:
     * return:
     * exception:
     * Description : page initialization
     */
    init() {
      this.onSearch();
    },
    /**
     * param: pageIndex -> int
     * return:
     * exception:
     * Description : search
     */
    onSearch(pageIndex = 1) {
      this.currentPage = pageIndex;
      const params = {
        ...this.searchParam,
        pageIndex: this.currentPage,
        pageSize: this.pageSize,
      };
      // show Loading bar
      SDLUtil.showLoadingBar(true);
      axios
        .get(`${SDLUtil.API_URL}/batch-jobs-with-paging?${StringUtil.queryStringfy(params)}`)
        .then(response => {
          this.gridList = response.data.rows;
          this.records = response.data.records;

          this.selectAll = false;

          // hide Loading bar
          SDLUtil.showLoadingBar(false);
        })
        .catch(error => {
          SDLUtil.showLoadingBar(false);
          console.log(error);
          SDLUtil.errorAlert(error);
        });
    },
    /**
     * param:
     * return:
     * exception:
     * Description : add batchJob
     */
    addEvent() {
      SDLUtil.show(
        BatchJobPopup,
        {
          execFunc: () => {
            this.onSearch(this.currentPage);
          },
        },
        {
          width: '500px',
          height: 'auto',
        },
      );
    },
    /**
     * param:
     * return:
     * exception:
     * Description : event before deleting batchjob
     */
    delEvent() {
      const delList = [];

      this.gridList.forEach(obj => {
        if (obj.selected) {
          delList.push(obj.sysResourceId);
        }
      });

      if (delList.length === 0) {
        SDLUtil.alert('sdl.common.message.selectRow');
        return;
      }

      SDLUtil.confirm({
        msg: 'sdl.common.message.doDelete',
        title: 'sdl.common.label.delete',
        onOkEvt: () => {
          this.onDelete(delList);
        },
      });
    },
    /**
     * param:
     * return:
     * exception:
     * Description : delete batchJob
     */
    onDelete(list) {
      SDLUtil.showLoadingBar(true);
      // console.log(list);
      axios
        .delete(`${SDLUtil.API_URL}/batch-jobs`, { data: list })
        .then(() => {
          // Loading bar hidden
          SDLUtil.showLoadingBar(false);
          this.onSearch(this.currentPage);
        })
        .catch(error => {
          SDLUtil.showLoadingBar(false);
          console.log(error);
          SDLUtil.errorAlert(error);
        });
    },
    /**
     * param: item -> Object
     * return:
     * exception:
     * Description : show modify popup batchJob
     */
    modifyEvent(item) {
      SDLUtil.show(
        BatchJobPopup,
        {
          selectedItemInfo: { ...item },
          execFunc: () => {
            this.onSearch(this.currentPage);
          },
        },
        {
          width: '500px',
          height: 'auto',
        },
      );
    },
    /**
     * param: item -> Object
     * return:
     * exception:
     * Description : select batchJob
     */
    selectRow(item) {
      if (!item.selected) {
        if (this.selectAll) {
          this.selectAll = false;
        }
      }
    },
    /**
     * param:
     * return:
     * exception:
     * Description : select All Check
     */
    toggleSelectAll() {
      if (this.selectAll) {
        for (let i = 0; i < this.gridList.length; i += 1) {
          const selectItem = this.gridList[i];
          selectItem.selected = true;
          this.gridList.splice(i, 1, selectItem);
        }
      } else {
        for (let i = 0; i < this.gridList.length; i += 1) {
          const selectItem = this.gridList[i];
          selectItem.selected = false;
          this.gridList.splice(i, 1, selectItem);
        }
      }
    },
  },
  mounted() {
    this.init();
  },
};
</script>

<style scoped></style>
