<!--
 * @Author: zqm
 * @Date: 2021-06-25 13:48:23
 * @LastEditTime: 2022-03-30 17:34:47
 * @LastEditors: Please set LastEditors
 * @Description: 税种列表信息
 * @FilePath: \taxFormToolFramework\src\components\menuGroup.vue
-->
<template>
  <el-scrollbar ref="menuGroupScrollbar" class="menuGroupScrollbar" :style="'height:'+height">
    <el-menu class="el-menu-vertical-demo menuGroup" :default-active="defaultBbDm">
      <el-menu-item class="bbList" v-for="(item,index) in tableList" :key="item.bbDm+'_'+item.detailPcdm" :index="item.bbDm+'_'+item.detailPcdm">
        <el-col class="bbMc" :span="item.detailMultiPatch=='Y'?16:23">
          <span class="btbz" v-if="item.xtbz=='B'">【必填表】</span>
          <span class="bbMcTitle" v-html="item.bbMc" :title="item.bbMc" @click="menuClick(item)" v-if="item.detailMultiPatch!='Y'"></span>
          <span class="bbMcTitle" v-html="item.bbMc+'('+item.detailPcdm+')'" :title="item.bbMc+'('+item.detailPcdm+')'" @click="menuClick(item,'click')" v-if="item.detailMultiPatch=='Y'"></span>
        </el-col>
        <el-col class="pcBtn" :span="7" v-if="item.detailMultiPatch=='Y'">
          <button size="mini" :disabled="declarationStatus=='Y'" class="button_bar btn_small addDeletebtn" @click="addPcTableData(item,index)" v-if="showAddButton(item,index)">添加</button>
          <button size="mini" :disabled="declarationStatus=='Y'" class="button_bar btn_small addDeletebtn" @click="deletePcTableData(item,index)" v-if="item.detailPcdm!=1">删除</button>
        </el-col>
        <i class="el-icon-success success bbztIcon right" v-if="item.verificationState==true||item.verificationState=='true'"></i>
        <i class="el-icon-error error bbztIcon right" v-if="item.verificationState==false||item.verificationState=='false'"></i>
      </el-menu-item>
    </el-menu>
  </el-scrollbar>
</template>

