<template>
  <div class="eibs-tab">
    <c-col :span="12" style="padding-right: 20px">
      <c-col :span="24">
        <el-form-item label="Documents Required" prop="ltdgrp.blk.lcrdoc">
            <c-mul-row-input
              type="textarea"
              :rows="65"
              :cols="800"
              :autosize="{minRows: 20,	maxRows: 20}"
              v-model="model.ltdgrp.blk.lcrdoc"
              :charmod="3"
              show-word-limit
              placeholder="请输入单据要求"
              @change="handleLcrdocChanged"
              :disabled="isLcrdocDisabled"></c-mul-row-input>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <c-ptap-commons  :cols="65" :rows="800" :minRows="12" :maxRows="12" :model="model"
                         :disabledButton="isUsrblkDisabled"
                         :extCodes="['lttamep','lcrdocamep']"
                         lastModel="lcrdoc"
                         :charmod="3"
                         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="ltdgrp.blk.lcrdocame">
          <c-fullbox>
            <c-input
              disabled
              type="textarea"
              maxlength="52000"
              v-model="model.ltdgrp.blk.lcrdocame"
              :rows="20"
              show-word-limit
              placeholder="历史修改信息"></c-input>
          </c-fullbox>
        </el-form-item>
      </c-col>

      <c-col :span="24" class="">
        <el-form-item label=" " prop="lttamep.lcrdocamep.modflg" label-width="180px" style="float: left">
          <c-checkbox
                  v-model="model.lttamep.lcrdocamep.modflg"
                  class="checkbox-left"
                  true-label="X"
                  false-label=""
                  :disabled="this.model.lttamep.lcrdocamep.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="lttamep.lcrdocamep.modmanflg" label-width="180px" style="float: left">
            <c-checkbox
                    v-model="model.lttamep.lcrdocamep.modmanflg"
                    class="checkbox-left"
                    true-label="X"
                    false-label=""
                    :disabled="this.model.lttamep.lcrdocamep.modflg == 'X'"
                    @change="onModmanflgChange"
            >Modify Text Internally</c-checkbox
            >
          </el-form-item>
        </c-col>

      <c-col :span="24">
        <el-form-item label-width="160px" style="float: left">
          <text-diff
            :leftText="model.oldltdgrp.blk.lcrdoc"
            :rightText="model.ltdgrp.blk.lcrdoc"
            :isShowScale="false"
            :disabled="model.ltdgrp.blk.lcrdoc === model.oldltdgrp.blk.lcrdoc" />
        </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.lttamep.lcrdocamep.lcrdoc == null || this.model.lttamep.lcrdocamep.lcrdoc == ''">
            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.lttamep.lcrdocamep.lcrdoc == null || this.model.lttamep.lcrdocamep.lcrdoc == ''">
            Delete
          </c-button>
        </el-form-item>
      </c-col>

    </c-col>
  </div>
