@extends('layouts.admin')

@section('title', 'Quote Details - #' . $quote->id)

@section('admin-content')
<div class="container-fluid">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Dashboard</a></li>
            <li class="breadcrumb-item"><a href="{{ route('admin.quotes.index') }}">Quotes</a></li>
            <li class="breadcrumb-item active">Quote #{{ $quote->id }}</li>
        </ol>
    </nav>

    <div class="row g-4">
        <!-- Left: Buyer Requirement -->
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header bg-white py-3">
                    <h5 class="fw-bold mb-0">
                        <i class="bi bi-file-earmark-text me-2 text-primary"></i>
                        Buyer Requirement (RFQ)
                    </h5>
                </div>
                <div class="card-body">
                    @if($quote->rfq)
                        <div class="mb-4">
                            <h4 class="fw-bold">{{ $quote->rfq->title }}</h4>
                            <span class="badge bg-secondary">{{ $quote->rfq->category ?? 'General' }}</span>
                        </div>

                        <div class="row g-3 mb-4">
                            <div class="col-6">
                                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3">
                                    <small class="text-muted">Quantity</small>
                                    <div class="fw-bold">{{ $quote->rfq->quantity ?? 'N/A' }}</div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3">
                                    <small class="text-muted">Budget</small>
                                    <div class="fw-bold">{{ $quote->rfq->budget ? '₹' . number_format($quote->rfq->budget) : 'Open' }}</div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3">
                                    <small class="text-muted">Location</small>
                                    <div class="fw-bold">{{ $quote->rfq->delivery_location ?? 'India' }}</div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3">
                                    <small class="text-muted">Posted</small>
                                    <div class="fw-bold">{{ $quote->rfq->created_at->format('d M Y') }}</div>
                                </div>
                            </div>
                        </div>

                        <h6 class="fw-bold mb-2">Description</h6>
                        <p class="text-muted" style="white-space: pre-line;">{{ $quote->rfq->description }}</p>

                        <hr>

                        <h6 class="fw-bold mb-3">Buyer Info</h6>
                        <div class="d-flex align-items-center">
                            <div class="tw-w-12 tw-h-12 tw-bg-orange-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-text-orange-600 tw-font-bold tw-text-lg">
                                {{ strtoupper(substr($quote->rfq->buyer?->user?->name ?? 'B', 0, 1)) }}
                            </div>
                            <div>
                                <div class="fw-bold">{{ $quote->rfq->buyer?->user?->name ?? 'N/A' }}</div>
                                <small class="text-muted">{{ $quote->rfq->buyer?->user?->email ?? '' }}</small>
                            </div>
                        </div>
                    @else
                        <p class="text-muted">RFQ data not available.</p>
                    @endif
                </div>
            </div>
        </div>

        <!-- Right: Seller Quote -->
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                    <h5 class="fw-bold mb-0">
                        <i class="bi bi-send me-2 text-success"></i>
                        Seller Quote
                    </h5>
                    {!! $quote->status_badge !!}
                </div>
                <div class="card-body">
                    <div class="row g-3 mb-4">
                        <div class="col-6">
                            <div class="tw-bg-green-50 tw-rounded-lg tw-p-4 text-center">
                                <small class="text-muted">Quoted Price</small>
                                <div class="tw-text-2xl tw-font-bold tw-text-green-600">₹{{ number_format($quote->price, 2) }}</div>
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="tw-bg-blue-50 tw-rounded-lg tw-p-4 text-center">
                                <small class="text-muted">Delivery Timeline</small>
                                <div class="tw-text-lg tw-font-bold tw-text-blue-600">{{ $quote->delivery_timeline }}</div>
                            </div>
                        </div>
                    </div>

                    @if($quote->message)
                        <h6 class="fw-bold mb-2">Message from Seller</h6>
                        <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3 mb-4">
                            <p class="mb-0" style="white-space: pre-line;">{{ $quote->message }}</p>
                        </div>
                    @endif

                    @if($quote->revised_price)
                        <div class="alert alert-info">
                            <h6 class="fw-bold mb-1"><i class="bi bi-pencil me-1"></i> Revised Quote</h6>
                            <p class="mb-0">Price: <strong>₹{{ number_format($quote->revised_price, 2) }}</strong></p>
                            @if($quote->revised_message)
                                <p class="mb-0 mt-2">{{ $quote->revised_message }}</p>
                            @endif
                            <small class="text-muted">Revised on {{ $quote->seller_revised_at?->format('d M Y H:i') }}</small>
                        </div>
                    @endif

                    <hr>

                    <h6 class="fw-bold mb-3">Seller Info</h6>
                    <div class="d-flex align-items-center">
                        @if($quote->seller)
                            @if($quote->seller->logo)
                                <img src="{{ asset('storage/' . $quote->seller->logo) }}" alt="Logo" class="rounded-circle me-3" style="width: 48px; height: 48px; object-fit: cover;">
                            @else
                                <div class="tw-w-12 tw-h-12 tw-bg-blue-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-text-blue-600 tw-font-bold tw-text-lg">
                                    {{ strtoupper(substr($quote->seller->company_name ?? 'S', 0, 1)) }}
                                </div>
                            @endif
                            <div>
                                <div class="fw-bold">{{ $quote->seller->company_name ?? 'N/A' }}</div>
                                <small class="text-muted">{{ $quote->seller->city ?? '' }}, {{ $quote->seller->state ?? '' }}</small>
                            </div>
                        @else
                            <div class="tw-w-12 tw-h-12 tw-bg-gray-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 text-muted">
                                <i class="bi bi-question-lg"></i>
                            </div>
                            <div>
                                <div class="fw-bold text-muted">Unknown Seller</div>
                                <small class="text-muted">Seller information unavailable</small>
                            </div>
                        @endif
                    </div>
                </div>
            </div>

            <!-- Timeline -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3">
                    <h5 class="fw-bold mb-0">
                        <i class="bi bi-clock-history me-2 text-primary"></i>
                        Status Timeline
                    </h5>
                </div>
                <div class="card-body">
                    <div class="timeline">
                        <div class="timeline-item d-flex mb-3">
                            <div class="tw-w-10 tw-h-10 tw-bg-green-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                <i class="bi bi-send tw-text-green-600"></i>
                            </div>
                            <div>
                                <div class="fw-bold">Quote Submitted</div>
                                <small class="text-muted">{{ $quote->created_at->format('d M Y, H:i') }}</small>
                            </div>
                        </div>

                        @if($quote->buyer_viewed_at)
                            <div class="timeline-item d-flex mb-3">
                                <div class="tw-w-10 tw-h-10 tw-bg-blue-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                    <i class="bi bi-eye tw-text-blue-600"></i>
                                </div>
                                <div>
                                    <div class="fw-bold">Viewed by Buyer</div>
                                    <small class="text-muted">{{ $quote->buyer_viewed_at->format('d M Y, H:i') }}</small>
                                </div>
                            </div>
                        @endif

                        @if($quote->seller_revised_at)
                            <div class="timeline-item d-flex mb-3">
                                <div class="tw-w-10 tw-h-10 tw-bg-cyan-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                    <i class="bi bi-pencil tw-text-cyan-600"></i>
                                </div>
                                <div>
                                    <div class="fw-bold">Quote Revised</div>
                                    <small class="text-muted">{{ $quote->seller_revised_at->format('d M Y, H:i') }}</small>
                                </div>
                            </div>
                        @endif

                        @if($quote->status === 'accepted')
                            <div class="timeline-item d-flex">
                                <div class="tw-w-10 tw-h-10 tw-bg-green-500 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                    <i class="bi bi-check-lg tw-text-white"></i>
                                </div>
                                <div>
                                    <div class="fw-bold text-success">Quote Accepted</div>
                                    <small class="text-muted">{{ $quote->updated_at->format('d M Y, H:i') }}</small>
                                </div>
                            </div>
                        @elseif($quote->status === 'rejected')
                            <div class="timeline-item d-flex">
                                <div class="tw-w-10 tw-h-10 tw-bg-red-500 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                    <i class="bi bi-x-lg tw-text-white"></i>
                                </div>
                                <div>
                                    <div class="fw-bold text-danger">Quote Rejected</div>
                                    <small class="text-muted">{{ $quote->updated_at->format('d M Y, H:i') }}</small>
                                </div>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="mt-4">
        <a href="{{ route('admin.quotes.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-1"></i> Back to Quotes
        </a>
    </div>
</div>
@endsection
