<template>
  <div class="device-monitor">
    <!-- 顶部标题栏 -->
    <div class="header">
      <div class="header_title">
        <h2>{{ props.satlTitle }}</h2>
      </div>
      <div class="close-btn" @click="closePanel">×</div>
    </div>

    <!-- 标签页导航 -->
    <div class="tabs">
      <div class="tab-btn_forward"></div>
      <div class="tab-btn" :class="{ active: activeTab === 'info' }" @click="activeTab = 'info'">基本信息</div>
      <div class="tab-btn" :class="{ active: activeTab === 'devices' }" @click="activeTab = 'devices'">设备情况</div>
      <div class="tab-btn_backward"></div>
    </div>

    <!-- 设备列表内容区 -->
    <div class="content" v-if="activeTab === 'devices'">
      <!-- 设备列表头部 -->
      <div class="table-header" v-if="devices.length > 0">
        <div class="col device-name">设备名称</div>
        <div class="col status">状态</div>
      </div>

      <!-- 设备列表 -->
      <div class="device-list">
        <!-- 普通设备项 -->
        <div v-if="devices.length > 0">
          <div class="device-item" v-for="(device, index) in devices" :key="index" :class="{
            'has-error': device.status === 'error',
            expanded: device.expanded
          }">
            <!-- 主设备行 -->
            <div class="device-row" :class="device.status === 'error' ? 'has-error' : ''" @click="toggleExpand(device)">
              <div class="col device-name">
                <span v-if="device.status === 'error'" class="toggle-icon">
                  {{ device.expanded ? '▾' : '▸' }}
                </span>
                {{ device.name }}
              </div>
              <div class="col status">
                <span class="status-indicator" :class="device.status"></span>
                <span class="status-text">
                  {{ device.status === 'normal' ? '正常' : '故障' }}
                </span>
              </div>
            </div>

            <!-- 故障详情 -->
            <div class="error-details-container"
              v-if="device.status === 'error' && device.expanded && device.errorDetails">
              <div class="error-details">
                {{ device.errorDetails }}
              </div>
            </div>
          </div>
        </div>
        <div v-else>
          <div class="no-data">
            <p>暂无数据</p>
          </div>
        </div>
      </div>
    </div>
    <div class="content content_info" v-else>
      <!-- 设备列表 -->
      <div class="device-list1">
        <!-- 普通设备项 -->
        <div v-if="deviceStatusMap.length > 0">
          <div class="device-item" v-for="(items, index) in deviceStatusMap" :key="index">
            <!-- 主设备行 -->
            <div class="device-row" v-for="(item, index) in items" :key="index">
              <div class="col device-name">

                {{ item.label }}
                <!-- <span :class="items.status === 'error' ? 'has-error1' : 'notHas-error1'">(工作)</span> -->
              </div>
              <div class="col status">

                <span class="status-text">
                  {{ item.value }}
                </span>
              </div>
            </div>


          </div>
        </div>
        <div v-else>
          <div class="no-data">
            <p>暂无数据</p>
          </div>
        </div>
      </div>
      <!-- 设备列表头部 -->
      <!-- <div class="table-header">
        <div class="col device-name">设备名称12</div>
        <div class="col status">状态</div>
      </div> -->

      <!-- 设备列表 -->
      <!-- <div class="device-list">
        普通设备项
        <div 
          class="device-item" 
          v-for="(device, index) in devices" 
          :key="index" 
          :class="{ 
            'has-error': device.status === 'error',
            expanded: device.expanded 
          }"
        >
          主设备行
          <div class="device-row" :class="device.status === 'error' ? 'has-error' : ''" @click="toggleExpand(device)">
            <div class="col device-name">
              <span 
                v-if="device.status === 'error'" 
                class="toggle-icon"
              >
                {{ device.expanded ? '▾' : '▸' }}
              </span>
              {{ device.name }}
            </div>
            <div class="col status">
              <span class="status-indicator" :class="device.status"></span>
              <span class="status-text">
                {{ device.status === 'normal' ? '正常' : '故障' }}
              </span>
            </div>
          </div>
          
          故障详情
          <div 
            class="error-details-container" 
            v-if="device.status === 'error' && device.expanded && device.errorDetails"
          >
            <div class="error-details">
              {{ device.errorDetails }}
            </div>
          </div>
        </div>
      </div> -->
    </div>
  </div>
