<template>
  <div class="trigger-list">
    <div>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="关键字" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="关键字"
            clearable
            size="small"
            style="width: 240px"
            @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:trigger:add']"
          >新增</el-button>
        </el-col>
      </el-row>

      <el-table
        v-loading="listLoading"
        :default-sort="{prop: 'createTime', order: 'descending'}"
        @sort-change="sortChange"
        :data="tableList"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
          :index="indexFormatter"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="触发器名称"
          sortable="custom"
          width="250"
          align="left"
          prop="triggerName"
        />
        <el-table-column label="推送内容" align="left" :aria-hidden="true" prop="templateContent" />
        <el-table-column label="状态" align="center" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '0'">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          sortable="custom"
          align="center"
          prop="createTime"
          width="160"
        ></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-document-add"
              @click="handleCopperIn(scope.row, 'copper')"
              v-hasPermi="['iot:trigger:add']"
            >复制新增</el-button>

            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="details(scope.row)"
              v-hasPermi="['iot:trigger:edit']"
            >查看</el-button>-->

            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleCopperIn(scope.row, 'update')"
              v-hasPermi="['iot:trigger:edit']"
            >编辑</el-button>

            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeletes(scope.row)"
              v-hasPermi="['iot:trigger: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"
      />
    </div>

    <div>
      <dialog-template
        title="触发器详情"
        width="40%"
        :visible="triggerDetailsDialog"
        @close="triggerDetailsDialog = false"
        top="5vh"
      >
        <!-- <triggerDetails slot="dialog-center" v-if="triggerDetailsDialog === true" :triggerId="tempModel.triggerId"></triggerDetails> -->
      </dialog-template>

      <!-- 新增触发器dialog -->
      <dialog-template
        class="trigger-d-dialog"
        :close-on-click-modal="false"
        title="新增触发器"
        width="900px"
        :visible="outerVisible"
        @close="outerVisible = false"
        append-to-body
      >
        <component
          slot="dialog-center"
          v-if="outerVisible"
          :is="componectVal"
          :type="childOpt.type"
          :triggerId="childOpt.triggerId"
          @recover="dialogRecover"
        ></component>
      </dialog-template>
    </div>

    <!-- <div
      style="
      position: fixed;
      bottom: 10px;
      right: 10px;
      "
      v-show="componectVal !== ''"
      >
      <el-tooltip class="item" effect="dark" content="前往列表" placement="top">
        <el-button
          type="primary"
          style="
        width: 50px;
        height: 50px;
        font-size: 25px;
        z-index: 100;
        "
          icon="el-icon-back"
          @click="toTableClick"
          circle
        ></el-button>
      </el-tooltip>
    </div> -->
  </div>
</template>
<script>
// import TriggerDetails from "./triggerDetails";
// import { fetchList } from '@/api/admin/dict'
import { listTrigger, delTrigger } from "@/api/iot/trigger";
// // import { openDetailsPage } from '@/util/util'
import InsertTriggerDialog from "./profile/insertTriggerDialog";
import { listAlarmType } from "@/api/alarm/alarmType";
import DialogTemplate from "@/components/DialogTemplate";
export default {
  name: "Rule",
  components: {
    // TriggerDetails,
    InsertTriggerDialog,
    DialogTemplate
  },
  data() {
    return {
      listLoading: false,
      triggerDetailsDialog: false,
      outerVisible: false,
      tempModel: {},
      total: 0,
      queryParams: {
        searchValue: undefined,
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "createTime",
        isAsc: "desc"
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false //是否倒序
      },
      tableList: [],
      alarmTypeList: [],
      // 新增界面和修改界面的掺入参数
      componectVal: "",
      childOpt: {
        type: "create", // create, copper, update
        triggerId: 0 // 触发器id
      }
    };
  },
  watch: {
    $route() {
      console.log("routeUpdate");
    }
  },
  created() {
    this.handleQuery();
    this.getAlaramTypeList();
  },
  methods: {
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },

    toTableClick() {
      this.outerVisible = false;
      this.componectVal = "";
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      for (var i = 0; i < this.alarmTypeList.length; i++) {
        if (this.alarmTypeList[i].typeCode === row.alarmTypeCode) {
          return this.alarmTypeList[i].typeName;
        }
      }
      return "";
    },
    // 新增dialog 无操作 关闭.
    dialogRecover() {
      this.componectVal = "";
      this.outerVisible = false;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.listLoading = true;
      listTrigger(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = parseInt(response.total);
        this.listLoading = false;
      });
    },
    details(row) {
      this.tempModel = row;
      this.triggerDetailsDialog = true;
    },
    // 添加触发器： 弹窗展示添加触发器页面
    handleAdd() {
      this.childOpt.type = "create";
      this.childOpt.triggerId = "";
      this.outerVisible = true;
      this.componectVal = "InsertTriggerDialog";
    },
    handleCopperIn(row, type) {
      this.childOpt.type = type;
      this.childOpt.triggerId = row.triggerId;
      this.outerVisible = true;
      this.componectVal = "InsertTriggerDialog";
    },
    handleDeletes(row, index) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delTrigger(row.triggerId)
          .then(response => {
            this.msgSuccess("删除成功");
            this.getList();
          })
          .catch(err => {
            console.log(err);
          });
      });
    },
    /** 查询告警类型列表 */
    getAlaramTypeList() {
      listAlarmType({ pageNum: 1, pageSize: 9999 }).then(response => {
        this.alarmTypeList = response.rows;
      });
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    }
  }
};
</script>
<style lang="scss">
.trigger-list {
  padding: 20px;
  .title {
    box-sizing: inherit;
    background: #fff;
    padding: 20px 40px;
    font-size: 20px;
    font-weight: 700;
    color: #373d41;
    height: 68px;
  }
}
.trigger-d-dialog {
  .el-dialog__body {
    padding: 0px;
  }
  .el-dialog__header {
    border-bottom: 1px solid #929191;
  }
}
</style>
