<template>
  <view class="work-container">
    <!-- 轮播图 -->
    <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
      <swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
        <swiper-item v-for="(item, index) in data" :key="index">
          <view class="swiper-item" @click="clickBannerItem(item)">
            <image :src="item.image" mode="aspectFill" :draggable="false" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot>

    <!-- 宫格组件 -->
    <uni-section title="工作台" type="line"></uni-section>
    <view class="grid-body">
      <uni-grid :column="3" :showBorder="false" >
        <uni-grid-item v-if="hasRole(['admin'])">
          <view class="grid-item-box" @click="goPage('user')">
            <u--image :showLoading="true" src="/static/images/icons/user.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">客户列表</text>
          </view>
        </uni-grid-item>
        <uni-grid-item v-if="hasRole(['companymgr'])">
          <view class="grid-item-box" @click="goPage('device')">
            <u--image :showLoading="true" src="/static/images/icons/device.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">设备列表</text>
          </view>
        </uni-grid-item>
        <uni-grid-item>
          <view class="grid-item-box" @click="goPage('wifi')">
            <u--image :showLoading="true" src="/static/images/icons/wifi.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">设备配网</text>
          </view>
        </uni-grid-item>
        <uni-grid-item v-if="hasRole(['admin'])">
          <view class="grid-item-box" @click="goPage('scan')">
            <u--image :showLoading="true" src="/static/images/icons/code.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">绑码激活</text>
          </view>
        </uni-grid-item>


        <uni-grid-item v-if="hasRole(['companymgr'])">
          <view class="grid-item-box" @click="scanCode()">
            <u--image :showLoading="true" src="/static/images/icons/scan.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">用户绑定</text>
          </view>
        </uni-grid-item>

        <uni-grid-item>
          <view class="grid-item-box" @click="watchDevice()">
            <u--image :showLoading="true" src="/static/images/icons/scan.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">查看设备</text>
          </view>
        </uni-grid-item>

        <uni-grid-item v-if="hasRole(['companymgr'])">
          <view class="grid-item-box" @click="goPage('alert')">
            <u--image :showLoading="true" src="/static/images/icons/alert.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">设备告警</text>
          </view>
        </uni-grid-item>

        <uni-grid-item v-if="hasRole(['companymgr'])">
          <view class="grid-item-box" @click="goPage('error')">
            <u--image :showLoading="true" src="/static/images/icons/error.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">异常列表</text>
          </view>
        </uni-grid-item>

        <uni-grid-item v-if="hasRole(['admin'])">
          <view class="grid-item-box" @click="goPage('reset')">
            <u--image :showLoading="true" src="/static/images/icons/reset.png" width="50rpx" height="50rpx" ></u--image>
            <text class="text">注销设备</text>
          </view>
        </uni-grid-item>

      </uni-grid>

      <u-modal :show="show" :title="dlgTitle" @cancel="cancel" :showCancelButton="true" @confirm="bingDevice" >
        <view class="slot-content">

          <view>设备名称：{{ scanDevice.deviceName }}</view>
          <view>产品名称：{{ scanDevice.productName }}</view>
          <view>设备编号：{{ scanDevice.serialNumber }}</view>
          <view>激活日期：{{ scanDevice.activeTime }}</view>

        </view>
      </u-modal>
    </view>
  </view>
</template>

<script>
import authObj from '@/plugins/auth.js';
import {getDevcieByQrcode,bingDeviceDept} from '@/api/device/device';
import UImage from "../../uni_modules/uview-ui/components/u--image/u--image";
export default {
  components: {UImage},
  data() {
      return {
        dlgTitle:"",
        current: 0,
        swiperDotIndex: 0,
        show:false,
        scanDevice:{},
        data: [{
            image: '/static/images/banner/banner01.jpg'
          },
          // {
          //   image: '/static/images/banner/banner02.jpg'
          // },
          // {
          //   image: '/static/images/banner/banner03.jpg'
          // }
        ]
      }
    },
    methods: {
      bingDevice(){
          this.show = false;
        bingDeviceDept(this.scanDevice).then(res=>{
          if(res.code == 200){
            this.$modal.showToast('操作成功')
          }else{
            this.$modal.showToast(res.msg);
          }

        })
      },
      cancel(){
        this.show = false;
        this.scanDevice = {};
      },
      clickBannerItem(item) {
        console.log();
      },
      hasRole(role){
        return authObj.authRoleAdmin(role);
      },
      changeSwiper(e) {
        this.current = e.detail.current
      },
      changeGrid(e) {
        this.$modal.showToast('模块建设中~')
      },
      watchDevice(){
        let self = this;
        uni.scanCode({
          success: function (res) {
            console.log('条码内容：' + res.result);
            getDevcieByQrcode(res.result).then(res=>{
              let device = res.data;
              if(!self.hasRole(['admin'])){
                if(device.deptId == null || device.deptId == 0){
                  self.$modal.showToast('设备不存在或无权限')
                  return;
                }
              }

              if(device != null){
                uni.navigateTo({
                  url: '/pages/device/detail?id='+device.deviceId
                });
              }else{
                self.$modal.showToast('设备不存在或无权限')
              }

            });
          }
        });
      },
      scanCode(){
        let self = this;
        uni.scanCode({
          success: function (res) {
            console.log('条码内容：' + res.result);
            getDevcieByQrcode(res.result).then(res=>{
              let device = res.data;
              if(!device){
                self.$modal.showToast('设备不存在或该已经绑定用户')
              }else{
                if(device.deptId != null){
                  self.$modal.showToast('该设备已经绑定了用户')
                }else{
                  self.dlgTitle = "绑定设备";
                  self.scanDevice = device;
                  self.show = true;
                }
              }
            });
          }
        });
      },

      goPage(path){
        if('device' == path){
          uni.navigateTo({
            url: '/pages/device/index'
          });
        }
        if('wifi' == path){
          uni.navigateTo({
            url: '/pages/wifi/index'
          });
        }
        if('scan' == path){
          uni.navigateTo({
            url: '/pages/device/scan'
          });
        }
        if('user' == path){
          uni.navigateTo({
            url: '/pages/user/index'
          });
        }
        if('alert' == path){
          uni.navigateTo({
            url: '/pages/device/alert'
          });
        }
        if('error' == path){
          uni.navigateTo({
            url: '/pages/device/error'
          });
        }
        if('reset' == path){
          uni.navigateTo({
            url: '/pages/device/reset/reset'
          });
        }

      }
    }
  }
</script>

<style lang="scss">
  /* #ifndef APP-NVUE */
  page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background-color: #fff;
    min-height: 100%;
    height: auto;
  }

  view {
    font-size: 14px;
    line-height: inherit;
  }

  /* #endif */

  .text {
    text-align: center;
    font-size: 26rpx;
    margin-top: 10rpx;
  }

  .grid-item-box {
    flex: 1;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 15px 0;
  }

  .uni-margin-wrap {
    width: 690rpx;
    width: 100%;
    ;
  }

  .swiper {
    height: 300rpx;
  }

  .swiper-box {
    height: 150px;
  }

  .swiper-item {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: #fff;
    height: 300rpx;
    line-height: 300rpx;
  }

  @media screen and (min-width: 500px) {
    .uni-swiper-dot-box {
      width: 400px;
      /* #ifndef APP-NVUE */
      margin: 0 auto;
      /* #endif */
      margin-top: 8px;
    }

    .image {
      width: 100%;
    }
  }
</style>
