<template>
  <div class="demandAnalysis">
    <div class="demContent1">
      <div style="display: flex">
        <el-select
          v-model="store.state.siteId"
          @change="siteChange"
          placeholder="选择站点"
        >
          <el-option
            v-for="item in store.state.siteList"
            :key="item.id"
            :label="item.siteName"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>

      <div class="demSwitch">
        <el-tree
          ref="tree"
          :data="deviceBoxData"
          show-checkbox
          node-key="id"
          @node-click="handleNodeClick"
          accordion
          :check-strictly="true"
          :props="defaultProps"
          highlight-current
        />
      </div>
    </div>
    <div class="demContent2">
      <div class="demDivOne">
        <div class="demDivTwo">
          <div class="block">
            <span class="demonstration" style="color: black">日期:</span>
            <el-date-picker
              v-model="dateTime"
              type="month"
              placeholder="请选择日期"
            ></el-date-picker>
          </div>
        </div>

        <el-radio-group
          v-model="listTabPosition"
          @change="listTabsChange(listTabPosition)"
          style="display: flex"
        >
          <el-radio-button label="cycleValue">电费结算周期</el-radio-button>
          <el-radio-button label="monthValue">自然月</el-radio-button>
        </el-radio-group>

        <div style="margin-left: 10px">
          <el-button icon="el-icon-search" type="primary" @click="searchData()">
            查询
          </el-button>
        </div>
      </div>
      <div class="demEcharTable" v-if="flag">
        <div class="Echarts">
          <div class="ehcartOne">
            <i
              class="el-icon-s-data el-input__icon"
              style="font-size: 20px"
            ></i>
            <span style="font-size: 15px">{{}}每日最大需量趋势</span>
          </div>
          <div>
            <echarts :Height="Height" :echartsData="echartsData"></echarts>
          </div>
        </div>
        <div class="Tables">
          <el-table
            :data="tableData"
            :height="Height"
            style="width: 100%"
            id="tables1"
             border
        stripe
          >
            <el-table-column prop="listDate" label="日期" width=""  align="center"/>
            <el-table-column prop="list" label="最大需量(kw)" width=""  align="center"/>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { defineComponent, ref, reactive, toRefs, onMounted } from 'vue'
import echarts from './ehcarts/index.vue'
import { parseTime2 } from '@/utils'

import * as api from '@/api/dataManage/demandAnalysis.js'
import { ElMessage } from 'element-plus'
import { ElNotification } from 'element-plus'

