@extends('theme::layouts.app', ['title' => $title ?? __('account.title|My Reviews')])

@section('content')
<main class="flex-grow w-full max-w-[1440px] mx-auto px-6 lg:px-10 py-10">
    <div class="flex flex-col lg:flex-row gap-10">
        <!-- Sidebar -->
        <aside class="w-full lg:w-64 flex-shrink-0">
            @include('theme::partials.account.nav')
        </aside>

        <!-- Main Content -->
        <section class="flex-grow">
            @if(isset($pageHeader))
            <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-6 mb-8">
                <div>
                    <h1 class="text-2xl font-bold text-text-main dark:text-white">{{ $pageHeader['title'] }}</h1>
                    <p class="text-text-muted dark:text-gray-400 mt-1">{{ $pageHeader['subtitle'] }}</p>
                </div>
            </div>
            @endif

            {{-- Tabs --}}
            <div class="mb-6 border-b border-gray-100 dark:border-gray-800">
                <div class="flex space-x-8">
                    <button class="tab-btn active pb-3 text-sm font-semibold text-primary border-b-2 border-primary transition-all"
                        data-tab="pending">
                        {{ __('account.To Review') }}
                         @if($pendingReviews->count() > 0)
                            <span class="inline-flex items-center justify-center px-1.5 py-0.5 ml-2 text-[10px] font-bold text-primary bg-primary/10 rounded-full">
                                {{ $pendingReviews->count() }}
                            </span>
                        @endif
                    </button>
                    <button class="tab-btn pb-3 text-sm font-medium text-text-muted border-b-2 border-transparent hover:text-text-main dark:hover:text-white transition-all"
                        data-tab="history">
                        {{ __('account.History') }}
                    </button>
                </div>
            </div>

            {{-- To Review Tab --}}
            <div id="tab-pending">
                 <div class="bg-white dark:bg-surface-dark rounded-xl border border-gray-100 dark:border-gray-800 overflow-hidden shadow-sm">
                    <div class="overflow-x-auto">
                        <table class="w-full text-left border-collapse">
                            <thead>
                                <tr class="bg-gray-50/50 dark:bg-gray-800/50 border-b border-gray-100 dark:border-gray-800">
                                    <th class="px-6 py-4 text-[11px] font-bold uppercase tracking-wider text-text-muted">{{ __('account.Product') }}</th>
                                    <th class="px-6 py-4 text-[11px] font-bold uppercase tracking-wider text-text-muted">{{ __('account.Date Purchased') }}</th>
                                    <th class="px-6 py-4 text-[11px] font-bold uppercase tracking-wider text-text-muted">{{ __('account.Order ID') }}</th>
                                    <th class="px-6 py-4 text-[11px] font-bold uppercase tracking-wider text-text-muted text-right">{{ __('account.Action') }}</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100 dark:divide-gray-800">
                                @forelse($pendingReviews as $item)
                                    <tr class="group hover:bg-gray-50/50 dark:hover:bg-gray-800/30 transition-colors">
                                        <td class="px-6 py-5">
                                            <div class="flex items-center gap-3">
                                                 @if(!empty($item->product_image))
                                                    <img src="{{ $item->product_image }}" alt="{{ $item->product_name }}" 
                                                        class="size-10 rounded object-cover flex-shrink-0 bg-gray-100 dark:bg-gray-800">
                                                @else
                                                     <div class="size-10 rounded bg-gray-100 dark:bg-gray-800 flex items-center justify-center text-text-muted flex-shrink-0">
                                                        <span class="material-symbols-outlined text-[20px]">extension</span>
                                                    </div>
                                                @endif
                                                <span class="text-sm font-semibold text-text-main dark:text-white">{!! $item->product_name !!}</span>
                                            </div>
                                        </td>
                                        <td class="px-6 py-5 whitespace-nowrap">
                                            <span class="text-sm text-text-muted">{{ $item->purchase_date->format('M d, Y') }}</span>
                                        </td>
                                        <td class="px-6 py-5 whitespace-nowrap">
                                            <span class="text-sm text-text-muted">#{{ $item->order_reference }}</span>
                                        </td>
                                        <td class="px-6 py-5 text-right">
                                            <button 
                                                data-order-id="{{ $item->order_id }}"
                                                data-product-name="{{ $item->product_name }}"
                                                class="open-review-modal-btn inline-flex items-center gap-1.5 px-3 py-1.5 bg-primary text-white rounded-lg text-xs font-semibold hover:bg-primary-dark transition-colors shadow-sm shadow-primary/20">
                                                <span class="material-symbols-outlined text-[16px]">rate_review</span>
                                                {{ __('account.Write Review') }}
                                            </button>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="px-6 py-12 text-center">
                                            <div class="flex flex-col items-center gap-3">
                                                <span class="material-symbols-outlined text-[48px] text-gray-300 dark:text-gray-600">rate_review</span>
                                                <p class="text-text-muted dark:text-gray-400 font-medium">{{ __('account.No pending reviews') }}</p>
                                                <p class="text-xs text-text-muted">{{ __('account.You have reviewed all your purchased products.') }}</p>
                                            </div>
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

                {{-- History Tab --}}
                <div id="tab-history" class="hidden">
                     @if($submittedReviews->count() > 0)
                        <div class="space-y-4">
                             @foreach($submittedReviews as $review)
                                <div class="bg-white dark:bg-surface-dark border border-gray-100 dark:border-gray-800 rounded-xl p-6">
                                    <div class="flex flex-col md:flex-row gap-6">
                                        <div class="flex-grow">
                                            <div class="flex items-start justify-between mb-2">
                                                <h3 class="font-bold text-text-main dark:text-white">{!! $review->product_name_display !!}</h3>
                                            </div>
                                            <div class="flex text-amber-400 mb-3">
                                                @for ($i = 1; $i <= 5; $i++)
                                                    <span class="material-symbols-outlined text-[16px] {{ $i <= $review->rating ? 'text-amber-400' : 'text-gray-300' }}"
                                                        @if($i <= $review->rating) style="font-variation-settings: 'FILL' 1, 'wght' 400, 'GRAD' 0, 'opsz' 48;" @endif>
                                                        star
                                                    </span>
                                                @endfor
                                            </div>
                                            <p class="text-sm text-text-muted dark:text-gray-300">{{ $review->feedback }}</p>
                                        </div>
                                        <div class="whitespace-nowrap text-right">
                                            <span class="text-sm text-text-muted">{{ $review->created_at->format('M d, Y') }}</span>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        </div>
                        <div class="mt-6">
                            {{ $submittedReviews->links('theme::partials.pagination') }}
                        </div>
                    @else
                        <div class="text-center py-12 bg-gray-50 dark:bg-white/5 rounded-2xl border border-dashed border-gray-200 dark:border-gray-700">
                             <div class="size-16 bg-gray-100 dark:bg-gray-800 text-gray-400 rounded-full flex items-center justify-center mx-auto mb-4">
                                <span class="material-symbols-outlined text-[32px]">history</span>
                            </div>
                            <h3 class="text-lg font-bold text-text-main dark:text-white mb-2">{{ __('account.No reviews yet') }}</h3>
                            <p class="text-text-muted text-sm">{{ __('account.You haven\'t submitted any reviews yet.') }}</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    {{-- Review Modal --}}
    <div id="review-modal" class="fixed inset-0 z-50 hidden" aria-labelledby="modal-title" role="dialog" aria-modal="true">
        <div class="close-review-modal-btn fixed inset-0 bg-gray-900/50 backdrop-blur-sm transition-opacity"></div>
        
        <div class="fixed inset-0 z-10 w-screen overflow-y-auto">
            <div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">
                <div class="relative transform overflow-hidden rounded-2xl bg-white dark:bg-surface-dark text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg border border-gray-100 dark:border-gray-700">
                    <form id="review-form" action="" method="POST" class="p-6 sm:p-8">
                        @csrf
                        <div class="mb-6 text-center">
                            <h3 class="text-xl font-bold text-text-main dark:text-white mb-2" id="modal-product-name">Product Name</h3>
                            <p class="text-sm text-text-muted">{{ __('account.Share your experience with this product') }}</p>
                        </div>

                        {{-- Star Rating Input --}}
                        <div class="flex flex-col items-center gap-2 mb-8">
                            <div class="flex flex-row-reverse justify-center gap-1 group/rating">
                                @for($i = 5; $i >= 1; $i--)
                                <input type="radio" id="star{{$i}}" name="rating" value="{{$i}}" class="peer hidden" required />
                                <label for="star{{$i}}" class="material-symbols-outlined text-3xl cursor-pointer text-gray-300 peer-checked:text-amber-400 peer-hover:text-amber-400 hover:text-amber-400 transition-colors"
                                    style="font-variation-settings: 'FILL' 1, 'wght' 400, 'GRAD' 0, 'opsz' 48;">star</label>
                                @endfor
                            </div>
                            <span class="text-xs text-text-muted font-medium">{{ __('account.Select Rating') }}</span>
                        </div>

                        <div class="mb-6">
                            <label for="feedback" class="block text-xs font-bold uppercase tracking-wider text-text-muted mb-2">{{ __('account.Your Review') }}</label>
                            <textarea id="feedback" name="feedback" rows="4" 
                                class="w-full bg-gray-50 dark:bg-background-dark border border-gray-200 dark:border-gray-700 rounded-xl p-4 text-sm focus:ring-2 focus:ring-primary/20 focus:border-primary outline-none transition-all resize-none"
                                placeholder="Tell us what you liked or didn't like..."></textarea>
                        </div>

                        <div class="flex gap-3">
                            <button type="button" class="close-review-modal-btn flex-1 px-4 py-2.5 bg-gray-100 dark:bg-gray-800 text-text-main dark:text-white rounded-xl text-sm font-bold hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors">
                                {{ __('account.Cancel') }}
                            </button>
                            <button type="submit" 
                                class="flex-1 px-4 py-2.5 bg-primary text-white rounded-xl text-sm font-bold hover:bg-primary-dark transition-colors shadow-lg shadow-primary/20">
                                {{ __('account.Submit Review') }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</main>
@endsection
