<template>
  <view class="page">
    <!-- 导航栏 -->
    <NavBar title="库存预警" :showBack="true" />

    <!-- 预警统计 -->
    <view class="alert-section">
      <view class="alert-grid">
        <view class="alert-card warning">
          <view class="alert-icon">
            <view class="icon-alert"></view>
          </view>
          <view class="alert-info">
            <text class="alert-value">{{ stats.lowStockCount || 0 }}</text>
            <text class="alert-label">低库存商品</text>
          </view>
        </view>
        <view class="alert-card error">
          <view class="alert-icon">
            <view class="icon-out"></view>
          </view>
          <view class="alert-info">
            <text class="alert-value">{{ stats.zeroStockCount || 0 }}</text>
            <text class="alert-label">缺货商品</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 标签切换 -->
    <view class="tab-section">
      <view class="tab-list">
        <view
          class="tab-item"
          :class="{ active: currentTab === 'low' }"
          @click="changeTab('low')"
        >
          <text>低库存</text>
        </view>
        <view
          class="tab-item"
          :class="{ active: currentTab === 'out' }"
          @click="changeTab('out')"
        >
          <text>已缺货</text>
        </view>
      </view>
    </view>

    <!-- 预警列表 -->
    <scroll-view
      class="warning-scroll"
      scroll-y
      @scrolltolower="loadMore"
    >
      <view class="warning-list">
        <view
          class="warning-card"
          v-for="item in warningList"
          :key="item.id"
        >
          <view class="card-header">
            <view class="product-info">
              <text class="product-name">{{ item.productName }}</text>
              <text class="location">{{ item.shopName }} · {{ item.deviceName }}</text>
            </view>
            <view class="stock-badge" :class="currentTab">
              <text>{{ item.currentStock || 0 }}</text>
            </view>
          </view>

          <view class="card-body">
            <view class="progress-bar">
              <view
                class="progress-fill"
                :class="currentTab"
                :style="{ width: getProgressWidth(item) + '%' }"
              ></view>
            </view>
            <view class="stock-detail">
              <text class="detail-text">预警线: {{ item.minStock || 0 }}</text>
              <text class="detail-text">上限: {{ item.maxStock || 0 }}</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 && warningList.length > 0">
        <text>— 没有更多了 —</text>
      </view>

      <view class="empty-state" v-if="!loading && warningList.length === 0">
        <view class="empty-icon">
          <view class="empty-icon-inner"></view>
        </view>
        <text class="empty-text">{{ currentTab === 'low' ? '暂无低库存商品' : '暂无缺货商品' }}</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 { getInventoryList, getInventoryStatistics } from '@/api/inventory';

const currentTab = ref('low');
const warningList = ref<any[]>([]);
const stats = ref({
  lowStockCount: 0,
  zeroStockCount: 0
});
const loading = ref(false);
const hasMore = ref(true);
const page = ref(1);
const pageSize = 10;

const getProgressWidth = (item: any) => {
  if (!item.maxStock || item.maxStock === 0) return 0;
  return Math.min(100, ((item.currentStock || 0) / item.maxStock) * 100);
};

const loadStats = async () => {
  try {
    const res = await getInventoryStatistics();
    if (res) {
      stats.value.lowStockCount = res.lowStockCount || 0;
      stats.value.zeroStockCount = res.zeroStockCount || 0;
    }
  } catch (error) {
    logger.warn('加载库存统计失败', error);
  }
};

const changeTab = (tab: string) => {
  currentTab.value = tab;
  page.value = 1;
  hasMore.value = true;
  loadWarnings();
};

const loadWarnings = async () => {
  loading.value = true;
  try {
    const params: any = { page: page.value, pageSize };
    if (currentTab.value === 'low') {
      params.lowStock = true;
    } else if (currentTab.value === 'out') {
      params.stockStatus = 0;
    }

    const res = await getInventoryList(params);
    if (!res) return;
    const list = res.list || [];

    if (page.value === 1) {
      warningList.value = list;
    } else {
      warningList.value = [...warningList.value, ...list];
    }

    hasMore.value = warningList.value.length < (res.total || 0);
  } catch (error) {
    logger.warn('加载预警列表失败', error);
  } finally {
    loading.value = false;
  }
};

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

onMounted(() => {
  loadStats();
  loadWarnings();
});
</script>

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

/* 预警统计 */
.alert-section {
  padding: 16rpx 24rpx;
  background: $bg-color-card;
  border-bottom: 1rpx solid $border-color;
}

.alert-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12rpx;
}

