<template>
  <view class="page">
    <NavBar title="门店管理" :showBack="true" />

    <scroll-view
      class="shop-scroll"
      scroll-y
      @scrolltolower="loadMore"
    >
      <view class="shop-list">
        <view
          class="shop-card"
          v-for="shop in shopList"
          :key="shop.id"
          @click="goDetail(shop.id)"
        >
          <view class="card-header">
            <view class="shop-info">
              <text class="shop-name">{{ shop.name }}</text>
              <text class="shop-no">{{ shop.id }}</text>
            </view>
            <view class="status-badge" :class="getStatusClass(shop.status)">
              <view class="status-dot"></view>
              <text>{{ getStatusText(shop.status) }}</text>
            </view>
          </view>

          <view class="card-body">
            <view class="info-row">
              <text class="info-label">地址</text>
              <text class="info-text">{{ shop.address }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">联系人</text>
              <text class="info-text">{{ shop.contactName }}  {{ shop.contactPhone }}</text>
            </view>
          </view>

          <view class="card-stats">
            <view class="stat-item">
              <text class="stat-value">{{ shop.deviceCount }}</text>
              <text class="stat-label">设备数</text>
            </view>
            <view class="stat-item">
              <text class="stat-value">¥{{ formatMoney(shop.todaySales) }}</text>
              <text class="stat-label">今日销售</text>
            </view>
            <view class="stat-item">
              <text class="stat-value">¥{{ formatMoney(shop.monthSales) }}</text>
              <text class="stat-label">本月销售</text>
            </view>
          </view>
        </view>
      </view>

      <view class="loading-more" v-if="loading">
        <view class="loading-spinner"></view>
        <text>加载中...</text>
      </view>

      <view class="no-more" v-if="!hasMore && shopList.length > 0">
        <text>没有更多了</text>
      </view>

      <view class="empty-state" v-if="!loading && shopList.length === 0">
        <view class="empty-icon"></view>
        <text class="empty-title">暂无门店</text>
        <text class="empty-desc">门店数据将在此处展示</text>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { logger } from '@/utils/logger';
import NavBar from '@/components/NavBar.vue';
import { getShopList } from '@/api/shop';
import { ShopStatusText, ShopStatusColor } from '@/utils/constants';
import { formatMoney as formatMoneyUtil } from '@/utils/common';

const shopList = ref<any[]>([]);
const loading = ref(false);
const hasMore = ref(true);
const page = ref(1);
const pageSize = 10;

const formatMoney = formatMoneyUtil;

const getStatusText = (status: number) => ShopStatusText[status] || '未知';

const getStatusClass = (status: number) => {
  if (status === 1) return 'active';
  return 'inactive';
};

const loadShops = async () => {
  loading.value = true;
  try {
    const res = await getShopList({
      page: page.value,
      pageSize
    });
    
    if (page.value === 1) {
      shopList.value = res.list;
    } else {
      shopList.value = [...shopList.value, ...res.list];
    }
    
    hasMore.value = shopList.value.length < res.total;
  } catch (error) {
    logger.warn('加载门店列表失败', error);
  } finally {
    loading.value = false;
  }
};

const loadMore = () => {
  if (loading.value || !hasMore.value) return;
  page.value++;
  loadShops();
};

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

onMounted(() => {
  loadShops();
});
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background: $bg-color-page;
  display: flex;
  flex-direction: column;
}

.shop-scroll {
  flex: 1;
  height: 0;
}

.shop-list {
  padding: $spacing-2 $spacing-3;
}

.shop-card {
  background: $bg-color-card;
  border-radius: $radius-lg;
  margin-bottom: $spacing-2;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
  transition: transform 0.15s;

  &:active {
    transform: scale(0.985);
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: $spacing-3 $spacing-3 $spacing-2;

  .shop-info {
    flex: 1;

    .shop-name {
      display: block;
      font-size: $font-size-lg;
      font-weight: 600;
      color: $text-color-primary;
      margin-bottom: 4rpx;
      line-height: 1.3;
    }

    .shop-no {
      font-size: $font-size-xs;
      color: $text-color-muted;
    }
  }

  .status-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6rpx;
    padding: 6rpx 14rpx;
    border-radius: $radius-full;
    font-size: 20rpx;
    font-weight: 500;
    flex-shrink: 0;

    .status-dot {
      width: 10rpx;
      height: 10rpx;
      border-radius: 50%;
    }

    &.active {
      background: $success-color-bg;
      color: $success-color;
      .status-dot { background: $success-color; }
    }

    &.inactive {
      background: $bg-color-secondary;
      color: $text-color-muted;
      .status-dot { background: $text-color-placeholder; }
    }
  }
}

.card-body {
  padding: 0 $spacing-3 $spacing-2;

  .info-row {
    display: flex;
    align-items: flex-start;
    margin-bottom: 10rpx;

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

    .info-label {
      font-size: 24rpx;
      color: $text-color-muted;
      flex-shrink: 0;
      margin-right: $spacing-1;
    }

    .info-text {
      font-size: 26rpx;
      color: $text-color-secondary;
      line-height: 1.5;
    }
  }
}

.card-stats {
  display: flex;
  align-items: center;
  background: #FAFAFA;
  padding: 16rpx 8rpx;

  .stat-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .stat-value {
      font-size: 26rpx;
      font-weight: 600;
      color: $text-color-primary;
      margin-bottom: 4rpx;
    }

    .stat-label {
      font-size: 20rpx;
      color: $text-color-muted;
    }
  }
}

.loading-more {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  padding: 40rpx;
  color: $text-color-muted;
  font-size: 24rpx;

  .loading-spinner {
    width: 32rpx;
    height: 32rpx;
    border: 3rpx solid $border-color;
    border-top-color: $text-color-muted;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.no-more {
  text-align: center;
  padding: 40rpx;
  font-size: 24rpx;
  color: $text-color-placeholder;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 120rpx 0 80rpx;

  .empty-icon {
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
    background: $bg-color-secondary;
    margin-bottom: 24rpx;
    position: relative;

    &::before,
    &::after {
      content: '';
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      background: $text-color-placeholder;
    }

    &::before {
      top: 32rpx;
      width: 28rpx;
      height: 28rpx;
      border-radius: 50%;
    }

    &::after {
      top: 68rpx;
      width: 44rpx;
      height: 6rpx;
      border-radius: 3rpx;
    }
  }

  .empty-title {
    font-size: $font-size-base;
    font-weight: 500;
    color: $text-color-secondary;
    margin-bottom: 8rpx;
  }

  .empty-desc {
    font-size: $font-size-sm;
    color: $text-color-muted;
  }
}
</style>
