<template>
  <div class="page">
    <span style="font-size: 13px"
      ><b>设备列表</b> (<el-link
        style="margin-left: 10px"
        type="primary"
        :underline="false"
        >{{ siteName }}</el-link
      >
      站点编码：{{ siteCode }} )</span
    >
    <el-link style="margin-left: 10px" type="primary" :underline="false"
      >地图模式</el-link
    >
    <el-divider></el-divider>
    <LayoutTable :data="tableData" :config="tableConfig">
      <el-button
        slot="table-head-left2"
        style="margin-left: 10px"
        icon="el-icon-tickets"
        size="mini"
        @click="handleImport"
        >导入</el-button
      >
      <el-button
        slot="table-head-left2"
        style="margin-left: 10px"
        icon="el-icon-tickets"
        size="mini"
        @click="doExport"
        :disabled="isExport"
        >导出</el-button
      >
      <el-tag slot="table-head-left2" size="mini" style="margin-left: 20px"
        >设备总数：{{ tableData.totalCount }}台</el-tag
      >

      <el-tag
        slot="table-head-left2"
        size="mini"
        style="margin: 5px"
        type="success"
        >在线：{{ tableData.onlineCount }}台</el-tag
      >

      <el-tag
        slot="table-head-left2"
        size="mini"
        style="margin: 5px"
        type="danger"
        >离线：{{ tableData.offlineCount }}台</el-tag
      >

      <el-tag
        slot="table-head-left2"
        size="mini"
        style="margin: 5px"
        type="danger"
        >停用：{{ tableData.stopCount }}台</el-tag
      >

      <el-tag
        slot="table-head-left2"
        size="mini"
        style="margin: 5px"
        type="danger"
        >未激活：{{ tableData.unActiveCount }}台</el-tag
      >
    </LayoutTable>

    <!-- 设备导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据
          <el-link
            type="primary"
            style="font-size: 14px"
            @click="downloadTemplate"
            >下载模板</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- <dialog-show ref="dialogform" @ok="getData" /> -->

    <drawer-show ref="drawerform" @ok="getData" />

    <drawer-view ref="drawerViewform" @ok="getData" />
  </div>
</template>

<script>
/** 表单弹出框模式需引入 */
import drawerView from "./drawerview";
import drawerShow from "./drawershow";
import table from "@/assets/mixins/table";
export default {
  name: "Device",
  components: { drawerShow, drawerView },
  mixins: [table],
  created() {
    console.log(this.$route);
    this.siteId = this.$route.query.siteId;

    this.$get("/sitestat/siteInfo", {
      siteId: this.siteId,
    })
      .then((res) => {
        if (res.code == 1) {
          console.log("res", res);
          this.siteName = res.data.siteName;
          this.siteCode = res.data.siteCode;
        }
      })
      .catch((error) => {
        this.$message.error(error.message);
      });
  },
  methods: {
    /** 导入 */
    handleImport() {
      this.upload.title = "设备导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    downloadTemplate() {
      this.isExport = true;
      this.$download("/device/downloadTemplate", {}, { type: "excel" })
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getData();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出Excel */
    doExport() {
      this.isExport = true;
      this.$download(
        "/device/exportExcel",
        {
          "idList": this.selection,
          "deviceName": this.$route.query["deviceName"],
          "siteId": this.$route.query["siteId"],
        },
        { type: "excel" }
      )
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },
    /** 重写新增方法 */
    toAdd(row) {
      //this.$refs.dialogform.add(row);
      row.siteId = this.siteId;
      row.siteName = this.siteName;
      row.siteCode = this.siteCode;

      this.$refs.drawerform.add(row);
    },
    /** 重写编辑方法 */
    toEdit(row) {
      this.$refs.dialogform.edit(row);
    },
    /** 重写查看方法 */
    toView(row) {
      this.$refs.drawerViewform.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/active", {
        deviceCode: row.deviceCode,
      })
        .then((res) => {
          if (res.code == 1) {
            this.$message.success("激活设备成功！");
            this.getData();
          }
        })
        .catch((error) => {
          this.$message.error(error.message);
        });
    },
    // 操作菜单状态
    changeStatus(row, column) {
      return (
        <device-switch
          confirm
          url="/device/save"
          row={row}
          onChange={this.statusChange}
          value={this.tableData.data}
          onInput={(data) => {
            this.tableData.data = data;
          }}
        />
      );
    },
  },
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（设备导入）
        open: false,
        // 弹出层标题（设备导入）
        title: "导入设备数据",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 上传的地址
        url: "/m/device/importData",
      },
      isExport: false,
      siteId: null,
      siteName: "",
      siteCode: "",

      config: {
        getsocketData: null,
        search: [
          // {
          //   name: "deviceNum",
          //   type: "text",
          //   label: "设备编号",
          // },
          {
            name: "deviceStatus",
            type: "select",
            label: "状态",
          },

          // {
          //   name: "porductId",
          //   type: "select",
          //   label: "产品类型",
          // },
        ],
        columns: [
          { type: "selection", width: 60 },

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

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

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

          { label: "mac地址", prop: "deviceMac" },

          {
            label: "设备生产商",
            prop: "deviceFirmId",
            formatter: this.formatter,
          },
          { label: "负责人", prop: "leadingOfficial" },
          { label: "联系电话", prop: "leadingOfficialTelephone" },
          {
            label: "状态 ",
            prop: "deviceStatus",
            formatter: this.formatter,
          },

          {
            prop: "enabled",
            label: "启用/停用",
            width: 100,
            formatter: this.changeStatus,
          },

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

          // { label: "激活状态", prop: "active", formatter: this.formatterYES },
          {
            label: "操作",
            width: 240,
            formatter: (row) => {
              return (
                <div>
                  <table-buttons
                    noAdd
                    noEdit
                    row={row}
                    onEdit={this.toEdit}
                    onView={this.toView}
                    onDel={this.toDel}
                  />
                  <span> </span>
                  {row.deviceStatus === 0 ? (
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-open"
                      onClick={() => {
                        this.activeDevice(row);
                      }}
                    >
                      激活
                    </el-button>
                  ) : (
                    ""
                  )}
                  <span> </span>
                </div>
              );
            },
          },
        ],
      },
    };
  },
};
</script>
<style scoped>
.el-divider--horizontal {
  margin: 2px 0;
  background: 0 0;
  border-top: 1px solid #e8eaec;
}
</style>