<template>
  <div class="eibs-tab">
    <!-- 左边 -->
    <c-col :span="12" style="padding-right: 20px">
      <c-col :span="24">
        <c-fullbox>
          <el-form-item label="Description of Goods and/or Services(45B)" prop="lidgrp.blk.lcrgod">
            <c-mul-row-input
              type="textarea"
              v-model="model.lidgrp.blk.lcrgod"
              :rows="800"
              :cols="65"
              :autosize="{ minRows: 17, maxRows: 17 }"
              show-word-limit
              :charmod="3"
              @change="handleLcrgodChanged"
              :disabled="isLcrgodDisabled"
              placeholder="请输入货物描述"></c-mul-row-input>
          </el-form-item>
        </c-fullbox>
      </c-col>
      <c-col :span="24">
        <c-ptap-commons
          :cols="65"
          :rows="800"
          :minRows="12"
          :maxRows="12"
          :model="model"
          :disabledButton="isUsrblkDisabled"
          :isRules="false"
          :extCodes="['litamep', 'lcrgodamep']"
          lastModel="lcrgod"
          :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">
        <c-fullbox>
          <el-form-item label="Historic Overview" prop="lidgrp.blk.lcrgodame">
            <c-mul-row-input
              disabled
              type="textarea"
              :rows="800"
              :cols="65"
              :autosize="{ minRows: 17, maxRows: 17 }"
              v-model="model.lidgrp.blk.lcrgodame"
              show-word-limit
              placeholder="历史修改信息"></c-mul-row-input>
          </el-form-item>
        </c-fullbox>
      </c-col>
      <c-col :span="24">
        <el-form-item label="Contract No." prop="lidgrp.rec.rptref">
          <c-input
            type="textarea"
            v-model="model.lidgrp.rec.rptref"
            maxlength="80"
            placeholder="请输入合同号"></c-input>
        </el-form-item>
      </c-col>
      <c-col :span="24" class="custom-box-wrap">
        <el-form-item label="Contract Amount" prop="lidgrp.rec.rptcur">
          <c-select-cur
            disabled
            v-model="model.lidgrp.rec.rptcur"
            style="width: 115px"
            placeholder="请选择币种"
            dbCode="curtxt"
            sort="SRT">
          </c-select-cur>
        </el-form-item>
        <div style="width: calc(100% - 200px)">
          <el-form-item label-width="5px" prop="lidgrp.rec.rptamt">
            <c-input-currency
              v-model="model.lidgrp.rec.rptamt"
              :currency="model.lidgrp.rec.rptcur"
              style="width: 100%"
              placeholder="请输入金额">
            </c-input-currency>
          </el-form-item>
        </div>
      </c-col>
      <c-col :span="24">
        <c-col :span="8" class="">
          <el-form-item label=" " prop="litamep.lcrgodamep.modflg" label-width="180px" style="float: left">
            <c-checkbox
              v-model="model.litamep.lcrgodamep.modflg"
              class="checkbox-left"
              true-label="X"
              false-label=""
              :disabled="this.model.litamep.lcrgodamep.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.lcrgodamep.modmanflg" label-width="180px" style="float: left">
            <c-checkbox
              v-model="model.litamep.lcrgodamep.modmanflg"
              class="checkbox-left"
              true-label="X"
              false-label=""
              :disabled="this.model.litamep.lcrgodamep.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.lcrgod"
            :rightText="model.lidgrp.blk.lcrgod"
            :isShowScale="false"
            :disabled="model.lidgrp.blk.lcrgod === model.oldlidgrp.blk.lcrgod" />
        </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.lcrgodamep.lcrgod == null || this.model.litamep.lcrgodamep.lcrgod == ''">
            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.lcrgodamep.lcrgod == null || this.model.litamep.lcrgodamep.lcrgod == ''">
            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 {
      isLcrgodDisabled: true,
      isUsrblkDisabled: false,
      tempLcrgod: "",
    };
  },
  methods: {
    onAddClick() {
      if (this.model.litamep.lcrgodamep.lcrgod != null && this.model.litamep.lcrgodamep.lcrgod != "") {
        if (this.model.lidgrp.blk.lcrgod === "") {
          this.model.lidgrp.blk.lcrgod = this.model.lidgrp.blk.lcrgod + this.model.litamep.lcrgodamep.lcrgod;
        } else {
          this.model.lidgrp.blk.lcrgod = this.model.lidgrp.blk.lcrgod + "\n" + this.model.litamep.lcrgodamep.lcrgod;
        }

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

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

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

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

              this.isLcrgodDisabled = false;
              this.isUsrblkDisabled = true;
              this.model.litamep.lcrgodamep.lcrgod = "";
              this.model.lidgrp.blk.lcrgod = this.model.oldlidgrp.blk.lcrgod;
              // 值恢复,删除lcrgod的modify记录
              this.resetLcrgodNotModified();
            })
            .catch(() => {
              //No的功能在这里写
              this.model.litamep.lcrgodamep.modflg = "";

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

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

    setLcrgodModified() {
      this.customAddModify(this.model.lidgrp.blk, "lcrgod");
    },

    resetLcrgodNotModified() {
      this.customRemoveModify(this.model.lidgrp.blk, "lcrgod");
    },

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

    handleLcrgodChanged() {
      // 设置修改标记
      this.setLcrgodModified();

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

      if (this.model.litamep.lcrgodamep.modflg === "X") {
        changedText = changedText + "/REPALL/" + "\n";
      } else {
        changedText = changedText + "Description of Goods changed as follows:";
      }

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

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