<template>
  <div class="app-container iot-device">
    <component :is="componectVal" :sourceId="sourceId"></component>
    <div v-show="componectVal === ''">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备状态" prop="deviceState">
          <el-select v-model="queryParams.deviceState" placeholder="请选择设备状态" clearable size="small">
            <el-option
              :label="keys"
              v-for="(keys, vals) in deviceStatusOpt"
              :key="vals"
              :value="vals"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable size="small">
            <el-option
              :label="keys"
              :value="vals"
              v-for="(keys, vals) in deviceTypeList"
              :key="vals"
            />
          </el-select>
        </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="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="deviceList"
        :default-sort="{prop: 'createTime', order: 'descending'}"
        @sort-change="sortChange"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
          :index="indexFormatter"
          width="80px"
        ></el-table-column>
        <el-table-column label="设备名称" align="left" prop="deviceName" />
        <el-table-column label="所属型号" align="left" prop="modelName" />
        <el-table-column label="设备key" align="left" prop="deviceKey" />
        <el-table-column label="设备类型" align="left" width="120px" prop="deviceTypeName" />
        <el-table-column label="设备状态" align="center" width="120" prop="deviceState">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.deviceState === 'ONLINE'">在线</el-tag>
            <el-tag type="danger" v-else-if="scope.row.deviceState === 'OFFLINE'">离线</el-tag>
            <el-tag type="danger" v-else-if="scope.row.deviceState === 'OUTLINE'">脱线</el-tag>
            <el-tag type="info" v-else-if="scope.row.deviceState === 'UNACTIVE'">未激活</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          sortable="custom"
          width="160px"
          prop="createTime"
        />
        <el-table-column
          label="操作"
          align="center"
          width="200px"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="handleDetails(scope.row)"
            >详情</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 class="to-home-wrap2" @click="toTableClick" v-show="componectVal !== ''">
      <el-button icon="el-icon-d-arrow-left" title="返回列表" circle>返回列表</el-button>
    </div>
  </div>
</template>

<script>
import { listDevice, exportDevice, listDeviceTypeList } from "@/api/personal/device";
// import { listDeviceTypeList } from "@/api/tenant/device";
import DetailsWrap from "./profile/details";
const deviceStatusOpt = {
  ONLINE: "在线",
  OFFLINE: "离线",
  OUTLINE: "脱线",
  UNACTIVE: "未激活"
};
const lineTypeOpt = {
  MAIN: "总路",
  BRANCH: "支路"
};

export default {
  name: "Device",
  components: {
    DetailsWrap
  },
  data() {
    return {
      deviceStatusOpt,
      lineTypeOpt,
      sourceId: "",
      componectVal: "",
      selectTableShow: false,
      tableSelectOption: {},
      selectResult: {},
      queryModelOpt: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelId: null,
        parentId: null,
        deviceName: null,
        deviceState: null,
        deviceType: null,
        orderByColumn: "createTime",
        isAsc: "desc"
      },
      // 表单校验
      deviceTypeList: {}
    };
  },
  created() {
    this.getDeviceTypeList();
    this.getList();
  },
  methods: {
    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 = "DetailsWrap";
    },
    // 跳转详情页
    toTableClick() {
      this.componectVal = "";
    },
    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 => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有设备数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportDevice(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>

<style lang="scss">
.iot-device {
  .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;
    }
  }
  .to-home-wrap2 {
    width: 100px;
    height: 20px;
    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;
  }
}
.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;
}
</style>