.alert-card {
  display: flex;
  align-items: center;
  padding: 24rpx 20rpx;
  border-radius: 16rpx;
  border: 1rpx solid $border-color;

  &.warning {
    background: $accent-color-bg;
    border-color: $accent-color;
  }

  &.error {
    background: $error-color-bg;
    border-color: $error-color;
  }
}

.alert-icon {
  width: 64rpx;
  height: 64rpx;
  border-radius: 16rpx;
  background: $bg-color-card;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16rpx;
  flex-shrink: 0;

  .icon-alert {
    width: 28rpx;
    height: 28rpx;
    border: 3rpx solid $accent-color;
    border-radius: 50%;
    position: relative;

    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 3rpx;
      height: 12rpx;
      background: $accent-color;
    }

    &::after {
      content: '';
      position: absolute;
      bottom: 4rpx;
      left: 50%;
      transform: translateX(-50%);
      width: 3rpx;
      height: 3rpx;
      background: $accent-color;
      border-radius: 50%;
    }
  }

  .icon-out {
    width: 28rpx;
    height: 28rpx;
    border: 3rpx solid $error-color;
    border-radius: 50%;
    position: relative;

    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) rotate(45deg);
      width: 16rpx;
      height: 3rpx;
      background: $error-color;
    }

    &::after {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) rotate(-45deg);
      width: 16rpx;
      height: 3rpx;
      background: $error-color;
    }
  }
}

.alert-info {
  .alert-value {
    display: block;
    font-size: 40rpx;
    font-weight: 700;
    color: $text-color-primary;
    margin-bottom: 4rpx;
  }

  .alert-label {
    font-size: 24rpx;
    color: $text-color-tertiary;
  }
}

/* 标签切换 */
.tab-section {
  padding: 12rpx 24rpx;
  background: $bg-color-card;
}

.tab-list {
  display: flex;
  gap: 10rpx;

  .tab-item {
    flex: 1;
    padding: 14rpx 0;
    text-align: center;
    background: $bg-color-page;
    border: 2rpx solid $border-color;
    border-radius: 20rpx;
    font-size: 26rpx;
    color: $text-color-tertiary;
    transition: all 0.15s;

    &.active {
      background: $success-color-bg;
      border-color: $primary-color;
      color: $primary-color;
      font-weight: 500;
    }
  }
}

/* 预警列表 */
.warning-scroll {
  flex: 1;
  height: 0;
}

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

.warning-card {
  background: $bg-color-card;
  border: 1rpx solid $border-color;
  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 12rpx;

  .product-info {
    flex: 1;
    min-width: 0;

    .product-name {
      display: block;
      font-size: 28rpx;
      font-weight: 600;
      color: $text-color-primary;
      margin-bottom: 4rpx;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .location {
      font-size: 22rpx;
      color: $text-color-muted;
    }
  }
}

.stock-badge {
  padding: 8rpx 20rpx;
  border-radius: 12rpx;
  font-size: 28rpx;
  font-weight: 700;
  margin-left: 12rpx;
  flex-shrink: 0;

  &.low {
    background: $accent-color-bg;
    color: $accent-color;
  }

  &.out {
    background: $error-color-bg;
    color: $error-color;
  }
}

.card-body {
  padding: 0 24rpx 20rpx;

  .progress-bar {
    height: 10rpx;
    background: $bg-color-secondary;
    border-radius: 5rpx;
    overflow: hidden;
    margin-bottom: 10rpx;

    .progress-fill {
      height: 100%;
      border-radius: 5rpx;

      &.low { background: $accent-color; }
      &.out { background: $error-color; }
    }
  }

  .stock-detail {
    display: flex;
    justify-content: space-between;

    .detail-text {
      font-size: 22rpx;
      color: $text-color-muted;
    }
  }
}

/* 加载状态 */
.loading-more {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12rpx;
  padding: 32rpx;
  color: $text-color-muted;
  font-size: 24rpx;

  .loading-spinner {
    width: 32rpx;
    height: 32rpx;
    border: 3rpx solid $border-color;
    border-top-color: $primary-color;
    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: $text-color-placeholder;
}

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

  .empty-icon {
    width: 120rpx;
    height: 120rpx;
    background: $bg-color-secondary;
    border-radius: 24rpx;
    margin-bottom: 20rpx;
    display: flex;
    align-items: center;
    justify-content: center;

    .empty-icon-inner {
      width: 48rpx;
      height: 48rpx;
      border: 4rpx solid $text-color-placeholder;
      border-radius: 50%;
      position: relative;

      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 20rpx;
        height: 20rpx;
        background: $text-color-placeholder;
        border-radius: 50%;
      }
    }
  }

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