<script setup>
import { ref, onMounted, computed, onBeforeUnmount } from 'vue'
import { typeOneService } from '@/api/type'
import { informationListService } from '@/api/information'
import { useRoute } from 'vue-router'
import { useInfoStore } from '@/stores'
import DefaultImg from '@/assets/cover_1.jpg'
import BaseLayout from '@/components/BaseLayout.vue'
// 获取路由实例
const route = useRoute()

// 数据初始化
const showDropdown = ref(false)
const searchQuery = ref('')

// 场馆信息
const venueInfo = ref({})
// 获取场馆信息
const getVenueInfo = async () => {
  const {
    data: { data }
  } = await typeOneService(route.query.id ? route.query.id : 1)
  venueInfo.value = { ...data }
  getVenueCourts()
}
onMounted(() => {
  getVenueInfo()
})
// 场地数据
const venueCourts = ref([])
// 加载场馆下场地列表
const loading = ref(false)
const getVenueCourts = async () => {
  loading.value = true
  const {
    data: { data }
  } = await informationListService(venueInfo.value.id)
  console.log(data)

  venueCourts.value = data
  loading.value = false
}
// 计算属性：筛选场地
const filteredCourts = computed(() => {
  return venueCourts.value.filter((court) =>
    court.venueName.toLowerCase().includes(searchQuery.value.toLowerCase())
  )
})

const infoStore = useInfoStore()
const reserveCourt = (id, name, price) => {
  // 将场馆信息、场地信息加入pinia中
  infoStore.setInfo({
    typeId: venueInfo.value.id,
    typeName: venueInfo.value.vname,
    informationId: id,
    informationName: name,
    price: price
  })
}

// 点击页面其他区域关闭下拉菜单
const handleClickOutside = (event) => {
  const profileElement = document.querySelector('.user-profile')
  const dropdownElement = document.querySelector('.user-dropdown')

  if (profileElement && dropdownElement) {
    if (
      !profileElement.contains(event.target) &&
      !dropdownElement.contains(event.target)
    ) {
      showDropdown.value = false
    }
  }
}

// 添加点击事件监听
document.addEventListener('click', handleClickOutside)

// 组件卸载时移除事件监听
onBeforeUnmount(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<template>
  <BaseLayout>
    <div class="venue-details-container">
      <!-- 主要内容区域 -->
      <main class="main-content">
        <!-- 场馆信息卡片 -->
        <div class="venue-info-card">
          <div class="card-header">
            <div class="card-title">{{ venueInfo.vname }}</div>
            <div class="card-subtitle">场馆详情</div>
          </div>
          <div class="card-content">
            <div class="venue-banner">
              <img
                :src="venueInfo.imgUrl"
                :alt="venueInfo.vname"
                class="banner-image"
              />
            </div>
            <div class="venue-details">
              <div class="detail-item">
                <i class="el-icon-location-outline detail-icon"></i>
                <span class="detail-label">位置:</span>
                <span class="detail-value">{{ venueInfo.location }}</span>
              </div>
              <div class="detail-item">
                <i class="el-icon-time detail-icon"></i>
                <span class="detail-label">开馆时间:</span>
                <span class="detail-value">{{ venueInfo.openTime }}</span>
              </div>
              <div class="detail-item">
                <i class="el-icon-document detail-icon"></i>
                <span class="detail-label">闭馆时间:</span>
                <span class="detail-value">{{ venueInfo.closeTime }}</span>
              </div>
              <div class="detail-item">
                <i class="el-icon-coin detail-icon"></i>
                <span class="detail-label"
                  >场地数量：{{ venueCourts.length }}</span
                >
                <span class="detail-value"></span>
              </div>
            </div>
          </div>
        </div>

        <!-- 场地列表 -->
        <div class="court-list-container">
          <div class="section-header">
            <h2 class="section-title">场地列表</h2>
          </div>

          <div class="court-grid">
            <div
              v-for="court in filteredCourts"
              :key="court.id"
              class="court-card"
              v-loading="loading"
            >
              <div class="card-content">
                <div class="court-header">
                  <div class="court-name">{{ court.venueName }}</div>
                  <div class="court-type">
                    <i class="el-icon-ribbon"></i> {{ court.type }}
                  </div>
                </div>
                <div class="court-info">
                  <div class="info-item">
                    <img
                      :src="court.imgUrl === '' ? DefaultImg : court.imgUrl"
                      :alt="court.venueName + '图片信息'"
                      :title="court.venueName + '图片信息'"
                      style="width: 100%"
                    />
                  </div>
                  <div class="info-item">
                    <i class="el-icon-location-outline info-icon"></i>
                    <span>具体位置：{{ court.location }}</span>
                  </div>
                  <div class="info-item">
                    <i class="el-icon-coin info-icon"></i>
                    <span style="font-weight: 700"
                      >使用价格：¥ <span>{{ court.price }}</span> 元 /
                      小时</span
                    >
                  </div>
                </div>
              </div>
              <div class="card-footer">
                <router-link :to="{ path: '/mobile/time' }"
                  ><el-button
                    type="primary"
                    class="reserve-btn"
                    @click="
                      reserveCourt(court.id, court.venueName, court.price)
                    "
                  >
                    预约场地
                  </el-button>
                </router-link>
              </div>
            </div>
          </div>
        </div>
      </main>
    </div>
  </BaseLayout>
</template>
<style scoped>
/* 全局样式 */
:root {
  --primary-color: #1e40af;
  --secondary-color: #3b82f6;
  --text-color: #334155;
  --bg-gradient: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
  --card-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  --card-hover-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

/* 应用容器 */
.venue-details-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: linear-gradient(135deg, #f8fafc 0%, #6c83a1 100%);
}

/* 头部样式 */
.header {
  background: var(--bg-gradient);
  color: white;
  padding: 16px 0;
  position: relative;
  z-index: 1000;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 32px;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
}

.logo-icon {
  margin-right: 8px;
  font-size: 24px;
}

.title {
  font-size: 20px;
  font-weight: 500;
}

.user-profile {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: opacity 0.2s;
}

.user-profile:hover {
  opacity: 0.9;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.user-name {
  margin-right: 8px;
  font-weight: 500;
}

.user-icon {
  font-size: 14px;
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 32px;
  width: 280px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  animation: fadeIn 0.2s ease-in-out;
  margin-top: 8px;
  overflow: hidden;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e2e8f0;
}

.dropdown-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
}

.dropdown-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-color);
}

