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

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    //参数
    fileDataList:[],
    educationTypeList:[{text:'三级安全教育',id:4},{text:'班前讲话',id:5},{text:'专项培训',id:3},{text:'应急演练',id:6}],
    cooperationTeamList:[],
    trainingType:'',
    trainingStartTime:'',
    trainingEndTime:'',
    trainingContent:'',
    collaborateRanksId:'',
    collaborateRanksName:'',
    attendNumber:'',
    trainingData:{},
    maxDate:new Date(2100,1,1).getTime(),
  
  },
// 展示图片
showImg(){
this.setData({
  show:true
})
},
// 关闭遮罩层
closeShow(){
  this.setData({
    show:false
  })
},
  //培训开始时间
  onStartTime(e){
    this.setData({
      trainingStartTime:e.detail
    })
  },
  //培训结束时间
  onEndTime(e){
    this.setData({
      trainingEndTime:e.detail
    })
  },
  //培训内容
  onInputContent(e){
    this.setData({
      trainingContent:e.detail.value
    })
  },
  //协作队伍
  onCooperationTeam(e){
    let collaborateRanksId = '';
    let collaborateRanksName = '';
    for(let i = 0;i<e.detail.length;i++){
      collaborateRanksId += e.detail[i].id+ ","
      collaborateRanksName += e.detail[i].text+ ","
    }
    if (collaborateRanksId.length > 0) {
      collaborateRanksId = collaborateRanksId.substr(0, collaborateRanksId.length - 1);
    }if (collaborateRanksName.length > 0) {
      collaborateRanksName = collaborateRanksName.substr(0, collaborateRanksName.length - 1);
    }
    this.setData({
      collaborateRanksId:collaborateRanksId,
      collaborateRanksName:collaborateRanksName,
    })
  },
  //参与人数
  onInputNumber(e){
    this.setData({
      attendNumber:e.detail.value
    })
  },

  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,
          projectId:options.projectId,
          projectName:options.projectName,
        })  
        that.getTrainingDataById(options.id,options.type);
        that.getCollaborativeTeam(options.ranksId);
      }
    }) 
  },

  /**
   * 查询出单条数据
   */
  getTrainingDataById(id,type){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/training/getTrainingDataById',
      data:{
        "main_id":id,
        "trainingType":type,
        "projectId":this.data.projectId
      },
      method:"get",
      success:function(res){
        that.onClickHide();
        that.setData({
          trainingData:res.data,
          trainingEndTime:res.data.cultivate_end == null?'':res.data.cultivate_end,
          trainingContent:res.data.content,
          collaborateRanksId:res.data.company_id == null?'':res.data.company_id,
          collaborateRanksName:res.data.partnership,
          attendNumber:res.data.people_number
        }) 
      }
    })
  },

  /**
   * 协助队伍
   */
  getCollaborativeTeam(ranksId){
    var that = this;
    let ranksIds =ranksId.split(',');
    wx.request({
      url: app.globalData.reqUrl+'/weixin/training/getCollaborativeTeam',
      data:{
        "projectId":this.data.projectId,
      },
      method:"get",
      success:function(res){
        let array = [];
        for(let i =0;i<res.data.length;i++){
          let type =false;
          for(let j = 0;j<ranksIds.length;j++){
           
            if(res.data[i].unitId == ranksIds[j]){
              type = true;
            }
          }
          if(array.length <= 0){
            array.push({
              "id":res.data[i].unitId,
              "text":res.data[i].uninName,
              "type":type
            })
          }else{
            if(!array.some(item1=>item1.id == res.data[i].unitId)){
              array.push({
                "id":res.data[i].unitId,
                "text":res.data[i].uninName,
                "type":type
              })
            }
          }
         
        }
        that.setData({
          cooperationTeamList:array,
        })
        that.onClickHide();
      }
    })
  },

  onImagesArr(e){
    console.log(e);
    this.setData({fileDataList:[]});
    let fileDataList = [];
    for(let i = 0;i<e.detail.length;i++){
      if(e.detail[i].indexOf('http://fileimg.makalu.cc') == -1){
        wx.uploadFile({
          //图片上传地址
          url: app.globalData.reqUrl+'/weixin/security/fileUpload',
          filePath: e.detail[i],
          name: 'file',
          header: {
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
          },
          formData: { user: 'test' },
          success:res => {
            let data = JSON.parse(res.data);
            fileDataList.push(data.url);
          },
        });
      }else{
        fileDataList.push(e.detail[i]);
      }
    }
    this.setData({ fileDataList:fileDataList});
  },
  
  add:function(){
    //判断值是否为空
    if(this.data.trainingContent == '' || this.data.trainingContent == undefined){
      app.toast("请填写培训内容");
      return;
    }else if(this.data.collaborateRanksName == '' || this.data.collaborateRanksName == undefined){
      app.toast("请选择协作队伍");
      return;
    }else if(this.data.attendNumber == '' || this.data.attendNumber == undefined){
      app.toast("请填写参加人数");
      return;
    }
   
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url:app.globalData.reqUrl+'/weixin/training/updateTrainingData',
      data:{
        projectId:this.data.projectId,
        mainId:this.data.trainingData.main_id,
        trainingType:this.data.trainingData.cultivate_type,
        trainingEndTime:this.data.trainingEndTime,
        trainingContent:this.data.trainingContent,
        collaborateRanksId:this.data.collaborateRanksId,
        collaborateRanksName:this.data.collaborateRanksName,
        attendNumber:this.data.attendNumber,
        createUser:this.data.loginName,
        fileDataList:JSON.stringify(this.data.fileDataList)
      },
      method:"POST",
     success:function(res){
       if(res.data.code == 0){
        wx.redirectTo({
          url: '../educations-list/index',
        })
       }else{
         app.toast("操作失败");
         return;
       }
     }
    })
  },

  /**
   * 返回教育培训列表
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../educations-list/index'
    })
  },
})