</template>
<script>
import event from '../event';
import moment from "moment";
import commonDepend from "~/mixin/commonDepend";
import TextDiff from "~/components/business/TextDiff";
export default {
  components: { TextDiff },
  inject: ['root'],
  props: ['model', 'codes'],
  mixins: [event, commonDepend],
  data() {
    return {
      isLcrdocDisabled: true,
      isUsrblkDisabled: false,
      tempLcrdoc: "",
    };
  },
  methods: {
      onAddClick() {
        if (this.model.lttamep.lcrdocamep.lcrdoc != null && this.model.lttamep.lcrdocamep.lcrdoc != "") {
          this.model.ltdgrp.blk.lcrdoc = this.model.ltdgrp.blk.lcrdoc + "\n" + this.model.lttamep.lcrdocamep.lcrdoc;

          this.model.ltdgrp.blk.lcrdocame = this.model.ltdgrp.blk.lcrdocame+"\n"
                  +"*** Amendment "+this.model.lttamep.amenbr+ " Date:"+ moment(new Date()).format("MM/DD/YY")+":"+"\n"
                  +"/ADD/"+"\n"
                  +this.model.lttamep.lcrdocamep.lcrdoc+"\n";

          //设置lcrdoc被修改过
          this.setLcrdocModified();
          if (this.model.docAddDelRep === "") {
            this.model.docAddDelRep = "/ADD/\n" + this.model.lttamep.lcrdocamep.lcrdoc;
          } else {
            this.model.docAddDelRep = this.model.docAddDelRep + "\n/ADD/\n" + this.model.lttamep.lcrdocamep.lcrdoc;
          }
          this.model.lttamep.lcrdocamep.lcrdoc = "";
        }
      },
      onDeleteClick() {
        if (this.model.ltdgrp.blk.lcrdoc != null && this.model.ltdgrp.blk.lcrdoc != "") {
          let lcrdoc = this.model.ltdgrp.blk.lcrdoc;
          let realMatchText = this.specialCharactersProcess(this.model.lttamep.lcrdocamep.lcrdoc);
          // 使用正则表达式,匹配usrblk文本框中的内容
          // 正则表达式,开启匹配多行 flag: 'g',开启多行模式 flag:'m'
          let usrblkRegExp = new RegExp("^"+realMatchText+"$","gm");
          let matchArray = lcrdoc.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.ltdgrp.blk.lcrdoc = this.model.ltdgrp.blk.lcrdoc.replace("\n" + realMatchText, "");
            this.model.ltdgrp.blk.lcrdoc = this.model.ltdgrp.blk.lcrdoc.replace(realMatchText + "\n", "");
            this.model.ltdgrp.blk.lcrdoc = this.model.ltdgrp.blk.lcrdoc.replace(realMatchText, "");

            this.model.ltdgrp.blk.lcrdocame = this.model.ltdgrp.blk.lcrdocame+"\n"
                    +"*** Amendment "+this.model.lttamep.amenbr+ " Date:"+ moment(new Date()).format("MM/DD/YY")+":"+"\n"
                    +"/DEL/"+"\n"
                    +this.model.lttamep.lcrdocamep.lcrdoc+"\n";

            //设置lcrdoc被修改过
            this.setLcrdocModified();
            if (this.model.docAddDelRep === "") {
              this.model.docAddDelRep = "/DELETE/\n" + this.model.lttamep.lcrdocamep.lcrdoc;
            } else {
              this.model.docAddDelRep = this.model.docAddDelRep + "\n/DELETE/\n" + this.model.lttamep.lcrdocamep.lcrdoc;
            }
            this.model.lttamep.lcrdocamep.lcrdoc = "";
            return;
          }
        }
      },
      onModflgChange() {
        if (this.model.lttamep.lcrdocamep.modflg == "X") {

          // 如果blk.lcrdoc的值被修改过,则给出提示
          if(this.isLcrdocModified()){
            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.lttamep.lcrdocamep.modflg ="X"

              this.isLcrdocDisabled = false;
              this.isUsrblkDisabled = true;
              this.model.lttamep.lcrdocamep.lcrdoc = "";
              this.model.ltdgrp.blk.lcrdoc = this.model.oldltdgrp.blk.lcrdoc;
              // 值恢复,删除lcrdoc的modify记录
              this.resetLcrdocNotModified();
              // 清空add/delete内容
              this.model.docAddDelRep = "";
            }).catch(() => {//No的功能在这里写
              this.model.lttamep.lcrdocamep.modflg = ""

              this.isLcrdocDisabled = true;
              this.isUsrblkDisabled = false;
            });
          }else{
            this.isLcrdocDisabled = false;
            this.isUsrblkDisabled = true;
          }
        } else {

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

              this.isLcrdocDisabled = true;
              this.isUsrblkDisabled = false;
              this.model.lttamep.lcrdocamep.lcrdoc = "";
              this.model.ltdgrp.blk.lcrdoc = this.model.oldltdgrp.blk.lcrdoc;
              // 值恢复,删除lcrdoc的modify记录
              this.resetLcrdocNotModified();

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

            });

          }else{
            this.isLcrdocDisabled = true;
            this.isUsrblkDisabled = false;
          }
        }
      },
      onModmanflgChange() {
        if (this.model.lttamep.lcrdocamep.modmanflg == "X") {
          this.isLcrdocDisabled = false;
          this.isUsrblkDisabled = true;
          this.model.lttamep.lcrdocamep.lcrdoc = "";
          this.tempLcrdoc = this.model.ltdgrp.blk.lcrdoc;
        } else {
          if (this.tempLcrdoc !== this.model.ltdgrp.blk.lcrdoc) {
            // 如果有修改过,则需提示
            this.$confirm("Do you really want to reset your manual modifications?", "提示", {
              confirmButtonText: "OK",
              cancelButtonText: "Cancel",
              type: "warning",
              showClose: false,
            })
              .then(() => {
                // 确认取消勾选
                this.isLcrdocDisabled = true;
                this.isUsrblkDisabled = false;
                // 将blk的值还原成手动修改前的值
                this.model.ltdgrp.blk.lcrdoc = this.tempLcrdoc;
              })
              .catch(() => {
                // 不取消勾选
                this.model.lttamep.lcrdocamep.modmanflg = "X";
              });
          } else {
            // 没做过手动修改
            this.isLcrdocDisabled = true;
            this.isUsrblkDisabled = false;
          }
        }
      },

      setLcrdocModified() {
      this.customAddModify(this.model.ltdgrp.blk, "lcrdoc");
    },

    resetLcrdocNotModified() {
      this.customRemoveModify(this.model.ltdgrp.blk, "lcrdoc");
    },

      isLcrdocModified(){
        if(!this.model.ltdgrp.blk.modifySet){
          return false;
        }else{
          if (this.model.ltdgrp.blk.modifySet.includes("lcrdoc")) {
            return true;
          }
        }
        return false;
      },

      handleLcrdocChanged(){
        // 设置修改标记
        this.setLcrdocModified();


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

        if(this.model.lttamep.lcrdocamep.modflg==='X'){
          changedText = changedText+"/REPALL/"+"\n";
        }else{
          changedText = changedText+"Documents Required changed as follows:"
        }

        changedText = changedText+this.model.ltdgrp.blk.lcrdoc+"\n";

        this.model.ltdgrp.blk.lcrdocame = this.model.ltdgrp.blk.lcrdocame+"\n"+changedText;


      },


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