<template>
  <div class="app-container alarm-type">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="编码" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入报警类型编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="标签">
        <el-input
          v-model="queryParams.tag"
          placeholder="请输入报警标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iot:type:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['iot:type:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iot:type:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:type:export']"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      @sort-change="sortChange"
      :data="alarmTypeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码" align="center" sortable="custom" prop="typeCode" />
      <el-table-column label="名称" align="center" sortable="custom" prop="typeName" />
      <el-table-column label="设备类型" align="center" prop="deviceTypeName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1">启用</span>
          <span v-else>关闭</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.tenantId === tenantId"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:type:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.tenantId === tenantId"
            v-hasPermi="['iot:type: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 class="eldialog-wrap" :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入报警类型名称" />
        </el-form-item>

        <el-form-item label="编码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入编码" />
        </el-form-item>

        <el-form-item label="标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入标签" />
        </el-form-item>

        <el-form-item label="设备类型" prop="deviceType">
          <el-select
            v-model="form.deviceType"
            style="width: 100%;"
            clearable
            placeholder="请输入报警设备类型"
          >
            <el-option
              v-for="(keys, vals) in deviceTypeList"
              :label="keys"
              :value="vals"
              :key="vals"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#dad5d5"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAlarmType,
  getAlarmType,
  delAlarmType,
  addAlarmType,
  updateAlarmType
} from "@/api/alarm/alarmType";
import { listDeviceTypeList } from "@/api/iot/device";
// import { selectedProdmodel } from "@/api/device/prodmodel";
export default {
  name: "AlarmType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 告警类型表格数据
      alarmTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeCode: undefined,
        typeName: undefined,
        tag: undefined,
        orderByColumn: "",
        isAsc: "desc"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "类型名称不能为空", trigger: "blur" }
        ],
        tag: [{ required: true, message: "告警标签不能为空", trigger: "blur" }]
      },
      tenantId: "",
      deviceTypeList: []
    };
  },
  created() {
    this.tenantId = this.$store.getters.tenantId;
    this.getList();
  },
  methods: {
    // 查询设备类型列表
    getDeviceTypeList() {
      listDeviceTypeList().then(response => {
        this.deviceTypeList = response.data;
      });
    },
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    // 获取物模型列表
    getModelList() {
      // selectedProdmodel().then(res => {
      //   this.deviceTypeList = res.data;
      // });
    },
    /** 查询告警类型列表 */
    getList() {
      this.loading = true;
      listAlarmType(this.queryParams).then(response => {
        this.alarmTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceType: undefined,
        typeCode: undefined,
        typeName: undefined,
        status: "1",
        tag: ""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getDeviceTypeList();
      this.open = true;
      this.title = "添加告警类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getDeviceTypeList();
      const typeId = row.typeId || this.ids;
      getAlarmType(typeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.form.status = response.data.status.toString();
        this.title = "修改告警类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.typeId != undefined) {
            updateAlarmType(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAlarmType(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeIds = row.typeId || this.ids;
      this.$confirm(
        // '是否确认删除告警类型编号为"' + typeIds + '"的数据项?',
        "是否删除该选项",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delAlarmType(typeIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "iot/alarmType/export",
        {
          ...this.queryParams
        },
        `iot_alarmType.xlsx`
      );
    }
  }
};
</script>
<style lang="scss">
.alarm-type {
  .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;
      }
    }
    .form-params-wrap {
      height: 100%;
      width: calc(100% + 110px);
      position: relative;
      top: 35px;
      left: -90px;
      max-height: 250px;
      overflow: auto;
      padding: 10px;
      border: 1px solid #009688;
      border-radius: 5px;
    }
  }
}
</style>
