<template>
  <view class="work-container">
    <!-- 轮播图 -->
  <view style="padding:0 26rpx;">
    <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" style="border-radius: 12rpx" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot>
  </view>
    <view style="box-sizing: border-box;margin-top:20rpx;padding:0 20rpx">

      <view class="grid-item-box" v-if="hasRole(['admin'])" @click="goPage('user')" style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/user.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">客户列表</view>
          <view class="bottom">Customer List</view>
        </view>
      </view>

      <view class="grid-item-box" v-if="isCompanyUser()" @click="goPage('deptuser')" style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
          <view class="grid-image">
            <u-image :showLoading="true" src="/static/images/icons/user.png" width="80rpx" height="80rpx" ></u-image>
          </view>
          <view class="grid-text">
            <view class="top">用户管理</view>
            <view class="bottom">User Management</view>
          </view>
      </view>

      <view class="grid-item-box" v-if="isCompanyUser()" @click="goPage('device')"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/device.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">设备列表</view>
          <view class="bottom">Device List</view>
        </view>
      </view>


      <view class="grid-item-box" @click="goPage('wifi')"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/wifi.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">设备配网</view>
          <view class="bottom">Wifi Setting</view>
        </view>
      </view>

      <view class="grid-item-box" v-if="isCompanyUser()" @click="scanCode()"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/bind.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">设备绑定</view>
          <view class="bottom">Device Binding</view>
        </view>
      </view>


      <view class="grid-item-box" v-if="hasRole(['admin'])" @click="goPage('scan')"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/bind.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">绑码激活</view>
          <view class="bottom">Bind Code Activation</view>
        </view>
      </view>


      <view class="grid-item-box" @click="watchDevice"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/scan.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">查看设备</view>
          <view class="bottom">View Device</view>
        </view>
      </view>


      <view class="grid-item-box" v-if="isCompanyUser()" @click="goPage('alert')"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/alert.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">设备告警</view>
          <view class="bottom">Device Alert</view>
        </view>
      </view>


      <view class="grid-item-box" v-if="isCompanyUser()" @click="goPage('alert')"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/error.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">异常列表</view>
          <view class="bottom">Exception List</view>
        </view>
      </view>

      <view class="grid-item-box" @click="goPage('reset')" v-if="hasRole(['admin'])"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/reset.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">注销设备</view>
          <view class="bottom">Unregister Device</view>
        </view>
      </view>


      <view class="grid-item-box" @click="goPage('time')"  style="background-image: url('https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jianyaoji/images/icons/bg.png');background-size: 100% 100%">
        <view class="grid-image">
          <u-image :showLoading="true" src="/static/images/icons/rank.png" width="80rpx" height="80rpx" ></u-image>
        </view>
        <view class="grid-text">
          <view class="top">时长排名</view>
          <view class="bottom">Duration Ranking</view>
        </view>
      </view>
    </view>
    <!-- 宫格组件 -->


      <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>
</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 {
        deptId:0,
        dlgTitle:"",
        current: 0,
        swiperDotIndex: 0,
        show:false,
        scanDevice:{},
        data: [{
            image: '/static/images/banner/banner01.png'
          },
          // {
          //   image: '/static/images/banner/banner02.jpg'
          // },
          // {
          //   image: '/static/images/banner/banner03.jpg'
          // }
        ]
      }
    },
  onLoad(){
    if(this.isCompanyUser()){
      this.deptId = this.$store.state.user.user.deptId;
      console.log( this.deptId );
    }

  },
    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);
      },
      isCompanyUser(){
        return authObj.authRoleAdmin(["companymgr"]) || authObj.authRoleAdmin(["companyuser"]);
      },
      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('deptuser' == path){
          uni.navigateTo({
            url: '/pages/user/list?deptId='+this.deptId
          });
        }
        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'
          });
        }

        if('time' == path){
          uni.navigateTo({
            url: '/pages/device/rank'
          });
        }

      }
    }
  }
</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: inline-block;
    position: relative;
    /* #endif */
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 15px 0;
    height: 250rpx;
    width:50%;
  }

  .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%;
    }
  }
  .grid-image{
    position: absolute;
    left: 61rpx;
    top: 49rpx;
  }
  .grid-text{
    position: absolute;
    left:184rpx;
    top:62rpx;
    color: #545454;
    .top{
      font-weight: bold;
      font-size: 32rpx;
      margin-bottom: 30rpx;
    }
    .bottom{
      font-size: 18rpx;
      font-weight: 400;
    }
  }

</style>
