<!--设备管理页-->
<template>
  <div class="app-container iot-device">
    <component
      :is="componectVal"
      :isTenant="false"
      :sourceId="sourceId"
      @toTableClick="toTableClick"
    ></component>
    <div v-show="componectVal === ''">
      <el-form
        v-show="showSearch"
        ref="queryForm"
        :inline="true"
        :model="queryParams"
        class="main-search-card"
        label-width="68px"
      >
        <el-form-item label="所属项目" prop="inProject">
          <el-select
            v-model="queryParams.inProject"
            clearable
            placeholder="请选择所属项目"
            size="small"
            @change="changeProject"
          >
            <el-option
              v-for="(item, index) in inProjectList"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="queryParams.inProject"
          label="设备组"
          prop="groupId"
        >
          <el-select
            v-model="queryParams.groupId"
            clearable
            placeholder="请选择设备组"
            size="small"
          >
            <el-option
              v-for="(item, index) in projectGroupList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="型号名称" prop="modelName">
          <el-input
            v-model="queryParams.modelName"
            clearable
            placeholder="请输入型号名称"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="queryParams.deviceName"
            clearable
            placeholder="请输入设备名称"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="设备Key" prop="deviceKey">
          <el-input
            v-model="queryParams.deviceKey"
            clearable
            placeholder="请输入设备Key"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="设备状态" prop="deviceState">
          <el-select
            v-model="queryParams.deviceState"
            clearable
            placeholder="请选择设备状态"
            size="small"
            @change="handleQuery"
          >
            <el-option
              v-for="(keys, vals) in deviceStatusOpt"
              :key="vals"
              :label="keys"
              :value="vals"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select
            v-model="queryParams.deviceType"
            clearable
            placeholder="请选择设备类型"
            size="small"
            @change="handleQuery"
          >
            <el-option
              v-for="(keys, vals) in deviceTypeList"
              :key="vals"
              :label="keys"
              :value="vals"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            size="mini"
            type="primary"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <div class="main-content-card">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['iot:device:add']"
              icon="el-icon-plus"
              plain
              size="mini"
              type="primary"
              @click="handleAdd"
            >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              icon="el-icon-download"
              plain
              size="mini"
              type="warning"
              @click="handleExport"
            >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            :view-type="viewType"
            @changeViewType="changeViewType"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-if="viewType!='card'"
          v-loading="loading"
          :data="deviceList"
          :default-sort="{ prop: 'createTime', order: 'descending' }"
          @sort-change="sortChange"
        >
          <el-table-column
            :index="indexFormatter"
            align="center"
            label="序号"
            type="index"
            width="60px"
          ></el-table-column>
          <el-table-column align="center" label="设备key" prop="deviceKey" />
          <el-table-column align="center" label="设备名称" prop="deviceName">
            <template slot-scope="scope">
              <el-link
                :underline="false"
                type="primary"
                @click="handleDetails(scope.row)"
              >{{ scope.row.deviceName }}</el-link
              >
            </template>
          </el-table-column>
          <el-table-column align="center" label="所属型号" prop="modelName" />
          <el-table-column align="center" label="所属项目" prop="inProjectName" />
          <el-table-column
            align="center"
            label="设备码"
            min-width="120"
            prop="deviceState"
            width="60"
          >
            <template slot-scope="scope">
              <svg-icon
                icon-class="qrcode"
                style="color: #1890FF;cursor: pointer"
                @click="lookQrCode(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="设备类型"
            prop="deviceTypeName"
          />
          <!-- <el-table-column
            label="设备标签"
            align="left"
            width="120px"
            :formatter="statusFormat"
            prop="deviceTag"
          /> -->
          <!-- <el-table-column label="父设备" align="left" prop="parentName" /> -->
          <el-table-column align="center" label="设备状态" prop="deviceState">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.deviceState === 'ONLINE'" type="success"
              >在线</el-tag
              >
              <el-tag
                v-else-if="scope.row.deviceState === 'OFFLINE'"
                type="danger"
              >离线</el-tag
              >
              <el-tag
                v-else-if="scope.row.deviceState === 'OUTLINE'"
                type="warning"
              >脱线</el-tag
              >
              <el-tag v-else-if="scope.row.deviceState === 'UNACTIVE'" type="warning"
              >未激活</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column align="center" label="设备组" prop="groupName" />
          <el-table-column align="center" label="设备标签" prop="deviceLabel">
            <template slot-scope="scope">
              <el-tag
                v-for="(val, i) in scope.row.deviceLabel"
                :key="i"
                size="mini"
                style="margin-right: 5px;margin-bottom: 5px"
              >{{ val }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            sortable="custom"
            width="160px"
          />
          <el-table-column
            align="center"
            class-name="small-padding fixed-width"
            label="操作"
            width="200px"
          >
            <template slot-scope="scope">
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-search"
                v-if="scope.row.deviceType === 'GATEWAY_CONTROLLER'"
                @click="handleDetails(scope.row)"
              >子设备管理</el-button> -->
              <el-button
                icon="el-icon-search"
                size="mini"
                type="text"
                @click="handleDetails(scope.row)"
              >详情</el-button
              >
              <el-button
                v-hasPermi="['iot:device:edit']"
                icon="el-icon-edit"
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
              >修改</el-button
              >
              <el-button
                v-if="scope.row['parentId'] && scope.row['parentId'] !== '0'"
                v-hasPermi="['iot:device:edit']"
                icon="el-icon-error"
                size="mini"
                type="text"
                @click="handleDisassociate(scope.row)"
              >解绑</el-button
              >

              <el-button
                v-else
                v-hasPermi="['iot:device:remove']"
                icon="el-icon-delete"
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
              >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div v-else class="card-list-box">
          <el-row :gutter="20">
            <el-col v-for="(item, index) in deviceList" :key="index" :lg="8" :md="12" :sm="12" :xs="24" class="card-item-col">
              <div class="device-card">
                <div class="card-header">
                  <div class="device-img-box">
                    <el-image
                      :preview-src-list="item.deviceImage?[imgPath+item.deviceImage]:[deviceCardImgUrl]"
                      :src="item.deviceImage?imgPath+item.deviceImage:deviceCardImgUrl"
                      style="width: 100%; height: 100%">
                    </el-image>
                  </div>
                  <div class="card-header-right">
                    <div class="card-header-right-top">
                      <div class="device-name">{{ item.deviceName }}</div>
                      <div>
                        <svg-icon
                          icon-class="qrcode"
                          style="color: #1890FF;cursor: pointer;width: 18px;height: 18px"
                          @click="lookQrCode(item)"
                        />
                      </div>
                    </div>
                    <div class="card-header-right-content">
                      <el-tag :type="item.deviceState === 'ONLINE' ? 'success' : item.deviceState === 'OFFLINE' ? 'danger' :'warning'" class="status-tag" size="mini">
                        {{ item.deviceState === 'ONLINE' ? '在线' : (item.deviceState === 'OFFLINE' ? '离线' : (item.deviceState === 'OUTLINE' ? '脱线' : '未激活')) }}
                      </el-tag>
                      <el-tag
                        v-for="(val, i) in item.deviceLabel"
                        :key="i"
                        class="status-tag"
                        size="mini"
                        type="info"
                      >{{ val }}</el-tag
                      >
                    </div>
                  </div>
                </div>
                <div class="card-content">
                  <div class="info-item">
                    <span class="label">Key：</span>
                    <span class="value">{{ item.deviceKey }}</span>
                  </div>
                  <div class="info-item">
                    <span class="label">设备组：</span>
                    <el-tooltip :content="item.groupName" effect="dark" placement="top">
                      <span class="value">{{ item.groupName }}</span>
                    </el-tooltip>

                  </div>
                  <div class="info-item">
                    <span class="label">型号：</span>
                    <el-tooltip :content="item.modelName" effect="dark" placement="top">
                      <span class="value">{{ item.modelName }}</span>
                    </el-tooltip>
                  </div>
                  <div class="info-item">
                    <span class="label">所属项目：</span>
                    <el-tooltip v-if="item.inProjectName" :content="item.inProjectName" effect="dark" placement="top">
                      <span class="value">{{ item.inProjectName || '-' }}</span>
                    </el-tooltip>
                    <span v-else class="value">{{ item.inProjectName || '-' }}</span>
                  </div>
                  <div class="info-item">
                    <span class="label">类型：</span>
                    <span class="value">{{ item.deviceTypeName }}</span>
                  </div>
                  <div class="info-item">
                    <span class="label">创建时间：</span>
                    <el-tooltip :content="item.createTime" effect="dark" placement="top">
                      <span class="value">{{ item.createTime }}</span>
                    </el-tooltip>
                  </div>
                </div>
                <div class="card-footer">
                  <el-button icon="el-icon-search" type="text" @click="handleDetails(item)">查看详情</el-button>
                  <el-button v-hasPermi="['iot:device:edit']" type="text"><el-divider direction="vertical"></el-divider></el-button>
                  <el-button v-hasPermi="['iot:device:edit']" icon="el-icon-edit" type="text" @click="handleUpdate(item)">修改</el-button>
                  <el-button v-hasPermi="['iot:device:edit','iot:device:remove']" type="text"><el-divider direction="vertical"></el-divider></el-button>
                  <el-button v-if="item['parentId'] && item['parentId'] !== '0'" v-hasPermi="['iot:device:edit']" icon="el-icon-error" type="text" @click="handleDisassociate(item)">解绑</el-button>
                  <el-button v-else v-hasPermi="['iot:device:remove']" icon="el-icon-delete" type="text" @click="handleDelete(item)">删除</el-button>
                </div>
              </div>
            </el-col>
          </el-row>
          <el-empty v-if="total === 0"></el-empty>
        </div>
        <pagination
          v-show="total > 0"
          :limit.sync="queryParams.pageSize"
          :page-sizes="[12, 24, 36, 60]"
          :page.sync="queryParams.pageNum"
          :total="total"
          @pagination="getList"
        />
      </div>



      <!-- 添加或修改设备对话框 -->
      <div style="height: 0px;">
        <dialog-template
          :close-on-click-modal="false"
          :title="title"
          :visible="open"
          class="eldialog-wrap"
          width="500px"
          @close="open = false"
        >
          <el-form
            ref="form"
            slot="dialog-center"
            :model="form"
            :rules="rules"
            label-width="100px"
          >
            <el-form-item label="所属型号：" prop="modelId">
              <e-dialog-table-input
                v-model="form.modelName"
                :refurbishFun="listModel"
                :table="selectModelTable"
                fileName="modelId,modelName,deviceType,prodKey"
                psize="small"
                showFileName="modelName"
                title="型号"
                @change="handleModelDialogEvent"
              />
            </el-form-item>

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

            <el-form-item
              v-if="
                form.deviceType === 'MINIATURE_BREAKER' ||
                  form.deviceType === 'MOLDED_BREAKER' ||
                  form.deviceType === 'DOOR_SENSOR'
              "
              label="父设备："
              prop="parentId"
            >
              <e-dialog-table-input
                v-model="form.parentName"
                :refurbishFun="listDevice"
                :table="selectDeviceTable"
                fileName="deviceId,deviceName"
                psize="small"
                showFileName="deviceName"
                title="设备"
                @change="handleDeviceDialogEvent"
              />
            </el-form-item>

            <el-form-item
              v-if="form.deviceType === 'MINIATURE_BREAKER'"
              label="线路类型："
              prop="lineType"
            >
              <el-select
                v-model="form.lineType"
                clearable
                placeholder="请选择线路类型"
                style="width: 100%"
              >
                <el-option
                  v-for="(keys, vals) in lineTypeOpt"
                  :key="vals"
                  :label="keys"
                  :value="vals"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="设备名称：" prop="deviceName">
              <el-input
                v-model="form.deviceName"
                placeholder="请输入设备名称"
              />
            </el-form-item>

            <el-form-item label="设备KEY：" prop="deviceKey">
              <el-input
                v-model="form.deviceKey"
                :disabled="form.deviceId"
                placeholder="请输入设备KEY"
              />
            </el-form-item>

            <el-form-item
              v-if="
                form.deviceType !== 'VIDEO_CONTROLLER' &&
                  form.deviceType !== 'GATEWAY_CONTROLLER'
              "
              label="设备标签："
              prop="deviceTag"
            >
              <el-select
                v-model="form.deviceTag"
                clearable
                filterable
                multiple
                placeholder="请选择设备标签"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in deviceTagList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="参数设置：">
              <span style="color: red; font-size: 12px"
                >*注意：锁定即参数不可修改；未锁则可以修改。</span
              >
              <div class="form-params-wrap">
                <param-wrap v-if="open" ref="paramWrap" typeKeys></param-wrap>
              </div>
            </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>
    </div>
<!--    <div-->
<!--      v-show="componectVal !== ''"-->
<!--      :class="-->
<!--        componectVal === 'GatewayDetail'-->
<!--          ? 'to-home-wrap2 gateway-wrap'-->
<!--          : 'to-home-wrap2 '-->
<!--      "-->
<!--      @click="toTableClick"-->
<!--    >-->
<!--      <el-button circle icon="el-icon-d-arrow-left" title="返回列表"-->
<!--        >返回列表</el-button-->
<!--      >-->
<!--    </div>-->
    <el-dialog :visible.sync="imgModelShow" title="设备二维码" width="400px">
      <img ref="qrCode" height="100%" width="100%" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listDevice,
  getDevice,
  delDevice,
  addDevice,
  updateDevice,
  exportDevice,
  listDeviceTypeList,
  disassociateChild
} from "@/api/iot/device";
import { listModel, getModel } from "@/api/iot/model";
import DetailsWrap from "./profile/details";
import ParamWrap from "@/components/ParamWrap/deviceParam";
import GatewayDetail from "@/views/profile/DeviceDetailsView/index";
import EDialogTableInput from "@/components/EDialogTableInput";
import DialogTemplate from "@/components/DialogTemplate";
import JsBarcode from "jsbarcode";
import { getProjectGroupList, listProject } from "@/api/tenant/project";
import deviceCardImgUrl from "@/assets/images/device/device-card.png";

