<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="测量时间">
        <el-date-picker
          clearable
          v-model="time"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="searchDate"
          :editable="false"
        >
          >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="是否报警" prop="state">
        <el-select
          v-model="state"
          clearable
          placeholder="请选择"
          @change="stateChange"
        >
          <el-option
            v-for="item in stateoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入是否报警"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >

        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:data:export']"
          >导出</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column label="设备名称" align="center" prop="sensorname" />

      <el-table-column label="	X方向偏移(mm) " align="center" prop="dispX" />
      <el-table-column label="Y方向偏移(mm) " align="center" prop="dispY" />
      <el-table-column label="Z方向变化量(mm) " align="center" prop="dispH" />

      <el-table-column label="测量时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, "{y}年{m}月{d}日 {h}点") }}</span>
        </template>
      </el-table-column>

      <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>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
} from "@/api/bianpo/history";

export default {
  name: "Data",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 面位移数据表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beginTime: null,
        endTime: null,
        state: null,
      },
      stateoptions: [
        {
          value: "0",
          label: "正常",
        },
        {
          value: "1",
          label: "报警",
        },
      ],
      state: "",
      // 表单参数
      form: {},
      // 表单校验
      time: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    stateChange(e) {
      console.log(e);
      if (!e) {
        this.queryParams.state = null;
      } else {
        this.queryParams.state = e;
      }
    },
    searchDate(e) {
      if (!e) {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      } else {
        this.queryParams.beginTime = e[0];
        this.queryParams.endTime = e[1];
      }
    },
    /** 查询面位移数据列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      listData(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sensorid: null,
        sensorname: null,
        time: null,
        dispX: null,
        dispY: null,
        dispH: null,
        createtime: null,
        state: null,
        bjjb: null,
        updateTime: 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;
      getData(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) {
            updateData(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(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 delData(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/data/export",
        {
          ...this.queryParams,
        },
        `data_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
