<template>
  <div class="page-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px" class="search-form_container">
      <el-form-item label="预测时间" prop="daterange">
        <el-date-picker
          :editable="false"
          value-format="YYYY/MM/DD HH:mm:ss"
          v-model="queryParams.daterange"
          type="datetimerange"
          range-separator="To"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="预测类型" prop="category">
        <el-select v-model="queryParams.category">
          <el-option value="" label="全部"></el-option>
          <el-option
            :key="item"
            :value="item.value"
            :label="item.label"
            v-for="item in category"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-card shadow="never" class="card-chart_container">
      <LineEchart :category="category"></LineEchart>
      </el-card>
    <el-card shadow="never" body-class="card-table_container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="download"
            size="small"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar @update:showSearch="(v) => showSearch = v" @queryTable="getList" :showSearch="showSearch"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="comparisonList" @selection-change="handleSelectionChange">
        <el-table-column label="时间" align="center" prop="remark" width="120"/>
        <el-table-column label="预测类型" align="center" prop="category" width="120"/> 
        <el-table-column label="预测1时间" align="center" prop="forecastTimeOne" width="120"/>
        <el-table-column label="实际1小时值" align="center" prop="realOne" width="120"/>
        <el-table-column label="TFT预测1小时值" align="center" prop="hsForecastOne" width="120"/>
        <el-table-column label="TFT预测1小时误差值" align="center" prop="hsOneSubtract" width="150"/>
        <el-table-column label="TFT预测1小时百分比误差" align="center" prop="hsErrorRateOneStr" width="180"/>
        <el-table-column label="LSTM预测1小时值" align="center" prop="yyForecastOne" width="140"/>
        <el-table-column label="LSTM预测1误差值" align="center" prop="yyOneSubtract" width="150"/>
        <el-table-column label="LSTM预测1小时百分比误差" align="center" prop="yyErrorRateOneStr" width="200"/>
        <el-table-column label="预测2时间" align="center" prop="forecastTimeTwo" width="120"/>
        <el-table-column label="实际2小时值" align="center" prop="realTwo" width="120"/>
        <el-table-column label="TFT预测2小时值" align="center" prop="hsForecastTwo" width="120"/>
        <el-table-column label="TFT预测2误差值" align="center" prop="hsTwoSubtract" width="150"/>
        <el-table-column label="TFT预测2小时百分比误差" align="center" prop="hsErrorRateTwoStr" width="180"/>
        <el-table-column label="LSTM预测2小时值" align="center" prop="yyForecastTwo" width="140"/>
        <el-table-column label="LSTM预测2误差值" align="center" prop="yyTwoSubtract" width="140"/>
        <el-table-column label="LSTM预测2小时百分比误差" align="center" prop="yyErrorRateTwoStr" width="200"/>
        <el-table-column label="预测3时间" align="center" prop="forecastTimeThree" width="120"/>
        <el-table-column label="实际3小时值" align="center" prop="realThree" width="120"/>
        <el-table-column label="TFT预测3小时值" align="center" prop="hsForecastThree" width="120"/>
        <el-table-column label="TFT预测3误差值" align="center" prop="hsThreeSubtract" width="120"/>
        <el-table-column label="TFT预测3小时百分比误差" align="center" prop="hsErrorRateThreeStr" width="180"/>
        <el-table-column label="LSTM预测3小时值" align="center" prop="yyForecastThree" width="140"/>
        <el-table-column label="LSTM预测3误差值" align="center" prop="yyThreeSubtract" width="140"/>
        <el-table-column label="LSTM预测3小时百分比误差" align="center" prop="yyErrorRateThreeStr" width="190"/>
      </el-table>
  
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改信义预测对比对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="预测类型" prop="category">
          <el-input v-model="form.category" placeholder="请输入预测类型" />
        </el-form-item>
        <el-form-item label="预测时间1" prop="forecastTimeOne">
          <el-input v-model="form.forecastTimeOne" placeholder="请输入预测时间1" />
        </el-form-item>
        <el-form-item label="实际值1" prop="realOne">
          <el-input v-model="form.realOne" placeholder="请输入实际值1" />
        </el-form-item>
        <el-form-item label="红杉预测值1" prop="hsForecastOne">
          <el-input v-model="form.hsForecastOne" placeholder="请输入红杉预测值1" />
        </el-form-item>
        <el-form-item label="红杉值1误差率" prop="hsErrorRateOne">
          <el-input v-model="form.hsErrorRateOne" placeholder="请输入红杉值1误差率" />
        </el-form-item>
        <el-form-item label="跃渊预测值1" prop="yyForecastOne">
          <el-input v-model="form.yyForecastOne" placeholder="请输入跃渊预测值1" />
        </el-form-item>
        <el-form-item label="跃渊值1误差率" prop="yyErrorRateOne">
          <el-input v-model="form.yyErrorRateOne" placeholder="请输入跃渊值1误差率" />
        </el-form-item>
        <el-form-item label="预测时间2" prop="forecastTimeTwo">
          <el-input v-model="form.forecastTimeTwo" placeholder="请输入预测时间2" />
        </el-form-item>
        <el-form-item label="实际值2" prop="realTwo">
          <el-input v-model="form.realTwo" placeholder="请输入实际值2" />
        </el-form-item>
        <el-form-item label="红杉预测值2" prop="hsForecastTwo">
          <el-input v-model="form.hsForecastTwo" placeholder="请输入红杉预测值2" />
        </el-form-item>
        <el-form-item label="红杉值2误差率" prop="hsErrorRateTwo">
          <el-input v-model="form.hsErrorRateTwo" placeholder="请输入红杉值2误差率" />
        </el-form-item>
        <el-form-item label="跃渊预测值2" prop="yyForecastTwo">
          <el-input v-model="form.yyForecastTwo" placeholder="请输入跃渊预测值2" />
        </el-form-item>
        <el-form-item label="跃渊值2误差率" prop="yyErrorRateTwo">
          <el-input v-model="form.yyErrorRateTwo" placeholder="请输入跃渊值2误差率" />
        </el-form-item>
        <el-form-item label="预测时间3" prop="forecastTimeThree">
          <el-input v-model="form.forecastTimeThree" placeholder="请输入预测时间3" />
        </el-form-item>
        <el-form-item label="实际值3" prop="realThree">
          <el-input v-model="form.realThree" placeholder="请输入实际值3" />
        </el-form-item>
        <el-form-item label="红杉预测值3" prop="hsForecastThree">
          <el-input v-model="form.hsForecastThree" placeholder="请输入红杉预测值3" />
        </el-form-item>
        <el-form-item label="红杉值3误差率" prop="hsErrorRateThree">
          <el-input v-model="form.hsErrorRateThree" placeholder="请输入红杉值3误差率" />
        </el-form-item>
        <el-form-item label="跃渊预测值3" prop="yyForecastThree">
          <el-input v-model="form.yyForecastThree" placeholder="请输入跃渊预测值3" />
        </el-form-item>
        <el-form-item label="跃渊值3误差率" prop="yyErrorRateThree">
          <el-input v-model="form.yyErrorRateThree" placeholder="请输入跃渊值3误差率" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="乐观锁" prop="revision">
          <el-input v-model="form.revision" placeholder="请输入乐观锁" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComparison, getComparison, delComparison, addComparison, updateComparison } from "@/api/business/comparison";
