<script setup lang="ts">
import { ref, watchEffect } from 'vue'

import { syncSendCmd } from '@/apis/system'
import { useDeviceStore } from '@/stores/deviceStore'

const deviceStore = useDeviceStore()
const appEvents = ref<Record<string, any>[]>([])

watchEffect(() => {
  appEvents.value = deviceStore.deviceStete.appEvents
})

const confirmClose = (item: Record<string, any>) => {
  console.log('item === ', item)
  const evenid = item.uuid
  const params = {
    className: 'AppCore',
    fnName: 'appEventConfirm',
    params: {
      evenid,
    },
  }
  syncSendCmd(params)
}
</script>

<template>
  <div v-if="appEvents.length" class="reconnect-modal-overlay">
    <div class="reconnect-modal-container">
      <h2 class="reconnect-title">
        错误信息：
      </h2>
      <ul class="recipe-list">
        <li
          v-for="(item, index) in appEvents"
          :key="index"
        >
          <span v-if="item.errCheckPoints">
            <span v-for="checkItem in item.errCheckPoints" :key="checkItem.ecode">
              {{ checkItem.ecodeInfo }}
            </span>
          </span>
          <span v-else>{{ item.description || item.message }}</span>
          <div class="actions">
            <button class="delete-button" @click.stop="confirmClose(item)">
              关闭
            </button>
            <span class="selected-icon">
              <i class="fa fa-check-circle" />
            </span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<style lang="scss" scoped>
  .reconnect-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
  }

  .reconnect-modal-container {
    background-color: white;
    border-radius: 12px;
    padding: 40px;
    width: 40vw;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    text-align: center;
    animation: fadeIn 0.3s ease-out;
    .recipe-list {
      list-style: none;
      padding: 0;
      margin: 0;
    }

    .recipe-list li {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px;
      border-radius: 4px;
      margin-bottom: 8px;
      transition: all 0.2s;
      cursor: pointer;
    }

    .recipe-list li:hover {
      background-color: #f5f7fa;
    }

    .recipe-list li.selected {
      background-color: #e6f7ff;
      /* border: 1px solid #1890ff; */
      /* padding: 9px; */
    }
  }

  .reconnect-spinner {
    width: 64px;
    height: 64px;
    border: 6px solid #f3f3f3;
    border-radius: 50%;
    border-top: 6px solid #3b82f6;
    margin: 0 auto 24px;
    animation: spin 1s linear infinite;
  }

  .reconnect-title {
    font-size: 1.12rem;
    font-weight: bold;
    color: #1f2937;
    margin-bottom: 12px;
  }

  .reconnect-message {
    font-size: 16px;
    color: #4b5563;
  }

  .actions {
    display: flex;
    align-items: center;
  }

  .view-button, .delete-button {
    border: none;
    padding: 4px 8px;
    border-radius: 3px;
    cursor: pointer;
    margin-right: 5px;
    transition: all 0.3s;
  }

  .view-button {
    background-color: #e6f7ff;
    color: #1890ff;
  }

  .view-button:hover {
    background-color: #1890ff;
    color: white;
  }

  .delete-button {
    background-color: #ffe6e6;
    color: #ff4d4f;
  }

  .delete-button:hover {
    background-color: #ff4d4f;
    color: white;
  }

  .selected-icon {
    color: #1890ff;
    /* margin-left: 8px; */
  }

  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }

  @keyframes fadeIn {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
  }
</style>
