// miniprogram/pages/yujingxinxi/yujingxinxi.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    a:0,
    b:0,
    c:0,
    projectName:'',
    projectId:'',
    loginName:'',
    userName:'',
    environList:{},
    //数据加载参数
    show:false, 
    initData:{},

    stateShow:false,
    nav:1,
    //预警处理信息
    warningId:'',
    warningType:2,
    remarks:'',
    labelShow:true,


  },

  //项目切换 返回值
  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
    })
    this.onLoad();
  },

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

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

  onWarningInfo(e){
    this.setData({
      stateShow:true,
      warningId:e.currentTarget.dataset.id,
      remarks:''
    })
  },
  //标签切换
  onLabelNav(e){
    this.setData({
      nav:e.currentTarget.dataset.nav,
      warningType:e.currentTarget.dataset.nav == 1?2:3,
      remarks:''
    })
  },
  //备注
  remarks(e){
    this.setData({
      remarks:e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //启动蒙版
    that.onClickShow();
      //获取缓存数据
      wx.getStorage({
        key: 'userinfo',
        success:function(res){
          that.setData({
            loginName:res.data.loginName,
            userName:res.data.userName,
            projectName: app.globalData.projectName,
            projectId:app.globalData.projectId,
            initData:{text:app.globalData.projectName,id:app.globalData.projectId}
          })  
        }
      });

     that.selectEnvironDayAndMonth(1);
  },

  /**
   * 加载预警信息数据
   */
  selectEnvironDayAndMonth(warningType){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/applets/selectEnvironDayAndMonth',
      data:{
        "projectId":app.globalData.projectId,
        "warningType":warningType
      },
      method:"GET",
      success:function(res){
        //关闭蒙版
        that.onClickHide();
        that.setData({
          environList:res.data
        })
        that.getEnvironDayAndMonthCount(warningType);
      }
    })
  },

  /**
   * 预警个数
   */
  getEnvironDayAndMonthCount(warningType){
    var that = this;
    wx.request({
      url: app.globalData.reqUrl+'/weixin/applets/getEnvironDayAndMonthCount',
      data:{
        "projectId":app.globalData.projectId,
        "warningType":warningType
      },
      method:"GET",
      success:function(res){
        //关闭蒙版
        that.onClickHide();
        that.setData({
          a:res.data.dcl,
          b:res.data.clz,
          c:res.data.ycl
        })
      }
    })
  },

  /**
   * 添加预警信息
   */
  submit:function(){
    var that = this;
    if(this.data.remarks == '' || this.data.remarks == undefined){
      app.toast("请选择备注！");
      return;
    }
    let warningType ='';
    let afootUser ='';
    let afootRemarks ='';
    let processedUser ='';
    let processedRemarks ='';
    if(this.data.labelShow && this.data.warningType == 2){
      warningType = 2;
      afootUser = this.data.loginName;
      afootRemarks = this.data.remarks;
    }if((this.data.labelShow && this.data.warningType == 3) || !this.data.labelShow){
      warningType = 3;
      processedUser = this.data.loginName;
      processedRemarks = this.data.remarks;
    }
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url:app.globalData.reqUrl+'/weixin/applets/addWarningHandle',
      data:{
        warningId:this.data.warningId,
        warningType:warningType,
        afootUser:afootUser,
        afootRemarks:afootRemarks,
        processedUser:processedUser,
        processedRemarks:processedRemarks,
      },
      method:"POST",
     success:function(res){
       if(res.data > 0){
          that.selectEnvironDayAndMonth(1);
          that.setData({ stateShow:false });
       }else{
         app.toast("操作失败！");
         return;
       }
     }
    })
  },

  /**
   * 关闭
   */
  closeBox(){
    this.setData({
      stateShow:false
    })
  },

  orderChange(e){
    let name = e.detail.name;
    if(name == 'a'){
      this.selectEnvironDayAndMonth(1);
      this.setData({labelShow:true});
    }else if(name == 'b'){
      this.selectEnvironDayAndMonth(2);
      this.setData({labelShow:false});
    }else if(name == 'c'){
      this.selectEnvironDayAndMonth(3);
      this.setData({labelShow:false});
    }
  }

})