<template>
  <div class="eibs-tab">
    <!-- 左边 -->
    <c-col :span="12" style="padding-right: 20px">
      <c-col :span="24">
        <el-form-item label="Special Payment Conditions for Bank only" prop="ledgrp.blk.spcrcb">
          <c-fullbox>
            <c-input
                    type="textarea"
                    maxlength="52000"
                    v-model="model.ledgrp.blk.spcrcb"
                    :rows="20"
                    show-word-limit
                    @change="handleSpcrcbChanged"
                    :disabled="isSpcrcbDisabled"></c-input>
          </c-fullbox>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <c-ptap-commons  :cols="65" :rows="800" :minRows="12" :maxRows="12" :model="model"
                         :disabledButton="isUsrblkDisabled"
                         :isRules='false'
                         :extCodes="['letamep','spcrcbamep']"
                         lastModel="spcrcb"
                         title="Adding/Deleting Text in Current Amendment">
        </c-ptap-commons>

      </c-col>
    </c-col>
    <!-- 右边 -->
    <c-col :span="12" style="padding-left: 20px">
      <c-col :span="24">
        <el-form-item label="Historic Overview" prop="oldledgrp.blk.spcrcbame">
          <c-fullbox>
            <c-input
                    disabled
                    type="textarea"
                    maxlength="52000"
                    v-model="model.oldledgrp.blk.spcrcbame"
                    :rows="20"
                    show-word-limit
                    placeholder="历史修改信息"></c-input>
          </c-fullbox>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <c-col :span="24" class="">
          <el-form-item label=" " prop="letamep.spcrcbamep.modflg" label-width="180px" style="float: left">
            <c-checkbox
                    v-model="model.letamep.spcrcbamep.modflg"
                    class="checkbox-left"
                    true-label="X"
                    false-label=""
                    :disabled="this.model.letamep.spcrcbamep.modmanflg == 'X'"
                    @change="onModflgChange"
            >Modify Text to Replace</c-checkbox
            >
          </el-form-item>
        </c-col>
        <c-col :span="24" class="">
          <el-form-item label=" " prop="letamep.spcrcbamep.modmanflg" label-width="180px" style="float: left">
            <c-checkbox
                    v-model="model.letamep.spcrcbamep.modmanflg"
                    class="checkbox-left"
                    true-label="X"
                    false-label=""
                    :disabled="this.model.letamep.spcrcbamep.modflg == 'X'"
                    @change="onModmanflgChange"
            >Modify Text Internally</c-checkbox
            >
          </el-form-item>
        </c-col>
      </c-col>
      <c-col :span="24">
        <el-form-item label-width="160px" style="float: left">
          <text-diff
                  :leftText="model.oldledgrp.blk.spcrcb"
                  :rightText="model.ledgrp.blk.spcrcb"
                  :isShowScale="false"
                  :disabled="model.ledgrp.blk.spcrcb === model.oldledgrp.blk.spcrcb" />
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label-width="180px" style="float: left">
          <c-button
                  type="primary"
                  @click="onAddClick"
                  :disabled="this.model.letamep.spcrcbamep.spcrcb == null || this.model.letamep.spcrcbamep.spcrcb == ''">
            Add
          </c-button>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label-width="180px" style="float: left">
          <c-button
                  type="primary"
                  @click="onDeleteClick"
                  :disabled="this.model.letamep.spcrcbamep.spcrcb == null || this.model.letamep.spcrcbamep.spcrcb == ''">
            Delete
          </c-button>
        </el-form-item>
      </c-col>
    </c-col>
  </div>
