@extends('layouts.master')

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

@section('content')
<div class="tw-bg-gray-50 tw-min-h-screen tw-py-8">
    <div class="container">
        <div class="row g-4">
            <!-- Sidebar -->
            <div class="col-lg-3">
                @include('buyer.partials.sidebar')
            </div>

            <!-- Main Content -->
            <div class="col-lg-9">
                <!-- Success/Error Messages -->
                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show d-flex align-items-center mb-4" role="alert">
                        <i class="bi bi-check-circle-fill me-2 fs-4"></i>
                        <div>
                            <strong>Success!</strong> {{ session('success') }}
                        </div>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                @endif

                @if(session('error'))
                    <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
                        <i class="bi bi-exclamation-circle-fill me-2"></i>{{ session('error') }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                @endif

                <!-- Breadcrumb -->
                <nav aria-label="breadcrumb" class="mb-4">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('buyer.dashboard') }}">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('buyer.quotes.index') }}">Quote Requests</a></li>
                        <li class="breadcrumb-item active">Review Quote</li>
                    </ol>
                </nav>

                <div class="row g-4">
                    <!-- Left Column -->
                    <div class="col-lg-6">
                        <!-- My Requirement -->
                        <div class="card border-0 shadow-sm mb-4">
                            <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>
                                    My Requirement
                                </h5>
                            </div>
                            <div class="card-body">
                                @if($quote->rfq)
                                    <h4 class="fw-bold mb-3">{{ $quote->rfq->title }}</h4>

                                    <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">My Budget</small>
                                                <div class="fw-bold">{{ $quote->rfq->budget ? '₹' . number_format($quote->rfq->budget) : 'Open' }}</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>
                                @else
                                    <p class="text-muted">Requirement data not available.</p>
                                @endif
                            </div>
                        </div>

                        <!-- Quote Timeline -->
                        <div class="card border-0 shadow-sm">
                            <div class="card-header bg-white py-3">
                                <h6 class="fw-bold mb-0">
                                    <i class="bi bi-clock-history me-2 text-info"></i>Quote Timeline
                                </h6>
                            </div>
                            <div class="card-body">
                                <div class="timeline position-relative ps-4">
                                    <!-- Quote Received -->
                                    <div class="timeline-item d-flex mb-4">
                                        <div class="timeline-marker position-absolute tw-w-10 tw-h-10 tw-bg-blue-100 tw-rounded-full tw-flex tw-items-center tw-justify-center" style="left: -12px;">
                                            <i class="bi bi-send-fill tw-text-blue-600"></i>
                                        </div>
                                        <div class="ms-4">
                                            <div class="fw-bold">Quote Received</div>
                                            <small class="text-muted">{{ $quote->created_at->format('d M Y, h:i A') }}</small>
                                            <p class="small text-muted mb-0">Seller submitted quote of ₹{{ number_format($quote->price, 0) }}</p>
                                        </div>
                                    </div>

                                    <!-- Viewed -->
                                    @if($quote->buyer_viewed_at)
                                        <div class="timeline-item d-flex mb-4">
                                            <div class="timeline-marker position-absolute tw-w-10 tw-h-10 tw-bg-info tw-rounded-full tw-flex tw-items-center tw-justify-center" style="left: -12px;">
                                                <i class="bi bi-eye-fill tw-text-white"></i>
                                            </div>
                                            <div class="ms-4">
                                                <div class="fw-bold">You Viewed</div>
                                                <small class="text-muted">{{ $quote->buyer_viewed_at->format('d M Y, h:i A') }}</small>
                                            </div>
                                        </div>
                                    @endif

                                    <!-- Negotiation -->
                                    @if($quote->status == 'negotiation' && $quote->buyer_response)
                                        <div class="timeline-item d-flex mb-4">
                                            <div class="timeline-marker position-absolute tw-w-10 tw-h-10 tw-bg-warning tw-rounded-full tw-flex tw-items-center tw-justify-center" style="left: -12px;">
                                                <i class="bi bi-chat-dots-fill tw-text-white"></i>
                                            </div>
                                            <div class="ms-4">
                                                <div class="fw-bold text-warning">Negotiation Requested</div>
                                                <small class="text-muted">{{ $quote->updated_at->format('d M Y, h:i A') }}</small>
                                                <div class="tw-bg-yellow-50 tw-rounded tw-p-2 mt-2">
                                                    <small class="fw-bold">Your message:</small>
                                                    <p class="small mb-0">{{ $quote->buyer_response }}</p>
                                                </div>
                                            </div>
                                        </div>
                                    @endif

                                    <!-- Revised -->
                                    @if($quote->revised_price && $quote->seller_revised_at)
                                        <div class="timeline-item d-flex mb-4">
                                            <div class="timeline-marker position-absolute tw-w-10 tw-h-10 tw-bg-primary tw-rounded-full tw-flex tw-items-center tw-justify-center" style="left: -12px;">
                                                <i class="bi bi-pencil-fill tw-text-white"></i>
                                            </div>
                                            <div class="ms-4">
                                                <div class="fw-bold text-primary">Seller Revised Quote</div>
                                                <small class="text-muted">{{ $quote->seller_revised_at->format('d M Y, h:i A') }}</small>
                                                <p class="small text-muted mb-0">New price: ₹{{ number_format($quote->revised_price, 0) }}</p>
                                                @if($quote->revised_message)
                                                    <div class="tw-bg-blue-50 tw-rounded tw-p-2 mt-2">
                                                        <small class="fw-bold">Seller's note:</small>
                                                        <p class="small mb-0">{{ $quote->revised_message }}</p>
                                                    </div>
                                                @endif
                                            </div>
                                        </div>
                                    @endif

                                    <!-- Accepted -->
                                    @if($quote->status == 'accepted')
                                        <div class="timeline-item d-flex">
                                            <div class="timeline-marker position-absolute tw-w-10 tw-h-10 tw-bg-success tw-rounded-full tw-flex tw-items-center tw-justify-center" style="left: -12px;">
                                                <i class="bi bi-check-lg tw-text-white"></i>
                                            </div>
                                            <div class="ms-4">
                                                <div class="fw-bold text-success">Quote Accepted</div>
                                                <small class="text-muted">{{ $quote->updated_at->format('d M Y, h:i A') }}</small>
                                                <p class="small tw-text-green-600 mb-0">Deal confirmed! Contact seller to proceed.</p>
                                            </div>
                                        </div>
                                    @endif

                                    <!-- Rejected -->
                                    @if($quote->status == 'rejected')
                                        <div class="timeline-item d-flex">
                                            <div class="timeline-marker position-absolute tw-w-10 tw-h-10 tw-bg-danger tw-rounded-full tw-flex tw-items-center tw-justify-center" style="left: -12px;">
                                                <i class="bi bi-x-lg tw-text-white"></i>
                                            </div>
                                            <div class="ms-4">
                                                <div class="fw-bold text-danger">Quote Rejected</div>
                                                <small class="text-muted">{{ $quote->updated_at->format('d M Y, h:i A') }}</small>
                                            </div>
                                        </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Right Column: 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's Quote
                                </h5>
                                {!! $quote->status_badge !!}
                            </div>
                            <div class="card-body">
                                <!-- Seller Info -->
                                <div class="d-flex align-items-center mb-4 tw-p-3 tw-bg-gray-50 tw-rounded-lg">
                                    @if($quote->seller->logo)
                                        <img src="{{ asset('storage/' . $quote->seller->logo) }}" alt="Logo" class="rounded-circle me-3" style="width: 50px; height: 50px; 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' }}
                                            @if($quote->seller && $quote->seller->isPaid())
                                                <i class="bi bi-patch-check-fill text-warning ms-1" title="Premium Seller"></i>
                                            @endif
                                        </div>
                                        <small class="text-muted">
                                            <i class="bi bi-geo-alt me-1"></i>{{ $quote->seller->city ?? '' }}, {{ $quote->seller->state ?? '' }}
                                            @if($quote->seller->is_verified)
                                                <span class="badge bg-success ms-2"><i class="bi bi-patch-check-fill me-1"></i>Verified</span>
                                            @endif
                                        </small>
                                    </div>
                                </div>

                                <!-- Quote Details -->
                                <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->revised_price ?? $quote->price, 2) }}</div>
                                            @if($quote->revised_price)
                                                <small class="text-muted"><del>₹{{ number_format($quote->price, 2) }}</del></small>
                                            @endif
                                        </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</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">Seller's Message</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

                                <!-- Action Buttons (if quote not yet actioned) -->
                                @if(!in_array($quote->status, ['accepted', 'rejected']))
                                    <hr>
                                    <h6 class="fw-bold mb-3">Take Action</h6>
                                    
                                    <div class="d-flex gap-2 mb-3">
                                        <form action="{{ route('buyer.quotes.respond', $quote) }}" method="POST" class="d-inline">
                                            @csrf
                                            <input type="hidden" name="action" value="accept">
                                            <button type="submit" class="btn btn-success" onclick="return confirm('Accept this quote?')">
                                                <i class="bi bi-check-lg me-1"></i> Accept Quote
                                            </button>
                                        </form>
                                        
                                        <button type="button" class="btn btn-outline-warning" data-bs-toggle="collapse" data-bs-target="#negotiateForm">
                                            <i class="bi bi-chat-dots me-1"></i> Negotiate
                                        </button>
                                        
                                        <form action="{{ route('buyer.quotes.respond', $quote) }}" method="POST" class="d-inline">
                                            @csrf
                                            <input type="hidden" name="action" value="reject">
                                            <button type="submit" class="btn btn-outline-danger" onclick="return confirm('Reject this quote?')">
                                                <i class="bi bi-x-lg me-1"></i> Reject
                                            </button>
                                        </form>
                                    </div>

                                    <!-- Negotiate Form (Collapsible) -->
                                    <div class="collapse {{ $quote->status == 'negotiation' ? 'show' : '' }}" id="negotiateForm">
                                        <div class="card card-body tw-bg-yellow-50">
                                            <form action="{{ route('buyer.quotes.respond', $quote) }}" method="POST">
                                                @csrf
                                                <input type="hidden" name="action" value="negotiate">
                                                <div class="mb-3">
                                                    <label class="form-label fw-bold">Your Counter Offer / Message</label>
                                                    <textarea name="response_message" class="form-control" rows="3" placeholder="E.g., Can you reduce the price to ₹X? Or faster delivery?" required>{{ old('response_message') }}</textarea>
                                                </div>
                                                <button type="submit" class="btn btn-warning">
                                                    <i class="bi bi-send me-1"></i> Send Negotiation Request
                                                </button>
                                            </form>
                                        </div>
                                    </div>
                                @else
                                    <div class="alert {{ $quote->status === 'accepted' ? 'alert-success' : 'alert-danger' }}">
                                        <i class="bi {{ $quote->status === 'accepted' ? 'bi-check-circle-fill' : 'bi-x-circle-fill' }} me-2"></i>
                                        You have <strong>{{ $quote->status }}</strong> this quote.
                                    </div>
                                @endif
                            </div>
                        </div>

                        <!-- Contact Seller -->
                        @if($quote->status === 'accepted')
                            <div class="card border-0 shadow-sm tw-bg-green-50">
                                <div class="card-body tw-p-4">
                                    <h6 class="fw-bold mb-3"><i class="bi bi-telephone-fill me-2 tw-text-green-600"></i>Contact Seller</h6>
                                    <p class="text-muted small mb-3">You can now contact the seller directly to finalize the order.</p>
                                    <div class="d-flex gap-2">
                                        <a href="tel:{{ $quote->seller->mobile ?? '' }}" class="btn btn-success flex-fill">
                                            <i class="bi bi-telephone me-1"></i> Call
                                        </a>
                                        <a href="mailto:{{ $quote->seller->user->email ?? '' }}" class="btn btn-outline-success flex-fill">
                                            <i class="bi bi-envelope me-1"></i> Email
                                        </a>
                                    </div>
                                </div>
                            </div>
                        @endif
                    </div>
                </div>

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

<style>
.timeline::before {
    content: '';
    position: absolute;
    left: 7px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: linear-gradient(to bottom, #0d6efd, #198754);
}
.timeline-item:last-child::after {
    display: none;
}
</style>
@endsection
