<!-- 江河水查看数据页面-->
<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.sectionName" :value="item.sectionName">
                {{ item.sectionName }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="河段名称">
            <a-select v-model:value="where.riverLakeLibrary" placeholder="请选择河段名称" allowClear showSearch>
              <a-select-option v-for="item in waterSystemList" :key="item">{{ item }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="监测类型">
            <a-select v-model:value="where.monitorType" placeholder="请选择监测类型" @change="handleChange" allowClear showSearch>
              <a-select-option :value="'1'">全因子</a-select-option>
              <a-select-option :value="'2'">纳污红线</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="起始日期:">
            <a-month-picker valueFormat="YYYY-MM" v-model:value="where.startTime"></a-month-picker>
          </a-form-item>
          <a-form-item label="结束日期:">
            <a-month-picker valueFormat="YYYY-MM" v-model:value="where.endTime"></a-month-picker>
          </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="datasource" :columns="columns" :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 {
    listNationalWaterFunctionTotal
  } from "@/api/ecology/river-statis";
  import {
    listInfo
  } from "@/api/ecology/water/river-api-relate";
  import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
  import {
    nationalTableColumns
  } from "./colums";
  import XLSX from "xlsx";
  export default {
    name: "RiverCollectWater",
    components: {},
    data() {
      return {
        data: [],
        datasource: {},
        locale,
        bill: {},
        // 表格数据接口
        // url: nationalWaterFunctionTotalUrl,
        selection: [],
        nationalTableColumns,
        // 表格列配置
        columns: [
          ...nationalTableColumns,
        ],
        // 表格搜索条件
        where: {},
        sectionInfo: {},
        loading: false,
        rules: {},
        waterSystemList: [],
      };
    },
    mounted() {
      this.loading = true;
      this.$refs.table.reload({
        where: this.where,
      });
      this.loading = false;
    },

    created() {
      listInfo().then((res) => {
        if (res.data.code == 0) {
          console.log(res.data.data)
          this.visibleWater = true;
          this.sectionInfo = res.data.data
          res.data.data.forEach((m) => {
            this.waterSystemList.push(m.waterBody);
          })
          this.waterSystemList = [...new Set(this.waterSystemList)];
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    methods: {
      handleChange() {
        this.columns.forEach((e,index)=>{
          console.log("jjjjj"+e.title+"index"+index);
        })
        console.log(this.columns.length);
        console.log(this.where.monitorType);
        let item1 = {
          title: "水功能区限值纳污红线主要控制项目达标评价",
          children: [{
              title: "水功能区水质类别",
              dataIndex: "lprlWaterFunctionAreaWaterQualityCategory"
            },
            {
              title: "达标评价结论",
              dataIndex: "lprlCompliantEvaluationConclusion"
            },
            {
              title: "主要超标项目及其超标倍数",
              dataIndex: "lprlMajorOverStandardItemsMultiples"
            },
          ]
        };
        let item2 = {
          title: "全因子评价",
          children: [{
              title: "水功能区水质类别",
              dataIndex: "ffeWaterFunctionAreaWaterQualityCategory"
            },
            {
              title: "达标评价结论",
              dataIndex: "ffeCompliantEvaluationConclusion"
            },
            {
              title: "主要超标项目及其超标倍数",
              dataIndex: "ffeMajorOverStandardItemsMultiples"
            },
          ]
        };
        let item3 = {title: "系统编码", dataIndex: "systemCode", sorter: true,};
        let item4 = {title: "备注", dataIndex: "remark", sorter: true,};
        if(this.where.monitorType == undefined){
            this.columns.splice(37, 1, item2);
            this.columns.splice(38, 1, item1);
            this.columns.splice(39, 1, item3);
            this.columns.splice(40, 1, item4);
        }
        if (this.columns.length == 40) {
          if (this.where.monitorType == '2') {
            this.columns.splice(37, 1, item1);
          } else if (this.where.monitorType == '1') {
            this.columns.splice(37, 1, item2);
          }
        }
        if (this.columns.length == 41) {
          if (this.where.monitorType == '2') {
            this.columns.splice(37, 1);
          } else if (this.where.monitorType == '1') {
            this.columns.splice(38, 1);
          } 
        }
      },
      /* 刷新表格 */
      reload() {
        this.loading = true;
        listNationalWaterFunctionTotal(this.where).then((res) => {
          console.log(res.data.data);
          if (res.data.code == 0) {
            this.$message.success(res.data.msg);
            this.datasource = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
          this.loading = false;
        });
        this.loading = false;
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
        this.reload();
      },
      /* 导出 */
      exportFile() {
        const arr = [];
        const th1 = [];
        const th2 = [];
        const merges = []; // 合并
        const columnsTemp = []; // 树形结构整理成list
        this.columns.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.datasource.forEach((d) => {
          const td = columnsTemp.map((item) => d[item.dataIndex]);
          arr.push(td);
        });
        let sheet = XLSX.utils.aoa_to_sheet(arr);
        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>
