<!--
Program: Link Site Popup(insert/modify)
Author: SDL
Description: insert/modify Link Site Popup
-->
<template>
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{ title }}</h5>
        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close" @click="$emit('close')"></button>
      </div>
      <div class="modal-body">
        <form name="linkSiteForm" 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.contents') }}<span class="ui--text-required">*</span>
                </th>
                <td>
                  <textarea
                    class="form-control"
                    aria-label="textarea"
                    placeholder=""
                    v-model.trim="comment"
                    v-validation=""
                    errorMessage="sdl.samsung.validate.required"
                  ></textarea>
                </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">{{ saveBtnLabel }}</button>
      </div>
    </div>
  </div>
</template>

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

export default {
  data() {
    return {
      title: '',
      saveBtnLabel: '',
      comment: '',
    };
  },
  props: {
    apprDoc: {
      type: Object,
      default: () => {},
    },
    commentType: {
      type: String,
      default: '',
    },
    execFunc: {
      type: Function,
    },
  },
  methods: {
    onBeforeSave() {
      if (SDLUtil.onSubmitValidation()) return;

      let msg = '';
      if (this.commentType === 'CONFIRM') {
        msg = this.$t('sdl.user.message.approve');
      } else if (this.commentType === 'CANCELLED') {
        msg = this.$t('sdl.approval.docStatus.message.CANCELLED');
        // } else if (this.commentType === 'AGREEMENT') {
        //   msg = '통보하시겠습니까?';
      } else if (this.commentType === 'REJECTED') {
        msg = this.$t('sdl.approval.docStatus.message.REJECTED');
      }

      SDLUtil.confirm({
        msg,
        title: this.saveBtnLabel,
        onOkEvt: () => {
          this.onSave();
        },
      });
    },
    /**
     * param:
     * return:
     * exception:
     * Description : save linksite
     */
    onSave() {
      SDLUtil.showLoadingBar(true);
      let actionApi = '';
      if (this.commentType === 'CONFIRM') {
        actionApi = `${SDLUtil.API_URL}/approval/internal-approvals/${this.apprDoc.approvalRequestId}/confirm`;
        // } else if (this.commentType === 'AGREEMENT') {
        //   actionApi = `${SDLUtil.API_URL}/approval/internal-approvals/${this.apprDoc.approvalRequestId}/agreement`;
      } else if (this.commentType === 'CANCELLED') {
        actionApi = `${SDLUtil.API_URL}/approval/internal-approvals/${this.apprDoc.approvalRequestId}/cancel`;
      } else if (this.commentType === 'REJECTED') {
        actionApi = `${SDLUtil.API_URL}/approval/internal-approvals/${this.apprDoc.approvalRequestId}/reject`;
      }

      axios
        .post(`${actionApi}?description=${this.comment}`)
        .then(() => {
          // hide Loading bar
          SDLUtil.showLoadingBar(false);
          this.execFunc();
          this.$emit('close');
        })
        .catch(error => {
          SDLUtil.showLoadingBar(false);
          console.log(error);
          SDLUtil.errorAlert(error);
        });
    },
  },

  created() {
    this.title = this.$t('sdl.batchLogger.label.comment');
    if (this.commentType === 'CONFIRM') {
      this.title = this.$t('sdl.approval.label.comment');
      this.saveBtnLabel = `${this.$t('sdl.approval.label.APPROVED')}/${this.$t('sdl.approval.label.AGREEMENT')}`;
    } else if (this.commentType === 'CANCELLED') {
      this.saveBtnLabel = this.$t('sdl.approval.docStatus.label.CANCELLED');
      // } else if (this.commentType === 'AGREEMENT') {
      //   this.saveBtnLabel = this.$t('sdl.approval.label.AGREEMENT');
    } else if (this.commentType === 'REJECTED') {
      this.saveBtnLabel = this.$t('sdl.approval.docStatus.label.REJECTED');
    }
  },
};
</script>
<style></style>
