<template>
  <div class="port-overview">
    <div class="left" style="width: 500px;">
      <div class="card digital-twin-card--deep-blue ">
        <div style="display: flex; align-items: center; justify-content: space-between;">
          <div class="card-title">
            <div class="vertical-line"></div>
            <img src="@/assets/svgs/data.svg" class="title-icon" />
            <span class="title-text">船舶状态</span>
          </div>
          <input class="search-container" type="text" placeholder="搜索船舶" />
        </div>

        <div class=" card-content">
          <div class="ship-table">
            <div class="ship-table-header">
              <div class="ship-table-column ship-name-header">轮船名称</div>
              <div class="ship-table-column ship-status-header">状态</div>
            </div>
            <div class="ship-table-body">
              <div v-for="ship in shipStatusData" :key="ship.id" class="ship-table-row" @click="handleSwitch(ship)">
                <div class="ship-table-column ship-name">
                  <div class="ship-icon">🚢</div>
                  <span class="ship-name-text">{{ ship.shipBasicInfo.name }}</span>
                </div>
                <div class="ship-table-column ship-status">
                  <div class="status-tag" :class="getStatusClass('正常')">
                    正常
                  </div>
                  <div class="status-tag" :class="getStatusClass('空闲')">
                    空闲
                  </div>
                  <div class="status-tag" :class="getStatusClass('故障')">
                    故障
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="right" style="width: 1000px;">
      <div v-if="selectedShip" class="card digital-twin-card--deep-blue" style="flex: 1;">
        <div class="card-title">
          <div class="vertical-line"></div>
          <img src="@/assets/svgs/data.svg" class="title-icon" />
          <span class="title-text">{{ selectedShip.shipBasicInfo.name }}</span>
        </div>
        <div class="card-content">
          <div class="overview-grid">
            <div class="overview-item">
              <div class="overview-value">{{ selectedShip.shorePowerAndShip?.powerUsage || 0 }}</div>
              <div class="overview-label">功率（kW）</div>
            </div>
            <div class="overview-item">
              <div class="overview-value">{{ shorePowerStatusMap[selectedShip.shorePowerAndShip?.status || ''] }}</div>
              <div class="overview-label">岸电状态</div>
            </div>
          </div>
        </div>
      </div>
      <div v-else class="card digital-twin-card--deep-blue" style="flex: 1;">
        <div class="card-title">
          <div class="vertical-line"></div>
          <img src="@/assets/svgs/data.svg" class="title-icon" />
          <span class="title-text">船舶详情</span>
        </div>
        <div class="card-content">
          <div class="no-selection">请选择一艘船舶以查看详细信息</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

// 定义组件属性
interface ShipItem {
  id: string;
  shipBasicInfo: {
    name: string;
  };
  shorePowerAndShip?: {
    status?: string;
    powerUsage?: number;
  };
  modelInstance?: any;
}

interface Props {
  shipStatusData: ShipItem[];
  selectedShip: ShipItem | null;
}

const props = defineProps<Props>()

// 定义事件
const emit = defineEmits<{
  (e: 'switch-ship', ship: ShipItem): void;
}>()

// 处理船舶选择
const handleSwitch = (ship: ShipItem) => {
  emit('switch-ship', ship)
}

// 状态样式映射
const shorePowerStatusMap = {
  'on': '使用中',
  'off': '未使用',
  'fault': '故障',
  '': '未知'
}

// 获取状态样式类
const getStatusClass = (status: string) => {
  return `status-${status === '使用中' ? 'shorepower' : status === '故障' ? 'fault' : 'default'}`
}
</script>

<style scoped>
.port-overview {
  display: flex;
  gap: 10px;
  height: 100%;
}

.search-container {
  height: 100%;
  width: 200px;
  border-radius: 4px;
  border: 1px solid rgb(10, 130, 170);
  padding: 0 10px;
  margin-bottom: 8px;
  background-color: rgba(255, 255, 255, 0.1);
}
</style>