<template>
  <div class="container">
    <div class="left-side">
      <div class="panel">
        <Banner />
        <DataPanel :deviceInfo="computedDeviceInfo" :alarmInfo='alarmInfo' :productInfo='productInfo' />
        <ContentPublishingSource v-if="computedDeviceInfo && alarmInfo && productInfo" :deviceInfo="computedDeviceInfo" :alarmInfo='alarmInfo' :productInfo='productInfo' />
      </div>
      <a-grid :cols="24" :col-gap="16" :row-gap="16" style="margin-top: 16px">
        <a-grid-item
          :span="{ xs: 24, sm: 24, md: 24, lg: 12, xl: 12, xxl: 12 }"
        >
        </a-grid-item>
        <a-grid-item
          :span="{ xs: 24, sm: 24, md: 24, lg: 12, xl: 12, xxl: 12 }"
        >
        </a-grid-item>
      </a-grid>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { computed, onMounted, onUnmounted, ref } from 'vue';
  import { getAlarmInfo, getDeviceInfo, getProductInfo } from '@/api/dashboard';
  import { EventSourcePolyfill } from 'event-source-polyfill';
  import { getToken } from '@/utils/auth';

  import Banner from './components/banner.vue';
  import DataPanel from './components/data-panel.vue';
  import ContentPublishingSource from './components/content-publishing-source.vue';

  const deviceInfo = ref<any>();
  const computedDeviceInfo = computed(() => {
    return deviceInfo.value; // 返回 deviceInfo 的值
  });
  const alarmInfo = ref<any>();
  const productInfo = ref<any>();

  // const fetchData = async () => {
  //   const res = await getDeviceInfo();
  //   deviceInfo.value = res.data;
  //   const res1 = await getAlarmInfo();
  //   alarmInfo.value = res1.data;
  //   const res2 = await getProductInfo();
  //   productInfo.value = res2.data;
  // }
  // onMounted(() => {
  //   fetchData();
  // })

  let eventSource: EventSourcePolyfill | null = null;
  const token = getToken();
  const startSSE = () => {
    // 创建 EventSourcePolyfill 对象并连接到 SSE 接口
    eventSource = new EventSourcePolyfill(`http://127.0.0.1:8081/api/rest/device/sse/status`, {
      headers: {
        'X-CSRF-TOKEN': token
      }
    });

    // 监听消息事件
    eventSource.onmessage = (event:any) => {
      const data = JSON.parse(event.data); // 假设服务器发送的是 JSON 数据
      console.log('Received data:', data);
      // 在这里处理接收到的数据
    };

    // 监听错误事件
    eventSource.onerror = (error:any) => {
      console.error('SSE error:', error);
      // 在这里处理错误
    };
  };

  onMounted(() => {
    startSSE();
  });

  onUnmounted(() => {
    // 组件卸载时关闭 SSE 连接
    if (eventSource) {
      eventSource.close();
    }
  });



</script>

<script lang="ts">
  export default {
    name: 'Dashboard', // If you want the include property of keep-alive to take effect, you must name the component
  };
</script>

<style lang="less" scoped>
  .container {
    //background-color: var(--color-fill-2);
    padding: 16px 20px;
    padding-bottom: 0;
    display: flex;
  }

  .left-side {
    flex: 1;
    overflow: auto;
  }

  .right-side {
    width: 280px;
    margin-left: 16px;
  }

  .panel {
    //background-color: var(--color-bg-2);
    border-radius: 4px;
    overflow: auto;
  }
  :deep(.panel-border) {
    margin-bottom: 0;
    border-bottom: 1px solid rgb(var(--gray-2));
  }
  .moduler-wrap {
    border-radius: 4px;
    //background-color: var(--color-bg-2);
    :deep(.text) {
      font-size: 12px;
      text-align: center;
      color: rgb(var(--gray-8));
    }

    :deep(.wrapper) {
      margin-bottom: 8px;
      text-align: center;
      cursor: pointer;

      &:last-child {
        .text {
          margin-bottom: 0;
        }
      }
      &:hover {
        .icon {
          color: rgb(var(--arcoblue-6));
          background-color: #e8f3ff;
        }
        .text {
          color: rgb(var(--arcoblue-6));
        }
      }
    }

    :deep(.icon) {
      display: inline-block;
      width: 32px;
      height: 32px;
      margin-bottom: 4px;
      color: rgb(var(--dark-gray-1));
      line-height: 32px;
      font-size: 16px;
      text-align: center;
      background-color: rgb(var(--gray-1));
      border-radius: 4px;
    }
  }
</style>

<style lang="less" scoped>
  // responsive
  .mobile {
    .container {
      display: block;
    }
    .right-side {
      // display: none;
      width: 100%;
      margin-left: 0;
      margin-top: 16px;
    }
  }
</style>
