@extends('layouts.admin')

@section('title', 'Review Moderation')

@section('admin-content')
<style>
    /* Specific Reviews Styles */
    .reviews-page .rating-stars i { font-size: 0.9rem; }
    .reviews-page .status-badge.approved { background: #dcfce7; color: #166534; }
    .reviews-page .status-badge.pending { background: #fef9c3; color: #854d0e; }
</style>

<div class="reviews-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Review Moderation</h4>
            <p class="text-muted mb-0 small">Approve or remove user reviews</p>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success border-0 shadow-sm rounded-3 mb-4 d-flex align-items-center">
            <i class="bi bi-check-circle-fill me-2 fs-5"></i>
            {{ session('success') }}
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <!-- Stats -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-star-half"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $reviews->count() }}</div>
                    <div class="stat-label">Total Reviews</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $reviews->where('is_approved', true)->count() }}</div>
                    <div class="stat-label">Approved</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon warning">
                    <i class="bi bi-hourglass-split"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $reviews->where('is_approved', false)->count() }}</div>
                    <div class="stat-label">Pending Approval</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Reviews Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Buyer / Reviewer</th>
                        <th>Seller / Product</th>
                        <th>Rating</th>
                        <th>Comment</th>
                        <th class="text-center">Status</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($reviews as $review)
                        <tr>
                            <td class="ps-4">
                                <span class="fw-bold text-dark">{{ $review->buyer->user->name ?? 'Unknown Buyer' }}</span>
                            </td>
                            <td>
                                <span class="text-dark">{{ $review->seller->company_name ?? 'Unknown Seller' }}</span>
                            </td>
                            <td>
                                <div class="rating-stars text-warning">
                                    @for($i = 1; $i <= 5; $i++)
                                        <i class="bi bi-star{{ $i <= $review->rating ? '-fill' : '' }}"></i>
                                    @endfor
                                </div>
                            </td>
                            <td>
                                <span class="text-muted small" title="{{ $review->comment }}">
                                    {{ Str::limit($review->comment, 60) }}
                                </span>
                            </td>
                            <td class="text-center">
                                @if($review->is_approved)
                                    <span class="status-badge approved">
                                        <i class="bi bi-check-circle-fill"></i> Approved
                                    </span>
                                @else
                                    <span class="status-badge pending">
                                        <i class="bi bi-hourglass-split"></i> Pending
                                    </span>
                                @endif
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    @if(!$review->is_approved)
                                        <form action="{{ route('admin.reviews.approve', $review->id) }}" method="POST" class="d-inline">
                                            @csrf
                                            <button type="submit" class="action-btn success" title="Approve Review" data-bs-toggle="tooltip">
                                                <i class="bi bi-check-lg"></i>
                                            </button>
                                        </form>
                                    @endif
                                    
                                    <form action="{{ route('admin.reviews.destroy', $review->id) }}" method="POST" class="d-inline"
                                          onsubmit="return confirm('Are you sure you want to delete this review?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="action-btn danger" title="Delete Review" data-bs-toggle="tooltip">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-star fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Reviews Yet</h5>
                                    <p class="text-muted small mb-0">Reviews from buyers will appear here</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        <div class="px-3 border-top">
            {{ $reviews->links() }}
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });
</script>
@endsection
