<script setup>
import { ref } from "vue";
import { onLoad, onReachBottom } from "@dcloudio/uni-app";
import { getUserInfo, getUserSpots } from "@/api/user";
import { extractList } from "@/utils/request";
import { resolveImageUrl } from "@/utils/image";
import { formatSpotPrice } from "@/utils/spot";
import Skeleton from "@/components/skeleton/skeleton.vue";

const user = ref(null);
const spots = ref([]);
const loading = ref(true);
const spotsLoading = ref(false);
const page = ref(1);
const pageSize = 10;
const hasMore = ref(true);

let userId = null;

const identityMap = {
  photographer: "摄影师",
  cosplayer: "Coser",
  both: "摄影师 / Coser",
};

onLoad(async (query) => {
  if (!query.id) return;
  userId = Number(query.id);
  try {
    user.value = await getUserInfo(userId);
  } catch (e) {
    console.error(e);
  } finally {
    loading.value = false;
  }
  fetchSpots(true);
});

const fetchSpots = async (reset = false) => {
  if (spotsLoading.value) return;
  if (!reset && !hasMore.value) return;
  spotsLoading.value = true;
  if (reset) {
    page.value = 1;
    hasMore.value = true;
  }
  try {
    const res = await getUserSpots(userId, {
      page: page.value,
      page_size: pageSize,
    });
    const list = extractList(res);
    if (reset) {
      spots.value = list;
    } else {
      spots.value.push(...list);
    }
    if (list.length < pageSize) {
      hasMore.value = false;
    } else {
      page.value++;
    }
  } catch (e) {
    console.error(e);
  } finally {
    spotsLoading.value = false;
  }
};

const goDetail = (id) => {
  uni.navigateTo({ url: `/pages/spot/detail?id=${id}` });
};

const loadMore = () => {
  if (!spotsLoading.value && hasMore.value) fetchSpots();
};

onReachBottom(() => {
  loadMore();
});
</script>

<template>
  <view class="user-page">
    <view v-if="loading" class="loading-state">
      <view class="sk-header shimmer" />
      <Skeleton :rows="2" card />
      <Skeleton :rows="2" card />
    </view>

    <template v-else-if="user">
      <view class="user-header">
        <image
          v-if="user.avatar_url"
          class="avatar"
          :src="resolveImageUrl(user.avatar_url)"
          mode="aspectFill"
        />
        <view v-else class="avatar avatar-default">
          <uni-icons type="person" size="32" color="#ffffff" />
        </view>
        <text class="nickname">{{ user.nickname }}</text>
        <text v-if="user.bio" class="bio">{{ user.bio }}</text>
        <view class="meta-row">
          <view v-if="user.city" class="meta-item">
            <uni-icons type="location" size="14" color="rgba(255,255,255,0.8)" />
            <text class="meta-text">{{ user.city }}</text>
          </view>
          <view v-if="user.identity" class="meta-item">
            <uni-icons type="person" size="14" color="rgba(255,255,255,0.8)" />
            <text class="meta-text">{{ identityMap[user.identity] || user.identity }}</text>
          </view>
        </view>
        <view class="stats-row">
          <view class="stat-item">
            <text class="stat-num">{{ spots.length }}</text>
            <text class="stat-label">投稿</text>
          </view>
        </view>
      </view>

      <view class="section-header">
        <text class="section-title">TA 的投稿</text>
      </view>

      <view class="spot-list">
        <view
          v-for="item in spots"
          :key="item.id"
          class="spot-item"
          @tap="goDetail(item.id)"
        >
          <image
            v-if="item.cover_image_url"
            class="spot-cover"
            :src="resolveImageUrl(item.cover_image_url)"
            mode="aspectFill"
          />
          <view v-else class="spot-cover spot-cover-empty">
            <uni-icons type="camera" size="28" color="#94a3b8" />
          </view>
          <view class="spot-info">
            <text class="spot-title">{{ item.title }}</text>
            <view class="spot-meta">
              <uni-icons type="location" size="14" color="#6366f1" />
              <text class="spot-city">{{ item.city }}</text>
            </view>
            <text
              class="spot-price"
              :class="{ free: formatSpotPrice(item).isFree, paid: !formatSpotPrice(item).isFree }"
            >
              {{ formatSpotPrice(item).label }}
            </text>
            <view v-if="item.avg_rating" class="spot-rating">
              <uni-icons type="star-filled" size="14" color="#f59e0b" />
              <text class="spot-rating-num">{{ item.avg_rating.toFixed(1) }}</text>
            </view>
          </view>
          <view class="spot-arrow">
            <uni-icons type="right" size="16" color="#cbd5e1" />
          </view>
        </view>

        <view v-if="spotsLoading" class="status-tip">
          <text>加载中...</text>
        </view>
        <view v-else-if="!hasMore && spots.length > 0" class="status-tip">
          <text>没有更多了</text>
        </view>
        <view v-else-if="!spotsLoading && spots.length === 0" class="status-tip empty">
          <uni-icons type="location" size="40" color="#94a3b8" />
          <text class="empty-text">暂无投稿</text>
        </view>
      </view>
    </template>

    <view v-else class="error-state">
      <text>用户不存在</text>
    </view>
  </view>
