// pageage/safetyManagement/securityCheckGR/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userId:"",
        projectId:"",
        projectData:{},
        initData: {},
        show:false,
        list:[],
        listData:[],
        pageNum:1,
        pageSize:10,
        lastDataSize:10,
        activeState:"await",
        allCount:0,
        awaitCount:0,
        finishedCount:0,
    },

     /**
   * 标签切换
   */
  typeJump(e){
    let index = e.currentTarget.dataset.index;
    let nav = "";
    if(index == 1){
      nav = 'all';
    }else if(index == 2){
      nav = 'await';
    }else if(index == 3){
      nav = 'finished';
    }

    this.setData({
      activeState:nav,
      pageNum:1,
      pageSize:10,
      lastDataSize:10,
      listData:[],
      list:[]
    });
    this.getListData(this.data.userId);
  },

    getInfo(e){
        let {deployId,procInsId,taskId,taskName,procDefKey,startDeptName,startUserName,procDefName,businessKeyName,finishTime} = e.currentTarget.dataset.set
        if(!finishTime){
          finishTime="";
        }
        if(taskName=="提交申请"){
          if(procDefKey=="flow_xmglzdl_qjspb"){
            wx.redirectTo({
              url: `../editLeaveTask/index?deployId=${deployId}&procInsId=${procInsId}&nickName=${startUserName}&deptName=${startDeptName}&procDefName=${procDefName}&taskId=${taskId}&taskName=${taskName}&projectName=${businessKeyName}&ret=myProcess`,
            })
          }else{
            wx.redirectTo({
              url: `../editTask/index?deployId=${deployId}&procInsId=${procInsId}&nickName=${startUserName}&deptName=${startDeptName}&procDefName=${procDefName}&taskId=${taskId}&taskName=${taskName}&projectName=${businessKeyName}&ret=myProcess`,
            })
          }
        }else{
          //根据不同业务跳转不同表单
          if(procDefKey=="flow_xmglzdl_qjspb"){
            wx.redirectTo({
            url: `../detailLeaveTask/index?deployId=${deployId}&procInsId=${procInsId}&nickName=${startUserName}&deptName=${startDeptName}&procDefName=${procDefName}&taskId=${taskId}&taskName=${taskName}&projectName=${businessKeyName}&finishTime=${finishTime}&ret=myProcess`,
            })
          }else{
            wx.redirectTo({
              url: `../detailTask/index?deployId=${deployId}&procInsId=${procInsId}&nickName=${startUserName}&deptName=${startDeptName}&procDefName=${procDefName}&taskId=${taskId}&taskName=${taskName}&projectName=${businessKeyName}&finishTime=${finishTime}&ret=myProcess`,
            })
          }
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
     //获取缓存数据
      wx.getStorage({
        key: 'userinfo',
        success:res=>{
          this.setData({
            userId:res.data.userId,
            projectId:app.globalData.projectId,
            initData:{text:app.globalData.projectName,id:app.globalData.projectId}
          })  
          this.getListData(res.data.userId);
        }
      })
    },

  /**
    * 查询项目举牌验收数据
    */
   getListData(userId) {
    //进入这里说明数据加载完毕
    if(this.data.lastDataSize<this.data.pageSize){
      //app.toast("已经到底了，暂无可继续加载数据!")
      return;
    } 
    var that = this;
    //businessKey注解掉可以跨项目审批
    let param = {
       "activeName":this.data.activeState,
       "nowRole":"99",//这里只能查看自己的数据
       "nowUser":userId
    }
    this.queryCount(param);
    param.pageNum=that.data.pageNum;
    param.pageSize=that.data.pageSize;
    wx.request({
      url: app.globalData.reqUrl + '/wechat/flowTask/allList',
      data:param,
      method: "get",
      success: function (res) {
        res = res.data;
        if(res.code=="200"){
          //这里处理this.data.lastDataSize=this.data.pageSize
          if(that.data.list.length>0 && res.rows.length>0 && that.data.list[0].taskId==res.rows[0].taskId){
            that.setData({
              lastDataSize:0,
            })
          }else{
            let _list = [];
            res.rows.forEach( it => {
              it.duration = that.getDurationDate(it.duration);
              _list.push(it);
            });
            that.setData({
              pageNum:that.data.pageNum+1,
              lastDataSize:res.rows.length,
              list:res.rows,
              listData:that.data.listData.concat(_list)
            })
          }
        }else{
          app.toast(res.msg);
        }
      }
    });
    },

    queryCount(param){
      let that = this;
      //查询统计数量
      wx.request({
        url: app.globalData.reqUrl + '/wechat/flowTask/queryCount',
        data: param,
        method: "get",
        success: function (res) {
          let awaits=0;
          if(res.data.data.await){
            awaits=res.data.data.await;
          }
          let finished=0;
          if(res.data.data.finished){
            finished=res.data.data.finished;
          }
          that.setData({
            allCount:awaits+finished,
            awaitCount:awaits,
            finishedCount:finished
          });
        }
      });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    returnToPage: function () {
      /*关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面*/
        wx.redirectTo({
          url: '../await/index',
        })
    },

    onScrollToLower(){
      console.log("滚动条到底了，开始加载新数据");
      this.getListData(this.data.userId);
    },

      //项目切换 返回值
  onProjectSelect(e){
    let projectId = e.detail.id;
    let projectName = e.detail.text;
    app.globalData.projectId = projectId;
    app.globalData.projectName = projectName;  
    this.onLoad();
  },

  getDurationDate(val){
    let day=0;
    let hours=0;
    let min = val; 
    if(min>1440){
     day = parseInt(min/1440);
     min = min % 1440;
     if(min>60){
      hours = parseInt(min/60);
      min = min % 60;
     }
    }else if(min>60){
      hours = parseInt(min/60);
      min = min % 60;
    }
    if(day>0){
      if(day<10) day="0"+day;
      if(hours<10) hours="0"+hours;
      if(min<10) min="0"+min;
      return day+"天"+hours+"小时"+min+"分钟";
    }
    if(hours>0){
      if(hours<10) hours="0"+hours;
      if(min<10) min="0"+min;
      return hours+"小时"+min+"分钟";
    }
    if(min>0){
      if(min<10) min="0"+min;
      return min+"分钟";
    }
    if(min==0){
      return "1分钟";
    }
  },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})