@extends('layouts.seller')

@section('title', 'Subscription Plans')

@section('content')
<div x-data="{ currency: '{{ Auth::user()->seller->country == 'India' ? 'INR' : 'USD' }}' }">
    <div class="page-header">
        <div>
            <h4 class="page-title">Subscription Plans</h4>
            <p class="text-muted mb-0">Choose the right plan to grow your business</p>
        </div>
        <div class="d-flex align-items-center gap-3">
            <!-- Currency Toggle -->
            <div class="bg-white p-1 rounded-pill border shadow-sm d-flex">
                <button @click="currency = 'INR'" 
                        :class="currency === 'INR' ? 'bg-primary text-white shadow-sm' : 'text-muted hover:bg-light'"
                        class="btn btn-sm rounded-pill fw-bold px-3 transition-all">
                    INR (₹)
                </button>
                <button @click="currency = 'USD'" 
                        :class="currency === 'USD' ? 'bg-primary text-white shadow-sm' : 'text-muted hover:bg-light'"
                        class="btn btn-sm rounded-pill fw-bold px-3 transition-all">
                    USD ($)
                </button>
            </div>

            <a href="{{ route('seller.subscription.index') }}" class="btn btn-light border shadow-sm">
                <i class="bi bi-arrow-clockwise me-1"></i> Refresh
            </a>
        </div>
    </div>

    <div class="alert alert-warning border-0 shadow-sm mb-4">
        <div class="d-flex align-items-center">
            <i class="bi bi-exclamation-triangle-fill fs-4 me-3 text-warning"></i>
            <div>
                <h6 class="fw-bold mb-1">Important Note</h6>
                <p class="mb-0 small">Once the subscription plan is purchased, it cannot be cancelled or refunded. Thanks.</p>
            </div>
        </div>
    </div>

    <div class="row g-4">
        @if($currentSubscription)
            <div class="col-12">
                <div class="card border-0 shadow-sm bg-primary-subtle border-primary border-start border-4">
                    <div class="card-body p-4">
                        <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
                            <div class="d-flex align-items-center">
                                <div class="rounded-circle bg-primary text-white p-3 me-3 shadow-sm">
                                    <i class="bi bi-shield-check fs-3"></i>
                                </div>
                                <div>
                                    <h5 class="fw-bold mb-1 text-primary">Current Plan: {{ $currentSubscription->plan->name }}</h5>
                                    <p class="mb-0 text-muted">
                                        Active until <strong class="text-dark">{{ $currentSubscription->end_date->format('M d, Y') }}</strong>
                                        <span class="badge bg-primary rounded-pill ms-2">{{ $currentSubscription->end_date->diffInDays(now()) }} days left</span>
                                    </p>
                                </div>
                            </div>
                            <div>
                                <button class="btn btn-primary disabled opacity-75">
                                    <i class="bi bi-check-circle-fill me-1"></i> Active
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        @else
            <div class="col-12">
                <div class="alert alert-warning border-0 shadow-sm d-flex align-items-center p-4 rounded-3">
                    <i class="bi bi-exclamation-triangle-fill text-warning fs-3 me-3"></i>
                    <div>
                        <h5 class="fw-bold mb-1">No Active Subscription</h5>
                        <p class="mb-0 text-muted">Choose a plan below to unlock premium features and reach more buyers.</p>
                    </div>
                </div>
            </div>
        @endif

        <!-- Plans Grid -->
        @foreach($plans as $plan)
            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100 position-relative overflow-hidden hover-shadow transition-all {{ $currentSubscription && $currentSubscription->plan_id == $plan->id ? 'ring-2 ring-primary' : '' }}">
                    @if($plan->is_featured)
                        <div class="position-absolute top-0 end-0">
                            <div class="bg-warning text-dark fw-bold small px-3 py-1 rounded-bottom-start shadow-sm">
                                <i class="bi bi-star-fill me-1"></i> POPULAR
                            </div>
                        </div>
                    @endif
                    
                    <div class="card-body p-4 d-flex flex-column">
                        <div class="text-center mb-4">
                            <h5 class="fw-bold text-uppercase text-muted small mb-2">{{ $plan->name }}</h5>
                            
                            @if($plan->price_inr == 0)
                                <div class="d-flex justify-content-center align-items-baseline text-success">
                                    <span class="display-4 fw-bold">FREE</span>
                                </div>
                                <span class="text-muted">Lifetime Validity</span>
                            @else
                                <!-- INR Price -->
                                <div x-show="currency === 'INR'">
                                    <div class="d-flex justify-content-center align-items-baseline text-primary">
                                        <span class="h2 fw-bold mb-0">₹</span>
                                        <span class="display-4 fw-bold">{{ number_format($plan->price_inr, 2) }}</span>
                                    </div>
                                </div>

                                <!-- USD Price -->
                                <div x-show="currency === 'USD'">
                                    <div class="d-flex justify-content-center align-items-baseline text-primary">
                                        <span class="h2 fw-bold mb-0">$</span>
                                        <span class="display-4 fw-bold">{{ number_format($plan->price_usd, 2) }}</span>
                                    </div>
                                </div>

                                <span class="text-muted">/ {{ $plan->duration_days }} days</span>
                            @endif
                        </div>

                        <ul class="list-unstyled mb-4 flex-grow-1">
                             <li class="py-2 d-flex align-items-center">
                                <i class="bi bi-check-circle-fill text-success me-2"></i>
                                <span><strong>{{ $plan->product_limit }}</strong> Products</span>
                            </li>

                            @if($plan->features && is_array($plan->features))
                                @foreach($plan->features as $feature)
                                    @if(!str_contains(strtolower($feature), 'product listing'))
                                        <li class="py-2 d-flex align-items-center">
                                            <i class="bi bi-check-circle-fill text-success me-2"></i>
                                            <span>{{ trim($feature) }}</span>
                                        </li>
                                    @endif
                                @endforeach
                            @elseif($plan->features && is_string($plan->features))
                                 @foreach(explode(',', $plan->features) as $feature)
                                    @if(!str_contains(strtolower($feature), 'product listing'))
                                        <li class="py-2 d-flex align-items-center">
                                            <i class="bi bi-check-circle-fill text-success me-2"></i>
                                            <span>{{ trim($feature) }}</span>
                                        </li>
                                    @endif
                                @endforeach
                            @endif
                        </ul>

                        <div class="mt-auto">
                            @if($currentSubscription && $currentSubscription->plan_id == $plan->id)
                                <button class="btn btn-success w-100 py-2 fw-bold disabled">
                                    <i class="bi bi-check-lg me-1"></i> Current Plan
                                </button>
                            @else
                                @if($plan->price_inr == 0)
                                    <button class="btn btn-outline-secondary w-100 py-2 fw-bold disabled">
                                        Free Plan
                                    </button>
                                @else
                                    <a :href="'{{ route('seller.subscription.checkout', $plan) }}?currency=' + currency" 
                                       class="btn btn-primary-gradient w-100 py-2 fw-bold shadow-sm">
                                        Choose Plan <i class="bi bi-arrow-right ms-1"></i>
                                    </a>
                                @endif
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
</div>

<style>
    .hover-shadow:hover { transform: translateY(-5px); box-shadow: 0 10px 20px rgba(0,0,0,0.1) !important; }
    .transition-all { transition: all 0.3s ease; }
    .ring-2 { box-shadow: 0 0 0 2px var(--bs-primary); }
</style>
@endsection