</template>

<script setup>
import { onActivated, onMounted, ref, watch } from 'vue';

const props = defineProps(['satlTitle', 'errorDeviceList', "baseDeviceList"])
const emits = defineEmits(['errorDialoghandleClose'])
//console.log("展示了",props.errorDeviceList,props.baseDeviceList);
// 状态管理
const activeTab = ref('devices'); // 默认显示设备情况标签页
const devices = ref([])
const deviceStatusMap = ref([


  // {label:  '天线发射频率',value: 12,status: 1 ? 'normal' : 'error',},
  // {label:  '天线发射EIRP',value: 587 || '未知设备',status: 0 ? 'normal' : 'error',},
  // {label:  '天线方位角',value: 57 || '未知设备',status: 1 ? 'normal' : 'error',},
  // {label:  '天线俯仰角',value: 27 || '未知设备',status: 1 ? 'normal' : 'error',}


])

// 监听 errorDeviceList 的变化
watch(() => props.errorDeviceList, (newVal) => {
  console.log(props.errorDeviceList, '故障详情');

  if (newVal.length === 0) {
    devices.value = []
    return
  }
  if (newVal && typeof newVal === 'object' && !Array.isArray(newVal)) {
    // 如果是对象且包含 groundNetworkElementResponses 数组
    if (newVal.groundNetworkElementResponses && Array.isArray(newVal.groundNetworkElementResponses)) {
      devices.value = newVal.groundNetworkElementResponses.map(item => ({
        name: item.elementName || '未知设备',
        status: item.status === '正常' ? 'normal' : 'error',
        expanded: false,
        errorDetails: item.stAlarmInfoResponse || null
      }))
    }
  } else if (Array.isArray(newVal)) {
    // 如果是数组，直接映射
    devices.value = newVal.map(item => ({
      name: item.elementName || '未知设备',
      status: item.statue === '正常' ? 'normal' : 'error',
      expanded: false,
      errorDetails: item.stAlarmInfoResponse?.locationInfo || null
    }))
  } else if (newVal && typeof newVal === 'object') {
    // 如果是普通对象，尝试直接使用
    devices.value = [{
      name: newVal.elementName || '未知设备',
      status: newVal.statue === '正常' ? 'normal' : 'error',
      expanded: false,
      errorDetails: newVal.stAlarmInfoResponse?.locationInfo || null
    }]
  }
}, { immediate: true })

watch(() => props.baseDeviceList, (newVal) => {
  deviceStatusMap.value = []

  newVal.forEach(item => {
    deviceStatusMap.value.push([{
      label: `天线${item.number}发射功率 `,
      value: item.power || '0',
      status: item.power !== 0 ? 'normal' : 'error',
    },
    {
      label: `天线${item.number}发射EIRP `,
      value: item.eip || '-',
      status: item.power !== 0 ? 'normal' : 'error',
    },
    {
      label: `天线${item.number}方位角 `,
      value: item.azimuth || '-',
      status: item.power !== 0 ? 'normal' : 'error',
    },
    {
      label: `天线${item.number}俯仰角 `,
      value: item.elevation || '-',
      status: item.power !== 0 ? 'normal' : 'error',
    },

    ])
  })
  //console.log("11111EIRP111111baseDeviceList",newVal,deviceStatusMap.value)
  //  deviceStatusMap.value = [
  //     {label:  '天线发射频率',value: newVal.antennaTransmitPower ,status: 1 ? 'normal' : 'error',},
  //     {label:  '天线发射EIRP',value: newVal.antennaEip ,status: 0? 'normal' : 'error',},
  //     {label:  '天线方位角',value: newVal.antennaAzimuth ,status: 1 ? 'normal' : 'error',},
  //     {label:  '天线俯仰角',value: newVal.antennaElevation ,status: 1 ? 'normal' : 'error',}
  // ]


}, { immediate: true })