import dayjs from "dayjs";
import LineEchart from "./components/LineEchart";
export default {
  components: { LineEchart },
  name: "Comparison",
    data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信义预测对比表格数据
      comparisonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预测类型
      category: [
        { label: "#1NO₃⁻", value: 'xsy1' },
        { label: "#2NO₃⁻", value: 'xsy2' },
        { label: "PO₄³⁻ ", value: 'zlsy' },
        { label: "NH₃-N ", value: 'nh3' },
        { label: "COD ", value: 'cod' },
        { label: "SS ", value: 'ss' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        daterange: [],
        category: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询信义预测对比列表 */
    getList() {
      this.loading = true;
      const [ begin, end ] = this.queryParams.daterange || [];
      let timeBegin = '';
      let timeEnd = '';
   
      if ( begin ) {
        timeBegin = dayjs(begin).format('YYYY/MM/DD H');
        timeEnd = dayjs(end).format('YYYY/MM/DD H');
      }
      
      const params = { ...this.queryParams, timeBegin, timeEnd };
      delete params.daterange;
      listComparison(params).then(response => {
        this.comparisonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        category: null,
        forecastTimeOne: null,
        realOne: null,
        hsForecastOne: null,
        hsErrorRateOne: null,
        yyForecastOne: null,
        yyErrorRateOne: null,
        forecastTimeTwo: null,
        realTwo: null,
        hsForecastTwo: null,
        hsErrorRateTwo: null,
        yyForecastTwo: null,
        yyErrorRateTwo: null,
        forecastTimeThree: null,
        realThree: null,
        hsForecastThree: null,
        hsErrorRateThree: null,
        yyForecastThree: null,
        yyErrorRateThree: null,
        delFlag: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信义预测对比";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getComparison(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信义预测对比";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComparison(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComparison(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除信义预测对比编号为"' + ids + '"的数据项？').then(function() {
        return delComparison(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/comparison/export', {
        ...this.queryParams
      }, `comparison_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
.echart-wrap {
  // height: 420px;
}
</style>
