// 选择专利js
export const fastSelectPatent = {
  data() {
    return {
      // 数据
      tableData: [],
      //总数
      total: 0,
      //分页及选择专利的信息
      queryParams: {
        current: 1,
        size: 10,
        groupField: '0',
        selected: [],
        isAdd: [],
        isDelete: [],
      },
      //是否编辑//编辑按钮暂时隐藏，改为默认为true
      refresh: true,
      //选择的专利集合
      patentNoList: [],
      //选择总数
      selectedTotal: 0,
      //选择专利加载
      selectNumberLoading: false,
      // 起始位置
      startNumber: 1,
      // 结束位置
      endNumber: 0,
      // 判断是否是全部选择或范围选择
      quickSelect: false,
      quickSelectArr: [],
    }
  },
  methods: {
    // 是否刷新
    isRefresh() {
      this.refresh = false
      this.$nextTick(() => {
        this.refresh = true
      })
    },
    //快速选择
    handleSelectNumber(type) {
      switch (type) {
        case 0: //本页选择
          this.patentNoList = [...new Set(this.patentNoList.concat(this.$store.state.patent.records.map((item) => item.patentNo))),];
          this.queryParams.isAdd = [...new Set(this.queryParams.isAdd.concat(this.$store.state.patent.records.map((item) => item.patentNo))),];
          break;
        case 1: //全部选择
          this.startNumber = 1;
          this.endNumber = this.total;
          this.$set(this.queryParams, 'startNumber', 1)
          this.$set(this.queryParams, 'endNumber', this.total)
        case 2: //范围选择
          this.queryParams.isDelete = [];
          this.queryParams.isAdd = [];
          this.patentNoList = [];
          this.quickSelect = true;
          if (type == 2) {
            if (!Number(this.queryParams.startNumber) || !Number(this.queryParams.endNumber)) {
              this.$set(this.queryParams, 'startNumber', this.startNumber > 0 ? this.startNumber : 1)
              this.$set(this.queryParams, 'endNumber', this.endNumber > 0 ? this.endNumber : this.total)
              break;
            }
            this.startNumber = this.queryParams.startNumber;
            this.endNumber = this.queryParams.endNumber;
          }
          this.commonSwitch();
          break;
      }
      this.getSelectedTotal()
      this.isRefresh()
    },
    //每页全部选择或范围选择的专利
    commonSwitch() {
      var arr = []
      if (this.queryParams.size * this.queryParams.current >= this.startNumber) {
        if (this.queryParams.size * this.queryParams.current >= this.endNumber) {
          if (this.queryParams.size * (this.queryParams.current - 1) + 1 <= this.startNumber) {
            var a = this.startNumber - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            var b = this.endNumber - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            for (var y = a; y <= b; y++) {
              arr.push(this.tableData[y].patentNo)
            }
          } else {
            var a = this.queryParams.size * (this.queryParams.current - 1) + 1 - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            var b = this.endNumber - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            for (var y = a; y <= b; y++) {
              arr.push(this.tableData[y].patentNo)
            }
          }
        } else {
          if (this.queryParams.size * (this.queryParams.current - 1) + 1 <= this.startNumber) {
            var a = this.startNumber - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            var b = this.queryParams.size * this.queryParams.current - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            for (var y = a; y <= b; y++) {
              arr.push(this.tableData[y].patentNo)
            }
          } else {
            var a = this.queryParams.size * (this.queryParams.current - 1) + 1 - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            var b = this.queryParams.size * this.queryParams.current - (this.queryParams.size * (this.queryParams.current - 1) + 1);
            for (var y = a; y <= b; y++) {
              arr.push(this.tableData[y].patentNo)
            }
          }
        }
      }
      // this.patentNoList = JSON.parse(JSON.stringify(arr))
      this.quickSelectArr = JSON.parse(JSON.stringify(arr))
      this.getHaveChoose(arr)
    },
    //获取已选择的专利
    getHaveChoose(arr) {
      var arr1 = [...new Set(arr.concat(this.queryParams.isAdd))]
      this.patentNoList = arr1.filter((x) => this.queryParams.isDelete.indexOf(x) == -1)
    },
    //获取已选择的总条数
    getSelectedTotal() {
      this.selectedTotal = Number(this.endNumber) - Number(this.startNumber) + 1 + Number(this.queryParams.isAdd.length) - Number(this.queryParams.isDelete.length)
    },
    //获取手动选择的专利
    getChoosePatentNo(patentNo) {
      if (this.quickSelect) {
        var index = this.queryParams.isDelete.findIndex(item => {
          return item == patentNo
        })
        if (index == -1) {
          var index2 = this.quickSelectArr.findIndex(item => {
            return item == patentNo
          })
          if (index2 == -1) {
            this.setIsAdd(patentNo)
          } else {
            this.queryParams.isDelete.push(patentNo)
          }
        } else {
          this.queryParams.isDelete.splice(index, 1)
        }
      } else {
        this.setIsAdd(patentNo)
      }
      this.getSelectedTotal()
    },
    //是否加入isAdd里面
    setIsAdd(patentNo) {
      var index = this.queryParams.isAdd.findIndex(item => {
        return item == patentNo
      })
      if (index != -1) {
        this.queryParams.isAdd.splice(index, 1)
      } else {
        this.queryParams.isAdd.push(patentNo)
      }
    },
    //取消选择
    handleCancelSelectNumber() {
      this.patentNoList = [];
      this.queryParams.isAdd = []
      this.queryParams.isDelete = []
      this.startNumber = 1
      this.queryParams.startNumber = 1
      this.queryParams.endNumber = this.total
      this.endNumber = 0
      this.quickSelect = false
      this.selectedTotal = 0
      // this.getList()
      this.isRefresh()
    },
    // 范围选择起始数字框
    change1(val) {
      if (!isNaN(val)) {
        if (!val || val <= 0) {
          this.queryParams.startNumber = 1
        } else {
          if (this.queryParams.startNumber > this.total) {
            this.queryParams.startNumber = this.total
          }
        }
      } else {
        this.queryParams.startNumber = 1
      }
    },
    // 范围选择截止数字框
    change2(val) {
      if (!isNaN(val)) {
        if (!val || val <= 0) {
          this.queryParams.endNumber = this.total
        } else {
          if (this.queryParams.endNumber > this.total) {
            this.queryParams.endNumber = this.total
          }
        }
      } else {
        this.queryParams.endNumber = this.total
      }
    },
  },
}