<template>
  <div class="eibs">
    <c-col :span="12" style="padding-right: 20px">
      <c-col :span="24">
        <c-fullbox>
          <el-form-item label="Additional Conditions(47B)" prop="lidgrp.blk.adlcnd">
            <c-mul-row-input
              type="textarea"
              v-model="model.lidgrp.blk.adlcnd"
              :rows="800"
              :cols="65"
              :autosize="{ minRows: 10, maxRows: 10 }"
              show-word-limit
              :charmod="3"
              @change="handleAdlcndChanged"
              :disabled="isAdlcndDisabled"
              placeholder="请输入附加条款"></c-mul-row-input>
          </el-form-item>
        </c-fullbox>
      </c-col>
      <c-col :span="24">
        <c-ptap-commons
          :cols="65"
          :rows="800"
          :minRows="5"
          :maxRows="5"
          :model="model"
          :disabledButton="isUsrblkDisabled"
          :isRules="false"
          :extCodes="['litamep', 'adlcndamep']"
          lastModel="adlcnd"
          :charmod="3"
          title="Adding/Deleting Text in Current Amendment">
        </c-ptap-commons>
      </c-col>
      <c-col :span="24">
        <c-ptap-commons
          :cols="35"
          :rows="6"
          :minRows="6"
          :maxRows="6"
          :model="model"
          :extCodes="['lidgrp', 'blk']"
          lastModel="feetxt"
          :charmod="3"
          title="Details of Charges(71D)">
        </c-ptap-commons>
      </c-col>

      <c-col :span="24">
        <el-form-item label="" class="checkbox-left" prop="lidgrp.rec.spcbenflg">
          <c-checkbox
            v-model="model.lidgrp.rec.spcbenflg"
            true-label="X"
            false-label=""
            :disabled="this.model.lidgrp.blk.spcben != null && this.model.lidgrp.blk.spcben != ''"
            >Special Payment Cond.for Beneficiary</c-checkbox
          >
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="" class="checkbox-left" prop="lidgrp.rec.spcrcbflg">
          <c-checkbox
            v-model="model.lidgrp.rec.spcrcbflg"
            true-label="X"
            false-label=""
            :disabled="this.model.lidgrp.blk.spcrcb != null && this.model.lidgrp.blk.spcrcb != ''"
            >Special Payment Cond.for Bank Only</c-checkbox
          >
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" style="padding-left: 20px">
      <c-col :span="24">
        <c-fullbox>
          <el-form-item label="Historic Overview" prop="lidgrp.blk.adlcndame">
            <c-mul-row-input
              disabled
              type="textarea"
              :rows="800"
              :cols="65"
              :autosize="{ minRows: 10, maxRows: 10 }"
              v-model="model.lidgrp.blk.adlcndame"
              show-word-limit
              placeholder="历史修改信息"></c-mul-row-input>
          </el-form-item>
        </c-fullbox>
      </c-col>
      <c-col :span="24">
        <c-ptap-commons
          :cols="65"
          :rows="12"
          :minRows="6"
          :maxRows="6"
          :model="model"
          :extCodes="['lidgrp', 'blk']"
          lastModel="insbnk"
          :charmod="2"
          title="Instructions to Pay.,Accept., Negot. Bank(78)">
        </c-ptap-commons>
      </c-col>
      <c-col :span="24">
        <c-col :span="8" class="">
          <el-form-item label=" " prop="litamep.adlcndamep.modflg" label-width="180px" style="float: left">
            <c-checkbox
              v-model="model.litamep.adlcndamep.modflg"
              class="checkbox-left"
              true-label="X"
              false-label=""
              :disabled="this.model.litamep.adlcndamep.modmanflg == 'X'"
              @change="onModflgChange"
              >Modify Text to Replace</c-checkbox
            >
          </el-form-item>
        </c-col>
        <c-col :span="10" class="">
          <el-form-item label=" " prop="litamep.adlcndamep.modmanflg" label-width="180px" style="float: left">
            <c-checkbox
              v-model="model.litamep.adlcndamep.modmanflg"
              class="checkbox-left"
              true-label="X"
              false-label=""
              :disabled="this.model.litamep.adlcndamep.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.oldlidgrp.blk.adlcnd"
            :rightText="model.lidgrp.blk.adlcnd"
            :isShowScale="false"
            :disabled="model.lidgrp.blk.adlcnd === model.oldlidgrp.blk.adlcnd" />
        </el-form-item>
      </c-col>
      <c-col :span="4">
        <el-form-item label-width="180px" style="float: left">
          <c-button
            type="primary"
            @click="onAddClick"
            :disabled="this.model.litamep.adlcndamep.adlcnd == null || this.model.litamep.adlcndamep.adlcnd == ''">
            Add
          </c-button>
        </el-form-item>
      </c-col>
      <c-col :span="4">
        <el-form-item label-width="180px" style="float: left">
          <c-button
            type="primary"
            @click="onDeleteClick"
            :disabled="this.model.litamep.adlcndamep.adlcnd == null || this.model.litamep.adlcndamep.adlcnd == ''">
            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 {
      isAdlcndDisabled: true,
      isUsrblkDisabled: false,
      tempLcradc: "",
    };
  },
  methods: {
    onAddClick() {
      if (this.model.litamep.adlcndamep.adlcnd != null && this.model.litamep.adlcndamep.adlcnd != "") {
        if (this.model.lidgrp.blk.adlcnd === "") {
          this.model.lidgrp.blk.adlcnd = this.model.lidgrp.blk.adlcnd + this.model.litamep.adlcndamep.adlcnd;
        } else {
          this.model.lidgrp.blk.adlcnd = this.model.lidgrp.blk.adlcnd + "\n" + this.model.litamep.adlcndamep.adlcnd;
        }

        this.model.lidgrp.blk.adlcndame =
          this.model.lidgrp.blk.adlcndame +
          "\n" +
          "*** Amendment " +
          this.model.litamep.amenbr +
          " Date:" +
          moment(new Date()).format("MM/DD/YY") +
          ":" +
          "\n" +
          "/ADD/" +
          "\n" +
          this.model.litamep.adlcndamep.adlcnd +
          "\n";

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

          this.model.lidgrp.blk.adlcndame =
            this.model.lidgrp.blk.adlcndame +
            "\n" +
            "*** Amendment " +
            this.model.litamep.amenbr +
            " Date:" +
            moment(new Date()).format("MM/DD/YY") +
            ":" +
            "\n" +
            "/DEL/" +
            "\n" +
            this.model.litamep.adlcndamep.adlcnd +
            "\n";

          //设置adlcnd被修改过
          this.setAdlcndModified();
          if (this.model.adcAddDelRep === "") {
            this.model.adcAddDelRep = "/DELETE/\n" + this.model.litamep.adlcndamep.adlcnd;
          } else {
            this.model.adcAddDelRep = this.model.adcAddDelRep + "\n/DELETE/\n" + this.model.litamep.adlcndamep.adlcnd;
          }
          this.model.litamep.adlcndamep.adlcnd = "";
          return;
        }
      }
    },
    onModflgChange() {
      if (this.model.litamep.adlcndamep.modflg == "X") {
        // 如果blk.adlcnd的值被修改过,则给出提示
        if (this.isAdlcndModified()) {
          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.litamep.adlcndamep.modflg = "X";

              this.isAdlcndDisabled = false;
              this.isUsrblkDisabled = true;
              this.model.litamep.adlcndamep.adlcnd = "";
              this.model.lidgrp.blk.adlcnd = this.model.oldlidgrp.blk.adlcnd;
              // 值恢复,删除adlcnd的modify记录
              this.resetAdlcndNotModified();
              // 清空add/delete内容
              this.model.adcAddDelRep = "";
            })
            .catch(() => {
              //No的功能在这里写
              this.model.litamep.adlcndamep.modflg = "";

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

              this.isAdlcndDisabled = true;
              this.isUsrblkDisabled = false;
              this.model.litamep.adlcndamep.adlcnd = "";
              this.model.lidgrp.blk.adlcnd = this.model.oldlidgrp.blk.adlcnd;
              // 值恢复,删除adlcnd的modify记录
              this.resetAdlcndNotModified();
            })
            .catch(() => {
              //No的功能在这里写,不执行rest,将modflg的继续选中
              this.model.litamep.adlcndamep.modflg = "X";
            });
        } else {
          this.isAdlcndDisabled = true;
          this.isUsrblkDisabled = false;
        }
      }
    },
    onModmanflgChange() {
      if (this.model.litamep.adlcndamep.modmanflg == "X") {
        this.isAdlcndDisabled = false;
        this.isUsrblkDisabled = true;
        this.model.litamep.adlcndamep.adlcnd = "";
        this.tempLcradc = this.model.lidgrp.blk.adlcnd;
      } else {
        if (this.tempLcradc !== this.model.lidgrp.blk.adlcnd) {
          // 如果有修改过,则需提示
          this.$confirm("Do you really want to reset your manual modifications?", "提示", {
            confirmButtonText: "OK",
            cancelButtonText: "Cancel",
            type: "warning",
            showClose: false,
          })
            .then(() => {
              // 确认取消勾选
              this.isAdlcndDisabled = true;
              this.isUsrblkDisabled = false;
              // 将blk的值还原成手动修改前的值
              this.model.lidgrp.blk.adlcnd = this.tempLcradc;
            })
            .catch(() => {
              // 不取消勾选
              this.model.litamep.adlcndamep.modmanflg = "X";
            });
        } else {
          // 没做过手动修改
          this.isAdlcndDisabled = true;
          this.isUsrblkDisabled = false;
        }
      }
    },

    setAdlcndModified() {
      this.customAddModify(this.model.lidgrp.blk, "adlcnd");
    },

    resetAdlcndNotModified() {
      this.customRemoveModify(this.model.lidgrp.blk, "adlcnd");
    },

    isAdlcndModified() {
      if (!this.model.lidgrp.blk.modifySet) {
        return false;
      } else {
        if (this.model.lidgrp.blk.modifySet.includes("adlcnd")) {
          return true;
        }
      }
      return false;
    },

    handleAdlcndChanged() {
      // 设置修改标记
      this.setAdlcndModified();

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

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

      changedText = changedText + this.model.lidgrp.blk.adlcnd + "\n";

      this.model.lidgrp.blk.adlcndame = this.model.lidgrp.blk.adlcndame + "\n" + changedText;
    },
  },
};
</script>
<style></style>