<!--
Program: Batch Job Popup(insert/modify)
Author: SDL
Description: insert/modify Batch Job Popup
-->
<template>
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" v-if="saveType === 'add'">
          {{ $t('sdl.batchLogger.label.batchJob') }} {{ $t('sdl.common.label.add') }}
        </h5>
        <h5 class="modal-title" v-else>{{ $t('sdl.batchLogger.label.batchJob') }} {{ $t('sdl.common.label.edit') }}</h5>
        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close" @click="$emit('close')"></button>
      </div>
      <div class="modal-body">
        <form name="batchJobForm" class="needs-validation" novalidate="" @submit.prevent>
          <table class="table table-bordered ui--table-form ui--table-form-view">
            <colgroup>
              <col style="width:30%" />
              <col style="width:70%" />
            </colgroup>
            <tbody>
              <tr>
                <th class="text-nowrap" scope="row">
                  {{ $t('sdl.common.label.type') }}<span class="ui--text-required">*</span>
                </th>
                <td>
                  <select class="form-select d-block w-100" v-model="batchJobInfo.sysResourceTypeCodeId" v-validation="" errorMessage="sdl.samsung.validate.selected">
                    <option value="">{{ $t('sdl.common.label.select') }}</option>
                    <option v-for="code in batchTypeList" :key="code.codeId" :value="code.codeId">{{
                      getLabel(code)
                    }}</option>
                  </select>
                </td>
              </tr>
              <tr>
                <th class="text-nowrap" scope="row">
                  {{ $t('sdl.batchLogger.label.batchJobName') }}<span class="ui--text-required">*</span>
                </th>
                <td>
                  <input type="text" class="form-control" placeholder="" v-model.trim="batchJobInfo.description" v-validation="" errorMessage="sdl.samsung.validate.required" />
                </td>
              </tr>
              <tr>
                <th class="text-nowrap" scope="row">
                  {{ $t('sdl.batchLogger.label.batchJobClass') }}<span class="ui--text-required">*</span>
                </th>
                <td>
                  <input type="text" class="form-control" placeholder="" v-model.trim="batchJobInfo.sysResourceName" v-validation="" errorMessage="sdl.samsung.validate.required" />
                </td>
              </tr>
              <tr>
                <th class="text-nowrap" scope="row">{{ $t('sdl.common.label.createdBy') }}</th>
                <td><input type="text" class="form-control" disabled v-model.trim="batchJobInfo.firstRegrName" /></td>
              </tr>
              <tr>
                <th class="text-nowrap" scope="row">{{ $t('sdl.common.label.createdDate') }}</th>
                <td>
                  <input type="text" class="form-control" placeholder="" disabled v-model.trim="batchJobInfo.firstRegDatetime" />
                </td>
              </tr>
              <tr>
                <th class="text-nowrap" scope="row">URL<span class="ui--text-required">*</span></th>
                <td>
                  <input type="text" class="form-control" placeholder="" v-model.trim="batchJobInfo.sysResourceUrl" v-validation="" errorMessage="sdl.samsung.validate.required" />
                </td>
              </tr>
            </tbody>
          </table>
        </form>
      </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="onBeforeSave">{{ $t('sdl.common.label.save') }}</button>
      </div>
    </div>
  </div>
</template>

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

export default {
  mixins: [SDLLocale],
  data() {
    return {
      batchTypeList: SDLUtil.getCommCodeList({ commCodeTypeId: 'BATCHGUBUN' }),
      batchJobInfo: {
        sysResourceId: '',
        sysResourceName: '',
        description: '',
        firstRegrId: SDLUtil.getLoginedUserInfo().userId,
        firstRegrName: SDLUtil.getLoginedUserInfo().userName,
        firstRegDatetime: moment().format(SDLUtil.getMsgProp('data-format.date.ymd')),
        sysResourceUrl: '',
        sysResourceTypeCodeId: '',
        deleted: false,
        label: 'User Mgmt',
        lastModDatetime: '2019-07-31T06:34:20',
        lastModrId: 'sdp-front',
        sysId: 'SYSTEM',
      },
      saveType: 'add',
    };
  },
  props: {
    selectedItemInfo: {
      type: Object,
      default: () => {},
    },
    execFunc: {
      type: Function,
    },
  },
  methods: {
    /**
     * param:
     * return:
     * exception:
     * Description : event before Saving
     */
    onBeforeSave() {
      if (SDLUtil.onSubmitValidation()) return;

      SDLUtil.confirm({
        msg: 'sdl.common.message.doSave',
        title: 'sdl.common.label.save',
        onOkEvt: () => {
          this.onSave();
        },
      });
    },
    /**
     * param:
     * return:
     * exception:
     * Description : save BatchJob
     */
    onSave() {
      SDLUtil.showLoadingBar(true);
      if (this.saveType === 'add') {
        const { description, sysResourceName, sysResourceTypeCodeId, sysResourceUrl } = this.batchJobInfo;
        const param = { description, sysResourceName, sysResourceTypeCodeId, sysResourceUrl };
        axios
          .post(`${SDLUtil.API_URL}/batch-jobs`, param)
          .then(() => {
            // hide Loading bar
            SDLUtil.showLoadingBar(false);
            this.execFunc();
            this.$emit('close');
          })
          .catch(error => {
            SDLUtil.showLoadingBar(false);
            console.log(error);
            SDLUtil.errorAlert(error);
          });
      } else {
        const {
          sysResourceId,
          description,
          sysResourceName,
          sysResourceTypeCodeId,
          sysResourceUrl,
        } = this.batchJobInfo;
        const param = { sysResourceId, description, sysResourceName, sysResourceTypeCodeId, sysResourceUrl };

        axios
          .put(`${SDLUtil.API_URL}/batch-jobs`, param)
          .then(() => {
            // Loading bar hidden
            SDLUtil.showLoadingBar(false);
            this.execFunc();
            this.$emit('close');
          })
          .catch(error => {
            SDLUtil.showLoadingBar(false);
            console.log(error);
            SDLUtil.errorAlert(error);
          });
      }
    },
  },

  created() {
    if (typeof this.selectedItemInfo !== 'undefined') {
      this.batchJobInfo = Object.assign(this.batchJobInfo, this.selectedItemInfo);
      this.batchJobInfo.firstRegDatetime = moment(this.selectedItemInfo.firstRegDatetime).format(
        SDLUtil.getMsgProp('data-format.date.ymd'),
      );
      this.saveType = 'modify';
    }
  },
};
</script>
<style></style>
