<template>
  <view class="content">
    <view class="banner">
      <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="imgUrl+item.image" mode="aspectFill" :draggable="false" />
            </view>
          </swiper-item>
        </swiper>
      </uni-swiper-dot>
    </view>

    <view class="container">
      <view class="item" @click="buleTeeth">
        <image :src = "imgUrl+'/index/bluetooth.png'"/>
        <view class="item-text">{{i18('蓝牙连接')}}</view>
      </view>

      <view class="item" @click="scan">
        <image :src = "imgUrl+'/index/scan.png'"/>
        <view class="item-text">{{i18('扫码绑定')}}</view>
      </view>
      <view class="item" @click="devicelist">
        <image :src = "imgUrl+'/index/device-2.png'"/>
        <view class="item-text">{{i18('设备列表')}}</view>
      </view>
    </view>
  </view>
</template>

<script>
import { getDeviceInfoFromQrcode,bindDevice } from '@/api/device/device.js'
import i18 from '@/utils/i18.js'
  export default {
    data:function(){
      return {
        current: 0,
        swiperDotIndex: 0,
        data: [{
          image: '/index/banner-index.png'
        }
        ]
      }
    },
    computed: {
      imgUrl() {
        return getApp().globalData.config.imgUrl;
      }
    },
    onLoad: function() {
    },
    onShow(){
      uni.setNavigationBarTitle({
        title: this.$t('page.worktai')
      })
    },
    methods:{
      devicelist(){
        uni.navigateTo({
          url: '/pages/weitiandi/deviceList'
        });
      },
      i18(text){
        return i18(text)
      },
      buleTeeth(){
        uni.navigateTo({
          url: '/pages/bluetooth/index/index'
        });
      },
      toBind(qrcode){
        bindDevice(qrcode).then(res=>{
          let data = res.data;
          if(data != null){
            let imei = res.data.imei;
            let ccid = res.data.ccid;
            let qrcode = res.data.qrcode;
            let uuid = res.data.uuid;
            this.$modal.showToast("绑定成功");
          }else{
            this.$modal.showToast("绑定失败");
          }
        })
      },
      scan(){
        let self = this;
        uni.scanCode({
          success: function (res) {
            console.log('条码类型：' + res.scanType);
            console.log('条码内容：' + res.result);
            getDeviceInfoFromQrcode(res.result).then(res=>{
              if(res.data != null){
                let imei = res.data.imei;
                let ccid = res.data.ccid;
                let qrcode = res.data.qrcode;
                let uuid = res.data.userId;
                if(uuid){
                  self.$modal.showToast("该设备已被绑定");
                }else{
                  self.$modal.confirm("确认绑定该设备？").then(res=>{
                    self.toBind(qrcode)
                  })
                }
                // uni.navigateTo({
                //   url: '/pages/weitiandi/device/status?qrcode='+qrcode+'&id='+imei+'&ccid='+ccid
                // });
              }else{
                self.$modal.showToast("找不到设备,请联系管理员");
              }
            });
          }
        });
      },
      scan2(){
        let self = this;
        getDeviceInfoFromQrcode("http://wetiandi.com/app/index.php?i=1&j=1&c=entry&m=wdl_shopping&do=wepay&sn=GD1B342399").then(res=>{
            if(res.data != null){
                  let imei = res.data.imei;
                  let ccid = res.data.ccid;
                  let qrcode = res.data.qrcode;
                  let uuid = res.data.userId;
                  if(uuid){
                    self.$modal.showToast("该设备已被绑定");
                  }else{
                    self.$modal.confirm("确认绑定该设备？").then(res=>{
                      self.toBind(qrcode)
                    })
                  }
                  // uni.navigateTo({
                  //   url: '/pages/weitiandi/device/status?qrcode='+qrcode+'&id='+imei+'&ccid='+ccid
                  // });
            }else{
              self.$modal.showToast("找不到设备,请联系管理员");
            }
        });
      }
    }
  }
</script>

<style>
  .content {
    background: rgb(249, 252, 255);
    inset: 0;
    position: absolute;
  }


  .swiper {
    height: 22vh;
  }
.banner{
  height: 22vh;
  width: 100%;
  margin-top:1vh
}
  .swiper-box {
    height: 22vh;
  }

  .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%;
    }
  }
  .item{
    display: inline-block;
    width: 50%;
    position: relative;
  }
  .item image{
    height: 12vh;
  }
  .item-text{
    position: absolute;
    top:30px;
    left: 50%;
  }

</style>
