<template>
  <web-view src="http://192.168.1.55:5178/" @message="onmessage"></web-view>
</template>

<script setup>
  import { onLoad, onReady } from "@dcloudio/uni-app"
  import { nextTick, reactive, ref } from "vue"
  import { getLocationManager, searchDevices, print,
    cancelDiscovery } from '@/js_sdk/wangqingzhou311-android-blueTooth/searchDevices.js';
  import { commands } from '@/js_sdk/wangqingzhou311-android-blueTooth/command.js';
  import * as pos from "@/utils/EscPostUtils.js"
  const vw = ref(null); // web-view
  
  onLoad(() => {
    // #ifdef APP-PLUS
    // plus.screen.lockOrientation('default'); //设置横屏
    plus.navigator.setFullscreen(true); //隐藏状态栏
    // #endif
    getLocationManager(() => {});
  })

  onReady(() => {
    {
      // #ifdef APP-PLUS
      // plus.screen.lockOrientation('landscape-primary'); 
      nextTick(()=>{
        setTimeout(()=>{ //延迟获取webview， 否则会获取不到
          const pages = getCurrentPages();
          vw.value = pages[pages.length - 1].$getAppWebview().children()[0];
        }, 3000) 
      })
      // #endif
    }
  })

  const state = reactive({
    current: 'esc',
    deviceData: [],
    data: [
      { id: "12345", width: 100, height: 100, title1: 'title_1', title2: 'title_2', title3: 'title_3' },
    ],
    // deviceId: '86:67:7A:2A:0B:08'
  });
  const onPrintClick = (str) => {
    const stringData = [];
    // state.data.forEach(item => {
    //   // stringData.push(commands(state.current, item));
    //   let str = '\n';
    //   str += '********************************\n';
    //   str += '小票打印测试\n';
    //   str += '测试商品1 * 1 \t ¥1.00 \n\n';
    //   str += '测试商品2 * 1 \t ¥1.00 \n\n';
    //   str += '测试商品3 * 1 \t ¥1.00 \n\n';
    //   str += '================================\n';
    //   str += '\n\t\t\t';
    //   stringData.push(str)
    // })
    if(str) stringData.push(str);
    else return uni.showToast({
        title: '不可打印空内容',
        icon: 'exception'
      })
    console.log('打印内容', stringData);
    let deviceId = uni.getStorageSync('deviceId');
    if(!deviceId) {
      let obj = state.deviceData.find(item=>item.name=='T58');
      if(!obj) return uni.showToast({
        title: '蓝牙未连接到T58打印机, 请检查是否已配对T58打印机',
        icon: 'exception'
      })
      else {
        deviceId = obj.address;
      }
    }
    console.log('打印机地址=>', deviceId);
    print(deviceId, stringData, () => {
      uni.showToast({
        title: '打印成功',
        duration: 2000,
      });
      uni.setStorageSync('deviceId', deviceId)
    })
  }
  const onBtnClick = (deviceId) => {
    state.deviceId = deviceId;
    searchDevices(deviceId);
  }

  // searchDevices('',state.deviceData);
  let t = 1;
  // 接收webview消息
  const onmessage = (e)=>{
    console.log('收到消息', e);
    if(e.detail?.data[0]?.type=='print'){
      if(e.detail?.data[0]?.content){
        onPrintClick(e.detail?.data[0]?.content)
      }
    }
    else if(e.detail?.data[0]?.type=='connect') {
      searchDevices('',state.deviceData);
      uni.$off('connect');
      uni.$once('connect', (e)=>{
        uni.hideLoading();
        let obj = {
          type: 'connect',
          data: '打印机已连接'
        }
        console.log('连接成功',t++);
        vw.value.evalJS(`receiveData('${JSON.stringify(obj)}')`);
      })
    }
  }

  // // 页面关闭时清除横屏正方向
  // onUnload() {
  // 	// #ifdef APP-PLUS
  // 	plus.screen.lockOrientation('portrait-primary');
  // 	// #endif
  // },
</script>

<style>

</style>