<template>
  <div class="ms-page container-height">
    <!-- ===== AURORA BACKGROUND ===== -->
    <div class="ms-aurora-bg" aria-hidden="true">
      <div class="ms-orb ms-orb-1"></div>
      <div class="ms-orb ms-orb-2"></div>
      <div class="ms-orb ms-orb-3"></div>
      <div class="ms-grid-overlay"></div>
      <div class="ms-noise-texture"></div>
    </div>

    <!-- ===== MAIN CONTENT ===== -->
    <div class="ms-content">
      <!-- Breadcrumb -->
      <div class="ms-breadcrumb-wrapper">
        <Breadcrumb />
      </div>

      <!-- Page Header -->
      <div class="ms-header">
        <div class="ms-header-content">
          <div class="ms-title-wrapper">
            <!-- <div class="ms-title-line"></div> -->
            <h1 class="ms-title">{{$t('route.mibiShop')}}</h1>
          </div>
          <p class="ms-subtitle">{{$t('common.mibiShopSubtitle')}}</p>
        </div>
      </div>

      <!-- Products Grid -->
      <div class="ms-products-grid">
        <div
          v-for="(item, index) in list"
          :key="item.itemId"
          class="ms-product-card"
          :style="{ animationDelay: `${index * 50}ms` }"
        >
          <!-- Product Image -->
          <div class="ms-product-image">
            <img :src="item.imageUrl" :alt="item.itemName" class="ms-image">
            <div class="ms-image-overlay"></div>
            <div class="ms-image-glow"></div>
            <div v-if="item.discountPoints" class="ms-discount-badge">
              <span class="ms-discount-text">{{ Math.round((1 - item.discountPoints / item.points) * 100) }}% OFF</span>
            </div>
          </div>

          <!-- Product Info -->
          <div class="ms-product-info">
            <h3 class="ms-product-title">{{ item.itemName }}</h3>
            
            <div class="ms-price-section">
              <div class="ms-price-container">
                <div v-if="item.discountPoints" class="ms-price-wrapper">
                  <span class="ms-discount-price">{{ item.discountPoints }}</span>
                  <span class="ms-currency">{{ $t('common.mibi') }}</span>
                  <span class="ms-original-price">{{ item.points }} {{ $t('common.mibi') }}</span>
                </div>
                <div v-else class="ms-price-wrapper">
                  <span class="ms-discount-price">{{ item.points }}</span>
                  <span class="ms-currency">{{ $t('common.mibi') }}</span>
                </div>
              </div>
              
              <button 
                class="ms-exchange-btn gradient"
                @click="isLogin({callback: openLoginDialog,t}) && confirmBuy({
                  callback: initList,
                  appStore,
                  router,
                  type: 'pointsBalance',
                  price: item.discountPoints ? item.discountPoints : item.points,
                  t,
                  productId: item.itemId,
                  orderType: 'mi_mall',
                  payMethod: 'MI',
                  paySuccessMsg: $t('common.mibiShopPaypSuccessMsg')
                })"
              >
                <span class="ms-btn-text">{{ $t('common.exchange') }}</span>
                <div class="ms-btn-glow"></div>
              </button>
            </div>
          </div>

          <div class="ms-card-indicator"></div>
        </div>
      </div>

      <!-- Empty State -->
      <div v-if="list.length === 0" class="ms-empty-state">
        <el-empty :image-size="120" :description="$t('common.noData')" />
      </div>

      <!-- Pagination -->
      <div v-if="list.length > 0" class="ms-pagination">
        <Pagination 
          :total="listTotal"
          :page-size="searchFom.pageSize"
          :current-page="searchFom.pageNum"
          @page-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import Pagination from '@/components/Pagination.vue'
import Breadcrumb from '@/components/Breadcrumb.vue'
import { ref, onMounted, reactive, inject } from 'vue'
import { useRouter } from 'vue-router'
import { useAppStore } from '@/pinia/appStore'
import { getMallList } from '@/api/mall.js'
import { confirmBuy, isLogin, openFullScreenLoading } from '@/utils/util.js'
import { useI18n } from 'vue-i18n' 
const { t } = useI18n() 
const appStore = useAppStore()
const router = useRouter()
const openLoginDialog = inject('openLoginDialog')

// Pagination data
const list = ref([]);
const listTotal = ref(0);
const searchFom = reactive({
  pageNum: 1,
  pageSize: 20,
})

onMounted(() => {
  getList('init');
});

const handlePageChange = (page, pageSize) => {
  searchFom.pageNum = page;
  if (pageSize) searchFom.pageSize = pageSize;
  getList();
}

const initList = () => {
  getList('init');
};

