<!-- 物联硬件 -->
<template>
  <div class="app-container iotHardware">
    <div class="page-container iotHardwarePage" v-if="pageType === 1">
      <div class="class-list-max-box scrollbar-x-box">
        <div class="class-for-box"
             :class="classTypeId == item.typeId?'check-class-box':''"
             @click="checkClassClick(item.typeId)"
             v-for="(item,index) in classList" :key="index">
          <p class="class-for-title-box">{{item.typeName}}</p>
          <div>
            <p v-if="item.total>0">总数：{{item.total}}</p>
            <p>在线：{{item.online}}</p>
            <p v-if="item.typeId">离线：{{item.offline}}</p>
          </div>
        </div>
      </div>
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="searchValue" label-width="50px">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="请输入名称"
              style="width: 140px"
            />
          </el-form-item>
          <el-form-item label="" prop="schoolId">
            <el-select v-model="queryParams.schoolId" @change="changeSchool"
                       placeholder="请选择校区" style="width: 150px">
              <el-option v-for="(item,index) in schoolOption"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="buildId">
            <el-select v-model="queryParams.buildId" @change="buildSchool"
                       placeholder="请选择楼栋" style="width: 150px">
              <el-option v-for="(item,index) in buildOption"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="subjectId">
            <el-select v-model="queryParams.subjectId"
                       placeholder="请选择实验室" style="width: 150px">
              <el-option v-for="(item,index) in subjectOption"
                         :key="item.subId"
                         :label="item.subName"
                         :value="item.subId"/>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="state">
            <el-select v-model="queryParams.state"
                       placeholder="请选择状态" style="width: 140px">
              <el-option label="启用" :value="true"/>
              <el-option label="停用" :value="false"/>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="online">
            <el-select v-model="queryParams.online"
                       placeholder="是否在线" style="width: 140px">
              <el-option label="在线" :value="true"/>
              <el-option label="离线" :value="false"/>
            </el-select>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="tableButton(1)"
             v-hasPermiRouter="['iot:device:add']"
          >新增</p>
          <p class="page-save-common-style-button"
             style="float: right;margin-right:20px;"
             @click="tableButton(7)"
             v-hasPermiRouter="['iot:device:batch']"
          >批量新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="名称" prop="deviceName"  show-overflow-tooltip/>
          <el-table-column label="编号" prop="deviceNo" width="300" show-overflow-tooltip/>
          <el-table-column label="设备类型" prop="typeName" width="120" show-overflow-tooltip/>
          <!--<el-table-column label="网关" align="left" prop="gatewayNo" width="140" show-overflow-tooltip/>-->
          <el-table-column label="位置" width="300" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.collegeName}} {{scope.row.buildName}} {{scope.row.floorName}} {{scope.row.subjectName}} {{scope.row.room}}</template>
          </el-table-column>
          <el-table-column label="是否在线" align="left" prop="online" width="100" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.online?'在线':'离线'}}</template>
          </el-table-column>
          <el-table-column label="状态" align="left" prop="state" width="100" show-overflow-tooltip>
            <template slot-scope="scope">{{scope.row.state?'启用':'停用'}}</template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" prop="createTime" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="left" width="200" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['iot:device:detail']"
                >详情</p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['iot:device:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['iot:device:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <info-page :propsData="propsData" v-if="pageType === 2"></info-page>
    <add-page :propsData="propsData" v-if="pageType === 3"></add-page>
    <batch-add-page :propsData="propsData" v-if="pageType === 4"></batch-add-page>
  </div>
</template>
<script>
  import { iotDeviceList,iotDeviceDetail,iotDeviceDeviceCount,iotDeviceDelete } from "@/api/iotDevice/index";
  import { systemBuildingGetTreeList,laboratorySubRelInfoGetListByFloor } from "@/api/commonality/permission";
  import addPage from "./addPage.vue";
  import infoPage from "./infoPage.vue";
  import batchAddPage from "./batchAddPage.vue";
  export default {
    name: 'index',
    components: {
      addPage,
      infoPage,
      batchAddPage,
    },
    data () {
      return {
        tableButtonType:this.hasPermiDom(['iot:device:detail','iot:device:edit','iot:device:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          schoolId:null,
          buildId:null,
          subjectId:null,
          state :null,
          online :null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //组件传参
        propsData:{},
        //设备类型列表
        classList:[],
        classTypeId:null,
        //校区楼栋原始数据
        addressList:[],
        //校区下拉列表
        schoolOption:[],
        //楼栋下拉列表
        buildOption:[],
        //实验室下拉列表
        subjectOption:[],
      }
    },
    created () {

    },
    mounted () {
      this.systemBuildingGetTreeList();
      this.iotDeviceDeviceCount();
      this.getList();
    },
    methods: {
      //类型选中
      checkClassClick(type){
        this.$set(this,'classTypeId',type);
        this.getList();
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'buildOption',[]);
        this.$set(this,'subjectOption',[]);
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          schoolId:null,
          buildId:null,
          subjectId:null,
          state :null,
          online :null,
        });
        this.getList();
      },
      //获取设备数量
      iotDeviceDeviceCount(){
        iotDeviceDeviceCount({}).then(response => {
          this.$set(this,'classList',response.data);
        });
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        obj.typeId = this.classTypeId;
        iotDeviceList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //新增
          this.$set(this,'pageType',3);
          this.$set(this,'propsData',{});
        }else if(type == 2){
          //详情
          iotDeviceDetail({id:row.id}).then(response => {
            let obj = JSON.parse(JSON.stringify(response.data))
            this.$set(this,'propsData',obj);
            this.$set(this,'pageType',2);
          });
        }else if(type == 3){
          //编辑
          iotDeviceDetail({id:row.id}).then(response => {
            let obj = JSON.parse(JSON.stringify(response.data))
            this.$set(this,'propsData',obj);
            this.$set(this,'pageType',3);
          });
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            iotDeviceDelete({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //启用&停用
          let text = row.state  ? "停用" : "启用";
          this.$confirm('是否确认' + text + '?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            stateFunction({id:row.id,state:!row.state,}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 6){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }else if(type == 7){
          //新增
          this.$set(this,'pageType',4);
          this.$set(this,'propsData',{});
        }
      },
      //获取校区
      systemBuildingGetTreeList(){
        systemBuildingGetTreeList({}).then(response => {
          let list = [];
          for(let i=0;i<response.data.length;i++){
            list.push({
              id:response.data[i].id,
              name:response.data[i].name,
            })
          }
          this.$set(this,'schoolOption',list);
          this.$set(this,'addressList',response.data);
        })
      },
      //校区选中
      changeSchool(val){
        let self = this;
        let list = [];
        for(let i=0;i<self.addressList.length;i++){
          if(val == self.addressList[i].id && self.addressList[i].buildFloorVoList[0]){
            for(let o=0;o<self.addressList[i].buildFloorVoList.length;o++){
              list.push({
                id:self.addressList[i].buildFloorVoList[o].id,
                name:self.addressList[i].buildFloorVoList[o].name,
              })
            }
          }
        }
        this.$set(this.queryParams,'buildId',null);
        this.$set(this.queryParams,'subjectId',null);
        this.$set(this,'buildOption',list);
        this.$set(this,'subjectOption',[]);
      },
      //楼栋选中
      buildSchool(val){
        laboratorySubRelInfoGetListByFloor({buildId:val}).then(response => {
          this.$set(this.queryParams,'subjectId',null);
          this.$set(this,'subjectOption',response.data);
        })
      },
    },
  }
</script>
<style scoped lang="scss">
  .iotHardware{
    .iotHardwarePage{
      .class-list-max-box{
        padding:20px;
        .class-for-box{
          width:180px;
          height:90px;
          display: inline-block;
          border:1px solid #eeeeee;
          border-radius:8px;
          margin:0 20px 0 0;
          /*box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);*/
          cursor: pointer;
          .class-for-title-box{
            text-align: center;
            margin:20px 0 10px;
            line-height:20px;
            font-size:16px;
            font-weight:700;
            color:#999;
          }
          div{
            display: flex;
            p{
              flex:1;
              text-align: center;
              font-size:14px;
              color:#333;
            }
          }
        }
        .check-class-box{
          border:1px solid rgba(1,131,250,0.4);
          box-shadow: 0 0 8px 2px rgba(1,131,250,0.4);
          .class-for-title-box{
            color:#0183FA;
          }
        }
      }
    }
  }
</style>
