<!-- 江河水查看数据页面-->
<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :model="where" :label-col="{ md: { span: 8 }, sm: { span: 24 } }"
        :wrapper-col="{ md: { span: 16 }, sm: { span: 24 } }">
        <a-row>
          <a-form-item label="断面名称">
           <a-select allow-clear mode="multiple" placeholder="请选择断面名称" v-model:value="where.name">
              <a-select-option v-for="item in sectionInfo" :key="item.stationName" :value="item.stationName">
                {{ item.stationName }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="时间:">
              <a-month-picker
              v-model:value="where.time"
              :disabled-date="disabledDate"
              valueFormat="YYYY-MM"
            />
          </a-form-item>
          <a-form-item label="类型:" style="width:100px"> 
            <a-select v-model:value="where.type" allowClear showSearch>
              <a-select-option value="0">上半月</a-select-option>
              <a-select-option value="1">下半月</a-select-option>
              <a-select-option value="2">整月</a-select-option>
            </a-select>
          </a-form-item>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item class="ele-text-right" :wrapper-col="{ span: 24 }">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
                <a-button @click="exportFile">导出Excel</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <a-spin :spinning="loading">
      <ele-pro-table ref="table" :datasource="momData" :columns="columnsMom" :where="where"
        :scroll="{ x: 'max-content' }" @done="(d) => (data = d.data)">
      </ele-pro-table>

      <ele-pro-table ref="table" :datasource="yoyData" :columns="columnsYoy" :where="where"
        :scroll="{ x: 'max-content' }" @done="(d) => (data = d.data)">
      </ele-pro-table>
    </a-spin>
    </a-card>
  </div>
  <!-- 编辑弹窗 -->
</template>

<script>
  import moment from "moment";
 import {
  listWaterSiteYoy
  } from "@/api/ecology/river-statis";
  import {
    listInfo
  } from "@/api/ecology/water/river-water";
  import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
  // import {
  //   tableColumns
  // } from "./colums";
  import XLSX from "xlsx";
  export default {
    name: "RiverCollectWater",
    components: {},
    data() {
      return {
        data: [],
        momData:[],
        yoyData:[],
        datasource: {},
        columnsMom: [{
            title: '所在河流名称',
            dataIndex: 'riverName',
            key: 'riverName',
          },
          {
            title: '自动站名称',
            dataIndex: 'stationName',
            key: 'stationName',
          },
          {
            title: "溶解氧（mg/L）", children: [
              {title: "本半月", dataIndex: "oxygenMonth"},
              {title: "上期", dataIndex: "oxygenLast"},
              {title: "增减", dataIndex: "oxygenLastvalue"},
            ]
          },
          {
            title: "高锰酸盐指数（mg/L）", children: [
              {title: "本半月", dataIndex: "permanganateMonth"},
              {title: "上期", dataIndex: "permanganateLast"},
              {title: "增减", dataIndex: "permanganateLastValue"},
            ]
          },
          {
            title: "氨氮（mg/L）", children: [
              {title: "本半月", dataIndex: "ammoniaMonth"},
              {title: "上期", dataIndex: "ammoniaLast"},
              {title: "增减", dataIndex: "ammoniaLastValue"},
            ]
          },
          {
            title: "总磷（mg/L）", children: [
              {title: "本半月", dataIndex: "phosphorusMonth"},
              {title: "上期", dataIndex: "phosphorusLast"},
              {title: "增减", dataIndex: "phosphorusLastValue"},
            ]
          },
        ],
        columnsYoy: [
        {
            title: '所在河流名称',
            dataIndex: 'riverName',
            key: 'riverName',
          },
          {
            title: '自动站名称',
            dataIndex: 'stationName',
            key: 'stationName',
          },
          {
            title: "溶解氧（mg/L）", children: [
              {title: "本半月", dataIndex: "oxygenMonth"},
              {title: "去年同期", dataIndex: "oxygenLastYearStage"},
              {title: "增减", dataIndex: "oxygenYearvalue"},
            ]
          },
          {
            title: "高锰酸盐指数（mg/L）", children: [
              {title: "本半月", dataIndex: "permanganateMonth"},
              {title: "去年同期", dataIndex: "permanganateLastYearStage"},
              {title: "增减", dataIndex: "permanganateYearValue"},
            ]
          },
          {
            title: "氨氮（mg/L）", children: [
              {title: "本半月", dataIndex: "ammoniaMonth"},
              {title: "去年同期", dataIndex: "ammoniaLastYearStage"},
              {title: "增减", dataIndex: "ammoniaYearValue"},
            ]
          },
          {
            title: "总磷（mg/L）", children: [
              {title: "本半月", dataIndex: "phosphorusMonth"},
              {title: "去年同期", dataIndex: "phosphorusLastYearStage"},
              {title: "增减", dataIndex: "phosphorusYearValue"},
            ]
          },
        ],
        locale,
        bill: {},
        // 表格数据接口
        // url: nationalWaterFunctionTotalUrl,
        selection: [],
        // tableColumns,
        // // 表格列配置
        // columns: [
        //   ...tableColumns,
        // ],
        // 表格搜索条件
        where: {},
        sectionInfo: {},
        loading: false,
        rules: {},
      };
    },
    created() {
      listInfo().then((res) => {
        if (res.data.code == 0) {
          console.log(res.data.data)
          this.visibleWater = true;
          this.sectionInfo = res.data.data
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    methods: {
      /* 刷新表格 */
      reload() {
        this.loading = true;
        listWaterSiteYoy(this.where).then((res) => {
          this.momData = [];
          this.yoyData = [];
          console.log(res.data.data);
          if (res.data.code == 0) {
              this.$message.success(res.data.msg);
              res.data.data.momData.forEach((m)=> {
                this.momData.push(m);
              })
              res.data.data.yoyData.forEach((y)=> {
                this.yoyData.push(y);
              })
            } else {
              this.$message.error(res.data.msg);
            }
            this.loading = false;
        });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
        this.reload();
      },
      /* 导出 */
      exportFile() {
        //环比数据
        const arr = [];
        const th1 = [];
        const th2 = [];
        const merges = []; // 合并
        const columnsTemp = []; // 树形结构整理成list
        this.columnsMom.forEach((item, index) => {
          if (item.children) {
            item.children.forEach((citem) => {
              th1.push(item.title)
              th2.push(citem.title)
              columnsTemp.push(citem)
            })
            merges.push({
              s: {
                r: 0,
                c: th1.length - item.children.length
              },
              e: {
                r: 0,
                c: th1.length - 1
              }
            })
          } else {
            th1.push(item.title)
            th2.push("")
            columnsTemp.push(item)
            merges.push({
              s: {
                r: 0,
                c: index
              },
              e: {
                r: 1,
                c: index
              }
            })
          }
        })
        arr.push(th1, th2);
        this.momData.forEach((d) => {
          const td = columnsTemp.map((item) => d[item.dataIndex]);
          arr.push(td);
        });

        //同比数据
        const arr2 = [];
        const th3 = [];
        const th4 = [];
        const merges2 = []; // 合并
        const columnsTemp2 = []; // 树形结构整理成list
        this.columnsYoy.forEach((item, index) => {
          if (item.children) {
            item.children.forEach((citem) => {
              th3.push(item.title)
              th4.push(citem.title)
              columnsTemp2.push(citem)
            })
            merges2.push({
              s: {
                r: 0,
                c: th3.length - item.children.length
              },
              e: {
                r: 0,
                c: th3.length - 1
              }
            })
          } else {
            th3.push(item.title)
            th4.push("")
            columnsTemp2.push(item)
            merges2.push({
              s: {
                r: 0,
                c: index
              },
              e: {
                r: 1,
                c: index
              }
            })
          }
        })
        arr2.push(th3, th4);
        this.yoyData.forEach((d) => {
          const td = columnsTemp2.map((item) => d[item.dataIndex]);
          arr2.push(td);
        });
        var dataSheet=arr.concat(arr2);
        console.log("dataSheet"+dataSheet);
        let sheet = XLSX.utils.aoa_to_sheet(dataSheet);
        sheet['!merges'] = merges;
        this.$util.exportSheet(XLSX, sheet, moment(this.where.timeStart).format("YYYY年MM月DD日") + "至" + moment(this.where
          .timeEnd).format("YYYY年MM月DD日") + "统计数据");

      }

    },
  };

</script>

<style scoped lang="less">
</style>
