<template>
  <el-dialog
    v-loading="loading"
    :title="'变更详情'"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24" class="clear">
          <div class="fl"><span>操作人：</span><span></span></div>
          <div class="fr">
            <span class="s">&nbsp;</span><span>表示相同的点</span>
          </div>
        </el-col>
        <el-col :span="24" class="clear">
          <span>操作时间：</span><span></span>
          <div class="fr">
            <span class="w">&nbsp;</span><span>表示不同的点</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="contrast-title">操作前</div>
          <div class="contrast-main" v-html="x"></div>
        </el-col>
        <el-col :span="12">
          <div class="contrast-title">操作前</div>
          <div class="contrast-main" v-html="y">
          </div>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
export default {
  name: "contrastModel",
  props: ["showModel", "id",  "sitem"],
  data() {
    return {
      a: "作为一个软 阿大幅度 让我无法惹我 !",
      b: "作为一个软 阿大幅度 234324324 让我无法惹我",
      x: "",
      y: "",
      loading: true,
      showModelThis: this.showModel,
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },

  methods: {
    async initForm() {
      this.loading = true;
      function StringBuffer() {
        this.__strings__ = [];
      }
      StringBuffer.prototype.append = function (str) {
        this.__strings__.push(str);
        return this;
      };
      //格式化字符串
      StringBuffer.prototype.appendFormat = function (str) {
        for (var i = 1; i < arguments.length; i++) {
          var parent = "\\{" + (i - 1) + "\\}";
          var reg = new RegExp(parent, "g");
          str = str.replace(reg, arguments[i]);
        }

        this.__strings__.push(str);
        return this;
      };
      StringBuffer.prototype.toString = function () {
        return this.__strings__.join("");
      };
      StringBuffer.prototype.clear = function () {
        this.__strings__ = [];
      };
      StringBuffer.prototype.size = function () {
        return this.__strings__.length;
      };

      var flag = 1;

      function getHighLightDifferent(a, b) {
        //console.log("输入：" + a);
        //console.log("输入：" + b);

        var temp = getDiffArray(a, b);
        var a1 = getHighLight(a, temp[0]);
        //console.log("输出：" + a1);

        var a2 = getHighLight(b, temp[1]);
        //console.log("输出：" + a2);
        //console.log(flag);
        return new Array(a1, a2);
      }

      function getHighLight(source, temp) {
        var result = new StringBuffer();
        var sourceChars = source.split("");
        var tempChars = temp.split("");
        var flag = false;
        for (var i = 0; i < sourceChars.length; i++) {
          if (tempChars[i] != " ") {
            if (i == 0) {
              result.append("<span style='color:blue'>");
              result.append(sourceChars[i]);
            } else if (flag) {
              result.append(sourceChars[i]);
            } else {
              result.append("<span style='color:blue'>");
              result.append(sourceChars[i]);
            }
            flag = true;
            if (i == sourceChars.length - 1) {
              result.append("</span>");
            }
          } else if (flag == true) {
            result.append("</span>");
            result.append(sourceChars[i]);
            flag = false;
          } else {
            result.append(sourceChars[i]);
          }
        }
        return result.toString();
      }

      function getDiffArray(a, b) {
        var result = new Array();
        //选取长度较小的字符串用来穷举子串
        if (a.length < b.length) {
          var start = 0;
          var end = a.length;
          result = getDiff(a, b, start, end);
        } else {
          var start = 0;
          var end = b.length;
          result = getDiff(b, a, 0, b.length);
          result = new Array(result[1], result[0]);
        }
        return result;
      }

      //将a的指定部分与b进行比较生成比对结果
      function getDiff(a, b, start, end) {
        var result = new Array(a, b);
        var len = result[0].length;
        while (len > 0) {
          for (var i = start; i < end - len + 1; i++) {
            var sub = result[0].substring(i, i + len);
            var idx = -1;
            if ((idx = result[1].indexOf(sub)) != -1) {
              result[0] = setEmpty(result[0], i, i + len);
              result[1] = setEmpty(result[1], idx, idx + len);
              if (i > 0) {
                //递归获取空白区域左边差异
                result = getDiff(result[0], result[1], start, i);
              }
              if (i + len < end) {
                //递归获取空白区域右边差异
                result = getDiff(result[0], result[1], i + len, end);
              }
              len = 0; //退出while循环
              break;
            }
          }
          len = parseInt(len / 2);
          //len = len - 1;
          //console.log(len);
        }
        //console.log(result.join(""));
        return result;
      }

      //将字符串s指定的区域设置成空格
      function setEmpty(s, start, end) {
        var array = s.split("");
        for (var i = start; i < end; i++) {
          array[i] = " ";
        }
        return array.join("");
      }
      let list = getHighLightDifferent(this.a, this.b);
      this.x = list[0];
      this.y = list[1];
      this.loading = false;
    },
  },
};
</script>

   <style lang="scss" scoped>
.account {
}
</style>
