var optionBar = echartGetOptionBar()
optionBar.xAxis.data = []
optionBar.legend.data = ['发电电量', '入网电量']
optionBar.series = [
  { name: '发电电量', type: 'bar', data: [] },
  { name: '入网电量', type: 'bar', data: [] },
]
optionBar.yAxis[0].name = '电量'
optionBar.yAxis[1].name = ''
var mychartSolar = echarts.init(document.getElementById('mychartSolar'))
mychartSolar.setOption(optionBar)

optionBar.legend.data = ['充电电量', '放电电量']
optionBar.series = [
  { name: '充电电量', type: 'bar', data: [] },
  { name: '放电电量', type: 'bar', data: [] },
]
optionBar.yAxis[0].name = '电量'
optionBar.yAxis[1].name = ''
var mychartStorage = echarts.init(document.getElementById('mychartStorage'))
mychartStorage.setOption(optionBar)

optionBar.legend.data = ['用电电量', '最大功率']
optionBar.series = [
  { name: '用电电量', type: 'bar', data: [] },
  { name: '最大功率', type: 'bar', data: [] },
]
optionBar.yAxis[0].name = '电量'
optionBar.yAxis[1].name = '功率'
var mychartLoad = echarts.init(document.getElementById('mychartLoad'))
mychartLoad.setOption(optionBar)

optionBar.legend.data = ['充电电量', '充电次数', '充电收益']
optionBar.series = [
  { name: '充电电量', type: 'bar', data: [] },
  { name: '充电次数', type: 'bar', data: [] },
  { name: '充电收益', type: 'bar', data: [] },
]
optionBar.yAxis[0].name = '电量'
optionBar.yAxis[1].name = '收益'
var mychartCharge = echarts.init(document.getElementById('mychartCharge'))
mychartCharge.setOption(optionBar)

optionBar.legend.data = ['光伏设备', '储能设备', '负荷设备']
optionBar.series = [
  { name: '光伏设备', type: 'bar', data: [] },
  { name: '储能设备', type: 'bar', data: [] },
  { name: '负荷设备', type: 'bar', data: [] },
]
optionBar.yAxis[0].name = '次数'
optionBar.yAxis[1].name = ''
var mychartAlert = echarts.init(document.getElementById('mychartAlert'))
mychartAlert.setOption(optionBar)

var optionCurve = echartGetOptionCurve()
optionCurve.legend.data = ['发电功率', '辐照度']
optionCurve.series = [
  { name: '发电功率', type: 'line', hoverAnimation: false, smooth: false, symbolSize: 0, data: [] },
  { name: '辐照度', type: 'line', hoverAnimation: false, smooth: false, symbolSize: 0, data: [] },
]
optionCurve.yAxis[0].name = '功率kw'
optionCurve.yAxis[1].name = '光照Lux'

var mychartRunning = echarts.init(document.getElementById('mychartRunning'))
mychartRunning.setOption(optionCurve)

window.onresize = () => {
  mychartSolar.resize()
  mychartStorage.resize()
  mychartLoad.resize()
  mychartCharge.resize()
  mychartAlert.resize()
  mychartRunning.resize()
}

function updatePageData() {
  // 获取统计数据(累计统计)
  G.cppNative.getStatisticTotal().then((res => {
    // {"ccers":100,"elect_charge":0,"elect_gen":153.8,"elect_in":130.2,"income_charge":100,"income_elect":100}
    $('#windTurbineNum').text('8')
    $('#solarNum').text('207')
    $('#electricTotal').text(res['elect_gen'])
    $('#electricInTotal').text(res['elect_in'])
    $('#carbonReduction').text(res['ccers'])
  }))

  // 获取统计数据(日统计，7天的数据)
  G.cppNative.getStatisticDay(7).then((res => {
    var xAxisData = []
    var solar = { d1: [], d2: [], d3: [] }
    var storage = { d1: [], d2: [], d3: [] }
    var load = { d1: [], d2: [], d3: [] }
    var charge = { d1: [], d2: [], d3: [] }
    var alert = { d1: [], d2: [], d3: [] }

    res.forEach(item => {
      solar.d1.push(item['elect_gen'])
      solar.d2.push(item['elect_in'])

      storage.d1.push(item['elect_store'])
      storage.d2.push(item['elect_discharge'])

      load.d1.push(item['elect_load'])
      load.d2.push(item['power_max_load'])

      charge.d1.push(item['elect_charge'])
      charge.d2.push(item['num_charge'])

      alert.d1.push(item['num_fault_solar'])
      alert.d2.push(item['num_fault_store'])
      alert.d3.push(item['num_fault_charge'])
    })

    // 服务端获取光伏设备7天统计数据，更新图表
    // var data1 = [1, 2, 3, 4, 3, 2, 1]
    // var data2 = [1, 2, 3, 4, 3, 2, 1]
    // var data3 = [1, 2, 3, 4, 3, 2, 1]

    var xdata = []
    for (let i = 6; i >= 0; i--) {
      const date = new Date();
      date.setDate(date.getDate() - i);
      xdata.push(G.formatDateMD(date));
    }
    updateEchartBar(mychartSolar, xdata, [{ data: solar.d1 }, { data: solar.d2 }, { data: solar.d3 }])
    updateEchartBar(mychartStorage, xdata, [{ data: storage.d1 }, { data: storage.d2 }, { data: storage.d3 }])
    updateEchartBar(mychartLoad, xdata, [{ data: load.d1 }, { data: load.d2 }, { data: load.d3 }])
    updateEchartBar(mychartCharge, xdata, [{ data: charge.d1 }, { data: charge.d2 }, { data: charge.d3 }])
    updateEchartBar(mychartAlert, xdata, [{ data: alert.d1 }, { data: alert.d2 }, { data: alert.d3 }])
  }))

  G.cppNative.getStatisticPowerDay().then(res => {
    mychartRunning.setOption({ series: [{ data: res }] })
  })
  G.cppNative.getStatisticIrradianceDay().then(res => {
    mychartRunning.setOption({ series: [{}, { data: res, yAxisIndex: 1 }] })
  })

  // 查询数据获取环境信息：光照、风速、环境温度、湿度
  G.cppNative.getEnvironmentInfo().then(res => {
    $('#envIllumination').text(res['illumination'] + ' Lux')
    $('#envWindspeed').text(res['windspeed'] + ' m/s')
    $('#envTemperture').text(res['temperature'] + ' ℃')
    $('#envHumidity').text(res['humidity'] + ' %')
  })
}

/// 清理资源
var timerId = null
document.currentScript.addEventListener('DOMNodeRemoved', () => {
  G.cppNative.log('DOMNodeRemoved: 运行监控')
  clearInterval(timerId);
});

$(document).ready(function () {


  updatePageData()

  // 定时器更新页面数据
  timerId = setInterval(updatePageData, 1000)
})