var Api = require("../../../utils/util.js");
import * as echarts from '../../ec-canvas/echarts';
let chart = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    ec: {
      lazyLoad: true //开启懒加载
    },
    xData:[],    //用于存放x轴的数据
    seriesData:[] //用于存放折线图的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    this.echartsComponnet = this.selectComponent('#mychart');
    if(options.id){
      this.setData({
        code: options.code,
        id: options.id,
        entryId: options.eid
      })
    }
    this.getDetail()
    this.getList()
    this.getianqi()
  },
  //天气
  getianqi(){
    var that = this
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req('nh/home/weather', {
      stationId: this.data.id
    }, 'get')
      .then(res => {
        wx.hideLoading()
        if(res.code == 200){
          that.setData({
            weatherObj: res.data.now
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }  
      })
  },
  //详情
  getDetail(){
    var that = this
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req('nh/powerStation/detail', {
      stationCode: this.data.code
    }, 'get')
      .then(res => {
        wx.hideLoading()
        if(res.code == 200){
          that.setData({
            detail: res.data
          })
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }  
      })
  },
  //设备列表
  goDevice(e){   
    wx.navigateTo({
      url: '../deviceList/deviceList?id=' + e.currentTarget.dataset.id,
    })
  },
  //7日发电
  getList(){
    Api.req('nh/powerStation/powerGenerationTrends', {
      entryId: this.data.id
    }, 'get')
      .then(res => {
        if(res.code == 200){
          this.setData({
            xData: res.data.XStr,
            seriesData: res.data.fdlY
          })
          this.initChart(); //初始化图表
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }  
      })
    //模拟从后端获取数据，并赋值给xData 、seriesData 
    // setTimeout(() => {
    //   const xData = ['1日', '2日', '3日', '4日', '5日', '3日', '7日']
    //   const seriesData = [500, 270, 890, 1344, 300, 320, 310]
    //   this.setData({xData,seriesData})
    //   this.initChart(); //初始化图表
    // },500)
  },
  //初始化echarts图表
  initChart() {
    //如果是二次赋值，就需要先清空，避免bug
    if(this.chart){
      chart.clear(); 
    }
    this.echartsComponnet.init((canvas, width, height,dpr) => {
      // 初始化图表
      chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr //清晰度 使canvas的图表更加清晰
      });
      chart.setOption(this.getOption())
      return chart;
    });
  },
   
  //设置图表各项数据
  getOption() {
    let option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: { 
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        },
        confine: true
      },
      legend: {
        data: ['发电量(kW)']
      },
      grid: {
        left: 20,
        right: 20,
        bottom: 15,
        top: 40,
        containLabel: true
      },
      xAxis: [{
        type: 'category',
        data: this.data.xData
      }],
      yAxis: [{
        type: 'value',  
      }],
      series: [{
        name: '发电量(kW)',
        type: 'bar',
        label: {
          normal: {
            show: true,
            position: 'inside'
          }
        },
        data: this.data.seriesData, 
      }]
    }
    return option;
  },

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

  },   
  
})