.dropdown-email {
  font-size: 14px;
  color: #64748b;
}

.dropdown-divider {
  height: 1px;
  background: #e2e8f0;
}

.dropdown-menu {
  border: none !important;
}

.logout-item {
  color: #ef4444 !important;
}

.logout-item:hover {
  background: #fef2f2 !important;
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  max-width: 1240px;
  width: 100%;
  margin: 0 auto;
  padding: 32px;
}

/* 返回按钮 */
.back-button-container {
  margin-bottom: 24px;
}

/* 场馆信息卡片 */
.venue-info-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--card-shadow);
  margin-bottom: 40px;
}

.card-header {
  background: var(--bg-gradient);
  color: rgb(8, 7, 7);
  padding: 20px 32px;
}

.card-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 4px;
}

.card-subtitle {
  font-size: 16px;
  opacity: 0.8;
}

.card-content {
  padding: 32px;
}

.venue-banner {
  margin-bottom: 24px;
  border-radius: 8px;
  overflow: hidden;
}

.banner-image {
  width: 70%;
  height: 30%;
  margin-left: 11vw;
  object-fit: cover;
}

.venue-details {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.detail-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
}

.detail-icon {
  color: var(--secondary-color);
  font-size: 18px;
  margin-right: 12px;
}

.detail-label {
  font-weight: 500;
  color: #64748b;
  margin-right: 8px;
}

.detail-value {
  color: var(--text-color);
}

/* 场地列表部分 */
.court-list-container {
  margin-bottom: 40px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-color);
}

.section-filter {
  display: flex;
  gap: 16px;
}

.search-input {
  width: 280px;
}

.search-icon {
  color: #94a3b8;
}

.court-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
}

.court-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--card-shadow);
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.court-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--card-hover-shadow);
}

.card-content {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.court-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.court-name {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-color);
}

.court-type {
  background: #e0f2fe;
  color: #0284c7;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.court-info {
  /* display: flex; */
  flex-wrap: wrap;
  gap: 12px;
}

.info-item {
  /* display: flex; */
  margin-bottom: 15px;
  align-items: center;
  font-size: 16px;
  color: #64748b;
}

.info-icon {
  margin-right: 4px;
  color: var(--secondary-color);
}

.card-footer {
  padding: 16px;
  border-top: 1px solid #e2e8f0;
  display: flex;
  justify-content: center;
}

.reserve-btn {
  width: 100%;
  padding: 10px 16px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
}

/* 页脚样式 */
.footer {
  background: var(--bg-gradient);
  color: white;
  padding: 40px 0;
}

.footer-content {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 32px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 32px;
}

.footer-info {
  max-width: 300px;
}

.footer-logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 12px;
}

.footer-desc {
  font-size: 16px;
  line-height: 1.6;
  color: rgba(255, 255, 255, 0.8);
}

.footer-links {
  display: flex;
  gap: 64px;
}

.footer-column {
  min-width: 200px;
}

.column-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
}

.column-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.column-list li {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  color: rgba(255, 255, 255, 0.8);
}

.footer-icon {
  margin-right: 8px;
  width: 16px;
  text-align: center;
}

.link-item {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  transition: color 0.2s;
}

.link-item:hover {
  color: white;
  text-decoration: underline;
}

.footer-copyright {
  text-align: center;
  padding-top: 24px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 14px;
  color: rgba(255, 255, 255, 0.6);
}

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

/* 响应式布局 */
@media (max-width: 1024px) {
  .header-content,
  .main-content,
  .footer-content {
    padding: 0 24px;
  }

  .title {
    display: none;
  }

  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }

  .section-filter {
    width: 100%;
    flex-direction: column;
    gap: 12px;
  }

  .search-input {
    width: 100%;
  }

  .footer-content {
    flex-direction: column;
    gap: 32px;
  }

  .footer-links {
    flex-direction: column;
    gap: 32px;
  }

  .venue-banner {
    height: 200px;
  }
}

@media (max-width: 768px) {
  .carousel-container {
    height: 200px;
  }

  .carousel-title {
    font-size: 18px;
  }

  .carousel-desc {
    font-size: 14px;
  }

  .venue-grid,
  .court-grid {
    grid-template-columns: 1fr;
  }
}
</style>
