<template>
  <div class="center-info-card-container" v-if="visible" @click="handleClick">
    <div
      class="center-info-card"
      :style="{ backgroundImage: `url(${districtIcon})` }"
    >
      <!-- 标题栏 -->
      <div class="card-header">
        <div class="header-left">
          <img class="location-icon" :src="locationIcon" alt="" />
          <span class="header-title">{{ title }}</span>
        </div>
        <span class="header-arrow">
          <el-icon><ArrowRight /></el-icon>
        </span>
      </div>
      <!-- 内容区域 -->
      <div class="card-content">
        <div class="info-row">
          <span class="info-text">人数：{{ item?.population || "-" }}人</span>
          <span class="info-text">
            <span v-if="item?.type == 'project'">项目：</span>
            <span v-else>路段：</span>
            <span>{{ item?.entityCount || "-" }}</span>
            <span v-if="item?.type == 'project'">个</span>
            <span v-else>条</span>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue";
import { ArrowRight, Close } from "@element-plus/icons-vue";
import districtIcon from "../../../assets/MaMap_img/区县弹窗背景@2x.png";
import locationIcon from "../../../assets/MaMap_img/区县icon@2x.png";

const props = defineProps({
  // 是否显示卡片
  visible: {
    type: Boolean,
    default: false,
  },
  // 卡片类型：'second' | 'third' | null
  type: {
    type: String,
    default: null,
  },
  // 标题
  title: {
    type: String,
    default: "",
  },
  // 数据列表
  dataList: {
    type: Array,
    default: () => [],
  },
  // 数据项
  item: {
    type: Object,
    default: () => {},
  },
  // 人数
  peopleCount: {
    type: [Number, String],
    default: 0,
  },
  // 路段数
  roadCount: {
    type: [Number, String],
    default: 117,
  },
  // 是否显示关闭按钮
  showClose: {
    type: Boolean,
    default: true,
  },
});

const emit = defineEmits(["click", "close", "itemClick", "onClick"]);

// 是否显示卡片（兼容旧版本）
const showCard = computed(() => {
  // 如果有 visible 属性，直接使用
  if (props.visible !== undefined) {
    return props.visible;
  }
  // 否则使用旧版本的类型判断
  return props.type === "second" || props.type === "third";
});

// 获取公路类型文本
const getRoadTypeText = (roadType) => {
  const roadTypeMap = {
    national: "国省道",
    rural: "农村公路",
  };
  return roadTypeMap[roadType] || roadType;
};

// 点击事件（兼容旧版本）
const handleClick = () => {
  console.log("centerInfoCard clicked, type:", props.type);
  // 触发 onClick 事件
  emit("onClick");
  // 同时触发旧版本的 click 事件
  emit("click", props.type);
};

// 关闭事件
const handleClose = () => {
  emit("close");
};

// 数据项点击事件
const handleItemClick = (item) => {
  emit("itemClick", item);
};
</script>

<style lang="scss" scoped>
@function vw($px) {
  @return calc($px / 1920 * 100vw);
}

.center-info-card-container {
  position: relative;
  width: 100%;
  min-width: 140px;
  z-index: 1000;
  cursor: pointer;
}

// 中心数据展示卡片
.center-info-card {
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  z-index: 50;
  cursor: pointer;
  transition: all 0.3s;
  overflow: hidden;

  &:hover {
    filter: brightness(1.1);
  }

  // 标题栏
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: vw(10) vw(14);

    .header-left {
      display: flex;
      align-items: center;
      gap: vw(6);

      .location-icon {
        width: vw(16);
        height: vw(16);
      }

      .header-title {
        font-size: vw(14);
        font-weight: 600;
        color: #fff;
      }
    }

    .header-arrow,
    .header-close {
      font-size: vw(14);
      color: rgba(255, 255, 255, 0.7);
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        color: #fff;
      }
    }
  }

  // 内容区域
  .card-content {
    padding: vw(8) vw(14) vw(12);

    .info-row {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .info-text {
        font-size: vw(12);
        color: rgba(255, 255, 255, 0.9);
      }
    }

    .data-list {
      .data-item {
        padding: vw(4) 0;

        .item-row {
          display: flex;
          align-items: center;
          margin-bottom: vw(2);

          &:last-child {
            margin-bottom: 0;
          }

          .item-label {
            font-size: vw(11);
            color: rgba(255, 255, 255, 0.7);
          }

          .item-value {
            font-size: vw(11);
            color: #fff;
            font-weight: 500;
          }
        }
      }
    }
  }
}
</style>
