@extends('layouts.buyer')

@section('title', 'My Inquiries')

@section('buyer-content')
    <!-- Page Header -->
    <div class="page-header">
        <div>
            <h3 class="page-title">My Inquiries</h3>
            <p class="text-muted mb-0">Track all your product inquiries</p>
        </div>
    </div>

    <!-- Stats -->
    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="card h-100 border-0 shadow-sm rounded-4 overflow-hidden group-hover-lift">
                <div class="card-body p-4 position-relative">
                    <div class="position-absolute top-0 end-0 p-3" style="opacity: 0.1;">
                        <i class="bi bi-chat-square-text display-1 text-primary"></i>
                    </div>
                    <div class="position-relative z-1">
                        <div class="d-flex align-items-center mb-3">
                            <div class="rounded-3 bg-primary bg-opacity-10 p-2 text-primary">
                                <i class="bi bi-chat-square-text fs-4"></i>
                            </div>
                            <h6 class="ms-3 mb-0 text-muted fw-bold small text-uppercase spacing-1">Total Sent</h6>
                        </div>
                        <h2 class="fw-bold mb-0 text-dark">{{ $inquiries->total() }}</h2>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card h-100 border-0 shadow-sm rounded-4 overflow-hidden group-hover-lift">
                <div class="card-body p-4 position-relative">
                    <div class="position-absolute top-0 end-0 p-3" style="opacity: 0.1;">
                        <i class="bi bi-hourglass-split display-1 text-warning"></i>
                    </div>
                    <div class="position-relative z-1">
                        <div class="d-flex align-items-center mb-3">
                            <div class="rounded-3 bg-warning bg-opacity-10 p-2 text-warning">
                                <i class="bi bi-hourglass-split fs-4"></i>
                            </div>
                            <h6 class="ms-3 mb-0 text-muted fw-bold small text-uppercase spacing-1">Awaiting Response</h6>
                        </div>
                        <h2 class="fw-bold mb-0 text-dark">{{ $inquiries->where('status', 'new')->count() + $inquiries->where('status', 'viewed')->count() }}</h2>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card h-100 border-0 shadow-sm rounded-4 overflow-hidden group-hover-lift">
                <div class="card-body p-4 position-relative">
                    <div class="position-absolute top-0 end-0 p-3" style="opacity: 0.1;">
                        <i class="bi bi-check-circle display-1 text-success"></i>
                    </div>
                    <div class="position-relative z-1">
                        <div class="d-flex align-items-center mb-3">
                            <div class="rounded-3 bg-success bg-opacity-10 p-2 text-success">
                                <i class="bi bi-check-circle fs-4"></i>
                            </div>
                            <h6 class="ms-3 mb-0 text-muted fw-bold small text-uppercase spacing-1">Responded</h6>
                        </div>
                        <h2 class="fw-bold mb-0 text-dark">{{ $inquiries->where('status', 'responded')->count() }}</h2>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Inquiries List -->
    <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
        <div class="card-header bg-transparent py-3">
            <h6 class="fw-bold mb-0">
                <i class="bi bi-envelope me-2 text-primary"></i>All Inquiries
            </h6>
        </div>
        <div class="list-group list-group-flush">
            @forelse($inquiries as $inquiry)
                <div class="list-group-item p-4 border-bottom {{ $inquiry->status === 'responded' ? 'bg-success-subtle bg-opacity-10' : '' }}">
                    <div class="d-flex flex-column flex-md-row justify-content-between align-items-start gap-3">
                        <div class="flex-grow-1 w-100">
                            <div class="d-flex align-items-center mb-3">
                                @if($inquiry->seller && $inquiry->seller->logo)
                                    <img src="{{ asset('storage/' . $inquiry->seller->logo) }}" class="rounded-circle me-3 shadow-sm" style="width: 50px; height: 50px; object-fit: cover;">
                                @else
                                    <div class="rounded-circle bg-primary bg-opacity-10 text-primary d-flex align-items-center justify-content-center me-3 fw-bold fs-4" style="width: 50px; height: 50px;">
                                        {{ strtoupper(substr($inquiry->seller->company_name ?? 'S', 0, 1)) }}
                                    </div>
                                @endif
                                <div>
                                    <h6 class="fw-bold mb-0 text-dark">
                                        {{ $inquiry->seller->company_name ?? 'Seller' }}
                                        @if($inquiry->seller && $inquiry->seller->isPaid())
                                            <i class="bi bi-patch-check-fill text-warning ms-1" title="Premium Seller"></i>
                                        @endif
                                    </h6>
                                    @if($inquiry->product)
                                        <div class="text-muted small mt-1">
                                            <i class="bi bi-box me-1"></i>
                                            <span class="text-secondary">Inquiry about:</span>
                                            <a href="{{ route('public.products.show', $inquiry->product->slug) }}" class="text-primary text-decoration-none fw-medium ms-1">
                                                {{ $inquiry->product->name }}
                                            </a>
                                        </div>
                                    @endif
                                </div>
                            </div>
                            
                            <!-- Buyer Message -->
                            <div class="bg-light rounded-4 p-3 mb-3 border-start border-4 border-primary position-relative" style="border-radius: 0 1rem 1rem 1rem !important;">
                                <div class="position-absolute top-0 start-0 translate-middle-y ms-3 px-2 bg-light text-primary small fw-bold" style="margin-top: -10px;">You sent</div>
                                <p class="mb-0 text-secondary">{{ $inquiry->message }}</p>
                                <div class="text-end mt-2">
                                    <small class="text-muted fst-italic">{{ $inquiry->created_at->format('d M, h:i A') }}</small>
                                </div>
                            </div>
                            
                            <!-- Seller Response -->
                            @if($inquiry->seller_response)
                                <div class="bg-white shadow-sm rounded-4 p-3 mb-2 border-start border-4 border-success position-relative ms-md-4" style="border-radius: 1rem 0 1rem 1rem !important;">
                                    <div class="position-absolute top-0 start-0 translate-middle-y ms-3 px-2 bg-white text-success small fw-bold" style="margin-top: -10px;">
                                        <i class="bi bi-reply-fill me-1"></i>Seller Replied
                                    </div>
                                    <p class="mb-0 text-dark">{{ $inquiry->seller_response }}</p>
                                    <div class="text-end mt-2">
                                        <small class="text-muted fst-italic">{{ $inquiry->responded_at?->format('d M, h:i A') }}</small>
                                    </div>
                                </div>
                            @endif
                        </div>
                        <div class="text-end ms-md-3 align-self-start mt-2 mt-md-0">
                            {!! $inquiry->status_badge !!}
                            <div class="text-muted small mt-2 d-none d-md-block">
                                {{ $inquiry->created_at->diffForHumans() }}
                            </div>
                        </div>
                    </div>
                </div>
            @empty
                <div class="text-center py-5">
                    <div class="rounded-circle bg-light d-inline-flex align-items-center justify-content-center mb-4" style="width: 80px; height: 80px;">
                        <i class="bi bi-chat-square-text fs-1 text-muted opacity-50"></i>
                    </div>
                    <h5 class="fw-bold mb-2">No Inquiries Sent</h5>
                    <p class="text-muted mb-4 opacity-75">Browse products and send inquiries to suppliers to see them here.</p>
                    <a href="{{ route('public.products.index') }}" class="btn btn-primary rounded-pill px-4 shadow-sm hover-lift">
                        <i class="bi bi-search me-2"></i>Browse Products
                    </a>
                </div>
            @endforelse
        </div>
        @if($inquiries->hasPages())
            <div class="p-3 bg-light border-top">
                {{ $inquiries->links() }}
            </div>
        @endif
    </div>
@endsection
