<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-select
          @change="gettype"
          v-model="valuetype"
          clearable
          placeholder="请选择监测项"
        >
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.value"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="equipno">
        <el-input
          @change="geteqno"
          v-model="queryParams.equipno"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="设备名称" prop="equipname">
        <el-input
          v-model="queryParams.equipname"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装日期" prop="installationdata">
        <el-date-picker
          clearable
          v-model="queryParams.installationdata"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择安装日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所在钻孔编号" prop="holeno">
        <el-input
          v-model="queryParams.holeno"
          placeholder="请输入所在钻孔编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数值单位" prop="eqdanwei">
        <el-input
          v-model="queryParams.eqdanwei"
          placeholder="请输入数值单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级预警阈值" prop="onelevelalarm">
        <el-input
          v-model="queryParams.onelevelalarm"
          placeholder="请输入一级预警阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级预警阈值" prop="twolevelalarm">
        <el-input
          v-model="queryParams.twolevelalarm"
          placeholder="请输入二级预警阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级预警阈值" prop="threelevelalarm">
        <el-input
          v-model="queryParams.threelevelalarm"
          placeholder="请输入三级预警阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="四级预警阈值" prop="fourlevelalarm">
        <el-input
          v-model="queryParams.fourlevelalarm"
          placeholder="请输入四级预警阈值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="传感器初始值" prop="initial">
        <el-input
          v-model="queryParams.initial"
          placeholder="请输入传感器初始值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ip地址" prop="ipaddress">
        <el-input
          v-model="queryParams.ipaddress"
          placeholder="请输入设备ip地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="添加时间" prop="addtime">
        <el-date-picker
          clearable
          v-model="queryParams.addtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择添加时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属巷道" prop="xiangdaono">
        <el-input
          v-model="queryParams.xiangdaono"
          placeholder="请输入所属巷道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属断面" prop="duanmianno">
        <el-input
          v-model="queryParams.duanmianno"
          placeholder="请输入所属断面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道号" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入通道号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="禁止报警时间" prop="noAlarmTime">
        <el-date-picker
          clearable
          v-model="queryParams.noAlarmTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择禁止报警时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:equip:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:equip:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:equip:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:equip:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="equipList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="设备编号" align="center" prop="equipno" />
      <el-table-column label="设备名称" align="center" prop="equipname" />
      <el-table-column
        label="安装日期"
        align="center"
        prop="installationdata"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.installationdata }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所在钻孔编号" align="center" prop="holeno" />
      <el-table-column label="数值单位" align="center" prop="eqdanwei" />
      <el-table-column label="监测项" align="center" prop="devicetype" />
      <el-table-column
        label="一级预警阈值"
        align="center"
        prop="onelevelalarm"
      />
      <el-table-column
        label="二级预警阈值"
        align="center"
        prop="twolevelalarm"
      />
      <el-table-column
        label="三级预警阈值"
        align="center"
        prop="threelevelalarm"
      />
      <el-table-column
        label="四级预警阈值"
        align="center"
        prop="fourlevelalarm"
      />
      <el-table-column label="传感器初始值" align="center" prop="initial" />
      <el-table-column label="设备ip地址" align="center" prop="ipaddress" />
      <el-table-column label="设备类型" align="center" prop="eqtype" />
      <el-table-column
        label="添加时间"
        align="center"
        prop="addtime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addtime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属巷道" align="center" prop="xiangdaono" />
      <el-table-column label="所属断面" align="center" prop="duanmianno" />
      <el-table-column label="和谐" align="center" prop="virtual">
        <template slot-scope="scope">
          <span>{{ scope.row.virtual | hexie }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通道号" align="center" prop="channel" />
      <!-- <el-table-column
        label="禁止报警时间"
        align="center"
        prop="noAlarmTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.noAlarmTime) }}</span>
        </template>
      </el-table-column> -->
      <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)"
            v-hasPermi="['project:equip:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:equip:remove']"
            >删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="equipno">
          <el-input v-model="form.equipno" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipname">
          <el-input v-model="form.equipname" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="监测项">
          <el-select
            @change="gettype1"
            v-model="valuetype"
            clearable
            placeholder="请输入监测项"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.value"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <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-form-item label="所在钻孔编号" prop="holeno">
          <el-input v-model="form.holeno" placeholder="请输入所在钻孔编号" />
        </el-form-item>
        <el-form-item label="数值单位" prop="eqdanwei">
          <el-input v-model="form.eqdanwei" placeholder="请输入数值单位" />
        </el-form-item>
        <el-form-item label="一级预警阈值" prop="onelevelalarm">
          <el-input
            v-model="form.onelevelalarm"
            placeholder="请输入一级预警阈值"
          />
        </el-form-item>
        <el-form-item label="二级预警阈值" prop="twolevelalarm">
          <el-input
            v-model="form.twolevelalarm"
            placeholder="请输入二级预警阈值"
          />
        </el-form-item>
        <el-form-item label="三级预警阈值" prop="threelevelalarm">
          <el-input
            v-model="form.threelevelalarm"
            placeholder="请输入三级预警阈值"
          />
        </el-form-item>
        <el-form-item label="四级预警阈值" prop="fourlevelalarm">
          <el-input
            v-model="form.fourlevelalarm"
            placeholder="请输入四级预警阈值"
          />
        </el-form-item>
        <el-form-item label="传感器初始值" prop="initial">
          <el-input v-model="form.initial" placeholder="请输入传感器初始值" />
        </el-form-item>
        <el-form-item label="设备ip地址" prop="ipaddress">
          <el-input v-model="form.ipaddress" placeholder="请输入设备ip地址" />
        </el-form-item>
        <el-form-item label="设备类型">
          <el-select
            @change="getdevtype"
            v-model="valuedevtype"
            clearable
            placeholder="请选择设备类型"
          >
            <el-option
              v-for="item in devtypeList"
              :key="item.value"
              :label="item.lable"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="添加时间" prop="addtime">
          <el-date-picker
            clearable
            v-model="form.addtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择添加时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所属巷道" prop="xiangdaono">
          <el-input v-model="form.xiangdaono" placeholder="请输入所属巷道" />
        </el-form-item>
        <el-form-item label="所属断面" prop="duanmianno">
          <el-input v-model="form.duanmianno" placeholder="请输入所属断面" />
        </el-form-item>
        <el-form-item label="和谐">
          <el-select
            @change="gethexie"
            v-model="valuehexie"
            clearable
            placeholder="请选择和谐"
          >
            <el-option
              v-for="item in hexieList"
              :key="item.value"
              :label="item.lable"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通道号" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入通道号" />
        </el-form-item>
        <!-- <el-form-item label="禁止报警时间" prop="noAlarmTime">
          <el-date-picker
            clearable
            v-model="form.noAlarmTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择禁止报警时间"
          >
          </el-date-picker>
        </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 {
  listEquip,
  getEquip,
  delEquip,
  addEquip,
  updateEquip,
} from "@/api/project/equip";

