@extends('layouts.app')
@section('title', "$product->name — God's Treat")

@push('styles')
<style>
  .detail-layout{display:grid;grid-template-columns:1fr 1fr;gap:60px;padding:110px 5% 60px;max-width:1300px;margin:0 auto}
  .gallery{position:sticky;top:100px;height:fit-content}
  .main-img-wrap{background:linear-gradient(135deg,rgba(255,107,26,.06),rgba(124,58,237,.05));border:1px solid var(--border);border-radius:var(--radius);height:400px;margin-bottom:14px;position:relative;cursor:zoom-in}
  .img-clip{overflow:hidden;position:absolute;inset:0;border-radius:var(--radius);display:flex;align-items:center;justify-content:center;}
  .main-img-wrap img{max-width:70%;max-height:360px;object-fit:contain;transition:opacity .25s;pointer-events:none;user-select:none}
  .zoom-lens{position:absolute;width:140px;height:140px;border:2px solid var(--orange);border-radius:8px;pointer-events:none;display:none;box-shadow:0 0 0 1px rgba(255,107,26,.3);background:rgba(255,107,26,.05);z-index:5}
  .zoom-result{display:none;position:absolute;left:calc(100% + 16px);top:0;width:340px;height:400px;border:1px solid var(--border);border-radius:var(--radius);background:#0f0f1a;overflow:hidden;box-shadow:0 20px 60px rgba(0,0,0,.6);z-index:10}
  .thumbs{display:flex;gap:10px;flex-wrap:wrap}
  .thumb{width:68px;height:68px;background:rgba(255,107,26,.06);border:2px solid var(--border);border-radius:10px;display:flex;align-items:center;justify-content:center;overflow:hidden;cursor:pointer;transition:all .2s;flex-shrink:0}
  .thumb:hover,.thumb.active{border-color:var(--orange)}
  .thumb img{max-width:80%;max-height:80%;object-fit:contain}
  .prod-category-tag{display:inline-block;background:rgba(255,107,26,.12);color:var(--orange);border:1px solid rgba(255,107,26,.25);padding:4px 14px;border-radius:50px;font-size:.72rem;font-weight:700;letter-spacing:.12em;text-transform:uppercase;margin-bottom:14px}
  .prod-title{font-family:'Bebas Neue',sans-serif;font-size:2.6rem;line-height:1.1;margin-bottom:14px}
  .star-row{display:flex;align-items:center;gap:8px;margin-bottom:20px}
  .stars{color:var(--gold);font-size:1.1rem}
  .price-block{margin-bottom:24px}
  .price-main{font-size:2.4rem;font-weight:900;margin-bottom:4px}
  .price-old{font-size:1rem;color:var(--muted);text-decoration:line-through;margin-right:10px}
  .price-save{background:rgba(6,214,160,.15);color:#06d6a0;padding:4px 12px;border-radius:50px;font-size:.8rem;font-weight:700}
  .qty-row{display:flex;align-items:center;gap:16px;margin-bottom:24px}
  .qty-btn{width:40px;height:40px;background:var(--card);border:1px solid var(--border2);color:var(--white);border-radius:10px;cursor:pointer;font-size:1.3rem;display:flex;align-items:center;justify-content:center;transition:all .2s}
  .qty-btn:hover{background:var(--orange);border-color:var(--orange);color:#0a0a14}
  .qty-num{font-weight:700;font-size:1.2rem;min-width:32px;text-align:center}
  .cta-row{display:flex;gap:14px;margin-bottom:28px}
  .btn-buynow{flex:1;padding:16px;background:transparent;border:2px solid var(--orange);color:var(--orange);border-radius:12px;font-weight:700;font-size:.95rem;cursor:pointer;transition:all .25s}
  .btn-buynow:hover{background:rgba(255,107,26,.1)}
  .trust-pills{display:flex;flex-wrap:wrap;gap:10px;margin-bottom:28px}
  .trust-pill{background:var(--card);border:1px solid var(--border);border-radius:50px;padding:7px 14px;font-size:.78rem;color:var(--muted);display:flex;align-items:center;gap:6px}
  .feature-list{list-style:none;margin-bottom:28px}
  .feature-list li{display:flex;align-items:center;gap:10px;padding:8px 0;border-bottom:1px solid var(--border);color:var(--muted);font-size:.9rem}
  .feature-list li::before{content:'✓';color:#06d6a0;font-weight:700}
  @media(max-width:900px){.detail-layout{grid-template-columns:1fr;padding:100px 5% 40px}.gallery{position:static}.zoom-result{display:none !important}}
</style>
@endpush

@section('content')
<div class="detail-layout">
  <!-- Gallery -->
  <div class="gallery">
    <div class="main-img-wrap" id="mainImgWrap">
      <div class="img-clip">
        <img src="{{ asset('images/' . $product->image) }}" alt="{{ $product->name }}" id="mainImg"
          onerror="this.src='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 width=%22200%22 height=%22200%22><rect width=%22200%22 height=%22200%22 fill=%22%23ff6b1a11%22/><text x=%22100%22 y=%22115%22 text-anchor=%22middle%22 fill=%22%23ff6b1a55%22 font-size=%2280%22>🌾</text></svg>'"/>
      </div>
      <div class="zoom-lens" id="zoomLens"></div>
      <div class="zoom-result" id="zoomResult"></div>
    </div>

    <!-- Thumbnails -->
    @php
      $allImages = collect();
      if($product->image) $allImages->push(['src' => asset('images/'.$product->image), 'primary' => true]);
      foreach($product->images as $img) {
        $allImages->push(['src' => asset('images/'.$img->image), 'primary' => false]);
      }
    @endphp

    @if($allImages->count() > 1)
    <div class="thumbs">
      @foreach($allImages as $idx => $img)
      <div class="thumb {{ $idx === 0 ? 'active' : '' }}" onclick="switchImg('{{ $img['src'] }}', this)">
        <img src="{{ $img['src'] }}" alt="Photo {{ $idx + 1 }}"
          onerror="this.src='data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 width=%22100%22 height=%22100%22><rect width=%22100%22 height=%22100%22 fill=%22%23ff6b1a11%22/><text x=%2250%22 y=%2258%22 text-anchor=%22middle%22 fill=%22%23ff6b1a55%22 font-size=%2240%22>🌾</text></svg>'"/>
      </div>
      @endforeach
    </div>
    @endif
  </div>

  <!-- Info -->
  <div>
    <div class="prod-category-tag">{{ $product->category }}</div>
    <h1 class="prod-title">{{ $product->name }}</h1>
    <div class="star-row">
      <span class="stars">★★★★★</span>
      <span style="color:var(--muted);font-size:.85rem">4.9 · 200+ reviews</span>
    </div>

    <div class="price-block">
      <div class="price-main">₹{{ number_format($product->price) }}</div>
      @if($product->old_price)
      <span class="price-old">₹{{ number_format($product->old_price) }}</span>
      <span class="price-save">{{ $product->discount_percent }}% OFF — You save ₹{{ number_format($product->old_price - $product->price) }}</span>
      @endif
      <div style="color:var(--muted);font-size:.82rem;margin-top:6px">{{ $product->weight }} · Tax included · Free delivery above ₹499</div>
    </div>

    <form method="POST" action="{{ route('cart.add') }}">
      @csrf
      <input type="hidden" name="product_id" value="{{ $product->id }}">
      <input type="hidden" name="qty" id="qtyInput" value="1">

      <div class="qty-row">
        <button type="button" class="qty-btn" onclick="changeQty(-1)">−</button>
        <div class="qty-num" id="qtyDisplay">1</div>
        <button type="button" class="qty-btn" onclick="changeQty(1)">+</button>
        <span style="color:var(--muted);font-size:.82rem">{{ $product->stock_qty }} in stock</span>
      </div>

      <div class="cta-row">
        <button type="submit" class="btn-primary btn-full" style="flex:2;padding:16px;font-size:1rem">🛒 Add to Cart</button>
        <button type="button" class="btn-buynow" onclick="buyNow(this)">⚡ Buy Now</button>
      </div>
    </form>

    <div class="trust-pills">
      <div class="trust-pill"><span>🚚</span> Free Delivery ₹499+</div>
      <div class="trust-pill"><span>🔄</span> 7-Day Returns</div>
      <div class="trust-pill"><span>🌿</span> 100% Organic</div>
      <div class="trust-pill"><span>🔐</span> Secure Payment</div>
      <div class="trust-pill"><span>📦</span> Airtight Packed</div>
    </div>

    @if($product->description)
    <p style="color:var(--muted);font-size:.9rem;line-height:1.7;margin-bottom:20px">{{ $product->description }}</p>
    @endif

    @if($product->features)
    <ul class="feature-list">
      @foreach($product->features as $feat)
      <li>{{ $feat }}</li>
      @endforeach
    </ul>
    @endif

    <!-- Share / Refer -->
    <div style="background:rgba(255,107,26,.06);border:1px solid rgba(255,107,26,.2);border-radius:12px;padding:16px;margin-top:8px">
      <div style="font-size:.82rem;color:var(--muted);margin-bottom:8px">Share this product:</div>
      <div style="display:flex;gap:10px;align-items:center">
        <input id="shareUrl" type="text" value="{{ url()->current() }}" readonly
          style="flex:1;background:var(--card2);border:1px solid var(--border2);color:var(--white);padding:8px 12px;border-radius:8px;font-size:.78rem"/>
        <button onclick="copyUrl()" class="btn-primary" style="padding:9px 16px;font-size:.78rem;white-space:nowrap">Copy Link</button>
      </div>
    </div>
  </div>
</div>

<!-- Related Products -->
@if($related->count() > 0)
<section style="padding:40px 5% 80px;max-width:1300px;margin:0 auto">
  <h2 style="font-family:'Bebas Neue',sans-serif;font-size:2rem;margin-bottom:24px">YOU MIGHT ALSO <span class="gradient-text">LIKE</span></h2>
  <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:20px">
    @foreach($related as $rel)
    <a href="{{ route('product.show', $rel->slug) }}" style="text-decoration:none">
      <div style="background:var(--card);border:1px solid var(--border);border-radius:14px;padding:18px;transition:transform .3s" onmouseover="this.style.transform='translateY(-6px)'" onmouseout="this.style.transform=''">
        <div style="height:180px;display:flex;align-items:center;justify-content:center;margin-bottom:12px">
          <img src="{{ asset('images/' . $rel->image) }}" alt="{{ $rel->name }}" style="max-width:82%;max-height:160px;object-fit:contain"/>
        </div>
        <div style="color:var(--orange);font-size:.7rem;font-weight:700;letter-spacing:.1em;text-transform:uppercase">{{ $rel->category }}</div>
        <div style="color:var(--white);font-weight:700;font-size:.9rem;margin:4px 0">{{ $rel->name }}</div>
        <div style="font-weight:800">₹{{ number_format($rel->price) }}
          @if($rel->old_price)<span style="font-size:.8rem;color:var(--muted);text-decoration:line-through;font-weight:400;margin-left:6px">₹{{ number_format($rel->old_price) }}</span>@endif
        </div>
      </div>
    </a>
    @endforeach
  </div>
</section>
@endif
@endsection

@push('scripts')
<script>
let qty = 1;
function changeQty(d) {
  qty = Math.max(1, qty + d);
  document.getElementById('qtyDisplay').textContent = qty;
  document.getElementById('qtyInput').value = qty;
}

function buyNow(btn) {
  document.getElementById('qtyInput').value = document.getElementById('qtyDisplay').textContent;
  btn.closest('form').action = '{{ route('checkout') }}';
  btn.closest('form').submit();
}

function copyUrl() {
  const input = document.getElementById('shareUrl');
  input.select();
  navigator.clipboard.writeText(input.value).then(() => showToast('Link copied!'));
}

function switchImg(src, thumb) {
  const img = document.getElementById('mainImg');
  img.style.opacity = '0';
  setTimeout(() => { img.src = src; img.style.opacity = '1'; currentSrc = src; }, 150);
  document.querySelectorAll('.thumb').forEach(t => t.classList.remove('active'));
  thumb.classList.add('active');
  initZoom();
}

// Magnify zoom
let currentSrc = '{{ asset('images/' . $product->image) }}';
function initZoom() {
  const wrap   = document.getElementById('mainImgWrap');
  const lens   = document.getElementById('zoomLens');
  const result = document.getElementById('zoomResult');
  const img    = document.getElementById('mainImg');

  result.innerHTML = '';
  const bgImg = document.createElement('div');
  bgImg.style.cssText = 'position:absolute;width:100%;height:100%;background-size:cover;background-repeat:no-repeat';
  result.appendChild(bgImg);

  wrap.addEventListener('mouseenter', () => {
    lens.style.display = 'block';
    result.style.display = 'block';
  });
  wrap.addEventListener('mouseleave', () => {
    lens.style.display = 'none';
    result.style.display = 'none';
  });
  wrap.addEventListener('mousemove', function(e) {
    const rect = wrap.getBoundingClientRect();
    const lw = lens.offsetWidth, lh = lens.offsetHeight;
    const rw = result.offsetWidth, rh = result.offsetHeight;
    let x = e.clientX - rect.left - lw/2;
    let y = e.clientY - rect.top  - lh/2;
    x = Math.max(0, Math.min(x, rect.width  - lw));
    y = Math.max(0, Math.min(y, rect.height - lh));
    lens.style.left = x + 'px';
    lens.style.top  = y + 'px';
    const cx = rw / lw, cy = rh / lh;
    const src = document.getElementById('mainImg').src;
    bgImg.style.backgroundImage = 'url(' + src + ')';
    bgImg.style.backgroundSize  = (rect.width * cx) + 'px ' + (rect.height * cy) + 'px';
    bgImg.style.backgroundPosition = '-' + (x*cx) + 'px -' + (y*cy) + 'px';
  });
}
initZoom();
</script>
@endpush
