<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="tabClick">
      <el-tab-pane label="总览" name="summery">

      </el-tab-pane>

      <el-tab-pane v-for="item in facsCategoryOptions" :key="item.code" :label="item.name" :name="item.code">
        <el-col :span="4" :xs="24">
          <div class="head-container">
            <el-input v-model="areaName" placeholder="请输入区域名称" clearable size="small"
                      prefix-icon="el-icon-search"
                      style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container" style="height: 100vh; overflow: hidden; position: relative;">
            <el-tree :data="areaOptions" :props="defaultProps" :expand-on-click-node="false"
                     :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
                     @node-click="handleNodeClick" style="height: calc(100vh - 50px); overflow-y: auto;"
            />
          </div>
        </el-col>
        <el-col :span="20" :xs="24">
          <div class="container-block">
            <el-form size="small" :inline="true" label-width="68px">
              <el-form-item label="地块">
                <template slot-scope="scope">
                  <span>{{ selectedLabel }}</span>
                </template>
              </el-form-item>
            </el-form>
          </div>
          <div class="container-block">
            <div class="ctl-container">
              <el-select v-model="objCode" placeholder="选择设施"  @visible-change="handleObjSelectClick" @change="getList">
                <el-option v-for="item in objOptions"
                           :label="item.facsName"
                           :value="item.facsCode"
                           :key="item.facsCode"
                />

              </el-select>
              <el-date-picker v-model="dateRange" type="datetimerange" @change="getList" :picker-options="pickerOptions"
                              value-format="yyyy-MM-dd hh:mm:ss" range-separator="至" start-placeholder="开始日期"
                              end-placeholder="结束日期"
                              align="right"
              >
              </el-date-picker>
            </div>

            <el-table v-loading="loading" :data="hList">
              <el-table-column label="设施" align="center" prop="objName">
                <template slot-scope="scope">
                  <span>{{ scope.row.objName }}</span>
                </template>
              </el-table-column>>
              <el-table-column label="日期" align="center" prop="date" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="时间" align="center" prop="time">
                <template slot-scope="scope">
                  <span>{{ scope.row.time }}</span>
                </template>
              </el-table-column>
              <el-table-column label="时间序列" align="center" prop="timeIndex"/>
              <el-table-column label="用电量(kW·h)" align="center" prop="elecQuantity"/>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </div>
        </el-col>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { listHSum } from '@/api/mgr/elecUseH'
import { getFacsCategorygetByCode } from '@/api/basecfg/emsfacs'
import { areaWithFacsCategoryAsTree } from '@/api/basecfg/area'
import { listAllFacs } from '@/api/basecfg/emsfacs'
import dayjs from 'dayjs'
import { DateTool } from '@/utils/DateTool'
import BaseChart from '@/components/BaseChart/index.vue'
import Block from '@/components/Block/block.vue'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'H',
  components: {
    Treeselect,
    BaseChart,
    Block
  },
  data() {
    return {
      activeName: 'summery',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用能计量-小时表格数据
      hList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      facsCategory: undefined,
      facsSubCategory: undefined,
      // 区域名称
      areaName: undefined,
      selectedLabel: '全部',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      areaOptions: [],
      facsCategoryOptions: [],
      objOptions: [],
      objCode: undefined,
      dateRange: [dayjs().format(DateTool.DateFormat.YYYY_MM_DD_00_00_00), dayjs().format(DateTool.DateFormat.YYYY_MM_DD_23_59_59)],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        objType: 1,
        objCode: null,
        facsCategory: 'Z',
        facsSubCategory: null,
        date: null,
        time: null,
        timeIndex: null
      },
      queryObjParams: {
        refArea: null,
        facsCategory: null,
        subCategory: null
      },
      // 表单参数
      form: {},
    }
  },
  watch: {
    // 根据名称筛选区域树
    areaName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.facsCategory = 'Z'
    this.facsSubCategory = ''
    this.getFacsCategory(this.facsCategory)
    this.getAreaTree(this.facsCategory, this.facsSubCategory)
    this.getList()
  },
  methods: {
    /** 查询用能计量-小时列表 */
    getList() {
      this.loading = true
      this.queryParams.objCode = this.objCode
      listHSum(this.queryParams).then(response => {
        this.hList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    async getFacsCategory(code) {
      getFacsCategorygetByCode(code).then(response => {
        this.facsCategoryOptions = response.data.subtypeList
      })
    },
    async getAreaTree(category, subCategory) {
      await areaWithFacsCategoryAsTree(category, subCategory).then(response => {
        this.areaOptions = [{
          id: '',
          label: '全部',
          children: response.data
        }]
      })
    },
    async getFacsObj(areaCode, category, subCategory) {
      if (areaCode !== null) {
        this.queryObjParams.refArea = areaCode
      }
      this.queryObjParams.facsCategory = category
      this.queryObjParams.subCategory = subCategory

      listAllFacs(this.queryObjParams).then(response => {
        this.objOptions = response.data
      })
    },
    tabClick() {
      this.reset()
      if (this.activeName !== 'summery') {
        this.dateRange = [dayjs().format(DateTool.DateFormat.YYYY_MM_DD_00_00_00), dayjs().format(DateTool.DateFormat.YYYY_MM_DD_23_59_59)]
        this.queryParams.facsCategory = this.facsCategory
        this.queryParams.facsSubCategory = this.activeName
        this.queryParams.pageNum = 1
        this.selectedLabel = '全部'
        this.getList()
      } else {

      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleObjSelectClick() {
      this.getFacsObj(this.queryParams.areaCode, this.facsCategory, this.activeName)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaCode = data.id
      this.selectedLabel = data.label
      this.objCode = null
      this.handleQuery()
    },
    reset() {
      this.objCode = null
      this.queryParams.areaCode = null
      this.queryParams.facsCategory = 'Z'
      this.queryParams.facsSubCategory = null
      this.queryParams.objCode = null
      this.objOptions = []
    }
  }
}
</script>
