import $ from './../../utils/Tool';
import router from './../../utils/router';
import {
  getPropertyData,getCurrentSeason,getTransfrom
} from "../../requests/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    envLevel:{
      temperatureWarm:[
        {name:"偏冷",value:"18-21°C",min:18,active:true},
        {name:"舒适",value:"22-24°C",min:22,active:true},
        {name:"偏热",value:"25-27°C",min:25,active:true},
      ],
      temperatureCooling:[
        {name:"偏冷",value:"21-23°C",min:21,active:true},
        {name:"舒适",value:"24-26°C",min:24,active:true},
        {name:"偏热",value:"26-28°C",min:26,active:true},
      ],
      temperatureTransition:[
        {name:"舒适",value:'',min:0,active:true},
        {name:"偏热",value:"26°C以上",min:26,active:true},
      ],
      temperatureTransitioncold:[
        {name:"偏冷",value:"18°C以下",min:-10,active:true},
        {name:"舒适",value:"",min:18,active:true},
      ],
      humidity:[
        {name:"",value:"0-35%",min:0,active:true},
        {name:"",value:"35-45%",min:35},
        {name:"适宜",value:"45-55%",min:45},
        {name:"",value:"55-65%",min:55,active:true},
        {name:"",value:"65-95%",min:65,active:true},
        {name:"",value:"95-100%",min:95,active:true},
      ],
      co2:[
        {name:"",value:"0-450ppm",min:0},
        {name:"合理范围",value:"450-700ppm",min:450},
        {name:"",value:"700-1000ppm",min:700,active:true},
        {name:"",value:"1000-2500ppm",min:1000,active:true},
        {name:"",value:"2500-5000ppm",min:2500,active:true}
      ],
      pm25:[
        {name:"优",value:"0~35μg/m³",min:0,active:true},
        {name:"良",value:"35~75μg/m³",min:35,active:true},
        {name:"轻度污染",value:"75~115μg/m³",min:75,active:true},
        {name:"中度污染",value:"115~150μg/m³",min:115,active:true},
        {name:"重度污染",value:"150~250μg/m³",min:150,active:true},
        {name:"严重污染",value:"大于250μg/m³及以上",min:250,active:true},
      ],
      hcho:[
        {name:"",value:"0-0.05mg/m³",min:0},
        {name:"合理范围",value:"0.05-0.08mg/m³",min:0.05},
        {name:"",value:"0.08-0.1mg/m³",min:0.08,active:true},
        {name:"",value:"0.1-0.15mg/m³",min:0.1,active:true},
        {name:"",value:"0.15-0.2mg/m³",min:0.15,active:true}
      ],
    },
    paramName:'',
    envType:'',
    localName:'',
    envValue:"",
    tableHight:null,
    pointerTop:0,
    funcid:"",
    spaceId:"",
    projectId:"",
    tragetChart:[],
    seasonType:''
  },
  goBack(){
    router.pop()
  },
  async getChartDate(){
    let data={
      projectId:this.data.projectId,
      spaceId:this.data.spaceId,
      funcid:this.data.funcid
    }
    $.loading();
    let res = await getPropertyData(data);
    $.hideLoading();
    if(!res){
      throw "网络错误";
    }
    let {avg,propertyData,dayTarget}=res;
    this.initChartdate(propertyData,dayTarget);
    let levelList=[];

    if(this.data.envType=="temperature"){
      let day = new Date();
      let year = day.getFullYear();
      let month = day.getMonth() + 1;
      month=month<10?('0'+month):month;
      let today = day.getDate();
      today=today<10?('0'+today):today;
      const data={
        projectId:$.store.get('projectId')||$.storage.get('projectId'),
        dateTiime:`${year}${month}${today}`
      }
      let res = await getTransfrom(data);
      let {seasonType,type} = (res.content&&res.content[0])||{seasonType:'Warm',type:2};
      this.setData({seasonType:seasonType,seasonNum:type},()=>{
        let that= this;
        wx.createSelectorQuery().select('.param_list').boundingClientRect(function(rect){
          that.setData({'tableHight':rect.height})
        }).exec()
      })

      if(seasonType==="Transition"){
        // 3 供冷-供暖
        if(type==3){
          this.data.envLevel[this.data.envType+seasonType+'cold'].forEach(item=>{
            levelList.push(item.min)
          })
          this.setData({'envLevel.temperatureTransition':this.data.envLevel.temperatureTransitioncold});
        }
        // 4 供暖-供冷
        if(type==4){
          this.data.envLevel[this.data.envType+seasonType].forEach(item=>{
            levelList.push(item.min)
          })
        }
      }else{
        this.data.envLevel[this.data.envType+seasonType].forEach(item=>{
          levelList.push(item.min)
        })
      }

    }else{
      this.data.envLevel[this.data.envType].forEach(item=>{
        levelList.push(item.min)
      })
    }

    levelList.push(Number(avg))
    let valueIndex = levelList.sort((a,b)=>{return a-b}).findIndex(item=>item===avg);
    valueIndex = valueIndex<0?0:valueIndex;
    console.log(valueIndex,levelList,"valueIndex")
    this.setData({pointerTop:38*valueIndex});
  },
  initChartdate(propertyData,dayTarget){
    if(!Array.isArray(propertyData)||!Array.isArray(dayTarget)){
      return ;
    }
    // 目标  { time: "1951",release:0, sales: 38,标准区间: [ 25, 45 ] },
    let baseArr=propertyData&&propertyData.slice(1) || [];
    let tragetChart = [];
    let initNum={
      PM2d5:function(value){
        // if(this.data.projectId==="Pj1101080259"){
        //   return parseInt(1000*value)
        // }
        return parseInt(value)
      },
      Tdb:function(value){
        return Number(Number(value).toFixed(1))
      },
      RH:function(value){
        return parseInt(value)
      },
      CO2:function(value){
        return parseInt(value)
      },
      HCHO:function(value){
        return Number(Number(value).toFixed(2))
      }
    }
    let funid = this.data.funcid;
    let specialProject=$.store.get("specialProject");
    let falgPro = (funid==="PM2d5"&&specialProject.includes(this.data.projectId));
    (baseArr.length>1)&&baseArr.forEach(item=>{
        item[1] = item[1]=="-9999"?null:falgPro?parseInt(item[1]*1000):initNum[funid](item[1]);
    });
    if(baseArr.length===dayTarget.length){
      
      dayTarget.forEach((item,index)=>{
        let time ='';
        item.time&&(time = item.time.slice(0,2)+':'+item.time.slice(2,4));
        let minarea = Number(item[`${this.data.envType}Min`].toFixed(1));
        let maxarea = Number(item[`${this.data.envType}Max`].toFixed(1));
        let initObj={
          time:time,
          release:index,
          sales:baseArr[index][1],
          标准区间:[minarea,maxarea],
        }
        tragetChart.push(initObj);
      })
    }
    this.setData({tragetChart})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options = JSON.parse(decodeURIComponent(JSON.stringify(options)))
    let {param,name,localname,spaceid,funcid,projectid} = options
    this.setData({paramName:name,envType:param,localName:localname,spaceId:spaceid,funcid,projectId:projectid})
    let that= this;
    wx.createSelectorQuery().select('.param_list').boundingClientRect(function(rect){
      that.setData({'tableHight':rect.height})
    }).exec()
    this.getChartDate();
  },
})