<template>
  <div class="page">
    <LayoutTable :data="tableData" :config="tableConfig">
      <el-tag slot="table-body-head" style="margin: 5px" type="success"
        >当前在线设备总计：{{ tableData.onlineCount }}个</el-tag
      >

      <el-tag slot="table-body-head" style="margin: 5px" type="danger"
        >当前离线设备总计：{{ tableData.offlineCount }}个</el-tag
      >

      <el-tag
        v-for="($label, $value) in tableData.offlineDeviceType"
        :key="$value"
        :label="$value"
        slot="table-body-head"
        style="margin: 5px"
        type="danger"
        >{{ $value }}离线设备：{{ $label }}个</el-tag
      >
    </LayoutTable>

    <dialog-show ref="dialogform" @ok="getData" />
  </div>
</template>

<script>
/** 表单弹出框模式需引入 */
import dialogShow from "./dialogshow";
import table from "@/assets/mixins/table";
export default {
  name: "Device",
  components: { dialogShow },
  mixins: [table],
  created() {},
  methods: {
    /** 重写新增方法 */
    toAdd(row) {
      this.$refs.dialogform.add(row);
    },
    /** 重写编辑方法 */
    toEdit(row) {
      this.$refs.dialogform.edit(row);
    },
    /** 重写查看方法 */
    toView(row) {
      this.$refs.dialogform.view(row);
    },

    downMsg(row) {
      this.$prompt("请输入下发消息内容", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          this.$post("/device/downMsg", {
            deviceId: row.id,
            content: value,
          })
            .then((res) => {
              if (res.code == 1) {
                this.$message.success("下发设备成功！");
                this.getData();
              }
            })
            .catch((error) => {
              this.$message.error(error.message);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },

    activeDevice(row) {
      this.$post("/device/save", {
        "entity.id": row.id,
        "entity.active": 1,
      })
        .then((res) => {
          if (res.code == 1) {
            this.$message.success("激活设备成功！");
            this.getData();
          }
        })
        .catch((error) => {
          this.$message.error(error.message);
        });
    },
  },
  data() {
    return {
      config: {
        getsocketData: null,
        search: [
          {
            name: "deviceNum",
            type: "text",
            label: "设备编号",
          },
          {
            name: "deviceOnlineStatus",
            type: "select",
            label: "在线状态",
          },

          {
            name: "deviceType",
            type: "select",
            label: "设备类型",
          },
        ],
        columns: [
          { type: "selection", width: 60 },

          //   { label: "设备名称", prop: "deviceName" },

          { label: "设备编码", prop: "deviceCode" },

          { label: "设备类型", prop: "deviceType", formatter: this.formatter },

          {
            label: "在线状态 ",
            prop: "deviceOnlineStatus",
            formatter: this.formatterYES,
          },

          {
            label: "最近上线时间",
            prop: "onlineTime",
            formatter: this.formatterDate,
          },

          { label: "激活状态", prop: "active", formatter: this.formatterYES },
          {
            label: "操作",
            width: 320,
            formatter: (row) => {
              return (
                <div>
                  <table-buttons
                    noAdd
                    row={row}
                    onEdit={this.toEdit}
                    onView={this.toView}
                    onDel={this.toDel}
                  />
                  <span> </span>
                  {row.active === 0 ? (
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-open"
                      onClick={() => {
                        this.activeDevice(row);
                      }}
                    >
                      激活
                    </el-button>
                  ) : (
                    ""
                  )}
                  <span> </span>

                  {row.active === 1 ? (
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-open"
                      onClick={() => {
                        this.downMsg(row);
                      }}
                    >
                      下发信息
                    </el-button>
                  ) : (
                    ""
                  )}
                </div>
              );
            },
          },
        ],
      },
    };
  },
};
</script>