export default defineComponent({
  name: 'demandAnalysis',
  components: { echarts },
  props: {},
  setup() {
    const store = useStore()
    const tableData=ref([])
    const deviceBoxData = ref([])
    const deviceChecked = ref([])
    const dateTime = ref(new Date())
    const typeSrarch = ref(1)
    const flag = ref(false)
    const echartsData = ref({})

    const siteOption = () => {
      console.log(store.state)
    }

    const listTabsChange = (value) => {
      value == 'monthValue' ? (typeSrarch.value = 2) : (typeSrarch.value = 1)
      console.log(value)
    }
    const handleNodeClick = (data, obj, node) => {
      data, node
      console.log(obj.data.deviceCode)
      deviceChecked.value = obj.data.deviceCode
    }

    const state = reactive({
      value1: '2021-5',
    })

    const Height = ref()
    Height.value = window.innerHeight - 250 + 'px'
    window.onresize = function () {
      Height.value = window.innerHeight - 250 + 'px'
    }
    //通过站点切换下拉框 change事件改变 tree树中的数据
    const siteChange = () => {
      store.commit('publicDeviceList')
      deviceBoxList()
    }
    function deviceBoxList() {
      api
        .deviceBoxList({
          siteId: store.state.siteId,
        })
        .then((requset) => {
          if (requset.status === 'SUCCESS') {
            deviceBoxData.value = requset.data
            deviceBoxData.value.forEach(function (i, index) {
              i.id = index
              i.children = []
            })
            console.log(deviceBoxData.value)
          } else {
            ElMessage.error(requset.msg)
          }
        })
    }
    function searchData() {
      if (deviceChecked.value.length > 0) {
        // alert(1)
        // store.commit('TimeAll_function', dateTime.value)
        // const time = store.state.Time_Data

        dateTime.value ? (dateTime.value = parseTime2(dateTime.value)) : ''
        api
          .demandAnalysis({
            cycle: typeSrarch.value,
            monthDate: dateTime.value,
            deviceCode: deviceChecked.value,
          })
          .then((requset) => {
            if (requset.status === 'SUCCESS') {
              flag.value = true
              echartsData.value = requset.data[0]


              //重组表格数据
              var list = requset.data[0].list
              var listDate = requset.data[0].listDate
              var newArr1 = []
              var newArr2 = []
              list.forEach((item) => {
                newArr1.push({ list: item })
              })
              listDate.forEach((item) => {
                newArr2.push({ listDate: item })
              })
              var newObj = newArr1.map((item, index) => {
                return { ...item, ...newArr2[index] }
              })
              tableData.value=newObj


            } else {
              ElMessage.error(requset.msg)
            }
          })
      } else {
        console.log('', 1)
        ElNotification({
          title: '提示',
          message: '请选择设备',
          type: 'warning',
        })
      }
    }
    onMounted(() => {
      deviceBoxList()
    })

    return {
      Height,
      ...toRefs(state), //时间选择默认参数
      siteValue: ref(1), //select站点选择
      listTabsChange, //单选change事件
      siteOption, //站点下拉change事件
      siteChange,
      searchData,
      handleNodeClick,
      store,
      listTabPosition: ref('cycleValue'), //单选按钮
      deviceBoxData,
      deviceChecked,
      dateTime,
      typeSrarch,
      flag,
      echartsData,
      data: [
        {
          id: 1,
          label: '设备1',
          children: [
            {
              id: 4,
              label: '母线停电次数',
            },
            {
              id: 4,
              label: '功率因数',
            },
            {
              id: 4,
              label: 'A相功率因数',
            },
          ],
        },
      ],
      options: ref([
        {
          value: 1,
          label: 'Option1',
        },
        {
          value: 2,
          label: 'Option2',
        },
        {
          value: 3,
          label: 'Option3',
        },
        {
          value: 4,
          label: 'Option4',
        },
      ]),
      defaultProps: {
        children: 'children',
        label: 'deviceName',
        deviceCode: 'deviceCode',
        id: 'id',
      },
      tableData
    }
  },
})
</script>

<style lang="scss" scoped>
.demandAnalysis {
  display: flex;
  height: calc(100vh - 130px);
  // padding: 30px;
  // margin-top: 50px;
  .demContent1 {
    background-color: #fff;
    width: 15%;
    height: 100%;
    margin-right: 25px;
    padding: 20px;
    min-width: 200px;
    .demSwitch {
      margin-top: 20px;
      width: 100%;
      height: 20px;
      cursor: pointer;
      .demSwitchOne {
        display: flex;
        border: 0.5px solid silver;
        padding: 8px;
        font-size: 13px;
        div:nth-child(1) {
          width: 100%;
        }
      }
      .demSwitchOne:hover {
        background-color: #409eff;
      }
      .demSwitchOne:focus {
        background-color: #409eff;
      }
    }
  }
  .demContent2 {
    background-color: #fff;
    width: 85%;
    min-width: 845px;
    height: 100%;
    .demDivOne {
      display: flex;
      margin: 15px 0px 15px 15px;
      .demDivTwo {
        font-weight: 600;
        color: #9d9d9d;
        margin-right: 20px;
      }
    }
    .demEcharTable {
      display: flex;
      .Echarts {
        width: 60%;
        margin-right: 5px;
        .ehcartOne {
          padding-left: 10px;
          background-color: rgb(233 233 233);
          border-bottom: 1px #bfbfbf solid;
        }
      }
      .Tables {
        width: 40%;
        display: flex;
      }
    }
  }
}
</style>
<style>
.el-table--scrollable-y .el-table__body-wrapper::-webkit-scrollbar {
  overflow-y: hidden;
}
</style>
<style lang="less">
.demSwitch {
  .el-tree-node__content .el-checkbox__input {
    display: none;
  }
  .el-tree-node
    .el-tree-node__children
    .el-tree-node__content
    .el-checkbox__input {
    display: block;
  }
}
</style>