export default {
  name: "Equip",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      equipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        devicetype: null,
        equipno: null,
        pageNum: 1,
        pageSize: 10,

        // equipname: null,
        // installationdata: null,
        // holeno: null,
        // eqdanwei: null,

        // onelevelalarm: null,
        // twolevelalarm: null,
        // threelevelalarm: null,
        // fourlevelalarm: null,
        // initial: null,
        // ipaddress: null,
        // eqtype: null,
        // addtime: null,
        // xiangdaono: null,
        // duanmianno: null,
        // virtual: null,
        // channel: null,
        // noAlarmTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipno: [
          { required: true, message: "设备编号不能为空", trigger: "blur" },
        ],
        equipname: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        initial: [
          { required: true, message: "传感器初始值不能为空", trigger: "blur" },
        ],
      },
      typeList: [
        {
          value: "内部位移",
        },
        {
          value: "裂缝",
        },
        {
          value: "锚杆",
        },

        {
          value: "钢筋应力",
        },
        {
          value: "渗压",
        },
        {
          value: "衬砌内应力",
        },
      ],
      hexieList: [
        {
          lable: "是",
          value: "1",
        },
        {
          lable: "否",
          value: "0",
        },
      ],
      devtypeList: [
        {
          lable: "IP",
          value: "IP",
        },
        {
          lable: "4G",
          value: "4G",
        },
      ],
      valuetype: "",
      valuehexie: "",
      valuedevtype: "",
    };
  },
  created(){
    this.getList();
  },
  filters: {
    hexie(v) {
      let value = v;
      let val;
      if (value == 1) {
        return (val = "是");
      } else {
        val = "否";
      }
      return val;
    },
  },
  methods: {
    geteqno(v) {
      if (v !== "") {
        this.queryParams.equipno = v;
      } else {
        delete this.queryParams.equipno;
      }
    },
    gettype(v) {
      if (v !== "") {
        this.queryParams.devicetype = v;
      } else {
        delete this.queryParams.devicetype;
      }
    },
    gethexie(v) {
      console.log(v);

      if (v !== "") {
        this.form.virtual = v;
      } else {
        this.form.virtual = null;
      }
    },
    getdevtype(v) {
      console.log(v);
      if (v !== "") {
        this.form.eqtype = v;
      } else {
        this.form.eqtype = null;
      }
    },
    gettype1(v) {
      if (v !== "") {
        this.form.devicetype = v;
      } else {
        this.form.devicetype = null;
      }
    },
    /** 查询设备信息列表 */
    getList() {
      this.loading = true;
      listEquip(this.queryParams).then((response) => {
        this.equipList = response.rows;

        this.total = response.total;
        this.loading = false;
        console.log(response.rows);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipno: null,
        equipname: null,
        installationdata: null,
        holeno: null,
        eqdanwei: null,
        devicetype: null,
        onelevelalarm: null,
        twolevelalarm: null,
        threelevelalarm: null,
        fourlevelalarm: null,
        initial: null,
        ipaddress: null,
        eqtype: null,
        addtime: null,
        xiangdaono: null,
        duanmianno: null,
        virtual: null,
        channel: null,
        noAlarmTime: 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;
      getEquip(id).then((response) => {
        this.valuetype = response.data.devicetype;
        this.valuehexie = response.data.virtual;
        this.valuedevtype = response.data.eqtype;
        this.form = response.data;
        this.open = true;
        this.title = "修改设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEquip(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquip(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 delEquip(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "project/equip/export",
        {
          ...this.queryParams,
        },
        `equip_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
