<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
    </el-form>

    <!-- <right-toolbar
      :showSearch.sync="showSearch"
      @queryTable="getList"
    ></right-toolbar> -->

    <el-table
      v-loading="loading"
      :data="abnormalList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键，自增" align="center" prop="id" /> -->
      <el-table-column label="设备编号" align="center" prop="equipno" />
      <el-table-column label="设备名称" align="center" prop="equipname" />
      <el-table-column label="监测项类型" align="center" prop="devicetype" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0" style="color: #67c23a">已解除</span>
          <span v-if="scope.row.state == 1" style="color: #f56c6c">报警</span>
        </template>
      </el-table-column>
      <el-table-column label="报警时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.time, "{y}-{m}-{d} {h}:{m}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警值" align="center" prop="evalue" />
      <el-table-column
        label="解除报警时间"
        align="center"
        prop="jcbjtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.jcbjtime, "{y}-{m}-{d} {h}:{m}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据报警原因" align="center" prop="reason" />
      <el-table-column label="解除报警原因" align="center" prop="jcreason" />
      <el-table-column label="报警级别" align="center" prop="alarmlevel">
        <template slot-scope="scope">
          <span v-if="scope.row.alarmlevel == 1" style="color: #fa0606"
            >一级报警</span
          >
          <span v-if="scope.row.alarmlevel == 2" style="color: #fc5004"
            >二级报警</span
          >
          <span v-if="scope.row.alarmlevel == 3" style="color: #98992b"
            >三级报警</span
          >
          <span v-if="scope.row.alarmlevel == 4" style="color: #0533fc"
            >四级报警</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        :selectable="checkSelectable"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.state == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(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"
    />

    <!-- 添加或修改边坡异常数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="解除报警原因" prop="jcreason">
              <el-input
                v-model="form.jcreason"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
          <el-col :span="15">
            <el-form-item label="该时间内不报警" prop="jchours">
              <el-input-number
                v-model="openAlarm"
                @change="cancelAlarmNum"
                :min="0"
                :step="0.5"
                label="请输入时间"
              ></el-input-number>
              <span>小时</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span>{{ newdate }}之前不再报警</span>
      <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 {
  listAbnormal,
  getAbnormal,
  delAbnormal,
  addAbnormal,
  updateAbnormal,
} from "@/api/bianpo/alarm";

export default {
  name: "Abnormal",
  data() {
    return {
      newdate: 0,
      //解除报警时间
      openAlarm: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 边坡异常数据表格数据
      abnormalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipno: null,
        equipname: null,
        devicetype: null,
        state: null,
        time: null,
        evalue: null,
        jcbjtime: null,
        reason: null,
        jcreason: null,
        jchours: null,
        alarmlevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipno: [
          { required: true, message: "设备编号不能为空", trigger: "blur" },
        ],
        devicetype: [
          {
            required: true,
            message: "监测项类型：表面位移，内部位移，裂缝监测等不能为空",
            trigger: "change",
          },
        ],
        state: [{ required: true, message: "状态，不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getYMDHMS(timestamp) {
      let time = new Date(timestamp);
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let date = time.getDate();
      let hours = time.getHours();
      let minute = time.getMinutes();
      let second = time.getSeconds();

      if (month < 10) {
        month = "0" + month;
      }
      if (date < 10) {
        date = "0" + date;
      }
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      if (second < 10) {
        second = "0" + second;
      }
      this.newdate =
        year +
        "-" +
        month +
        "-" +
        date +
        " " +
        hours +
        ":" +
        minute +
        ":" +
        second;
    },

    cancelAlarmNum(e) {
      let num = e;
      let date = Date.parse(new Date());
      console.log(date);
      let afterdate = date + num * 1000 * 60 * 60;
      this.getYMDHMS(afterdate);
      // let newcacleTime = dateFormat(cancleTime, "yyyy-MM-dd hh:mm:ss");
      console.log(this.newdate);
    },
    /** 查询边坡异常数据列表 */
    getList() {
      this.loading = true;
      listAbnormal(this.queryParams).then((response) => {
        this.abnormalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipno: null,
        equipname: null,
        devicetype: null,
        state: null,
        time: null,
        evalue: null,
        jcbjtime: null,
        reason: null,
        jcreason: null,
        jchours: null,
        createTime: null,
        updateTime: null,
        alarmlevel: null,
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that = this;
      console.log(row, "修改按钮操作");
      this.reset();
      const id = row.id;

      getAbnormal(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "解除报警";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let that = this;
          if (this.form.id != null) {
            let data = {
              id: that.form.id,
              jcbjtime: that.newdate,
              state: 0,
              jcreason: that.form.jcreason,
            };
            updateAbnormal(data).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAbnormal(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    checkSelectable(row) {
      if (row.state === 0) {
        return false;
      } else {
        return true;
      }
    },
  },
};
</script>