// Get product list
const getList = async (type) => {
  if (type === 'init') {
    searchFom.pageNum = 1
  }
  const loading = openFullScreenLoading();
  const res = await getMallList(searchFom)
  loading.close();
  if (res.code === 200) {
    listTotal.value = res.total || 0;
    list.value = res.rows || [];
  }
};
</script>

<style scoped lang="scss">
/* ===== PAGE CONTAINER ===== */
.ms-page {
  position: relative;
  min-height: 100vh;
  background: linear-gradient(160deg, #eef2ff 0%, #ede9fe 28%, #e8e4ff 52%, #f3e8ff 72%, #fce7f3 100%);
  overflow-x: hidden;
}

/* ===== AURORA BACKGROUND ===== */
.ms-aurora-bg {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.ms-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  animation: float 20s ease-in-out infinite;
}

.ms-orb-1 {
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, rgba(79, 70, 229, 0.15), transparent 70%);
  top: -200px;
  right: -100px;
  animation-delay: 0s;
}

.ms-orb-2 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, rgba(124, 58, 237, 0.12), transparent 70%);
  bottom: -150px;
  left: -100px;
  animation-delay: -7s;
}

.ms-orb-3 {
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(236, 72, 153, 0.1), transparent 70%);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-delay: -14s;
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) scale(1);
  }
  33% {
    transform: translate(30px, -30px) scale(1.1);
  }
  66% {
    transform: translate(-20px, 20px) scale(0.9);
  }
}

.ms-grid-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    linear-gradient(rgba(79, 70, 229, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(79, 70, 229, 0.03) 1px, transparent 1px);
  background-size: 50px 50px;
  opacity: 0.5;
}

.ms-noise-texture {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 400 400' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)'/%3E%3C/svg%3E");
  opacity: 0.03;
  mix-blend-mode: overlay;
}

/* ===== MAIN CONTENT ===== */
.ms-content {
  position: relative;
  z-index: 1;
  padding: 0 24px 60px;
  max-width: 1400px;
  margin: 0 auto;
}

/* ===== BREADCRUMB ===== */
.ms-breadcrumb-wrapper {
  padding: 20px 0;
  animation: fadeInDown 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ===== PAGE HEADER ===== */
.ms-header {
  margin-bottom: 40px;
  animation: fadeInUp 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ms-header-content {
  text-align: center;
}

.ms-title-wrapper {
  display: inline-flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 12px;
}

.ms-title-line {
  width: 5px;
  height: 36px;
  background: linear-gradient(180deg, #4f46e5, #7c3aed);
  border-radius: 3px;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4);
}

.ms-title {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, #1f2937 0%, #4f46e5 50%, #7c3aed 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
  line-height: 1.2;
}

.ms-subtitle {
  font-size: 16px;
  color: #6b7280;
  margin: 0;
  letter-spacing: 0.02em;
  font-weight: 400;
}

/* ===== PRODUCTS GRID ===== */
.ms-products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
  animation: fadeIn 1s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ===== PRODUCT CARD ===== */
.ms-product-card {
  position: relative;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  animation: slideInUp 0.6s cubic-bezier(0.16, 1, 0.3, 1) backwards;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(79, 70, 229, 0.03), rgba(124, 58, 237, 0.03));
    opacity: 0;
    transition: opacity 0.4s ease;
  }
  
  &:hover {
    transform: translateY(-8px) scale(1.02);
    box-shadow: 
      0 20px 40px rgba(79, 70, 229, 0.15),
      0 8px 24px rgba(0, 0, 0, 0.08),
      inset 0 1px 0 rgba(255, 255, 255, 0.9);
    border-color: rgba(79, 70, 229, 0.25);
    
    &::before {
      opacity: 1;
    }
    
    .ms-image {
      transform: scale(1.08);
    }
    
    .ms-image-glow {
      opacity: 0.3;
    }
    
    .ms-product-title {
      color: #4f46e5;
    }
    
    .ms-card-indicator {
      opacity: 1;
      transform: scaleX(1);
    }
  }
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ===== PRODUCT IMAGE ===== */
.ms-product-image {
  position: relative;
  height: 280px;
  overflow: hidden;
  border-radius: 20px 20px 0 0;
  background: linear-gradient(135deg, #f3f4f6, #e5e7eb);
  
  .ms-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  }
  
  .ms-image-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(79, 70, 229, 0.1), rgba(124, 58, 237, 0.1));
    opacity: 0;
    transition: opacity 0.4s ease;
  }
  
  .ms-image-glow {
    position: absolute;
    top: -50%;
    left: -50%;
    right: -50%;
    bottom: -50%;
    background: radial-gradient(circle at center, rgba(79, 70, 229, 0.3), transparent 60%);
    opacity: 0;
    transition: opacity 0.4s ease;
    pointer-events: none;
  }
  
  .ms-discount-badge {
    position: absolute;
    top: 16px;
    right: 16px;
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: white;
    padding: 6px 12px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 700;
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
    z-index: 2;
  }
}

