
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    activeIndex: 1,
    httpType: 'waiting',
    keyword: '',
    page: 1,
    isBh: false,//是否显示驳回弹窗
    textArea: "",
    disabled: false,
    objectArray: [
      {
        id: 0,
        name: '全部',
      },
      {
        id: 1,
        name: '积分任务',
      },
      {
        id: 2,
        name: '积分申请',
      },
      {
        id: 3,
        name: '积分奖扣',
      },
    ],
    arrIndex: 0,
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "我的审批" });
  },
  onShow() {
    if (app.globalData.userData) {
      that.refreshData();
    } else {
      app.login(app.globalData.corpId, function (is) {
        if (is) {
          that.refreshData();
        } else {
          dd.reLaunch({
            url: '../../noJurisdiction/noJurisdiction'
          })
        }
      },function(){})
    }
  },
  openPl(){
    dd.navigateTo({
      url: '../approval_batch/approval_batch'
    })
  },
  bindObjPickerChange(e) {
    this.setData({
      arrIndex: e.detail.value,
      page: 1,
    })
    this.getData();

  },
  //当在详情返回时刷新列表，但你请求接口
  refreshData(id) {
    if (this.data.page == 1) {
      dd.hideLoading();
      that.getData();
    } else {
      if (id) {//当page不为一并下一页返回上一页不执行onshow()
        var arr = this.data.dataList;
        arr.forEach((element, index) => {
          if (element.id == id) {
            arr.splice(index, 1);
          }
        });
        this.setData({
          dataList: arr
        })
      }
    }
  },
  bindTextAreaBlur(e) {
    this.setData({
      textArea: e.detail.value
    });
  },
  confirmBh() {
    var item = that.data.bhItem;
    if (that.data.textArea == '') {
      app.globalData.showToast("请输入审批意见");
      return;
    }
    this.setData({ disabled: true });
    app.$post("api/integral/review", { review_id: item.id, remark: that.data.textArea, action: "refuse", ticket_count: 0 }).then((res) => {
      that.closeBh()
      app.globalData.showToast("已驳回");
      that.setData({ disabled: false });
      that.refreshData(item.id);
    })
  },
  closeBh() {
    this.setData({ isBh: false, textArea: '' })
  },
  openBh(e) {
    var item = e.target.dataset.item;
    this.setData({
      isBh: true,
      bhItem: item
    })
  },
  activeItem(e) {
    var index = e.target.dataset.index;
    this.setData({
      activeIndex: index,
      httpType: index == '1' ? 'waiting' : 'reviewed',
      page: 1,
      dataList: []
    })
    this.getData();
  },
  onReset() {
    this.setData({
      keyword: '',
      isVal: false,
      page: 1,
    })
    this.getData();
  },
  //选择规则分类
  bindKeyInput(e) {
    this.setData({
      keyword: e.detail.value,
      page: 1,
      isVal: e.detail.value ? true : false,
    });
    this.getData();
  },
  openDetail(e) {
    var id = e.target.dataset.item.id;
    dd.navigateTo({
      url: '../approveDetail/approveDetail?id=' + id
    })
  },
  getData(type) {
    app.$get("api/integral/review/list", {source_type:that.data.arrIndex,type: that.data.httpType, page: that.data.page, pt_id: 0, page_size: 20, keyword: that.data.keyword }).then((res) => {
      var list = res.data.data.list || [];
      list.forEach(element => {
        if (element.pt_id) {
          element.pt_name = app.getTypesItem(element.pt_id).name
        }
      });
      if(that.data.page==1){
        this.setData({
          dataList:[]
        })
      }
      var data = list;
      var dataList = that.data.dataList || [];
      if (type == '2') {
        if (data.length < 20) {
          this.setData({
            dataList: dataList.concat(data),
            isData: true
          })
        } else {
          this.setData({
            dataList: dataList.concat(data),
          })
        }
      } else {
        this.setData({ dataList: data, isData: false })
      }
    })
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({ page: ++that.data.page })
      this.getData("2");
    }
  },
});
