<template>
  <div class="app-container">
    <new-card cardTitle="设备管理">
      <el-row :gutter="10" class="mb8 mt10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
      </el-row>
    </new-card>
    <new-card cardTitle="设备列表">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        class="mt10"
      >
        <el-form-item label="监测编号" prop="monNumber">
          <el-input
            v-model="queryParams.monNumber"
            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-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="eqListData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column label="设备名称" align="center" prop="monName" />
        <el-table-column label="监测项目" align="center" prop="monType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.slope_mon_eq"
              :value="scope.row.monType"
            />
          </template>
        </el-table-column>
        <el-table-column label="设备编号" align="center" prop="monNumber" />
        <el-table-column
          label="安装日期"
          align="center"
          prop="installationdata"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.installationdata, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="生产厂家" align="center" prop="manufacture" />

        <el-table-column
          label="操作"
          align="center"
          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>

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

    <!-- 添加或修改监测设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-col :span="12">
          <el-form-item label="设备名称" prop="monName">
            <el-input v-model="form.monName" placeholder="请输入设备名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监测项目" prop="monType">
            <el-select
              v-model="form.monType"
              placeholder="请输入监测项目"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.slope_mon_eq"
                :key="dict.value"
                :label="dict.label"
                :value="Number(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编号" prop="monNumber">
            <el-input v-model="form.monNumber" placeholder="请输入设备编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装日期" prop="installationdata">
            <el-date-picker
              clearable
              v-model="form.installationdata"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择安装日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂家" prop="manufacture">
            <el-input v-model="form.manufacture" placeholder="请输入生产厂家" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监测点位" prop="monPoint">
            <el-select
              v-model="form.monPoint"
              placeholder="请输入监测点位"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in mondicData"
                :key="item.id"
                :label="item.pointName"
                :value="Number(item.id)"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="供电和通讯方式" prop="electricitysupply">
            <el-input
              v-model="form.electricitysupply"
              placeholder="请输入供电和通讯方式"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备精度" prop="accuracy">
            <el-input v-model="form.accuracy" placeholder="请输入设备精度" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="一级预警阈值" prop="alarmLevelOne">
            <el-input
              v-model="form.alarmLevelOne"
              placeholder="请输入一级预警阈值"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="二级预警阈值" prop="alarmLevelTwo">
            <el-input
              v-model="form.alarmLevelTwo"
              placeholder="请输入二级预警阈值"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="三级预警阈值" prop="alarmLevelThr">
            <el-input
              v-model="form.alarmLevelThr"
              placeholder="请输入三级预警阈值"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="四级预警阈值" prop="alarmLevelFor">
            <el-input
              v-model="form.alarmLevelFor"
              placeholder="请输入四级预警阈值"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="初始值x方向" prop="initialx">
            <el-input v-model="form.initialx" placeholder="请输入初始值x方向" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初始值y方向" prop="initialy">
            <el-input v-model="form.initialy" placeholder="请输入初始值y方向" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初始值z方向" prop="initialz">
            <el-input v-model="form.initialz" placeholder="请输入初始值z方向" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="监测数据单位" prop="monUnit">
            <el-input v-model="form.monUnit" placeholder="请输入监测数据单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传感器初始值" prop="initial">
            <el-input v-model="form.initial" placeholder="请输入传感器初始值" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="埋深" prop="monDept">
            <el-input v-model="form.monDept" placeholder="请输入埋深" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="孔深" prop="holeDepth">
            <el-input v-model="form.holeDepth" placeholder="请输入孔深" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="安装位置" prop="installationlocate">
            <el-input
              v-model="form.installationlocate"
              placeholder="请输入安装位置"
            />
          </el-form-item>
        </el-col> -->
      </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 {
  eqList,
  pointmonList,
  eqDel,
  eqAdd,
  eqEdit,
} from "@/api/lclApi/lclapi";
import cardLcl from "@/components/lclCard/index.vue";
export default {
  name: "Eq",
  dicts: ["slope_mon_eq"],
  components: { "new-card": cardLcl },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测设备表格数据
      eqListData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        monNumber: null,
        monName: null,
        monPoint: null,
        monType: null,
        monUnit: null,
        monStatus: null,
        alarmLevelOne: null,
        alarmLevelTwo: null,
        alarmLevelThr: null,
        alarmLevelFor: null,
        alarmStatus: null,
        monDept: null,
        holeDepth: null,
        installationlocate: null,
        installationdata: null,
        manufacture: null,
        electricitysupply: null,
        accuracy: null,
        initial: null,
        initialx: null,
        initialy: null,
        initialz: null,
        realValue: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //监测点位
      mondicData: [{ pointName: "1-1测斜仪", id: 1 }],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getMonDic();
  },
  methods: {
    // 监测点位
    getMonDic() {
      let that = this;
      pointmonList({ pageNum: 1, pageSize: 9999 }).then((res) => {
        if (res && res.code === 200 && res.rows) {
          that.mondicData = res.rows;
        }
      });
    },
    /** 查询监测设备列表 */
    getList() {
      this.loading = true;
      eqList(this.queryParams).then((response) => {
        this.eqListData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        monNumber: null,
        monName: null,
        monPoint: null,
        monType: null,
        monUnit: null,
        monStatus: null,
        alarmLevelOne: null,
        alarmLevelTwo: null,
        alarmLevelThr: null,
        alarmLevelFor: null,
        alarmStatus: null,
        monDept: null,
        holeDepth: null,
        createTime: null,
        updateTime: null,
        installationlocate: null,
        installationdata: null,
        manufacture: null,
        electricitysupply: null,
        accuracy: null,
        initial: null,
        initialx: null,
        initialy: null,
        initialz: null,
        realValue: 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) {
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改监测设备";
      this.form = row;
      // getEq(id).then((response) => {
      //   this.form = response.data;

      // });
    },
    /** 提交按钮 */
    submitForm() {
      let that = this;
      if (this.form.id != null) {
        eqEdit(this.form).then((res) => {
          if (res && res.code === 200) {
            that.$modal.msgSuccess("修改成功");
            that.open = false;
            that.getList();
          }
        });
      } else {
        eqAdd(this.form).then((res) => {
          if (res && res.code === 200) {
            that.$modal.msgSuccess("新增成功");
            that.open = false;
            that.getList();
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let that = this;
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          eqDel(ids).then((res) => {
            if (res.code === 200 && res) {
              that.getList();
              that.$modal.msgSuccess("删除成功");
            }
          });
        })
        .catch(() => {});
    },
    test() {
      console.log(11);
    },
  },
};
</script>
