@extends('layouts.admin')

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

@section('admin-content')
<style>
    /* Specific Quotes Styles */
     .quotes-page .company-avatar {
        width: 38px;
        height: 38px;
        border-radius: 10px;
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 700;
        font-size: 0.9rem;
    }
    .quotes-page .price-tag {
        font-weight: 700;
        color: #2e7d32;
        font-size: 1rem;
        background: #e8f5e9;
        padding: 4px 10px;
        border-radius: 8px;
        display: inline-block;
    }
</style>

<div class="quotes-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Quote Management</h4>
            <p class="text-muted mb-0 small">Track and manage seller quotes and proposals</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.quotes.export') }}" class="btn btn-light border shadow-sm btn-lg d-flex align-items-center gap-2" 
               style="border-radius: 12px; font-size: 0.95rem;">
                <i class="bi bi-file-earmark-spreadsheet"></i> Export CSV
            </a>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row g-4 mb-4">
        <div class="col-lg-3 col-md-6">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-file-earmark-text"></i>
                </div>
                <div>
                    <div class="stat-value">{{ number_format($stats['total'] ?? 0) }}</div>
                    <div class="stat-label">Total Quotes</div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <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">{{ number_format($stats['pending'] ?? 0) }}</div>
                    <div class="stat-label">Pending Review</div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <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">{{ number_format($stats['accepted'] ?? 0) }}</div>
                    <div class="stat-label">Accepted Quotes</div>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon info">
                    <i class="bi bi-graph-up-arrow"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $stats['conversion_rate'] ?? 0 }}%</div>
                    <div class="stat-label">Conversion Rate</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="filter-card mb-4">
        <form action="{{ route('admin.quotes.index') }}" method="GET" class="row g-3 align-items-end">
            <div class="col-lg-4 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">
                    <i class="bi bi-search me-1"></i> Search
                </label>
                <div class="input-group">
                     <span class="input-group-text bg-white border-end-0 ps-3" style="border-radius: 10px 0 0 10px;">
                        <i class="bi bi-search text-muted"></i>
                    </span>
                    <input type="text" name="search" class="form-control form-control-lg border-start-0" 
                           placeholder="Search RFQ, Seller or Buyer..." 
                           value="{{ request('search') }}"
                           style="border-radius: 0 10px 10px 0;">
                </div>
            </div>
            <div class="col-lg-2 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">
                    <i class="bi bi-filter me-1"></i> Status
                </label>
                <select name="status" class="form-select form-select-lg" style="border-radius: 10px;">
                    <option value="all">All Status</option>
                    @if(class_exists('\App\Models\Quote'))
                        @foreach(\App\Models\Quote::getStatuses() as $key => $status)
                            <option value="{{ $key }}" {{ request('status') == $key ? 'selected' : '' }}>
                                {{ $status['label'] }}
                            </option>
                        @endforeach
                    @endif
                </select>
            </div>
            <div class="col-lg-2 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">
                    <i class="bi bi-calendar me-1"></i> From Date
                </label>
                <input type="date" name="from_date" class="form-control form-control-lg" 
                       value="{{ request('from_date') }}" style="border-radius: 10px;">
            </div>
            <div class="col-lg-2 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">
                    <i class="bi bi-calendar me-1"></i> To Date
                </label>
                <input type="date" name="to_date" class="form-control form-control-lg" 
                       value="{{ request('to_date') }}" style="border-radius: 10px;">
            </div>
            <div class="col-lg-2 col-md-12 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-lg w-100" style="border-radius: 10px;">
                    <i class="bi bi-funnel me-1"></i> Filter
                </button>
            </div>
        </form>
    </div>

    <!-- Quotes Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">RFQ Details</th>
                        <th>Seller Information</th>
                        <th>Buyer Information</th>
                        <th>Quote Price</th>
                        <th class="text-center">Status</th>
                        <th>Date</th>
                        <th class="text-end pe-4">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($quotes as $quote)
                        <tr>
                            <td class="ps-4">
                                <h6 class="fw-bold mb-0 text-dark" style="max-width: 250px;">
                                    <a href="{{ route('admin.quotes.show', $quote) }}" class="text-decoration-none text-dark">
                                        {{ Str::limit($quote->rfq->title ?? 'N/A', 40) }}
                                    </a>
                                </h6>
                                <small class="text-muted">ID: #{{ $quote->id }}</small>
                            </td>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <div class="company-avatar">
                                        {{ strtoupper(substr($quote->seller->company_name ?? 'S', 0, 1)) }}
                                    </div>
                                    <div>
                                        <div class="fw-bold small text-dark">{{ Str::limit($quote->seller->company_name ?? 'N/A', 20) }}</div>
                                        @if($quote->seller && $quote->seller->is_verified)
                                            <small class="text-success"><i class="bi bi-patch-check-fill"></i> Verified</small>
                                        @endif
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <div class="bg-light rounded-circle d-flex align-items-center justify-content-center text-muted border" 
                                         style="width: 32px; height: 32px; font-size: 0.8rem;">
                                        <i class="bi bi-person"></i>
                                    </div>
                                     <span class="text-dark small fw-medium">{{ $quote->rfq->buyer->user->name ?? 'Unknown Buyer' }}</span>
                                </div>
                            </td>
                            <td>
                                <div class="price-tag">₹{{ number_format($quote->price, 0) }}</div>
                            </td>
                            <td class="text-center">
                                {!! $quote->status_badge !!}
                            </td>
                            <td>
                                <div class="text-dark small fw-medium">{{ $quote->created_at->format('d M Y') }}</div>
                                <small class="text-muted">{{ $quote->created_at->diffForHumans() }}</small>
                            </td>
                            <td class="text-end pe-4">
                                <a href="{{ route('admin.quotes.show', $quote) }}" class="action-btn primary" title="View Details" data-bs-toggle="tooltip">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-chat-quote fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Quotes Found</h5>
                                    <p class="text-muted small mb-0">Try adjusting your search filters</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($quotes->hasPages())
            <div class="p-3 border-top bg-light" style="border-radius: 0 0 16px 16px;">
                {{ $quotes->links() }}
            </div>
        @endif
    </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
