// pages/educations-list/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    jumpState:'',
    //参数
    DeviceGroupData: [],
    numTime:0,
    attendNumber:0,
    imgShow:false,
    initData:{},
    trainingList:[],
    type:'',
    pageNo:1,
    pageSize:10,
    pageFlag:true,
    activeState:4,
    titleName:'三级安全教育',
    trainingType:'',
    startDate:'',
    endDate:'',
    collaborateRanksId:'',
    imgUrl:''
  },

  //项目切换 返回值
  onProjectSelect(e){
    this.onClickShow();
    let projectId = e.detail.id;
    let projectName = e.detail.text;
    app.globalData.projectId = projectId;
    app.globalData.projectName = projectName;
    this.setData({
      projectId:projectId,
      projectName:projectName,
    })
    let data = {"jumpState":this.data.jumpState};
	  this.onLoad(data);
  },
  winterPut(e){
    this.setData({
      imgShow:true,
      imgUrl:e.currentTarget.dataset.url
    })
  },
   //关闭放大的图片
  closeImages(){
    this.setData({
      imgShow:false
    })
  },

  //筛选条件
  screenCondition(e){
    this.setData({
      collaborateRanksId:e.detail.typeId,
      startDate:e.detail.startDate,
      endDate:e.detail.endDate,
      pageNo:1,
      type:1
    })
    this.getTrainingList();
    this.getTrainingNumberOfPeopleCount();
  },

  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  onClickShow() {
    this.setData({ loadShow: true });
  },

  onClickHide() {
    this.setData({ loadShow: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //加载蒙版
     that.onClickShow();
    //获取缓存数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({
          loginName:res.data.loginName,
          userName:res.data.userName,
          deptId:res.data.deptId,
          projectName: app.globalData.projectName,
          projectId:app.globalData.projectId,
          initData:{text:app.globalData.projectName,id:app.globalData.projectId},
          jumpState:options.jumpState,
          titleName:'三级安全教育',
          activeState:4,
          pageNo:1,
          type:1,
          trainingType:4,
          startDate:'',
          endDate:'',
          collaborateRanksId:''
        }) 
        that.getTrainingList();
        that.getTrainingNumberOfPeopleCount();
        that.getCollaborativeTeam();
      }
    }) 
  },

  /**
   * 教育培训列表
   */
  getTrainingList(){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/training/getTrainingList',
      data:{
        "projectId":this.data.projectId,
        "trainingType":this.data.trainingType,
        "pageNo":(this.data.pageNo-1)*this.data.pageSize,
        "startDate":this.data.startDate,
        "endDate":this.data.endDate,
        "collaborateRanksId":this.data.collaborateRanksId,
      },
      method:"get",
      success:function(res){
        if(res.data.length < that.data.pageSize){
          that.setData({ pageFlag:false })
        }else{
          that.setData({ pageFlag:true })
        }

        let array;
        if(that.data.type == 1){
          array = [];
        }else{
          array = that.data.trainingList
        }
        for(let i = 0;i<res.data.length;i++){
          array.push(res.data[i]);
        }
        that.setData({trainingList:array});
        that.onClickHide();
      }
    })
  },

  /**
   * 教育培训次数、人数
   */
  getTrainingNumberOfPeopleCount(){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/training/getTrainingNumberOfPeopleCount',
      data:{
        "projectId":this.data.projectId,
        "trainingType":this.data.trainingType,
        "startDate":this.data.startDate,
        "endDate":this.data.endDate,
        "collaborateRanksId":this.data.collaborateRanksId
      },
      method:"get",
      success:function(res){
        that.onClickHide();
        that.setData({
          numTime:res.data.typeNum,
          attendNumber:res.data.personNum
        })
      }
    })
  },

  /**
   * 标签切换
   */
  trainingTypeJump(e){
    this.onClickShow();
    let index = e.currentTarget.dataset.index;
    let titleName =  '';
    if(index == 4){
      titleName = '三级安全教育';
    }else if(index == 5){
      titleName = '班前讲话';
    }else if(index == 3){
      titleName = '专项培训';
    }else if(index == 6){
      titleName = '应急演练'
    }
    this.setData({
      titleName:titleName,
      activeState:index,
      pageNo:1,
      type:1,
      trainingType:index,
      startDate:'',
      endDate:'',
      collaborateRanksId:''
    });
    this.getTrainingList();
    this.getTrainingNumberOfPeopleCount();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pageNo:this.data.pageNo+1,
      type:2
    })
    if(this.data.pageFlag){
      this.getTrainingList();
    }
  },

  /**
   * 协助队伍
   */
  getCollaborativeTeam(){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/training/getCollaborativeTeam',
      data:{
        "projectId":this.data.projectId,
      },
      method:"get",
      success:function(res){
        let array = [{"id":'',"text":'请选择'}];
        for(let i =0;i<res.data.length;i++){
          array.push({
            "id":res.data[i].id,
            "text":res.data[i].uninName
          })
        }
        that.setData({
          DeviceGroupData:array
        })
      }
    })
  },

  /**
   * 详情页面
   * @param {*} e 
   */
  checkDetailsData(e){
    let id = e.currentTarget.dataset.id;
    let ranksId = e.currentTarget.dataset.ranksid;
    let type = e.currentTarget.dataset.type
    wx.redirectTo({
      url: '../educations-details/index?id='+id+'&projectName='+this.data.projectName+"&projectId="+this.data.projectId+"&ranksId="+(ranksId==null?'':ranksId)+"&type="+type,
    })
  },

  /**
   * 返回到更多功能页面
   */
  goGCLB:function(){
    let jumpState = this.data.jumpState;
    wx.redirectTo({
      url: jumpState != 1?'../../pages/gengduogongneng/index':'../../pages/newAddPage/safetyManagement/index'
    })
  },

  /**
   * 跳转到add页面
   */
  add:function(){
    wx.navigateTo({
      url: '../educations-add/index?projectName='+this.data.projectName+"&projectId="+this.data.projectId+"&activeState="+this.data.activeState,
    })
  }

})