<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="date">
            <el-date-picker clearable
                            v-model="queryParams.startRecTime"
                            type="month"
                            value-format="yyyy-MM"
                            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-table v-loading="loading" :data="forecastCaList" >
          <el-table-column label="园区名称" align="center" prop="areaName" />
          <el-table-column label="月份" align="center" prop="month" width="180"/>
          <el-table-column label="碳排量（kg）" align="center" prop="caEmission" />
        </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 { listForecastCa } from '@/api/prediction/forecastCa';
import { areaTreeByFacsCategory } from '@/api/basecfg/area'

export default {
  name: 'ForecastCa',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 碳排放预测表格数据
      forecastCaList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      areaOptions: [],
      areaName: undefined,
      facsCategory: 'Z',
      facsSubCategory: '',
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        startRecTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: [
          {
            required: true,
            message: '园区代码不能为空',
            trigger: 'blur',
          },
        ],
        date: [
          {
            required: true,
            message: '日期不能为空',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  created() {
    this.setTodayDate();
    this.getAreaList();
    this.getList();
  },
  methods: {
    /** 查询碳排放预测列表 */
    getList() {
      this.loading = true;
      listForecastCa(this.queryParams).then(response => {
        this.forecastCaList = 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'
      })
    },
    setTodayDate() {
      // 获取当前日期
      const today = new Date();
      // 格式化日期为 yyyy-MM-dd 格式
      const year = today.getFullYear();
      const month = String(today.getMonth() + 1).padStart(2, '0');
      // 设置日期选择器的值
      this.queryParams.startRecTime = `${year}-${month}`;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaCode: null,
        date: null,
        caEmission: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.queryParams.areaCode = data.id
      this.getList()
    },
  },
};
</script>
