<template>
  <div class="sameAnalysis">
    <div class="sameContent1">
      <div style="display: flex">
        <el-select
          v-model="store.state.siteId"
          @change="siteChange"
          placeholder="选择站点"
          filterable
        >
          <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="sameSwitch">
        <el-tree
          ref="tree"
          :data="store.state.deviceList"
          show-checkbox
          node-key="id"
          accordion
          :check-strictly="true"
          :props="defaultProps"
          @check="currentChecked"
        />
      </div>
    </div>
    <div class="sameContent2">
      <div class="sameDivOne">
        <div class="sameDivTwo">
          <div class="block">
            <span class="demonstration">选择时间范围：</span>
            <el-date-picker
              v-model="dateTime"
              type="datetimerange"
              range-separator="➥"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            ></el-date-picker>
          </div>
        </div>
        <el-radio-group
          v-model="listTabPosition"
          @change="listTabsChange(listTabPosition)"
          style="display: flex"
        >
          <el-radio-button label="allValue">所有值</el-radio-button>
          <el-radio-button label="dailyValue">每日值</el-radio-button>
          <el-radio-button label="monthValue">每月值</el-radio-button>

          <el-select
            v-model="valueCalculation"
            placeholder="Select"
            style="width: 100px; margin-left: 10px"
            v-if="typeSrarch"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>

          <div style="margin-left: 10px">
            <el-button
              icon="el-icon-searchData"
              type="primary"
              style="padding: 7px 12px"
              @click="searchData()"
            >
              查询
            </el-button>
          </div>
        </el-radio-group>
      </div>
      <div class="sameEcharts">
        <echarts
          :ecahrtsData="ecahrtsData"
          :typeSrarch="typeSrarch"
          v-if="flag && ecahrtsData[0]"
        ></echarts>
      </div>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { defineComponent, ref, onMounted } from 'vue'
import echarts from './ehcarts/index.vue'

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

export default defineComponent({
  name: 'sameAnalysis',
  components: { echarts },
  props: {},
  setup() {
    const store = useStore()
    const listTabPosition = ref('allValue') //tabs切换
    const treeCheckedList = ref([])
    const treeData = ref([])
    const tree = ref(null)
    const typeSrarch = ref(0)
    const flag = ref(false)

    const valueCalculation = ref()
    const dateTime = ref('')
    const ecahrtsData = ref({})

    const listTabsChange = (value) => {
      console.log(value)
      if (value == 'allValue') {
        typeSrarch.value = 0
      } else if (value == 'dailyValue') {
        typeSrarch.value = 1
        valueCalculation.value = 1
      } else {
        typeSrarch.value = 2
        valueCalculation.value = 1
      }
    }

    const currentChecked = (nodeObj, SelectedObj) => {
      nodeObj, SelectedObj
      treeCheckedList.value = SelectedObj.checkedNodes
      treeCheckedList.value = []
      SelectedObj.checkedNodes.forEach((item) => {
        treeCheckedList.value.push(item.deviceCode)
        remove(treeCheckedList.value, item.deviceCode)
      })
    }

    function remove(arr, item) {
      let newArr = arr.filter((ele) => {
        return ele !== item
      })
      return newArr
    }

    function searchData() {
      if (!dateTime.value) {
        ElNotification({
          title: '提示',
          message: '请选择开始和结束时间',
          type: 'warning',
        })
        return
      }

      if (treeCheckedList.value.length > 0) {
        console.log('', store.state.deviceList)
        console.log('treeCheckedList')
        console.log(treeCheckedList.value)
        store.commit('TimeAll_function', dateTime.value)
        const time = store.state.Time_Data
        api
          .yearOnYearList({
            startTime: time[0],
            endTime: time[1],
            type: typeSrarch.value,
            valueCalculation: typeSrarch.value ? valueCalculation.value : 0,
            displayField: treeCheckedList.value,
          })
          .then((requset) => {
            if (requset.status === 'SUCCESS') {
              flag.value = true
              ecahrtsData.value = requset.data

              if (!ecahrtsData.value[0]) {
                ElMessage.error('暂无结果')
              }
            } else {
              ElMessage.error(requset.msg)
            }
          })
      } else {
        console.log('', 1)
        ElNotification({
          title: '提示',
          message: '请选择测点',
          type: 'warning',
        })
      }
    }

    //通过站点切换下拉框 change事件改变 tree树中的数据
    const siteChange = () => {
      store.commit('publicDeviceList')
    }

    onMounted(() => {})

    return {
      store,
      listTabPosition,
      tree,
      treeData,
      defaultProps: {
        children: 'children',
        label: 'deviceName',
        deviceCode: 'deviceCode',
        id: 'id',
      },
      flag,
      remove,
      dateTime,
      searchData,
      currentChecked,
      siteChange,
      listTabsChange,
      typeSrarch,
      options: ref([
        {
          value: 1,
          label: '平均值',
        },
        {
          value: 2,
          label: '最大值',
        },
        {
          value: 3,
          label: '最小值',
        },
        {
          value: 4,
          label: '累计值',
        },
      ]),
      valueCalculation,
      ecahrtsData,
    }
  },
})
</script>

<style lang="scss" scoped>
.sameAnalysis {
  display: flex;
  height: calc(100vh - 130px);
  // padding: 30px;
  // margin-top: 50px;
  .sameContent1 {
    background-color: #fff;
    width: 15%;
    height: 100%;
    margin-right: 25px;
    padding: 20px;
    min-width: 200px;
        overflow-y: auto;
    .sameSwitch {
      margin-top: 20px;
      width: 100%;
      height: 20px;
      cursor: pointer;
      .sameSwitchOne {
        display: flex;
        border: 0.5px solid silver;
        padding: 8px;
        font-size: 13px;
        div:nth-child(1) {
          width: 100%;
        }
      }
      .sameSwitchOne:hover {
        background-color: #409eff;
      }
      .sameSwitchOne:focus {
        background-color: #409eff;
      }
    }
  }
  .sameContent2 {
    background-color: #fff;
    width: 85%;
    min-width: 845px;
    height: 100%;
    .sameDivOne {
      display: flex;
      margin: 15px 0px 15px 15px;
      .sameDivTwo {
        font-weight: 600;
        color: #9d9d9d;
        margin-right: 20px;
      }
    }
  }
}
</style>

<style lang="less">
.sameSwitch {
  .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>