<template>
  <div class="app-container iot-param">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      >
      <el-form-item label="功率限额" prop="powerLimit">
        <el-input
          v-model="queryParams.powerLimit"
          placeholder="请输入功率限额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="电压下限" prop="voltageMin">
        <el-input
          v-model="queryParams.voltageMin"
          placeholder="请输入电压下限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电压上限" prop="voltageMax">
        <el-input
          v-model="queryParams.voltageMax"
          placeholder="请输入电压上限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电流限额" prop="currentLimit">
        <el-input
          v-model="queryParams.currentLimit"
          placeholder="请输入电流限额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="漏电流限" prop="leakageCurrentLimit">
        <el-input
          v-model="queryParams.leakageCurrentLimit"
          placeholder="请输入漏电流限额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="温度限额" prop="temperatureLimit">
        <el-input
          v-model="queryParams.temperatureLimit"
          placeholder="请输入温度限额"
          clearable
          size="small"
          @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="['iot:param: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="['iot:param: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="['iot:param: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="['iot:param:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="paramList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" align="center" prop="index" width="50" />
      <el-table-column label="来源ID" align="center" prop="sourceId" />
      <el-table-column
        label="功率限额(单位:W)"
        align="center"
        prop="powerLimit"
      />
      <el-table-column
        label="电压下限(单位:V)"
        align="center"
        prop="voltageMin"
      />
      <el-table-column
        label="电压上限(单位:V)"
        align="center"
        prop="voltageMax"
      />
      <el-table-column
        label="电流限额(单位:A)"
        align="center"
        prop="currentLimit"
      />
      <el-table-column
        label="漏电流限额(单位:mA)"
        align="center"
        prop="leakageCurrentLimit"
      />
      <el-table-column
        label="温度限额(单位:℃)"
        align="center"
        prop="temperatureLimit"
      />
      <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="['iot:param:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:param: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"
    />

    <!-- 添加或修改设备参数对话框 -->
    <dialog-template class="eldialog-wrap" :title="title" @close="open = false" :visible="open" width="500px">
      <el-form ref="form" slot="dialog-center" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="功率限额(单位:W)" prop="powerLimit">
          <el-input
            v-model="form.powerLimit"
            placeholder="请输入功率限额(单位:W)"
          />
        </el-form-item>
        <el-form-item label="电压下限(单位:V)" prop="voltageMin">
          <el-input
            v-model="form.voltageMin"
            placeholder="请输入电压下限(单位:V)"
          />
        </el-form-item>
        <el-form-item label="电压上限(单位:V)" prop="voltageMax">
          <el-input
            v-model="form.voltageMax"
            placeholder="请输入电压上限(单位:V)"
          />
        </el-form-item>
        <el-form-item label="电流限额(单位:A)" prop="currentLimit">
          <el-input
            v-model="form.currentLimit"
            placeholder="请输入电流限额(单位:A)"
          />
        </el-form-item>
        <el-form-item label="漏电流限额(单位:mA)" prop="leakageCurrentLimit">
          <el-input
            v-model="form.leakageCurrentLimit"
            placeholder="请输入漏电流限额(单位:mA)"
          />
        </el-form-item>
        <el-form-item label="温度限额(单位:℃)" prop="temperatureLimit">
          <el-input
            v-model="form.temperatureLimit"
            placeholder="请输入温度限额(单位:℃)"
          />
        </el-form-item>
      </el-form>
      <div slot="dialog-footer"  class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </dialog-template>
  </div>
</template>

<script>
import {
  listParam,
  getParam,
  delParam,
  addParam,
  updateParam,
  exportParam
} from "@/api/iot/param";
import DialogTemplate from "@/components/DialogTemplate";

export default {
  name: "Param",
  components: {DialogTemplate},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备参数表格数据
      paramList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        powerLimit: null,
        voltageMin: null,
        voltageMax: null,
        currentLimit: null,
        leakageCurrentLimit: null,
        temperatureLimit: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备参数列表 */
    getList() {
      this.loading = true;
      listParam(this.queryParams).then(response => {
        this.paramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sourceId: null,
        powerLimit: null,
        voltageMin: null,
        voltageMax: null,
        currentLimit: null,
        leakageCurrentLimit: null,
        temperatureLimit: 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.sourceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sourceId = row.sourceId || this.ids;
      getParam(sourceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sourceId != null) {
            updateParam(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParam(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sourceIds = row.sourceId || this.ids;
      this.$confirm("是否删除该选项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delParam(sourceIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有设备参数数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportParam(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
<style lang="scss">
.iot-model {
  .eldialog-wrap {
    .el-dialog__header {
      border-bottom: 1px solid #747373;
    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;
      border-top: 1px solid #747373;
      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
  }
}
</style>