const deviceStatusOpt = {
  ONLINE: "在线",
  OFFLINE: "离线",
  OUTLINE: "脱线",
  UNACTIVE: "未激活"
};
const lineTypeOpt = {
  MAIN: "总路",
  BRANCH: "支路"
};

const selectModelTable = {
  otherOption: {
    tableType: "model"
  },
  queryOption: {
    disable: false,
    labelWidth: "68px",
    params: {
      protocolType: "",
      modelName: ""
    },
    inline: true,
    queryChilds: [
      {
        style: "",
        placeholder: "型号名称",
        clearable: true,
        label: "型号名称",
        type: "input",
        key: "modelName",
        size: "small",
        value: ""
      },
      {
        style: "",
        placeholder: "协议类型",
        clearable: true,
        label: "协议类型",
        type: "select",
        key: "protocolType",
        size: "small",
        value: "",
        options: [
          // {
          //   key: "IOTOS",
          //   label: "iot平台",
          //   value: "IOTOS"
          // },
          // {
          //   key: "ONENET",
          //   label: "ONENET",
          //   value: "ONENET"
          // },
          {
            key: "OFFICIAL",
            label: "官方平台",
            value: "OFFICIAL"
          }
        ],
        optionKey: {
          key: "key",
          label: "label",
          value: "value"
        }
      }
    ]
  },
  tableOption: {
    loading: false,
    rowKey: "deviceId",
    selection: false,
    maxHeight: "45vh",
    childs: [
      {
        style: "",
        label: "厂商名称",
        type: "",
        prop: "vendorName",
        align: "left",
        width: "200",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "型号名称",
        type: "",
        prop: "modelName",
        align: "left",
        width: "200",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "设备类型",
        type: "",
        prop: "deviceTypeName",
        align: "left",
        width: "120",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "产品PK",
        type: "",
        prop: "prodKey",
        align: "left",
        width: "",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "产品密钥",
        type: "",
        prop: "prodSecret",
        align: "left",
        width: "",
        "show-overflow-tooltip": false,
        tempType: "span"
      }
    ]
  }
};
const selectDeviceTable = {
  otherOption: {
    tableType: "device"
  },
  queryOption: {
    disable: false,
    labelWidth: "68px",
    params: {
      deviceName: "",
      deviceType: "GATEWAY_CONTROLLER"
    },
    inline: true,
    queryChilds: [
      {
        style: "",
        placeholder: "设备名称",
        clearable: true,
        label: "设备名称",
        type: "input",
        key: "deviceName",
        size: "small",
        value: ""
      }
    ]
  },
  tableOption: {
    loading: false,
    rowKey: "deviceId",
    selection: false,
    maxHeight: "45vh",
    childs: [
      {
        style: "",
        label: "所属型号",
        type: "",
        prop: "modelName",
        align: "left",
        width: "",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "设备名称",
        type: "",
        prop: "deviceName",
        align: "left",
        width: "",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "设备Key",
        type: "",
        prop: "deviceKey",
        align: "left",
        width: "",
        "show-overflow-tooltip": false,
        tempType: "span"
      },
      {
        style: "",
        label: "创建时间",
        type: "time",
        prop: "createTime",
        align: "center",
        width: "160",
        "show-overflow-tooltip": false,
        tempType: "span"
      }
    ],
    tableList: {
      type: Array
    }
  }
};