/* ===== PRODUCT INFO ===== */
.ms-product-info {
  padding: 20px;
  position: relative;
  z-index: 1;
  
  .ms-product-title {
    font-size: 16px;
    font-weight: 700;
    color: #1f2937;
    margin: 0 0 16px 0;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    transition: color 0.3s ease;
  }
}

/* ===== PRICE SECTION ===== */
.ms-price-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  
  .ms-price-container {
    flex: 1;
    min-width: 0;
    
    .ms-price-wrapper {
      display: flex;
      align-items: baseline;
      gap: 4px;
      flex-wrap: wrap;
      
      .ms-discount-price {
        font-size: 20px;
        font-weight: 800;
        color: #ef4444;
        line-height: 1;
      }
      
      .ms-currency {
        font-size: 14px;
        font-weight: 600;
        color: #6b7280;
      }
      
      .ms-original-price {
        font-size: 13px;
        color: #9ca3af;
        text-decoration: line-through;
        margin-left: 8px;
        white-space: nowrap;
      }
    }
  }
  
  .ms-exchange-btn {
    position: relative;
    padding: 10px 20px;
    background: linear-gradient(135deg, #4f46e5, #7c3aed);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 
      0 4px 16px rgba(79, 70, 229, 0.35),
      inset 0 1px 0 rgba(255, 255, 255, 0.2);
    white-space: nowrap;
    
    .ms-btn-text {
      position: relative;
      z-index: 1;
    }
    
    .ms-btn-glow {
      position: absolute;
      top: -50%;
      left: -50%;
      right: -50%;
      bottom: -50%;
      background: radial-gradient(circle at center, rgba(255, 255, 255, 0.25), transparent 60%);
      opacity: 0;
      transition: opacity 0.3s ease;
      pointer-events: none;
    }
    
    &:hover {
      transform: translateY(-2px) scale(1.05);
      box-shadow: 
        0 8px 24px rgba(79, 70, 229, 0.45),
        inset 0 1px 0 rgba(255, 255, 255, 0.3);
      
      .ms-btn-glow {
        opacity: 1;
        animation: glow-pulse 2s ease-in-out infinite;
      }
    }
    
    &:active {
      transform: translateY(0) scale(0.98);
    }
    
    &:disabled {
      opacity: 0.6;
      cursor: not-allowed;
      transform: none;
      box-shadow: none;
    }
  }
}

@keyframes glow-pulse {
  0%, 100% {
    opacity: 0.6;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.05);
  }
}

.ms-card-indicator {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #4f46e5, #7c3aed);
  border-radius: 20px 20px 0 0;
  opacity: 0;
  transform: scaleX(0);
  transform-origin: left;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  box-shadow: 0 0 12px rgba(79, 70, 229, 0.5);
}

/* ===== EMPTY STATE ===== */
.ms-empty-state {
  padding: 80px 20px;
  text-align: center;
  animation: fadeIn 1s cubic-bezier(0.16, 1, 0.3, 1);
}

/* ===== PAGINATION ===== */
.ms-pagination {
  margin-top: 40px;
  display: flex;
  justify-content: center;
  animation: fadeInUp 0.8s cubic-bezier(0.16, 1, 0.3, 1) 0.2s backwards;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 1200px) {
  .ms-products-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 20px;
  }
  
  .ms-product-image {
    height: 240px;
  }
}

@media (max-width: 768px) {
  .ms-content {
    padding: 0 16px 40px;
  }
  
  .ms-header {
    margin-bottom: 32px;
  }
  
  .ms-title {
    font-size: 24px;
  }
  
  .ms-subtitle {
    font-size: 14px;
  }
  
  .ms-products-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
  }
  
  .ms-product-image {
    height: 200px;
  }
  
  .ms-product-info {
    padding: 16px;
  }
  
  .ms-product-title {
    font-size: 15px;
    margin-bottom: 12px;
  }
  
  .ms-price-section {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    
    .ms-exchange-btn {
      width: 100%;
      text-align: center;
      justify-content: center;
    }
  }
  
  .ms-pagination {
    margin-top: 32px;
  }
}

@media (max-width: 480px) {
  .ms-content {
    padding: 0 12px 32px;
  }
  
  .ms-breadcrumb-wrapper {
    padding: 16px 0;
  }
  
  .ms-header {
    margin-bottom: 24px;
  }
  
  .ms-title {
    font-size: 20px;
  }
  
  .ms-products-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .ms-product-image {
    height: 220px;
  }
  
  .ms-price-container {
    .ms-price-wrapper {
      .ms-discount-price {
        font-size: 18px;
      }
    }
  }
}
</style>