<template>
	<view>
    <mumu-get-qrcode @success='qrcodeSucess' @error="qrcodeError" ></mumu-get-qrcode>
	</view>
</template>

<script>
import mumuGetQrcode from '@/uni_modules/mumu-getQrcode/components/mumu-getQrcode/mumu-getQrcode.vue'
import {getDeviceInfoFromQrcode,bindDevice} from "@/api/device/device";
	export default {
    components: {
      mumuGetQrcode
    },
		data() {
			return {
				
			}
		},
		methods: {
      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;
            this.$modal.showToast("绑定成功");
            setTimeout(function (){
              uni.navigateTo({
                url: '/pages/weitiandi/device/status?qrcode='+qrcode+'&id='+imei+'&ccid='+ccid
              });
            },2000)
          }else{
            this.$modal.showToast("绑定失败");
          }
        })
      },
      qrcodeSucess(data) {
        let self = this;
        getDeviceInfoFromQrcode(data).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){
              this.$modal.showToast("该设备已被绑定");
            }else{
              this.$modal.confirm("确认绑定该设备？").then(res=>{
                self.toBind(qrcode)
              })
            }
            // uni.navigateTo({
            //   url: '/pages/weitiandi/device/status?qrcode='+qrcode+'&id='+imei+'&ccid='+ccid
            // });
          }else{
            this.$modal.showToast("找不到设备,请联系管理员");
          }
        });
      },
      qrcodeError(err) {
        console.log(err)
        uni.showModal({
          title: '摄像头授权失败',
          content: '摄像头授权失败，请检测当前浏览器是否有摄像头权限。',
          success: () => {
            uni.navigateBack({})
          }
        })
      },
		}
	}
</script>

<style>

</style>
