import $ from './../../utils/Tool'
import router from './../../utils/router'
import {getSpaceNum ,getfeedbackSeason,getCurrentSeason} from '../../requests/api';
function chartFn(data=[]) {
  return function onInitChart(canvas, width, height, F2) { // 使用 F2 绘制图表F2, config
    const chart = new F2.Chart({
      el: canvas,
      width,
      height,
      // padding:[10,-10,0,30]
    });
    chart.legend(false);
    chart.source(data, {
      数值: {
        tickCount: 2,
        formatter: (v) => {
          if(v==0){
            return v
          }
          return v +'次'
        },
      }
    });
    chart.axis("数值",{
      grid:null,
    });
    let size = 9;
    if(data&&data.length&&data.length>=20){
        size=6;
    }
    chart.interval()
      .position('温度*数值')
      .color('name',['#04B49C','#FF8800']).size(size).adjust({
        type: 'dodge',
        marginRatio: 0.001 // 设置分组间柱子的间距
      });
      // .adjust('stack');
    chart.render();
    return chart;
  }
}

Page({
  data: {
    headerShow:false,
    seasonList:[ //Warm, Cooling, Transition
      {name:"供冷季",id:"Cooling"},
      {name:"供暖季",id:"Warm"},
      {name:"过渡季",id:"Transition"}
    ],
    seasonType:"",
    opts:{
      // onInit: onInitChart
    },
    spaceFeedbackNum:0,
    adjustSpaceNum:0,
    currentSeason:'',
    nochartDate:false,
    bgSeasonType:'Cooling',
    spaceIds:[],
  },
  goBack(){
    router.pop();
  },
  gotoFeedback(e){
    console.log(e)
    router.push("feedback")
  },
  gotoAdjustlog(e){
    let {type} =e.currentTarget.dataset;
    router.push("adjustlog",{spaceFeedbackNum:this.data.spaceFeedbackNum,type:type})
  },
  gotoSpacelog(e){
    router.push("spacelog",this.data.spaceIds)
  },
  changeSeason(e){
    let type=e.currentTarget.dataset.index;
    this.setData({seasonType:type},()=>{
      this.getSeasonchart();
    })
  },
  // opts.onInit.changeData
  getSeasonchart(){
    let data={
      "criteria": {
          "projectId":$.store.get('projectId') ,
          "openid":$.store.get('openId'),
          "userId":$.store.get('userId'),
          "seasonType":this.data.seasonType// 季节类型  Warm, Cooling, Transition
      },
      "orders": [
          {
              "column": "fbTemp",
              "asc": true
          }
      ]
    }
    getfeedbackSeason(data).then(res=>{
      if(res.count){
        let {content} =res;
        let arrChart=[];
        let arrlength = content&&(content.length-1);

        content.forEach((item,index) => {
          if(item.fbTemp=="-9999"){
            return
          }
          let objCold = {
            name:'冷',
            温度:item.fbTemp+'',
            数值:item.fbColdNum
          };
          let objHot= {
            name:'热',
            温度:item.fbTemp+'',
            数值:item.fbHotNum
          };
          if(arrlength===index){
            objCold = {
              name:'冷',
              温度:item.fbTemp+'°C',
              数值:item.fbColdNum
            };
            objHot= {
              name:'热',
              温度:item.fbTemp+'°C',
              数值:item.fbHotNum
            };
          }
          arrChart.push(objCold);
          arrChart.push(objHot);
        });
        if(arrChart.length){
          this.setData({nochartDate:false})
          let chartdom = this.selectComponent('#column-dom');
          chartdom.init(chartFn(arrChart));
        }else{
          this.setData({nochartDate:true})
        }
      }else{
        this.setData({nochartDate:true})
      }
    })
  },
  getSpacefeedNum(){
    let data={
        "criteria": {
            "projectId":$.store.get('projectId'),
            "userId":$.store.get('userId')
            },
            "orders": [
            {
                "column": "createTime",
                "asc": false
            }
        ]
    }
    getSpaceNum(data).then(res=>{
      this.setData({
        spaceFeedbackNum:res.spaceFeedbackNum,
        adjustSpaceNum:res.adjustSpaceNum,
        spaceIds:res.spaceIds||[],
      })
    })
  },
  async getnowSeason(){
    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'),
      date:`${year}${month}${today}`
    }
    await getCurrentSeason(data).then(res=>{
      if(res.result=="success"){
        this.setData({seasonType:res.data,bgSeasonType:res.data},()=>{

        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.getSpacefeedNum();
    await this.getnowSeason();
    this.getSeasonchart();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(()=>{
      this.setData({headerShow:true})
    },500)
  },

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

  },
})