<script>
export default {
  name: "menuGroup",
  props: {
    // 选项列表数据(树形结构的对象数组)
    height: { type: String, default: "" },//高度
    tableList: { type: Array, default: [] },//列表数据
    declarationStatus: { type: String, default: "" }//申报状态
  },
  watch: {
    '$store.state.data.defaultMenu': {
      handler(newName, oldName) {
        this.defaultBbDm = newName
      }
    }
  },
  data() {
    return {
      tableList: [],
      defaultBbDm: this.$store.state.data.defaultMenu,//默认选中的报表
      openTable: {},//当前开表
      pcData: {//多批次
        index: "",
        pcObj: {},
        pcAddAjax: false,
        pcDelAjax: false,
      }
    };
  },
  methods: {
    // 点击菜单
    menuClick(bbData) {
      var _self = this;
      var changeFlag = this.valuesEqual();
      if (!changeFlag && !this.$store.state.data.saveStatus) {
        this.$confirm("切换报表前，是否需要保存当前报表数据?", '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var obj = _self.recursionChild(_self.$parent.$children, "saveButton");
          obj.$refs.saveButton.saveFun("tab");
          _self.openTable = _self.deepClone(bbData);
          _self.$emit('menuSelect', bbData);
        }).catch(() => {
          _self.openTable = _self.deepClone(bbData);
          _self.$emit('menuSelect', bbData);
        });
      } else {
        this.openTable = this.deepClone(bbData);
        this.$emit('menuSelect', bbData);
      }
    },
    //控制是否显示新增按钮(不允许跨批次增加，避免数据混乱)
    showAddButton(item, index) {
      var len = Enumerable.From(this.tableList).Where(function (obj) {
        return obj.bbDm == item.bbDm
      }).ToArray();
      if (len.length > 0 && len[len.length - 1].detailPcdm == item.detailPcdm) {//只允许在最后一个批次的基础上继续新增
        return true;
      }
      return false;
    },
    //判断是否允许创建新的批次
    addPcTableData(item, index) {
      if (item.verificationState == 'empty') {//不允许跨批次增加，避免数据混乱
        this.$alert('《' + item.bbMc + '》批次' + item.detailPcdm + '尚未保存，暂不支持创建新批次报表！', '提示', { type: 'warning' });
        return false;
      }
      var _self = this;
      if (_self.pcData.pcAddAjax) {//添加批次 pcDelAjax：true后台添加，false前台添加
      } else {
        _self.addListPC(item, index);
      }
    },
    //前台增加新的批次
    addListPC(item, index) {
      var obj = this.deepClone(item);
      obj.detailPcdm = parseFloat(item.detailPcdm) + 1;
      obj.sbId = "";
      obj.state = "N";
      obj.verificationState = "empty";
      this.tableList.splice(index + 1, 0, obj);
      this.$parent.tableList = this.deepClone(this.tableList);
    },
    //判断是否允许删除批次
    deletePcTableData(item, index) {
      this.pcData.index = index;
      this.pcData.pcObj = this.deepClone(item);
      var _self = this;
      var titleTip = "《" + item.bbMc + "》批次" + item.detailPcdm;
      if (item.verificationState != 'empty') {
        // this.$alert("该批次数据已保存，请进入填报页面进行“重置当前报表”进行删除！", '提示', { type: 'warning' });
        //走后台删除逻辑
        var obj = this.recursionChild(this.$parent.$refs.buttonArea.$children, "deleteButton");
        obj.deleteFun("delete");
      } else {
        this.$confirm("该批次数据尚未保存，确定要删除该批次报表吗？", '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (item.verificationState != 'empty') {
          } else {
            _self.deletePcList('deletePc');
          }
        }).catch(() => {
        });
      }
    },
    //前台列表删除多批次
    deletePcList(type) {
      var _self = this;
      if (_self.pcData.pcDelAjax) {//删除批次 pcDelAjax：true后台删除，false前台删除
      } else {
        if (_self.openTable.bbDm == _self.pcData.pcObj.bbDm) {
          var lastPc = _self.tableList[_self.pcData.index - 1];
          _self.openTable = _self.deepClone(lastPc);
          _self.$parent.menuSelect(lastPc);
          _self.defaultBbDm = lastPc.bbDm;
        }
        _self.tableList.splice(_self.pcData.index, 1);
      }
    },
  },
}

</script>
<style scoped>
.menuGroup {
  width: 100% !important;
  overflow: hidden;
  border: none !important;
}
.menuGroup .el-icon-arrow-down {
  opacity: 1 !important;
}
.bbList {
  padding: 0px 10px !important;
  height: 45px !important;
  line-height: 45px;
}
.btbz {
  color: #ff1133;
  display: inline;
}
.bbMc {
  font-size: 14px;
  font-family: Microsoft YaHei Bold, Microsoft YaHei Bold-Bold;
  text-align: left;
  white-space: normal;
  word-break: break-all;
  overflow: hidden;
  height: 45px !important;
  line-height: 45px;
  white-space: nowrap;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
}
.bbMcTitle {
  display: inline;
}
.pcBtn {
  height: 45px !important;
  line-height: 45px;
}
.addDeletebtn {
  font-size: 10px !important;
  height: 22px !important;
  line-height: 20px !important;
  min-width: 45px !important;
  width: 45px !important;
  margin: 9px 4px 9px 0px !important;
}
.success {
  color: #4ec87c;
}
.error {
  color: #ff3d33;
}
.warning {
  color: orange;
}
.bbztIcon {
  width: 3% !important;
  font-size: 12px !important;
  line-height: 45px;
  position: absolute;
  top: 2px;
}
.el-menu-item.is-active {
  background-color: #d1ecff !important;
}
.menuGroupScrollbar {
  padding-left: 0px;
  padding-top: 0px;
  padding-right: 20px;
}
</style>