<template>
  <div class="name">
    <el-card class="mt10 box-card">
      <div class="lcl-title">人工观测</div>
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
        >新增</el-button
      >
      <div class="mt10" id=" radar-table">
        <el-table
          :data="dataList"
          border
          stripe
          style="width: 100%"
          :header-cell-style="{
            textAlign: 'center',
            height: '20px',
          }"
          :row-style="{ height: '20px' }"
        >
          <el-table-column align="center" prop="pointName" label="人工观测点">
          </el-table-column>
          <el-table-column
            align="center"
            prop="observationDate"
            label="观测时间"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="observationValue"
            label="人工观测值(mm)"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="onlineValue"
            label="在线监测值(mm)"
          >
          </el-table-column>

          <el-table-column
            align="center"
            class-name="small-padding fixed-width"
            label="历史数据"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
                >查看</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDel(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 新增修改 -->
    <el-dialog
      width="30%"
      :inline="true"
      title="人工观测数据"
      :visible.sync="watchDialog"
    >
      <el-form>
        <el-form-item label="人工观测点" label-width="120">
          <el-col :span="11">
            <el-select v-model="form.pointId" placeholder="请选择">
              <el-option
                v-for="item in pointList"
                :key="item.id"
                :label="item.pointName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="人工观测值" label-width="120">
          <el-col :span="11">
            <el-input-number
              width="100%"
              v-model="form.observationValue"
              @change="handleChange"
              label="描述文字"
            ></el-input-number>
          </el-col>
        </el-form-item>
        <el-form-item label="观测日期" required>
          <el-col :span="11">
            <el-form-item prop="form.observationDate">
              <el-date-picker
                type="date"
                placeholder="选择观测日期"
                v-model="form.observationDate"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="watchDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
    <!-- 查看对话框 -->

    <el-dialog
      :title="watchDialogTitle"
      :visible.sync="dialogView"
      width="70%"
      class="watch-view-dialog"
    >
      <div class="watchTable">
        <el-table :data="viewDataList">
          <el-table-column
            property="observationDate"
            label="观测时间"
            width="150"
          ></el-table-column>
          <el-table-column
            property="observationValue"
            label="人工观测值"
          ></el-table-column>
          <el-table-column property="onlineValue" label="在线监测值">
          </el-table-column>
        </el-table>
        <pagination
          v-show="queryParamsWatch.total > 0"
          :total="queryParamsWatch.total"
          :page.sync="queryParamsWatch.pageNum"
          :limit.sync="queryParamsWatch.pageSize"
          @pagination="observationHistoryList"
        />
      </div>
      <div id="charta">
        <ChartsForLine :ChartsForLineParant="chartNowData" class="nbnb" />
      </div>
      <div class="cb"></div>
    </el-dialog>
  </div>
</template>
<script>
import {
  observationList,
  observationAdd,
  pointList,
  observationHistory,
  observationEdit,
  delObservation,
} from "@/api/lclApi/lclapi";
import ChartsForLine from "@/components/lclEcharts/ChartsForLine";
import * as echarts from "echarts";
export default {
  name: "name",

  components: { ChartsForLine },
  props: [],
  data() {
    return {
      watchDialogTitle: "1#边坡沉降观测点",
      watchDialog: false,
      dialogView: false,
      pointList: [],
      dataList: [
        {
          pointName: "1#边坡沉降观测点",
          observationDate: "2023-03-24",
          observationValue: "7",
          onlineValue: "7",
        },
      ],
      form: {},
      viewDataList: [
        { observationDate: "2023-03-25", observationValue: 7, onlineValue: 7 },
      ],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParamsWatch: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
        pointId: null,
      },
      chartNowData: {
        chartId: "chart",
        color: ["#f7af0b", "#05f57a", "#f40852"],
        chartType: "line",
        axisLabel: "#0562AF",
        itemColor: "rgba(42,148,226,1)",
        unit: "mm",
        bgc: "#fff",
        grid: {
          left: "10%",
          right: "5%",
          bottom: "10%",
          top: "15%",
          z: 22,
        },
        showlegend: true,
        legendItem: ["a", "b"],
        xAxisData: ["q", "p"],
        yData: [],
      },
    };
  },
  watch: {},
  created() {
    this.$nextTick(() => {
      this.getPointList();
      this.dialogView = true;
    });
  },
  mounted() {
    this.$nextTick(() => {
      this.getList();
      this.dialogView = false;
    });
  },
  methods: {
    formRest() {
      this.form = {
        id: null,
        pointId: null,
        observationValue: null,
        observationDate: null,
      };
    },
    getPointList() {
      pointList().then((res) => {
        if (res && res.code === 200) {
          this.pointList = res.rows;
        }
      });
    },
    observationHistoryList() {
      let that = this;
      observationHistory(this.queryParamsWatch)
        .then((res) => {
          if (res && res.code === 200 && res.rows) {
            this.viewDataList = res.rows;
            this.queryParamsWatch.total = res.total;
          }
        })
        .then(() => {
          this.dialogView = true;
          this.$nextTick(() => {
            setTimeout(function () {
              that.chartNowData.yData = [];
              that.chartNowData.legendItem = ["人工", "在线"];
              that.chartNowData.xAxisData = that.viewDataList.map(
                (v) => v.observationDate
              );
              that.chartNowData.yData.push(
                that.viewDataList.map((v) => v.observationValue)
              );
              that.chartNowData.yData.push(
                that.viewDataList.map((v) => v.onlineValue)
              );
            });
          }, 100);
        });
    },

    handleView(row) {
      console.log(row, "handleView");
      this.watchDialogTitle = row.pointName;
      this.queryParamsWatch.pointId = row.pointId;
      this.observationHistoryList();
    },
    handleChange(e) {
      console.log(e);
    },
    addWatch() {},
    submitForm() {
      if (this.form.id) {
        observationEdit(this.form).then((res) => {
          if (res && res.code === 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          }
        });
      } else {
        observationAdd(this.form).then((res) => {
          this.$message({
            message: "提交成功",
            type: "success",
          });
        });
        this.getList();
        this.formRest();
        this.watchDialog = false;
      }
    },
    //人工观测列表
    getList() {
      observationList(this.queryParams).then((res) => {
        if (res && res.code === 200 && res.rows) {
          this.dataList = res.rows;
          this.total = res.total;
        }
      });
    },
    //新增人工观测
    handleAdd() {
      this.formRest();
      this.watchDialog = true;
    },
    // 修改
    handleEdit(row) {
      this.form = {
        id: row.id,
        pointId: row.pointId,
        observationValue: row.observationValue,
        observationDate: row.observationDate,
      };
      this.watchDialog = true;
    },
    handleDel(row) {
      let that = this;
      const id = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          delObservation(id).then((res) => {
            if (res.code === 200) {
              that.getList();
              that.$modal.msgSuccess("删除成功");
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.name {
  width: 100%;
  min-height: 100%;
  .box-card {
    width: 100%;
    .lcl-title {
      font-size: 22px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #333333;
      line-height: 30px;
      position: relative;
      padding-left: 10px;
      &:before {
        content: "";
        position: absolute;
        left: 0px;
        top: 4px;
        width: 6px;
        height: 22px;
        background: #0d85f4;
      }
    }
  }
}
.el-input-number--medium {
  width: 100%;
}
.watchTable {
  width: 50%;
  min-height: 500px;
  float: left;
}

.watch-view-dialog {
  width: 100%;
  overflow: hidden;
  #charta {
    width: 50%;
    height: 500px;
    float: left;
    .nbnb {
      width: 100%;
      height: 500px !important;
    }
  }
}
.cb {
  clear: both;
}
</style>