<!--
 * @Author: your name
 * @Date: 2021-07-09 17:03:14
 * @LastEditTime: 2022-03-30 13:56:31
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \taxFormToolFramework\src\components\inTablemodal.vue
-->
<template>
  <div class="modal" :style="{display:modalObj.isShow}">
    <div class="modal-bg" :style="{display:modalObj.isShow}"></div>
    <el-dialog :title="modalObj.title" :visible.sync="modalObj.isShow=='block'?true:false" :close-on-click-modal="false" :show-close="modalObj.showClose==false?modalObj.showClose:true" :close-on-press-escape="modalObj.closeOnPressEscape" :modal="false" custom-class="dialogClass" center>
      <div class="outBox">
        <span v-html="modalObj.info"></span>
      </div>
      <p>
        <el-input v-model="inputVal" @blur="onBlur" @input="onInput" v-show="modalObj.isInput"></el-input>
      </p>
      <p>
        <el-checkbox v-model="modalObj.check" :label="modalObj.checkText" v-show="modalObj.isCheck"></el-checkbox>
      </p>
      <div style="margin: 21px 0 auto 42%">
        <el-button type="primary" size="small" @click="sureChoose()">确定</el-button>
        <el-button size="small" @click="cancelChoose()" v-show="modalObj.showCancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "inTablemodal",
  data() {
    return {
      modalList: [],
      type: '',
      inputVal: '0.00',
      modalObj: {//开表提示弹框
        title: "提示", // 标题
        isShow: "none", // 是否显示
        closeOnPressEscape: false,
        showClose: false,
        info: '',//提示内容
        check: false
      },
      inputModal: ["0e850f7e-be5d-4745-b8f9-83bd41b11e84"]
    }
  },
  mounted: function () {
    var clientHeight = document.documentElement.clientHeight;
    var height = clientHeight - 128;
    var tableBox = document.getElementById('tableBox');
    var width = tableBox.offsetWidth;
    var left = tableBox.getBoundingClientRect().left;
    var top = tableBox.getBoundingClientRect().top;
    $(".modal-bg").css({
      width: width,
      height: height,
      left: left,
      top: top
    })
    var dialogTop = top / 2;
    var dialogLeft = left / 2;
    $(".dialogClass").css({
      top: dialogTop,
      left: dialogLeft,
    })
  },
  methods: {
    initModal(type) {
      var _self = this
      var alertList = this.$store.state.data.configData.alert
      console.log(JSON.stringify(alertList));
      if (type == 'init') {
        var modalList = Enumerable.From(alertList).Where(function (val) { return val.type == "1" }).ToArray();
      } else if (type == 'save') {
        var modalList = Enumerable.From(alertList).Where(function (val) { return val.type == "2" }).ToArray();
      }
      var inquireUrl = interfacePrefix + config.modalInquire;
      var params = {
        // frameId: obj.index,
        zsDjxh: this.$store.state.data.openData.zsDjxh,
      }
      this.ajax.post(inquireUrl, params, function (data) {
        if (data.returnCode == '00') {
          var selectList = data.content
          var tableData = _self.$store.state.data.engines;
          var bbDm = _self.$store.state.data.bblxHeader.bbDm
          for (var i = 0; i < modalList.length; i++) {
            var obj = modalList[i]
            if (obj.index == "09a0278e-3fea-4c0a-9cc8-23ed66985a20") {
              var saveTableData = tableData.getTableData();
              var pageData = saveTableData.tableData
              obj.content = obj.content.replace('${}', pageData.hidden_577)
            }
            var delFlag = false
            if (!["", undefined, null].includes(obj.openFormula)) {
              var formulaFlag = tableData.getPromiseResult(obj.openFormula)
              delFlag = !formulaFlag
            }
            if (selectList.length > 0) {
              var selectObj = Enumerable.From(selectList).Where(function (val) { return val.frameId == obj.index }).ToArray()[0]
              if (!["", undefined, null].includes(selectObj)) {
                delFlag = true
              }
            }
            if (bbDm == "10102_014") {
              delFlag = _self.popupAssign('init', bbDm, obj.index)
            }
            if (delFlag) {
              modalList.splice(i, 1);
              i--;
            }
          }
          modalList = _self.popupAssign('array', bbDm, "", modalList)
          var that = _self;
          var getUrl = interfacePrefix + config.tsxxGet;
          var params = {
            bblxDm: _self.$store.state.data.openData.bblxDm, // 报表类型代码
            bbDm: bbDm, // 报表代码
            djxh: _self.$store.state.data.openData.djxh, // 登记序号
          }
          _self.ajax.post(getUrl, params, function (data) {
            if (data.returnCode == "00") {
              if (data.content == 'N' && bbDm != "10101_003") { //content 返回N弹出提示，返回Y不弹出提示
              } else {
                modalList = Enumerable.From(modalList).Where(function (obj) { return obj.index != "8396fd03-9a00-47a7-b3b7-ed6df66e54c4" }).ToArray();
              }
            } else {
              modalList = Enumerable.From(modalList).Where(function (obj) { return obj.index != "8396fd03-9a00-47a7-b3b7-ed6df66e54c4" }).ToArray();
            }
            that.modalList = modalList
            that.type = type
            if (that.modalList.length > 0) {
              that.modalObj.check = false
              that.inputVal = "0.00"
              var firstObj = that.modalList[0];
              $(".dialogClass").css({ width: firstObj.area[0] })
              that.modalObj.title = firstObj.title
              that.modalObj.info = firstObj.content
              that.modalObj.showCancel = firstObj.btns == "1" ? false : true
              that.modalObj.sub = 0
              that.modalObj.checkText = firstObj.needReadText
              that.modalObj.isCheck = firstObj.needRead
              that.modalObj.isInput = that.inputModal.includes(firstObj.index) ? true : false
              that.modalObj.isShow = "block"
              that.modalObj.frameId = firstObj.index
            } else {
              if (that.type == 'save') {
                var tableData = that.$store.state.data.engines;
                var saveTableData = tableData.getTableData();
                var checkList = saveTableData.checkList.checkList
                that.$store.commit("checkList", checkList);
                if (checkList.length > 0) {
                  var obj = that.recursionParent(that.$parent, "checkResult")
                  obj.inTableCheck()
                }
              }
            }
          }, "json", true);
        }
      }, "json", false);
    },
    sureChoose() {
      if (this.modalObj.isCheck) {
        if (!this.modalObj.check) {
          var mes = "请勾选[" + this.modalObj.checkText + "]"
          this.$message({ message: mes, type: 'warning' });
          return
        }
      }
      var currentObj = this.modalList[this.modalObj.sub]
      var _self = this
      var saveUrl = interfacePrefix + config.modalSave;
      var params = {
        frameId: currentObj.index,
        zsDjxh: this.$store.state.data.openData.zsDjxh,
      }
      var bbDm = this.$store.state.data.bblxHeader.bbDm
      this.popupAssign('sure', bbDm, this.modalObj.frameId, this.inputVal)
      var len = this.modalList.length
      var sub = this.modalObj.sub + 1
      if (len == sub) {//最后一个
        if (currentObj.onlyOnce) {
          this.ajax.post(saveUrl, params, function (data) {
            if (data.returnCode == '00') {
              _self.modalObj.isShow = "none"
            }
          }, "json", true);
        } else {
          this.modalObj.isShow = "none"
        }
        if (this.type == "save") {
          var tableData = this.$store.state.data.engines;
          var saveTableData = tableData.getTableData();
          var checkList = saveTableData.checkList.checkList
          this.$store.commit("checkList", checkList);
          if (checkList.length > 0) {
            var obj = this.recursionParent(this.$parent, "checkResult")
            obj.inTableCheck()
          }
        }
      } else {//下一步
        if (currentObj.onlyOnce) {//每个企业永久只弹一次 //开表后只弹一次
          this.ajax.post(saveUrl, params, function (data) {
            if (data.returnCode == '00') {
              var len = _self.modalList.length
              var sub = _self.modalObj.sub + 1
              var obj = _self.modalList[sub];
              _self.modalObj.title = obj.title
              $(".dialogClass").css({ width: obj.area[0] })
              _self.modalObj.info = obj.content
              _self.modalObj.showCancel = obj.btns == "1" ? false : true
              _self.modalObj.sub = sub
              _self.modalObj.checkText = obj.needReadText
              _self.modalObj.isCheck = obj.needRead
              _self.modalObj.frameId = obj.index
            }
          }, "json", true);
        } else {
          var len = this.modalList.length
          var sub = this.modalObj.sub + 1
          var obj = this.modalList[sub];
          this.modalObj.title = obj.title
          $(".dialogClass").css({ width: obj.area[0] })
          this.modalObj.info = obj.content
          this.modalObj.showCancel = obj.btns == "1" ? false : true
          this.modalObj.sub = sub
          this.modalObj.checkText = obj.needReadText
          this.modalObj.isCheck = obj.needRead
          this.modalObj.isInput = this.inputModal.includes(obj.index) ? true : false
          this.modalObj.frameId = obj.index
        }
      }
    },
    cancelChoose() {
      var len = this.modalList.length
      var sub = this.modalObj.sub + 1
      if (len == sub) {//最后一个
        this.modalObj.isShow = "none"
        var tableData = this.$store.state.data.engines;
        var saveTableData = tableData.getTableData();
        var checkList = saveTableData.checkList.checkList
        this.$store.commit("checkList", checkList);
        if (checkList.length > 0) {
          var obj = this.recursionParent(this.$parent, "checkResult")
          obj.inTableCheck()
        }
      } else {//不是最后一个
        var len = this.modalList.length
        var bbDm = this.$store.state.data.bblxHeader.bbDm
        if (bbDm == "10102_001") {
          if (["35a53e15-9142-4327-bb0c-ca1672bc42e1", "0d08fd72-4692-4a38-899c-8fc6ce3cf98b"].includes(this.modalObj.frameId)) {
            if (this.modalList.length == 2) {
              this.modalObj.isShow = "none"
              var tableData = this.$store.state.data.engines;
              var saveTableData = tableData.getTableData();
              var checkList = saveTableData.checkList.checkList
              this.$store.commit("checkList", checkList);
              if (checkList.length > 0) {
                var obj = this.recursionParent(this.$parent, "checkResult")
                obj.inTableCheck()
              }
              return
            } else {
              var nextSub = this.modalObj.sub + 2
            }
          } else {
            var nextSub = this.modalObj.sub + 1
          }
        } else {
          var nextSub = this.modalObj.sub + 1
        }
        var obj = this.modalList[nextSub];
        this.modalObj.title = obj.title
        $(".dialogClass").css({ width: obj.area[0] })
        this.modalObj.info = obj.content
        this.modalObj.showCancel = obj.btns == "1" ? false : true
        this.modalObj.sub = nextSub
        this.modalObj.checkText = obj.needReadText
        this.modalObj.isCheck = obj.needRead
        this.modalObj.isInput = this.inputModal.includes(obj.index) ? true : false
        this.modalObj.frameId = obj.index
      }
    },
    onBlur() {
      this.inputVal = this.toDecimal2(this.inputVal);
    },
    onInput(value) {
      this.inputVal = value.replace(/[^\d.]/g, '');
    },
    toDecimal2(x) {
      if (x.split('.').length > 2) {
        x = x.split('.').slice(0, 2).join('.');
      }
      var f = parseFloat(x);
      if (isNaN(f)) {
        return '0.00';
      }
      f = Math.round(x * 100) / 100;

      var s = f.toString();
      var rs = s.indexOf('.');
      if (rs < 0) {
        rs = s.length;
        s += '.';
      }
      while (s.length <= rs + 2) {
        s += '0';
      }
      return s;
    }
  }
}
</script>
<style scoped>
.modal {
  display: none;
}

.modal-bg {
  display: none;
  position: absolute;
  background-color: black;
  z-index: 1001;
  -moz-opacity: 0.3;
  opacity: 0.3;
  filter: alpha(opacity=30);
}
.outBox {
  max-height: 500px;
  overflow-y: auto;
}
</style>