// 方法
const toggleExpand = (device) => {
  if (device.status === 'error') {
    device.expanded = !device.expanded;
  }
};

const closePanel = () => {
  emits('errorDialoghandleClose')
};

</script>

<style scoped>
/* 基础样式 */
.device-monitor {
  width: 280px;
  height: 380px;
  position: fixed;
  /* 改为fixed定位 */
  bottom: 0.5vh;
  left: 20%;
  background: #1a1a1a;
  border-radius: 4px;
  z-index: 1001;
  overflow: hidden;


  /* border-top: 2px solid;
  border-bottom: 2px solid;
  border-image: linear-gradient(to right, rgba(54, 27, 27, 0.93) 0%, rgba(255, 2, 2, 0.93) 50%, rgba(54, 27, 27, 0.93)) 1;
  box-shadow: inset 0 -20px 70px -25px rgba(255, 2, 2, 0.93); */
  /* background-color: #eea929; */


}

/* 顶部标题栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 20px;
  /* background: linear-gradient(90deg, #1a237e, #283593); */
  border-bottom: 1px solid #383850;
  position: relative;

}

.header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(56, 56, 80, 0.8), transparent);
}

.header_title h2 {
  margin: 3px;
  padding-left: 5px;
  font-size: 17px;
  font-weight: 500;
  color: #ffffff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.header_title {
  width: 100%;
  height: 100%;
  background-image: url('@/assets/images/lefts/errorHeader.png');
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.close-btn {
  width: 15px;
  height: 15px;
  background: #1E1E2D;
  border: none;
  color: #aaa;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  /* position: relative; */
  /* top: -0px; */
}

.close-btn:hover {
  color: #ffffff;

}

/* 标签页样式 */
.tabs {
  display: flex;
  background: linear-gradient(to bottom, #25253a, #1e1e2d);
  border-bottom: 1px solid #383850;
  justify-content: space-between;
  position: relative;
}

.tabs::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(56, 56, 80, 0.8), transparent);
}

.tab-btn_forward {
  width: 50px;
  padding: 2px 15px;
  background-image: url('@/assets/images/lefts/UnionLeft.png');
  background-size: auto;
  background-repeat: no-repeat;
  background-position: center;
}

.tab-btn_backward {
  width: 50px;
  background-image: url('@/assets/images/lefts/UnionRight.png');
  background-size: auto;
  background-repeat: no-repeat;
  background-position: center;
}

.tab-btn {
  padding: 5px 15px;
  /* width: 100px; */
  cursor: pointer;
  transition: all 0.3s;
  font-size: 15px;
  opacity: 0.8;
  /* border-radius: 6px; */
  position: relative;
  color: #b0b0b0;
  font-weight: 500;
  background: rgba(82, 82, 82, 0.38);
}

.tab-btn.active {
  background: linear-gradient(to bottom, rgba(68, 136, 245, 0.8), rgba(15, 23, 42, 0.8));
  color: #ffffff;
  opacity: 1;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, #4488f5, transparent);
}

.tab-btn:hover:not(.active) {
  color: #e0e0e0;
  opacity: 1;
}

/* 内容区样式 */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  background: linear-gradient(to bottom, #1e1e2d, #191925);
}

.content_info {
  height: 100%;
  /* background-color: #4488f5; */

}

/* 表格头部 */
.table-header {
  display: flex;
  padding: 5px 25px;
  /* background: linear-gradient(to bottom, #25253a, #1e1e2d); */
  font-weight: 500;
  font-size: 15px;
  color: #d1d0d0;
  border-bottom: 1px solid #383850;
  position: sticky;
  top: 0;
  z-index: 10;
}

