var app = getApp()
var that;
var animation;
Page({
  data: {
    item: {//录入选项列表
      checked: false,//判断是否开启指定规则
      isArrindex: true,
      isArrindex2: true,
      review_id: '',//id
      action: 'complete',
      rule_id: '',//积分分类id
      rule_id2: '',//分类ID
      rule_name: '',
      item_id: '',//积分细则id 不选可以为0
      point: '',//积分
      reviewer_id: '',//递交者
      remark: '',//备注
      ticket_count: false,//奖票数量
      disabled:false,
      defaultRuleId:'',
    },
    users: {
      id:''
    },
    animationInfo: '',
    isShowType: true,//是显示选择规则还是显示选择分类
    isData: false,
  },
  onLoad(e) {
    that = this;
    if(e.id){
    that.getData(e.id);
    }
    dd.setNavigationBar({ title: "审批" });
  },
  onShow() {
    animation = dd.createAnimation({
      duration: 200,
      timeFunction: "linear",
    });
  },
  //关闭规则
  onClose() {
    animation.translateY(750).step();
    that.setData({
      animationInfo: animation.export()
    });
  },
  //备注输入
  bindTextAreaBlur: function (e) {
    this.setData({
      ['item.remark']: e.detail.value,
    });
  },
  onSelectUser(item) {
    this.setData({
      users: item[0]
    })
  },


  bindTextAreaBlur(e) {
    this.setData({
      ['item.remark']: e.detail.value,
    });
  },
  showToast(text) {
    app.globalData.showToast(text);
  },
  //提交
  formSubmit() {
    var item = that.data.item;
    var data, point;
    var num = item.checked ? item.point : item.point2
    if (item.checked) {
      if (!item.rule_id) {
        this.showToast("请选择所属规则")
        return;
      }
      if (item.isArrindex != true) {
        point = 0 - num;
      } else {
        point = num;
      }
      if (point == 0 || !point) {
        this.showToast("积分不能为0或空")
        return;
      }
      if (point < item.selectRule.min_point || point > item.selectRule.max_point) {
        this.showToast(`积分超出可输入范围`);
        return;
      }
    } else {
      if (!item.rule_id2) {
        this.showToast("请选择所属分类")
        return;
      }
      if (item.isArrindex2 != true) {
        point = 0 - num;
      } else {
        point = num;
      }
      if (point == 0 || !point) {
        this.showToast("积分不能为0或空")
        return;
      }
    }

    data = {
      review_id: item.review_id,
      action: 'complete',
      rule_id: item.checked ? item.rule_id : item.rule_id2,
      item_id: item.checked ? item.item_id : 0,
      point: Number(point),
      reviewer_id: that.data.users.id ? that.data.users.id : 0,
      remark: item.remark,
      ticket_count: item.ticket_count ? 1 : 0,
    }
    this.setData({disabled:true});
    app.$post("api/integral/review", data).then((res) => {
      if(res.data.code==1){
        this.showToast("已审批");
        setTimeout(() => {
          this.setData({disabled:false});
          dd.navigateBack({delta: 2})
        }, 1000);
      }else{
        this.setData({disabled:false});
         this.showToast(res.data.msg);
      }
    }).catch(err=>{
        this.setData({disabled:false});
    })
  },
  bindKeyInput(e) {
    that.setData({
      ['item.point']: e.detail.value
    })
  },
  bindKeyInput2(e) {
    that.setData({
      ['item.point2']: e.detail.value
    })
  },
  switchChange(e) {
    that.setData({
      ['item.checked']: e.detail.value
    })
  },
  switchChange2(e) {
    that.setData({
      ['item.ticket_count']: e.detail.value
    })
  },
  bindPickerChange(e) {
    this.setData({
      ['item.isArrindex']: !that.data.item.isArrindex,
    });
  },
  bindPickerChange2(e) {
    this.setData({
      ['item.isArrindex2']: !that.data.item.isArrindex2,
    });
  },
  //选择规则分类
  onSelectType(item) {
    this.setData({
      ['item.rule_id2']: item.id,
      ['item.rule_name']: item.name,
    });
  },
  //选择规则
  onSelectItem(item) {
    var strMin;
    if (item.min_point < 0) {
      var min = item.min_point.toString();
      var strMin = min.slice(1, min.length);
    } else {
      strMin = item.min_point;
    }
    that.setData({
      ['item.rule_id']: item.rule_id,
      ['item.selectRule']: item,
      ['item.item_id']: item.id,
      ['item.point']: Number(strMin),
      ['item.isArrindex']: item.min_point > 0 ? true : false,
    })
  },
  //显示规则
  openSelect(e) {
    if(e.target.dataset.ruleId){
       that.setData({ defaultRuleId:e.target.dataset.ruleId});
    }
    that.setData({ activeIndex: '',isShowType:'' });
    that.setData({
      isShowType: e.target.dataset.num,
      activeIndex: e.target.dataset.index,
    });
    animation.translateY(0).step();
    that.setData({
      animationInfo: animation.export()
    });
  },
  getData(id) {
    var getPoint = 0;//积分1017 1030 1006 992 985 1389
    app.$get("api/integral/review", { review_id: id }).then((res) => {
      var data = res.data.data;
      if (data.point) {
        if (data.point < 0) {
          var min = data.point.toString();
          getPoint = min.slice(1, min.length);
        } else {
          getPoint = data.point;
        }
      }

      var isData, selectRule;
      var checked = true;
      if (data.rule_id && data.item_id) {
        console.log(1);
        isData = 1;
        selectRule = {
          range_type: data.item_range_type,
          min_point: data.item_min_point,
          max_point: data.item_max_point,
          remark: data.item_name,
          pt_Obj:app.getTypesItem(data.pt_id),
        }
      } else if (data.rule_id && data.point) {
        console.log(2);
        isData = 2
        selectRule = '';
      } else if (data.rule_id) {
        console.log(3);
        isData = 3;
        selectRule = '';
      } else {
        console.log(4);
        isData = 4
        selectRule = '';
      }
      that.setData({
        isData: isData,
        ['item.pid']: data.pid==0 ? false:true,
        ['item.review_id']: data.id,
        ['item.checked']: checked,
        ['item.selectRule']: selectRule,
        ['item.rule_id']: data.rule_id,
        ['item.item_id']: data.item_id,
        ['item.rule_name']: data.rule_name,
        ['item.point']: getPoint == 0 ? '' : getPoint,
        ['item.isArrindex']: data.point >= 0 ? true : false,
      })
      console.log(this.data.item);
    })
  },
});
