<template>
  <div class="iot-child-device">
    <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>
        <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-table v-loading="loading" :data="deviceList">
      <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="deviceStatus">
        <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" 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>
</template>
<script>
import {
  listDevice
} from "@/api/personal/device";
import { listDeviceTypeList } from "@/api/tenant/device";
const lineTypeOpt = {
  MAIN: "总路",
  BRANCH: "支路"
};
export default {
  name: "",
  props: ["sourceId", "pDevcieInfo"],
  components: {
  },
  data() {
    return {
      lineTypeOpt,
      selectTableShow: false,
      tableSelectOption: {},
      selectResult: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelId: null,
        parentId: null,
        deviceCode: null,
        deviceName: null,
        deviceStatus: null,
        deviceKey: null,
        deviceType: null,
        parentId: ""
      },
      open: false,
      title: "",
      showSearch: true,
      // 设备表格数据
      deviceList: [],
      total: 0,
      form: {},
      deviceTypeList: []
    };
  },
  created() {
    this.getDeviceTypeList();
    this.getList();
  },
  methods: {
    // 查询设备类型列表
    getDeviceTypeList() {
      listDeviceTypeList().then(response => {
        this.deviceTypeList = response.data;
      });
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.parentId = this.sourceId;
      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();
    },
    handleDetails(row) {
      if (row.deviceId) {
        this.$emit("toChildEvent", { deviceId: row.deviceId });
      }
    },
  }
};
</script>
