<template>
  <div class="app-container">
    <el-row :gutter="20">
      <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">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

          <el-form-item label="选择设施" prop="facsCode">
            <el-select v-model="queryParams.facsCode" clearable>
              <el-option v-for="item in facsOptions" :label="item.facsName" :value="item.facsCode"
                         :key="item.facsCode"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="开始日期" prop="date">
            <el-date-picker clearable
                            v-model="queryParams.startRecTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="predictionProdList" >
          <el-table-column label="位置名称" align="center" prop="areaName"/>
          <el-table-column label="设施名称" align="center" prop="facsName"/>
          <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="发电量(kW·h)" align="center" prop="elecProdQuantity"/>
          <el-table-column label="平均功率(kW)" align="center" prop="avgPower"/>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listPredictionProd } from '@/api/prediction/predictionProd';
import { areaTreeByFacsCategory } from '@/api/basecfg/area'
import { listAllFacs } from '@/api/basecfg/emsfacs'

export default {
  name: 'PredictionProd',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电力产能预测表格数据
      predictionProdList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      areaOptions: [],
      areaName: undefined,
      facsCategory: 'E',
      facsSubCategory: 'E5',
      // 设施选项
      facsOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        facsCode: null,
        startRecTime: null,
        elecProdQuantity: null,
        avgPower: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: [
          {
            required: true,
            message: '园区代码不能为空',
            trigger: 'blur',
          },
        ],
        facsCode: [
          {
            required: true,
            message: '设施代码不能为空',
            trigger: 'blur',
          },
        ],
        date: [
          {
            required: true,
            message: '日期不能为空',
            trigger: 'blur',
          },
        ],
        elecProdQuantity: [
          {
            required: true,
            message: '发电量不能为空',
            trigger: 'blur',
          },
        ],
        avgPower: [
          {
            required: true,
            message: '功率不能为空',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  watch: {
    // 根据名称筛选区域树
    areaName (val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getAreaList();
    this.getFacsOptions();
    this.setTodayDate();
    this.getList();
  },
  methods: {
    /** 查询电力产能预测列表 */
    getList() {
      this.loading = true;
      listPredictionProd(this.queryParams).then(response => {
        this.predictionProdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询区域列表
    async getAreaList () {
      await areaTreeByFacsCategory(this.facsCategory, this.facsSubCategory, false).then(response => {
        this.areaOptions = [{
          id: '-1',
          label: '全部',
          children: []
        }].concat(response.data)
        this.selectedLabel = '全部'
        this.queryParams.areaCode = '-1'
      })
    },
    getFacsOptions() {
      const getFacsParams = {
        facsCategory: this.facsCategory,
        subCategory: this.queryParams.deviceSubCategory,
        refArea: this.queryParams.areaCode,
      }
      listAllFacs(getFacsParams).then(response => {
        this.facsOptions = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaCode: null,
        facsCode: null,
        date: null,
        elecProdQuantity: null,
        avgPower: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    handle(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    setTodayDate() {
      // 获取当前日期
      const today = new Date();
      // 格式化日期为 yyyy-MM-dd 格式
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      const day = String(today.getDate()).padStart(2, '0');
      // 设置日期选择器的值
      this.queryParams.startRecTime = `${year}-${month}-${day}`;
    },
    // 筛选节点
    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.queryParams.facsCode = undefined
      this.getFacsOptions()
      this.getList()
    },
  },
};
</script>