</template>
<script>
  import event from "../event";
  import moment from 'moment';
  import TextDiff from "~/components/business/TextDiff";
  import commonDepend from "~/mixin/commonDepend";
  export default {
    components: { TextDiff },
    inject: ["root"],
    props: ["model", "codes"],
    mixins: [event,commonDepend],
    data() {
      return {
        isSpcrcbDisabled: true,
        isUsrblkDisabled: false,
      };
    },
    methods: {
      onAddClick() {
        if (this.model.letamep.spcrcbamep.spcrcb != null && this.model.letamep.spcrcbamep.spcrcb != "") {
          this.model.ledgrp.blk.spcrcb = this.model.ledgrp.blk.spcrcb + "\n" + this.model.letamep.spcrcbamep.spcrcb;

          this.model.ledgrp.blk.spcrcbame = this.model.ledgrp.blk.spcrcbame+"\n"
                  +"*** Amendment "+this.model.letamep.amenbr+ " Date:"+ moment(new Date()).format("MM/DD/YY")+":"+"\n"
                  +"/ADD/"+"\n"
                  +this.model.letamep.spcrcbamep.spcrcb+"\n";

          //设置spcrcb被修改过
          this.setSpcrcbModified();
          this.setSpcAddRep();

          this.model.letamep.spcrcbamep.spcrcb = "";



        }
      },
      onDeleteClick() {
        if (this.model.ledgrp.blk.spcrcb != null && this.model.ledgrp.blk.spcrcb != "") {
          let spcrcb = this.model.ledgrp.blk.spcrcb;
          let realMatchText = this.specialCharactersProcess(this.model.letamep.spcrcbamep.spcrcb);
          // 使用正则表达式,匹配usrblk文本框中的内容
          // 正则表达式,开启匹配多行 flag: 'g',开启多行模式 flag:'m'
          let usrblkRegExp = new RegExp("^"+realMatchText+"$","gm");
          let matchArray = spcrcb.match(usrblkRegExp);
          // 未找到匹配字符串
          if(matchArray == null || matchArray.length<=0){
            this.$alert('Text to delete not found in original text and thus cannot be deleted', "提示", {
              confirmButtonText: "OK",
            });
            return;
          }

          // 匹配到多个 字符串
          if(matchArray!=null&&matchArray.length>1){
            this.$alert('Text to delete found more than once in original text and thus cannot be deleted. \n'+
                    'Please extend portion to delete or use modify to replace.', "提示", {
              confirmButtonText: "OK",
            });
            return;
          }

          // 匹配到正好1个字符串
          if(matchArray!=null&&matchArray.length==1){
            this.model.ledgrp.blk.spcrcb = this.model.ledgrp.blk.spcrcb.replace(usrblkRegExp,"");

            this.model.ledgrp.blk.spcrcbame = this.model.ledgrp.blk.spcrcbame+"\n"
                    +"*** Amendment "+this.model.letamep.amenbr+ " Date:"+ moment(new Date()).format("MM/DD/YY")+":"+"\n"
                    +"/DEL/"+"\n"
                    +this.model.letamep.spcrcbamep.spcrcb+"\n";

            //设置spcrcb被修改过
            this.setSpcrcbModified();
            this.setSpcDelRep();

            this.model.letamep.spcrcbamep.spcrcb = "";
            return;
          }
        }
      },
      onModflgChange() {
        if (this.model.letamep.spcrcbamep.modflg == "X") {

          // 如果blk.spcrcb的值被修改过,则给出提示
          if(this.isSpcrcbModified()){
            this.$confirm('Do you really want to change to modification of the complete text?\n' +
                    'The information about single added and deleted lines will be lost.', '提示', {
              confirmButtonText: 'OK',
              cancelButtonText: 'Cancel',
              type: 'warning',
              showClose: false
            }).then(() => {//yes的执行在这里写
              this.model.letamep.spcrcbamep.modflg ="X"

              this.isSpcrcbDisabled = false;
              this.isUsrblkDisabled = true;
              this.model.letamep.spcrcbamep.spcrcb = "";
              this.model.ledgrp.blk.spcrcb = this.model.oldledgrp.blk.spcrcb;
              // 值恢复,删除spcrcb的modify记录
              this.resetSpcrcbNotModified();



            }).catch(() => {//No的功能在这里写
              this.model.letamep.spcrcbamep.modflg = ""

              this.isSpcrcbDisabled = true;
              this.isUsrblkDisabled = false;
            });
          }else{
            this.isSpcrcbDisabled = false;
            this.isUsrblkDisabled = true;
            this.model.letamep.spcrcbamep.spcrcb = "";
          }
        } else {

          // 如果blk.spcrcb的值被修改过,则给出reset提示
          if(this.isSpcrcbModified()){
            this.$confirm('Do you really want to reset your manual modifications?', '提示', {
              confirmButtonText: 'OK',
              cancelButtonText: 'Cancel',
              type: 'warning',
              showClose: false
            }).then(() => {//yes的执行在这里写,执行reset,将blk.spcrcb恢复到原值
              this.model.letamep.spcrcbamep.modflg = ""

              this.isSpcrcbDisabled = true;
              this.isUsrblkDisabled = false;
              this.model.letamep.spcrcbamep.spcrcb = "";
              this.model.ledgrp.blk.spcrcb = this.model.oldledgrp.blk.spcrcb;
              // 值恢复,删除spcrcb的modify记录
              this.resetSpcrcbNotModified();

            }).catch(() => {//No的功能在这里写,不执行rest,将modflg的继续选中
              this.model.letamep.spcrcbamep.modflg = "X"

            });

          }else{
            this.isSpcrcbDisabled = true;
            this.isUsrblkDisabled = false;
          }
        }
      },
      onModmanflgChange() {
        if (this.model.letamep.spcrcbamep.modmanflg == "X") {
          this.isSpcrcbDisabled = false;
          this.isUsrblkDisabled = true;
          this.model.letamep.spcrcbamep.spcrcb = "";
        } else {
          this.isSpcrcbDisabled = true;
          this.isUsrblkDisabled = false;
        }
      },

      setSpcrcbModified(){
        if(!this.model.ledgrp.blk.modifySet){
          this.$set(this.model.ledgrp.blk, 'modifySet', ["spcrcb"])
        }else{
          if (!this.model.ledgrp.blk.modifySet.includes("spcrcb")) {
            this.model.ledgrp.blk.modifySet.push("spcrcb")
          }
        }
      },

      resetSpcrcbNotModified(){
        this.model.spcrcbamep = "";
        this.customRemoveModify(this.model.ledgrp.blk,"spcrcb");
      },

      isSpcrcbModified(){
        if(!this.model.ledgrp.blk.modifySet){
          return false;
        }else{
          if (this.model.ledgrp.blk.modifySet.includes("spcrcb")) {
            return true;
          }
        }
        return false;
      },

      setSpcAddRep(){
        if (this.model.bnkAddDelRep === "") {
          this.model.bnkAddDelRep = "/ADD/\n" + this.model.letamep.spcrcbamep.spcrcb;
        } else {
          this.model.bnkAddDelRep = this.model.bnkAddDelRep + "\n/ADD/\n" + this.model.letamep.spcrcbamep.spcrcb;
        }
      },

      setSpcDelRep(){
        if (this.model.bnkAddDelRep === "") {
          this.model.bnkAddDelRep = "/DELETE/\n" + this.model.letamep.spcrcbamep.spcrcb;
        } else {
          this.model.bnkAddDelRep = this.model.bnkAddDelRep + "\n/DELETE/\n" + this.model.letamep.spcrcbamep.spcrcb;
        }
      },

      handleSpcrcbChanged(){
        // 设置修改标记
        this.setSpcrcbModified();


        let changedText = "*** Amendment "+this.model.letamep.amenbr+ " Date:"+ moment(new Date()).format("MM/DD/YY")+":"+"\n";

        if(this.model.letamep.spcrcbamep.modflg==='X'){
          changedText = changedText+"/REPALL/"+"\n";
        }else{
          changedText = changedText+"Special Payment Conditions for Bank only changed as follows:"
        }

        changedText = changedText+this.model.ledgrp.blk.spcrcb+"\n";

        this.model.ledgrp.blk.spcrcbame = this.model.ledgrp.blk.spcrcbame+"\n"+changedText;


      },


    },
  };
</script>
<style></style>