<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">ID: {{ 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">
              <view class="info-icon location">
                <view class="icon-inner"></view>
              </view>
              <text class="info-text">{{ shop.address }}</text>
            </view>
            <view class="info-row">
              <view class="info-icon contact">
                <view class="icon-inner"></view>
              </view>
              <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-divider"></view>
            <view class="stat-item">
              <text class="stat-value accent">¥{{ formatMoney(shop.todaySales) }}</text>
              <text class="stat-label">今日销售</text>
            </view>
            <view class="stat-divider"></view>
            <view class="stat-item">
              <text class="stat-value primary">¥{{ 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 class="empty-icon-inner"></view>
        </view>
        <text class="empty-text">暂无门店数据</text>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import NavBar from '@/components/NavBar.vue';
import { getShopList } from '@/api/shop';
import { ShopStatusText, ShopStatusColor } from '@/mock/shop';
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) {
    console.error('加载门店列表失败', error);
  } finally {
    loading.value = false;
  }
};

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

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

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

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

/* 门店列表 */
.shop-scroll {
  flex: 1;
  height: 0;
}

.shop-list {
  padding: 16rpx 24rpx;
}

.shop-card {
  background: #ffffff;
  border: 1rpx solid #e2e8f0;
  border-radius: 16rpx;
  margin-bottom: 12rpx;
  overflow: hidden;
  transition: transform 0.15s;
  
  &:active {
    transform: scale(0.98);
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 20rpx 24rpx 16rpx;
  
  .shop-info {
    .shop-name {
      display: block;
      font-size: 30rpx;
      font-weight: 600;
      color: #1e293b;
      margin-bottom: 4rpx;
    }
    
    .shop-no {
      font-size: 22rpx;
      color: #94a3b8;
      font-family: monospace;
    }
  }
  
  .status-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6rpx;
    padding: 8rpx 16rpx;
    border-radius: 8rpx;
    font-size: 22rpx;
    font-weight: 500;
    
    .status-dot {
      width: 8rpx;
      height: 8rpx;
      border-radius: 50%;
    }
    
    &.active {
      background: #ecfdf5;
      color: #10b981;
      .status-dot { background: #10b981; }
    }
    
    &.inactive {
      background: #fff7ed;
      color: #f97316;
      .status-dot { background: #f97316; }
    }
  }
}

.card-body {
  padding: 0 24rpx 16rpx;
  
  .info-row {
    display: flex;
    align-items: center;
    margin-bottom: 8rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .info-icon {
      width: 28rpx;
      height: 28rpx;
      border-radius: 6rpx;
      margin-right: 10rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      
      &.location {
        background: #ecfdf5;
        
        .icon-inner {
          width: 10rpx;
          height: 10rpx;
          background: #10b981;
          border-radius: 50% 50% 50% 0;
          transform: rotate(-45deg);
        }
      }
      
      &.contact {
        background: #f0f9ff;
        
        .icon-inner {
          width: 12rpx;
          height: 12rpx;
          background: #0ea5e9;
          border-radius: 50%;
        }
      }
    }
    
    .info-text {
      font-size: 26rpx;
      color: #475569;
    }
  }
}

.card-stats {
  display: flex;
  align-items: center;
  background: #f8fafc;
  border-top: 1rpx solid #f1f5f9;
  padding: 16rpx 12rpx;
  
  .stat-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    
    .stat-value {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26rpx;
      font-weight: 600;
      color: #1e293b;
      margin-bottom: 4rpx;
      
      &.accent {
        color: #f97316;
      }
      
      &.primary {
        color: #10b981;
      }
    }
    
    .stat-label {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 20rpx;
      color: #94a3b8;
    }
  }
  
  .stat-divider {
    width: 1rpx;
    height: 40rpx;
    background: #e2e8f0;
  }
}

/* 加载状态 */
.loading-more {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  padding: 32rpx;
  color: #94a3b8;
  font-size: 24rpx;
  
  .loading-spinner {
    width: 32rpx;
    height: 32rpx;
    border: 3rpx solid #e2e8f0;
    border-top-color: #10b981;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

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

.no-more {
  text-align: center;
  padding: 32rpx;
  font-size: 24rpx;
  color: #cbd5e1;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100rpx 0;
  
  .empty-icon {
    width: 120rpx;
    height: 120rpx;
    background: #f1f5f9;
    border-radius: 24rpx;
    margin-bottom: 20rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .empty-icon-inner {
      width: 40rpx;
      height: 32rpx;
      background: #cbd5e1;
      border-radius: 0 0 8rpx 8rpx;
      position: relative;
      
      &::before {
        content: '';
        position: absolute;
        top: -12rpx;
        left: 50%;
        transform: translateX(-50%);
        width: 0;
        height: 0;
        border-left: 24rpx solid transparent;
        border-right: 24rpx solid transparent;
        border-bottom: 14rpx solid #cbd5e1;
      }
    }
  }
  
  .empty-text {
    font-size: 28rpx;
    color: #94a3b8;
  }
}
</style>
