// pages/Standard-maintenance-room-monitoring/index.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        projectId:'',
        startTime:"",
        endTime:"",
        startTime2:"",
        endTime2:"",
        deviceNo:"1650041930114977794",
        jianceList: [
          {text:'1650041930114977794',id:'1650041930114977794'},
          {text:'1650042067465850882',id:'1650042067465850882'},
          {text:'1650042162466836481',id:'1650042162466836481'},
          {text:'1650042250748547074',id:'1650042250748547074'}
        ],
        indexNum: 0,
        biaoyangList: [
            { name: '内置平均温度', biaoyangX: [], biaoyangY: [] },
            { name: '外置平均温度', biaoyangX: [], biaoyangY: [] },
        
        ],
        value:'本月',
        value1:'本月',
        actions: [{ name: '本月',id:0 }, { name: '累计', id:1},],
        actions1: [{ name: '本月',id:0 }, { name: '累计', id:1},],
        indexNum2: 0,
        biaoyangList2: [
            { name: '内置成熟度', biaoyangX: [], biaoyangY: [] },
            { name: '外置成熟度', biaoyangX: [], biaoyangY: [] },
            { name: '内置强度', biaoyangX: [], biaoyangY: [] },
            { name: '外置强度', biaoyangX: [], biaoyangY: [] },
        ],
    },
    // 点击进行超耗tab切换
    onSelect(e) {
        if(e.currentTarget.dataset.set ==0){
            this.setData({
                startTime:this.getDate(-6,1),
                endTime:this.getDate(0,2),
            })
        }else{
            this.setData({
                startTime:this.getDate(-29,1),
                endTime:this.getDate(0,2),
            })
        }
        this.setData({
            indexNum: e.currentTarget.dataset.set,
        })
        this.runList();
    },
    showPopup() {
        this.setData({ show: true });
      },
      onClick(){
        this.setData({
            show1:true
        })
    },
    onClick1(){
        this.setData({
            show2:true
        })
    },
      onClose(){
        this.setData({
            show:false,
            show1:false,
            show2:false
        })
    },
    goGCLB:function(){
        wx.redirectTo({
          url: '../../pages/Quality-Assurance/index'
        })
      },
      onSelect1(e) {
        if(e.currentTarget.dataset.set ==0){
            this.setData({
                startTime2:this.getDate(-6,1),
                endTime2:this.getDate(0,2),
            })
        }else{
            this.setData({
                startTime2:this.getDate(-29,1),
                endTime2:this.getDate(0,2),
            })
        }
        this.setData({
            indexNum2: e.currentTarget.dataset.set,
        })
        this.runList2();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
         //获取缓存数据
         wx.getStorage({
            key: 'userinfo',
            success:res=>{
              this.setData({
                loginName:res.data.loginName,
                userName:res.data.userName,
                projectId:app.globalData.projectId,
                projectNameArgs:app.globalData.projectName,
                initData:{text:app.globalData.projectName,id:app.globalData.projectId}
              })  
              this.getProjectCorrespondence(app.globalData.projectId)
            }
          })
        this.setData({
            startTime:this.getDate(-6,1),
            endTime:this.getDate(0,2),
            startTime2:this.getDate(-6,1),
            endTime2:this.getDate(0,2),
        })
        this.getProjectCorrespondence();
    },
    getDate(num,type){
        var date = new Date(new Date().getTime()+num*24*60*60*1000);
        var year = date.getFullYear();
        var month = (date.getMonth()+1) > 9 ? (date.getMonth()+1):'0'+(date.getMonth()+1)
        var day = date.getDate() > 9 ? date.getDate():'0'+date.getDate()
        return year+"-"+month+"-"+day+(type==1?" 00:00:00":" 23:59:59");
    },
    /**
     * 项目对应关系
     */
    getProjectCorrespondence() {
        var that = this;
        wx.request({
            url: app.globalData.reqUrl + '/weixin/training/getProjectCorrespondence',
            data: {
                "projectId": 3//app.globalData.projectId,
            },
            method: "get",
            success: function (res) {
                that.setData({
                    projectId :res.data.szh
                })
                that.runList();
               that.runList2();
            }
        })
    },
    runList() {
        var that = this;
        wx.request({
            url: app.globalData.szhUrl + '/api/iot/power/gainHntRunList',
            data: {
                "projectId": '1649949529732075522',
                "startTime": that.data.startTime,
                "endTime": that.data.endTime,
                "deviceNo": that.data.deviceNo
            },
            header:{
              'content-type': 'application/x-www-form-urlencoded' 
            },
            method: "post",
            success: function (res) {
                var data = {}
                data.lineData = []
                data.color = ['#008ffd', '#f6d023']
                data.legend = ['内置平均温度', '外置平均温度']
                data.unit = '℃'
                data.date = []
                var data1 = []
                var data2 = []
              
                for (let row of res.data.data) {
                    data.date.push(row.add_time.substr(0, 14) + "00");
                    data1.push(row.min_temp)
                    data2.push(row.max_temp)
                }
                data.lineData.push(data1)
                data.lineData.push(data2)
                that.setData({
                    biaoyangList: data,
                })
             
            }
        })
    },
    runList2() {
        var that = this;
  
        wx.request({
          url: app.globalData.szhUrl + '/api/iot/power/gainHntRunList',
          data: {
              "projectId": '1649949529732075522',
              "startTime": that.data.startTime,
              "endTime": that.data.endTime,
              "deviceNo": that.data.deviceNo
          },
          header:{
            'content-type': 'application/x-www-form-urlencoded' 
          },
          method: "post",
          success: function (res) {
                var data = {}
                data.lineData = []
                data.color = ['#008ffd', '#f6d023', '#5af9fd', '#fc6902']
                data.legend = ['内置成熟度', '外置成熟度','内置强度','外置强度']
                data.unit = '%RH'
                data.date = []
                var data1 = []
                var data2 = []
                var data3 = []
                var data4 = []
              for (let row of res.data.data) {
                data.date.push(row.add_time.substr(0, 14) + "00");
                data1.push(row.strength)
                data2.push(row.current_stre_outside)
                data3.push(row.marturity)
                data4.push(row.marturity_outside)
              }
              data.lineData.push(data1)
              data.lineData.push(data2)
              data.lineData.push(data3)
              data.lineData.push(data4)
              that.setData({
                  biaoyangList2: data,
              })
           
          }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    getData1(e){
      this.setData({
        deviceNo:e.detail.text
      })
      this.runList();
      this.runList2();
      
    }
})