<template>
  <div class="info-window-content">
    <!-- 关闭按钮 -->
    <div class="info-window-header">
      <h3>{{ title }}</h3>
      <button class="close-btn" @click="handleClose">×</button>
    </div>

    <!-- 数据展示 -->
    <div class="info-window-body">
      <div v-if="data">
        {{ data }}
      </div>
      <div v-else class="no-data">
        暂无数据
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'InfoWindowComponent',
  props: {
    data: {
      type: [Object, Array],
      default: null
    },
    title: {
      type: String,
      default: '巡检计划详情'
    }
  },
  computed: {
    // 格式化数据，只显示简单类型（避免显示复杂对象）
    formattedData() {
      if (!this.data) return {}

      const result = {}
      const dataObj = Array.isArray(this.data) ? this.data[0] || {} : this.data

      Object.entries(dataObj).forEach(([key, value]) => {
        // 只显示简单类型（字符串、数字、布尔值）
        if (typeof value !== 'object' || value === null) {
          result[key] = value
        } else if (Array.isArray(value)) {
          result[key] = `数组(${value.length}项)`
        } else {
          result[key] = '对象'
        }
      })

      return result
    }
  },
  methods: {
    handleClose() {
      // 触发父组件传递的 close 事件
      this.$emit('close')
    }
  }
}
</script>

<style scoped>
.info-window-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  min-width: 200px;
  max-width: 300px;
  overflow: hidden;
}

.info-window-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: linear-gradient(135deg, #1890ff, #096dd9);
  color: white;
}

.info-window-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.info-window-body {
  padding: 16px;
  max-height: 400px;
  overflow-y: auto;
}

.data-item {
  margin-bottom: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;
}

.data-item:last-child {
  margin-bottom: 0;
  border-bottom: none;
}

.data-key {
  font-weight: 600;
  color: #666;
  margin-right: 8px;
}

.data-value {
  color: #333;
  word-break: break-all;
}

.no-data {
  text-align: center;
  color: #999;
  padding: 20px 0;
}
</style>