.col {
  flex: 1;
  font-size: 11px;
}

.device-name {
  flex: 1.2;
  overflow: hidden;
}

.device-name1 {
  flex: 1;
  text-align: right;
  padding-right: 10px;
}

.status {
  flex: 1;
  right: 10px;
  text-align: right;
}

.status1 {
  flex: 1;
  right: 10px;
  text-align: left;
}

/* 设备列表样式 */
.device-list {
  padding: 0 10px;
  /* max-height: 260px; */
  overflow-y: auto;
  z-index: 20;
  height: 260px;
  color: #d1d0d0;
}

.device-list1 {
  padding: 0 10px;
  max-height: 290px;
  overflow-y: auto;
  z-index: 20;
  color: #d1d0d0;
}

.device-item {
  margin-bottom: 1px;
  border-radius: 6px;
  overflow-Y: auto;
  transition: all 0.2s;

  /* background-color: rgba(255, 255, 255, 0.02); */
  /* border: 1px solid rgba(255, 255, 255, 0.05); */
}

.device-item:hover {
  background-color: rgba(255, 255, 255, 0.05);
  border-color: rgba(255, 255, 255, 0.1);
}



.device-row {
  display: flex;
  padding: 8px 15px;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.2s;
  overflow: auto;

}


.has-error {
  overflow: hidden;
}

.has-error .device-row {
  position: relative;
  left: -20px;
  width: 106%;
  /* background-color: rgba(248, 113, 113, 0.05); */
}

.has-error1 {
  color: red;
}

.notHas-error1 {
  color: green;
}

/* 展开/折叠图标 */
.toggle-icon {
  display: inline-block;
  width: 18px;
  text-align: center;
  font-size: 12px;
  /* color: #f87171; */
  /* margin-right: 6px; */
  position: relative;
  top: 3px;
  overflow: hidden;
  transition: transform 0.2s;
}

.device-item.expanded .toggle-icon {
  transform: rotate(120deg);
}

/* 状态指示样式 */
.status-indicator {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
  box-shadow: 0 0 6px currentColor;
}

.status-indicator.normal {
  background-color: #4ade80;
  color: #4ade80;
}

.status-indicator.error {
  background-color: #ff0000;
  color: #ff0000;
}

.status-text {
  vertical-align: middle;
  font-size: 12px;
  font-weight: 500;
}

/* 故障详情区域 */
.error-details-container {
  background-color: #25253a;
  border-radius: 0 0 6px 6px;
  /* overflow-Y: auto; */
  overflow: hidden;
  color: #ffffff;
  animation: fadeIn 0.3s ease-out;
  border-top: 1px solid rgba(248, 113, 113, 0.2);
}

.error-details {
  padding: 12px 20px;
  font-size: 10px;
  color: #ffffff;
  background-color: rgba(45, 45, 68, 0.7);
  line-height: 1.6;
  overflow: hidden;
}

.error-detail-item {
  margin-bottom: 6px;
  display: flex;
  align-items: flex-start;
}

.error-bullet {
  /* color: #f87171; */
  margin-right: 8px;
  font-size: 16px;
  line-height: 1.3;
}

/* 滚动条样式 */
/* .content::-webkit-scrollbar {
  width: 8px;
}

.content::-webkit-scrollbar-track {
  background: #25253a;
  border-radius: 4px;
}

.content::-webkit-scrollbar-thumb {
  background: #444460;
  border-radius: 4px;
}

.content::-webkit-scrollbar-thumb:hover {
  background: #555570;
} */
.no-data {
  text-align: center;
  padding: 20px;
  font-size: 14px;
  color: #888888;
  min-height: 260px;
}

::-webkit-scrollbar {
  width: 1px !important;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #444460;
  border-radius: 1px;
}

::-webkit-scrollbar-thumb:hover {
  background: #555570;
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式调整 */
@media (max-width: 480px) {
  .device-monitor {
    width: 100%;
    height: 100%;
    border-radius: 0;
  }
}
</style>