<template>
  <div class="task">
    <nut-sticky top="46">
      <div class="daily_tab">
        <span>{{name}}</span>
        <div class="float_right" @click="showFilter = true">
          <span>筛选</span>
          <Icon name="filter-o" color="#0B65FF" size="16"></Icon>
        </div>
      </div>
    </nut-sticky>
    <div class="content">
      <div class="monitor">
        <x-line text="实时监测"></x-line>
        <div class="monitor_flex">
          <div class="monitor_item">
            <div class="icon_temp"></div>
            <div>
              <div class="icon_min">用水量</div>
              <div class="icon_blue">{{energyData.waterValue}}<span class="font_min">吨</span></div>
            </div>
          </div>
          <div class="monitor_item">
            <div class="icon_hum"></div>
            <div>
              <div class="icon_min">用电量</div>
              <div class="icon_blue">{{energyData.electricityValue}}<span class="font_min">kw·h</span></div>
            </div>
          </div>
        </div>
      </div>
      <div class="monitor">
        <x-line text="用水曲线"></x-line>
        <div class="date_flex">
          <div :class="[ active === 2 ? 'icon_blue' : '' , 'margin-right']" @click="cut(2)">月</div>
          <div :class="[ active === 0 ? 'icon_blue' : '' ]" @click="cut(0)">日</div>
        </div>
        <div class="chart_content">
          <chart-water v-if="waterList.length > 0" :id="0" :list="waterList"></chart-water>
          <nut-empty v-else description="无用水曲线数据"></nut-empty>
        </div>
      </div>
      <div class="monitor">
        <x-line text="用电曲线"></x-line>
        <div class="date_flex">
          <div :class="[ active === 2 ? 'icon_blue' : '' , 'margin-right']" @click="cut(2)">月</div>
          <div :class="[ active === 0 ? 'icon_blue' : '' ]" @click="cut(0)">日</div>
        </div>
        <div class="chart_content">
          <chart-ele v-if="eleList.length > 0" :id="0" :list="eleList"></chart-ele>
          <nut-empty v-else description="无用电曲线数据"></nut-empty>
        </div>
      </div>
    </div>
  </div>
  <nut-cascader
    title="选择栋舍"
    v-model:visible="showFilter"
    v-model="unitOn"
    @change="onChangeLocation"
    :options="schoolList"
    text-key="name"
    value-key="id"
    children-key="child"
    ></nut-cascader>
</template>

<script>
export default {
  name: "Energy"
}
</script>
<script setup>
  import { ref, onMounted, getCurrentInstance } from 'vue'
  import { Icon } from "vant";
  import { getPigpenTree, getEnergyVal, getEnergyEleChart, getEnergyWaterChart } from "../../utils/api";
  import XLine from "../../components/XLine.vue";
  import ChartWater from "./charts/ChartWater.vue";
  import ChartEle from './charts/ChartEle.vue';

  const { proxy } = getCurrentInstance()

  const showFilter = ref(false)
  const schoolList = ref([
  ])

  const initSchool = () => {
    getPigpenTree({}).then(res => {
      if(res.code === 200) {
        schoolList.value = res.data
      }
    })
  }

  const unitOn = ref([])
  const name = ref('全场')
  const unitId = ref('');

  const onChangeLocation = (val, page) => {
    name.value = page[1].text;
    unitId.value = page[1].value;
    active.value = 0;
    init()
    initChart()
  }

  const energyData = ref({
    electricityValue: 0,
    waterValue: 0
  })

  const waterList = ref([])

  const eleList = ref([])

  const init = () => {
    let params = {
      pigpenId: unitId.value
    }
    getEnergyVal(params).then(res => {
      if(res.code === 200) {
        energyData.value = res.data
      }
    })
  }

  const active = ref(0)
  const cut = (val) => {
    if(active.value === val) {
      return
    } else {
      active.value = val
      initChart()
    }
  }

  const initChart = () => {
    let params = {
      pigpenId: unitId.value,
      type: active.value
    }
    getEnergyWaterChart(params).then(res => {
      if(res.code === 200) {
        if(res.data.list.length > 0) {
          res.data.list.forEach(item => {
            if(active.value === 0) {
              item.time = proxy.$moment(item.createTime).format('HH:mm')
            } else {
              item.time = proxy.$moment(item.createTime).format('MM-DD')
            }
            item.value = item.waterValue
          })
          waterList.value = res.data.list
        } else {
          waterList.value = []
        }
      }
    })
    getEnergyEleChart(params).then(res => {
      if(res.code === 200) {
        if(res.data.list.length > 0) {
          res.data.list.forEach(item => {
            if(active.value === 0) {
              item.time = proxy.$moment(item.createTime).format('HH:mm')
            } else {
              item.time = proxy.$moment(item.createTime).format('MM-DD')
            }
            item.value = item.electricityValue
          })
          eleList.value = res.data.list
        } else {
          eleList.value = []
        }
      }
    })
  }


  onMounted(() => {
    initSchool()
    init()
    initChart()
  })
</script>

<style scoped>
  .task {
    width: 100%;
    height: 100%;
  }
  .daily_tab {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: #0B65FF;
    background: #fff;
    box-shadow: 0 0 8px rgba(0, 0, 0, .12);
    position: relative;
  }
  .float_right {
    position: absolute;
    top: 0;
    right: 10px;
    height: 40px;
  }
  .content {
    width: 100%;
    height: calc(100% - 40px);
    overflow: auto;
    box-sizing: border-box;
    padding: 10px;
  }
  .monitor {
    box-sizing: border-box;
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
    margin-bottom: 10px;
    position: relative;
    box-shadow: 0 0 4px rgba(0,0,0,.12);
  }
  .monitor_flex {
    margin-top: 10px;
    width: 100%;
    height: 70px;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  .monitor_item {
    width: 46%;
    height: 100%;
    border: 1px solid #ddd;
    box-sizing: border-box;
    border-radius: 10px;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }
  .icon_temp {
    width: 20px;
    height: 30px;
    background: url("../../assets/images/energy/water.png") no-repeat;
    background-size: 100% 100%;
  }
  .icon_hum {
    width: 20px;
    height: 30px;
    background: url("../../assets/images/energy/ele.png") no-repeat;
    background-size: 100% 100%;
  }
  .icon_min {
    font-size: 12px;
  }
  .icon_blue {
    color: #0B65FF;
  }
  .chart_content {
    width: 100%;
    height: 300px;
  }
  .date_flex {
    position: absolute;
    display: flex;
    height: 25px;
    line-height: 25px;
    top: 10px;
    right: 20px;
  }
  .margin-right {
    margin-right: 20px;
  }
  .font_min {
    font-size: 10px;
  }
</style>
