<script setup>
import myHeader from "./myHeader.vue";
import myAside from "./myAside.vue";
import { ref, nextTick, onMounted, onUnmounted } from "vue";
import mitt from "@/utils/mitt.js";
import { useUserStore } from "@/store/user.js";
import { Push } from "@/common/push.js";

const userStore = useUserStore();
const connection = new Push({
  url: import.meta.env.VITE_PUSH_URL, // websocket地址
  app_key: '2ce3ce22329517213caa7dad261f5695',
});

// 浏览器监听user-1
const user_channel = connection.subscribe(`store_merchant_${userStore.userInfo.merchant.mer_id}`);
// const user_channel = connection.subscribe(`store_merchant_${1}`);

// 当user-2频道有message事件的消息时
user_channel.on('message', function (data) {
  console.log("收到消息--",data);
  try {
    if(data?.content?.type=='cash_register'&&data?.content?.msg=="您有一笔订单已支付"){
      mitt.emit('pay_success', data?.content?.data);
    }
    if(data?.content?.type=='store_merchant'&&data?.content?.msg=="您有一笔新的订单"){
      mitt.emit('new_order', data?.content);
    }
  } catch (error) {
    
  }
});
// 断线事件
user_channel.on('close', function () {
  
});

const KeyboardEvent = (e)=>{
  console.log('按下', e.keyCode);
  if(e.keyCode == 16) mitt.emit('shift');
  if(e.keyCode == 120) mitt.emit('F9');
  if(e.keyCode == 13) mitt.emit('enter');
  if(e.keyCode == 37) mitt.emit('left');
  if(e.keyCode == 39) mitt.emit('right');
  if(e.keyCode == 38) mitt.emit('up');
  if(e.keyCode == 40) mitt.emit('down');
  if(e.keyCode == 46) mitt.emit('delete');
  if(e.keyCode == 45) mitt.emit('insert');
}

onMounted(()=>{
  window.addEventListener('keydown', KeyboardEvent);
  console.log('开启键盘监听');
})

onUnmounted(()=>{
  window.removeEventListener('keydown', KeyboardEvent);
  console.log('关闭键盘监听');
})
</script>

<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <myHeader></myHeader>
      </el-header>
      <el-container>
        <el-aside width="6.25rem">
          <myAside></myAside>
        </el-aside>
        <el-main style="color: #333;">
          <router-view v-slot="{ Component }" class="my-main">
            <transition name="el-zoom-in-top">
              <component :is="Component"></component>
            </transition>
          </router-view>
          <!-- <transition name="el-zoom-in-top">
            <router-view class="my-main"></router-view>
          </transition> -->
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped lang="scss">
.common-layout {
  width: 100vw;
  height: 100vh;
  background-color: #001529;
  .el-header {
    color: #fff;
    height: 4rem;
  }
  .el-aside {
    color: #fff;
  }
  .el-main {
    background-color: #f5f5f5;
    width: calc(100vw - 6.25rem);
    height: calc(100vh - 4rem);
    border-radius: 2rem 0 0 0;
    overflow-y: scroll;
    padding: 1rem;
    
    .my-main {
      height: 100%;
      width: 100%;
    }
  }
}
</style>
