<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="设备编号11" prop="equipno">
        <el-input
          v-model="queryParams.equipno"
          placeholder="请输入设备编号11"
          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="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择安装日期"
        >
        </el-date-picker>
      </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="初始值z" prop="initialz">
        <el-input
          v-model="queryParams.initialz"
          placeholder="请输入初始值z"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初始值y" prop="initialy">
        <el-input
          v-model="queryParams.initialy"
          placeholder="请输入初始值y"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ip地址或dtu编号" prop="ipaddress">
        <el-input
          v-model="queryParams.ipaddress"
          placeholder="请输入设备ip地址或dtu编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="设备码 默认1,2,3，programid 1 4 5"
        prop="initialstateno"
      >
        <el-input
          v-model="queryParams.initialstateno"
          placeholder="请输入设备码 默认1,2,3，programid 1 4 5"
          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 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="['system: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="['system: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="['system: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="['system: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="${comment}" 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="eqdanwei" />
      <el-table-column label="监测项" align="center" prop="devicetype" />
      <el-table-column label="设备类型" align="center" prop="ipaddress" />
      <el-table-column label="初始值x" align="center" prop="initial" />
      <el-table-column label="初始值y" align="center" prop="initialy" />
      <el-table-column label="初始值z" align="center" prop="initialz" />

      <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="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{m}:{s}")
          }}</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="['system:equip:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system: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" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        style="overflow: hidden"
      >
        <el-col :span="11">
          <el-form-item label="设备编号" prop="equipno">
            <el-input
              v-model="form.equipno"
              placeholder="请输入设备编号"
              :disabled="disabledEdit"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="设备名称" prop="equipname">
            <el-input v-model="form.equipname" placeholder="请输入设备名称" />
          </el-form-item>
        </el-col>

        <el-col :span="11">
          <el-form-item label="监测项" prop="devicetype">
            <el-input v-model="form.devicetype" placeholder="请输入监测项" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="安装日期" prop="createTime">
            <el-date-picker
              clearable
              v-model="form.createTime"
              type="datetime"
              placeholder="请选择安装日期"
              @change="addDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="数值单位" prop="eqdanwei">
            <el-input v-model="form.eqdanwei" placeholder="请输入数值单位" />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="一级预警" prop="onelevelalarm">
            <el-input
              v-model="form.onelevelalarm"
              placeholder="请输入一级预警"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="二级预警" prop="twolevelalarm">
            <el-input
              v-model="form.twolevelalarm"
              placeholder="请输入二级预警"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="三级预警" prop="threelevelalarm">
            <el-input
              v-model="form.threelevelalarm"
              placeholder="请输入三级预警"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="四级预警" prop="fourlevelalarm">
            <el-input
              v-model="form.fourlevelalarm"
              placeholder="请输入四级预警"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="初始值X" prop="initial">
            <el-input
              v-model="form.initial"
              placeholder="请输入初始值X"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="初始值y" prop="initialy">
            <el-input
              v-model="form.initialy"
              placeholder="请输入初始值y"
              type="number"
            />
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="初始值z" prop="initialz">
            <el-input
              v-model="form.initialz"
              placeholder="请输入初始值z"
              type="number"
            />
          </el-form-item>
        </el-col>

        <el-col :span="11">
          <el-form-item label="设备类型" prop="ipaddress">
            <el-radio-group v-model="form.ipaddress">
              <el-radio label="4G"></el-radio>
              <el-radio label="ip"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="设备码" prop="initialstateno">
            <el-input
              v-model="form.initialstateno"
              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 {
  listEquip,
  getEquip,
  delEquip,
  addEquip,
  updateEquip,
} from "@/api/bianpo/equip";

export default {
  name: "Equip",
  data() {
    return {
      disabledEdit: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      equipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipno: null,
        equipname: null,
        createTime: null,
        eqdanwei: null,
        devicetype: null,
        onelevelalarm: null,
        twolevelalarm: null,
        threelevelalarm: null,
        fourlevelalarm: null,
        initial: null,
        initialz: null,
        initialy: null,
        ipaddress: null,
        initialstateno: null,
        eqtype: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equipno: [
          { required: true, message: "设备编号不能为空", trigger: "blur" },
        ],
        equipname: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addDate(e) {
      console.log(e);
      let date = new Date(e);
      let myYear = date.getFullYear();
      let myMonth = (date.getMonth() + 1).toString().padStart(2, "0");
      let myDay = date.getDate().toString().padStart(2, "0");
      let myHours = date.getHours().toString().padStart(2, "0");
      let myMinutes = date.getMinutes().toString().padStart(2, "0");
      let mySeconds = date.getSeconds().toString().padStart(2, "0");
      this.form.createTime = `${myYear}-${myMonth}-${myDay} ${myHours}:${myMinutes}:${mySeconds}`;
      console.log(this.form.createTime);
    },
    /** 查询设备信息列表 */
    getList() {
      this.loading = true;
      listEquip(this.queryParams).then((response) => {
        this.equipList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipno: null,
        equipname: null,
        createTime: null,
        eqdanwei: null,
        devicetype: null,
        onelevelalarm: null,
        twolevelalarm: null,
        threelevelalarm: null,
        fourlevelalarm: null,
        initial: null,
        initialz: null,
        initialy: null,
        ipaddress: null,
        initialstateno: null,
        eqtype: 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.disabledEdit = false;
      this.open = true;
      this.title = "新增设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabledEdit = true;
      const id = row.id || this.ids;
      getEquip(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) {
            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(
        "system/equip/export",
        {
          ...this.queryParams,
        },
        `equip_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