export default {
  name: "systemDevice",
  components: {
    DetailsWrap,
    ParamWrap,
    GatewayDetail,
    EDialogTableInput,
    DialogTemplate
  },
  data() {
    return {
      deviceCardImgUrl,
      //列表显示视图
      viewType: "card",
      deviceStatusOpt,
      selectModelTable,
      selectDeviceTable,
      lineTypeOpt,
      imgModelShow: false,
      qrCodeUrl: "",
      sourceId: "",
      componectVal: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      inProjectList: [],
      // 弹出层标题
      projectGroupList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        modelId: null,
        parentId: null,
        deviceName: null,
        deviceState: null,
        deviceType: null,
        modelName: null,
        inProject: null,
        groupId: null,
        orderByColumn: "createTime",
        isAsc: "desc"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        modelId: [
          { required: true, message: "所属型号不能为空", trigger: "change" }
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: false, message: "父设备不能为空", trigger: "change" }
        ],
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        lineType: [
          { required: false, message: "线路类型不能为空", trigger: "blur" }
        ],
        deviceKey: [
          {
            required: true,
            validator: this.chenking_deviceKey,
            trigger: "blur"
          }
        ],
        deviceTag: [
          { required: true, message: "设备标签不能为空", trigger: "change" }
        ]
      },
      deviceTypeList: {},
      deviceTagList: [],
      imgPath: "",
    };
  },
  created() {
    this.imgPath = (process.env.NODE_ENV === "production") ?  `${window.dasConfig.protocol}${window.dasConfig.ip}${window.dasConfig.port ? ':'+window.dasConfig.port : ''}${window.dasConfig.prodApi ? window.dasConfig.prodApi : ''}` : process.env.VUE_APP_BASE_API;''
    console.log(this.$route);
    if (this.$route.query.deviceState) {
      this.queryParams.deviceState = this.$route.query.deviceState;
    }
    this.init();
    this.getInProjectList();
  },
  methods: {
    listModel,
    listDevice,
    handleModelDialogEvent(e) {
      this.getModelInfoById(e.modelId);
      this.form.modelId = e.modelId;
      this.form.modelName = e.modelName;
      this.form.deviceType = e.deviceType;
      this.form.prodKey = e.prodKey;
      this.deviceTypeChange(this.form.deviceType);
    },
    changeViewType(type) {
      this.viewType=type;
    },
    // 查询所属项目列表
    getInProjectList() {
      listProject({
        pageNum: 1,
        pageSize: 100,
        orderByColumn: "createTime",
        isAsc: "desc"
      }).then(response => {
        this.inProjectList = response.rows;
        if (this.$route.query.projectId) {
          this.queryParams.inProject = parseInt(this.$route.query.projectId);
          this.getList();
        }
      });
    },
    changeProject(e) {
      console.log("changeProject", e);
      this.queryParams.groupId = null;
      this.projectGroupList = [];
      if (e) {
        this.getInProjectGroupList();
      }
    },
    // 查询所属项目列表
    getInProjectGroupList() {
      getProjectGroupList({
        projectId: this.queryParams.inProject
      }).then(response => {
        this.projectGroupList = response.rows || [];
      });
    },
    handleDeviceDialogEvent(e) {
      this.form.parentId = e.deviceId;
      this.form.parentName = e.deviceName;
    },
    lookQrCode(item) {
      this.imgModelShow = true;
      this.$nextTick(() => {
        JsBarcode(this.$refs.qrCode, item.deviceKey, {
          format: "CODE128", //条形码的格式
          width: 2, //线宽
          height: 65, //条码高度
          lineColor: "#000", //线条颜色
          displayValue: true, //是否显示文字
          margin: 3 //设置条形码周围的空白区域
        });
      });
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.deviceTagList, row.deviceTag);
    },
    init() {
      if (this.$route.query["deviceId"]) {
        this.handleDetails(this.$route.query);
      }
      this.getDicts("	device_tag").then(response => {
        this.deviceTagList = response.data;
      });
      this.getDeviceTypeList();
      this.getList();
    },
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    handleDetails(row) {
      this.sourceId = row.deviceId;
      this.componectVal =
        row.deviceType === "GATEWAY_CONTROLLER"
          ? "GatewayDetail"
          : "DetailsWrap";
    },
    // 跳转详情页
    toTableClick() {
      if (this.$route.query["deviceId"]) {
        this.$router.push("/device/device");
      }
      this.componectVal = "";
    },
    // 根据 型号id 获取 型号详情
    getModelInfoById(modelId) {
      getModel(modelId).then(res => {
        this.$refs.paramWrap.setList(res.data.paramList || []);
      });
    },
    deviceTypeChange(val) {
      if (val !== "MINIATURE_BREAKER") {
        this.form.parentId = 0;
        this.form.parentName = "";
      } else if (!val) {
        this.form.parentId = "";
        this.form.parentName = "";
      }
    },
    // 查询设备类型列表
    getDeviceTypeList() {
      listDeviceTypeList().then(response => {
        this.deviceTypeList = response.data;
      });
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        let list = response.rows.map(item => {
          item.deviceImage = item.deviceImage || "";
          return item;
        });
        this.deviceList = list;
        // this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        modelId: null,
        modelName: "",
        parentName: "",
        parentId: null,
        deviceName: null,
        deviceType: null,
        paramList: [],
        deviceKey: "",
        inProject: null,
        groupId: null,
        lineType: undefined,
        deviceTag: ""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids;
      const _this = this;
      getDevice(deviceId).then(response => {
        _this.form = response.data;
        _this.open = true;
        _this.title = "修改设备";
        setTimeout(() => {
          _this.$refs.paramWrap.setList(response.data.paramList || []);
        }, 100);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.paramList = this.$refs.paramWrap.getResult();
          this.form.lineType =
            this.form.deviceType === "MINIATURE_BREAKER"
              ? this.form.lineType
              : undefined;
          if (this.form.deviceId != null) {
            updateDevice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 取消关联 */
    handleDisassociate(row) {
      this.$confirm("是否确认取消父子设备关联?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return disassociateChild({ childIds: [row.deviceId] });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("取消成功");
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$confirm(
        "删除设备将会删除设备相关数据，请谨慎操作，是否继续?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delDevice(deviceIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有设备数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportDevice(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    chenking_deviceKey(rule, value, callback) {
      const isExp = /^[A-Z a-z 0-9 _ - $]{0,36}$/;
      if (this.form.deviceKey && !isExp.test(this.form.deviceKey)) {
        callback(new Error("格式不正确(数字，英文字母大小写，36个字符)！"));
      } else {
        callback();
      }
    }
  }
};
</script>

<style lang="scss">
.iot-device {
  .eldialog-wrap {
    .el-dialog__header {

    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;

      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: 15px;
      left: -90px;
      max-height: 250px;
      overflow: auto;
      padding: 10px;
      border: 1px solid #009688;
      border-radius: 5px;
    }
  }
  .to-home-wrap2 {
    width: 100px;
    height: 30px;
    position: absolute;
    right: 30px;
    top: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    color: #656363;
    font-size: 20px;
    cursor: default;
    .el-button--medium.is-circle {
      width: 25px;
      height: 20px;
      padding: 0;
      background: #f26a6a;
      color: #fff;
      font-size: 16px;
      border-radius: 5px;
      height: 30px;
      width: 100%;
      font-size: 14px;
    }
  }
  .to-home-wrap2:hover {
    color: #1890ff;
    font-size: 30px;
  }
  .gateway-wrap {
    top: 220px;
    display: none;
  }
}
.form-params-wrap::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 8px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 5px;
}
.form-params-wrap::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  box-shadow: inset 0 0 5px #c4c4c4;
  background: #dededea6;
}
.form-params-wrap::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px #f6f6f6;
  border-radius: 10px;
  background: #ffffff;
}
// 卡片视图样式
.card-list-box {
  .card-item-col {
    margin-top: 15px;
  }

  .device-card {
    height: 100%;
    border-radius: 10px;
    border: 1px solid #ebeef5;
    background-color: #fff;
    overflow: hidden;
    color: #303133;
    transition: 0.3s;
    cursor: pointer;
    &:hover {
      box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.2);
      //border: 1px solid #409EFF;
    }

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 20px;
      .device-img-box{
        width: 60px;
        height: 60px;
        img{
          width: 100%;
          height: 100%;
        }
      }
      .card-header-right{
        width: calc(100% - 110px);
        flex: 1;
        padding-left: 10px;
        .card-header-right-top{
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-bottom: 5px;
          .device-name {
            font-size: 16px;
            line-height: 16px;
            font-weight: bold;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: calc(100% - 30px);
          }
        }
        .card-header-right-content{
          .status-tag {
            margin-right: 5px;
          }
        }
      }
      .device-title {
        display: flex;
        flex-direction: column;
        .device-id {
          font-size: 12px;
          color: #909399;
          cursor: pointer;
        }
      }
    }
    .card-content {
      padding: 10px 20px;
      display: flex;
      flex-wrap: wrap;
      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 5px;
        font-size: 11px;
        width: 50%;
        .label {
          color: #909399;
          width: 35px;
        }
        .value {
          width: calc(100% - 35px);
          color: #606266;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        &:nth-child(2n){
          width: 50%;
          .label {
            width: 70px;
          }
        }
      }
    }

    .card-footer {
      display: flex;
      justify-content: space-around;
      align-items: center;
      padding: 10px 15px;
      border-top: 1px solid #ebeef5;

      .el-button {
        padding: 5px 8px;

        &:hover {
          color: #409EFF;
        }
      }
    }
  }
}
</style>
