<template>
  <div class="device-manage">
    <el-card class="box-card">
      <div class="lcl-title">监测点管理</div>
      <el-button
        class="mt20"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
        >新增</el-button
      >
    </el-card>
    <el-card class="box-card mt20">
      <div class="lcl-title">监测点</div>
      <div class="mt20">
        <!-- <el-select v-model="queryParams.equipno" placeholder="请选择设备">
          <el-option
            v-for="item in monDicList"
            :key="item.monNumber"
            :label="item.monName"
            :value="item.monNumber"
          ></el-option>
        </el-select> -->
        <el-input
          v-model="queryParams.pointName"
          placeholder="请输入监测点名称"
          clearable
          style="width: 240px"
        />
        <el-button
          type="primary"
          icon="el-icon-search"
          size="medium"
          @click="handleQuery"
          >搜索</el-button
        >
      </div>
      <div class="mt20">
        <el-table
          v-loading="loading"
          :data="pointList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="监测点名称" align="center" prop="pointName" />
          <!-- <el-table-column label="设备编号" align="center" prop="equipno" /> -->
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改观测点位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="监测点名称" prop="pointName">
          <el-input v-model="form.pointName" placeholder="请输入监测点名称" />
        </el-form-item>
        <!-- <el-form-item label="设备编号" prop="equipno">
          <el-select v-model="form.equipno" placeholder="请选择关联的设备编号">
            <el-option
              v-for="item in monDicList"
              :key="item.monNumber"
              :label="item.monName"
              :value="item.monNumber"
            ></el-option>
          </el-select>
        </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 { api } from '@vue/api'
import {
  pointmonList,
  addPointmon,
  updatePointmon,
  delPointmon,
  monDic,
} from "@/api/lclApi/lclapi";
export default {
  name: "deviceManage",
  components: {},
  props: [],
  data() {
    return {
      monDicList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 观测点位表格数据
      pointList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointName: null,
        equipno: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  watch: {},
  created() {
    this.$nextTick(function () {
      this.getMonDic();
      this.getList();
    });
  },
  mounted() {
    this.$nextTick(function () {});
  },
  methods: {
    /** 获取设备列表 */
    getMonDic() {
      monDic().then((res) => {
        if (res && res.code === 200) {
          this.monDicList = res.rows;
        }
      });
    },
    /** 查询观测点位列表 */
    getList() {
      this.loading = true;
      pointmonList(this.queryParams).then((response) => {
        this.pointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pointName: null,
        equipno: 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();
      this.form = row;
      this.open = true;
      this.title = "修改观测点位";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePointmon(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPointmon(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该观测点位？")
        .then(function () {
          return delPointmon(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.device-manage {
  width: 100%;
  min-height: 100%;
  // border-radius: 10px;
  // background-color: teal;
  .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;
      }
    }
  }
}
.chart {
  width: 100%;
  min-height: 70vh;
  border: 1px solid teal;
  overflow: hidden;
  .nbnb {
    width: 100%;
    height: 70vh !important;
  }
}
</style>