import {
  queryTaskCount,
  myInstanceList
} from '../../../api/flowable'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    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.getMyDataList();
  },

  infoTap(e) {
    let {
      deployId,
      procInsId,
      taskId,
      taskName,
      category,
      startDeptName,
      startUserName,
      procDefName,
      businessKey,
      businessKeyName,
      businessDeptId,
      businessKeyParName,
      startUserId,
      finishTime
    } = e.currentTarget.dataset.set
    if (taskName == "提交申请") {
      wx.redirectTo({
        url: `../editTask/index?deployId=${deployId}&procInsId=${procInsId}&nickName=${startUserName}&deptName=${startDeptName}&procDefName=${procDefName}&taskId=${taskId}&taskName=${taskName}&category=${category}&projectName=${businessKeyName}&businessKey=${businessKey}&businessKeyParName=${businessKeyParName}&businessDeptId=${businessDeptId}&startUserId=${startUserId}`,
      })
    } else {
      wx.redirectTo({
        url: `../detailTask/index?deployId=${deployId}&procInsId=${procInsId}&nickName=${startUserName}&deptName=${startDeptName}&procDefName=${procDefName}&taskId=${taskId}&taskName=${taskName}&category=${category}&projectName=${businessKeyName}&businessKey=${businessKey}&businessKeyParName=${businessKeyParName}&businessDeptId=${businessDeptId}&startUserId=${startUserId}&finishTime=${finishTime}&ret=myProcessIns`,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      pageNum: 1,
      pageSize: 10,
      lastDataSize: 10,
      listData: [],
      list: [],
      initData: app.globalData.searchProject
    })
    //查询我的申请数据
    this.getMyDataList();
  },

  /**
   * 查询我的流程申请
   */
  getMyDataList() {
    //进入这里说明数据加载完毕
    if (this.data.lastDataSize < this.data.pageSize) {
      //app.toast("已经到底了，暂无可继续加载数据!")
      return;
    }
    var that = this;
    let param = {
      "activeName": this.data.activeState
    }
    this.queryTaskCount(param);
    param.pageNum = that.data.pageNum;
    param.pageSize = that.data.pageSize;
    myInstanceList(param).then(res => {
      //这里处理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)
        })
      }
    });
  },

  /***
   * 查询申请发起数量统计
   */
  queryTaskCount(param) {
    queryTaskCount(param).then(res => {
      let awaits = 0;
      if (res.data.await) {
        awaits = res.data.await;
      }
      let finished = 0;
      if (res.data.finished) {
        finished = res.data.finished;
      }
      this.setData({
        allCount: awaits + finished,
        awaitCount: awaits,
        finishedCount: finished
      });
    });
  },

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

  },

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

  },

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

  /**
   * 分页加载数据
   */
  onScrollToLower() {
    console.log("滚动条到底了，开始加载新数据");
    this.getMyDataList();
  },

  //项目切换 返回值
  onProjectSelect(e) {
    let project = {
      id: e.detail.id,
      text: e.detail.text,
      projectId: e.detail.id,
      projectName: e.detail.text
    }
    app.globalData.searchProject = project;
    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() {

  }
})