</template>

<style scoped>
.user-page {
  min-height: 100vh;
  background: #f5f6fa;
}

.loading-state {
  padding-top: 0;
}
.sk-header {
  width: 100%;
  height: 360rpx;
  background: linear-gradient(135deg, #c7d2fe, #e0e7ff);
}
.shimmer {
  background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.error-state {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  color: #94a3b8;
  font-size: 28rpx;
}

.user-header {
  background: linear-gradient(135deg, #6366f1, #818cf8);
  padding: 48rpx 32rpx 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.avatar {
  width: 140rpx;
  height: 140rpx;
  border-radius: 70rpx;
  border: 4rpx solid rgba(255, 255, 255, 0.3);
  margin-bottom: 16rpx;
}

.avatar-default {
  background: rgba(255, 255, 255, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
}

.nickname {
  font-size: 36rpx;
  font-weight: 700;
  color: #ffffff;
  margin-bottom: 8rpx;
}

.bio {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.75);
  margin-bottom: 12rpx;
  text-align: center;
  max-width: 500rpx;
}

.meta-row {
  display: flex;
  align-items: center;
  gap: 24rpx;
  margin-bottom: 20rpx;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6rpx;
}

.meta-text {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.8);
}

.stats-row {
  display: flex;
  gap: 48rpx;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-num {
  font-size: 36rpx;
  font-weight: 700;
  color: #ffffff;
}

.stat-label {
  font-size: 22rpx;
  color: rgba(255, 255, 255, 0.7);
}

.section-header {
  padding: 24rpx 32rpx 12rpx;
}

.section-title {
  font-size: 30rpx;
  font-weight: 700;
  color: #1e293b;
}

.spot-list {
  padding-bottom: 32rpx;
}

.spot-item {
  display: flex;
  align-items: center;
  background: #ffffff;
  margin: 0 24rpx 16rpx;
  border-radius: 16rpx;
  padding: 20rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
}

.spot-cover {
  width: 140rpx;
  height: 140rpx;
  border-radius: 12rpx;
  flex-shrink: 0;
  margin-right: 20rpx;
}

.spot-cover-empty {
  background: #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.spot-info {
  flex: 1;
  min-width: 0;
}

.spot-title {
  font-size: 28rpx;
  font-weight: 600;
  color: #1e293b;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 8rpx;
}

.spot-meta {
  display: flex;
  align-items: center;
  gap: 4rpx;
  margin-bottom: 6rpx;
}

.spot-city {
  font-size: 24rpx;
  color: #64748b;
}

.spot-rating {
  display: flex;
  align-items: center;
  gap: 4rpx;
}

.spot-price {
  display: block;
  margin-bottom: 6rpx;
  font-size: 24rpx;
  font-weight: 600;
}

.spot-price.free {
  color: #16a34a;
}

.spot-price.paid {
  color: #d97706;
}

.spot-rating-num {
  font-size: 24rpx;
  color: #f59e0b;
  font-weight: 600;
}

.spot-arrow {
  flex-shrink: 0;
  margin-left: 8rpx;
}

.status-tip {
  text-align: center;
  padding: 40rpx 0;
  color: #94a3b8;
  font-size: 26rpx;
}

.status-tip.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60rpx 0;
}

.empty-text {
  margin-top: 12rpx;
  font-size: 26rpx;
  color: #94a3b8;
}
</style>
