@extends('layouts.admin')

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

@section('admin-content')
<style>
    /* Specific Inquiries Styles */
    .inquiries-page .avatar-circle { width: 36px; height: 36px; border-radius: 10px; background: linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; }
    
    .inquiries-page .status-badge.pending { background: #fff3cd; color: #856404; }
    .inquiries-page .status-badge.assigned { background: #d4edda; color: #155724; }
    .inquiries-page .status-badge.rejected { background: #f8d7da; color: #721c24; }
    .inquiries-page .status-badge.new { background: #cce5ff; color: #004085; }
    .inquiries-page .status-badge.responded { background: #e2e3e5; color: #383d41; }

    .inquiries-page .product-chip { display: inline-flex; align-items: center; padding: 4px 10px; background: #f8f9fa; border: 1px solid #e9ecef; border-radius: 6px; font-size: 0.8rem; color: #555; max-width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
</style>

<div class="inquiries-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Inquiry Management</h4>
            <p class="text-muted mb-0 small">Review, assign, and track buyer inquiries</p>
        </div>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-primary shadow-sm btn-lg d-flex align-items-center gap-2" 
                    data-bs-toggle="modal" data-bs-target="#bulkAssignModal" id="bulkAssignBtn" disabled
                    style="border-radius: 12px; font-size: 0.95rem;">
                <i class="bi bi-check2-all"></i> Bulk Assign
            </button>
            <a href="{{ route('admin.inquiries.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-download"></i> Export
            </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 warning">
                    <i class="bi bi-hourglass-split"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $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">{{ $stats['assigned'] ?? 0 }}</div>
                    <div class="stat-label">Assigned</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 danger">
                    <i class="bi bi-x-circle-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $stats['rejected'] ?? 0 }}</div>
                    <div class="stat-label">Rejected</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 primary">
                    <i class="bi bi-envelope-open-fill"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $stats['total'] ?? 0 }}</div>
                    <div class="stat-label">Total Inquiries</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="filter-card mb-4">
        <form action="{{ route('admin.inquiries.index') }}" method="GET" class="row g-3 align-items-end">
            <div class="col-lg-3 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">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="Buyer, email, product..." 
                           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">Admin Status</label>
                <select name="admin_status" class="form-select form-select-lg" style="border-radius: 10px;">
                    <option value="all">All Status</option>
                    <option value="pending" {{ request('admin_status') == 'pending' ? 'selected' : '' }}>Pending</option>
                    <option value="assigned" {{ request('admin_status') == 'assigned' ? 'selected' : '' }}>Assigned</option>
                    <option value="rejected" {{ request('admin_status') == 'rejected' ? 'selected' : '' }}>Rejected</option>
                </select>
            </div>
            <div class="col-lg-2 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">Seller Status</label>
                <select name="status" class="form-select form-select-lg" style="border-radius: 10px;">
                    <option value="all">All</option>
                    <option value="new" {{ request('status') == 'new' ? 'selected' : '' }}>New</option>
                    <option value="responded" {{ request('status') == 'responded' ? 'selected' : '' }}>Responded</option>
                </select>
            </div>
            <div class="col-lg-3 col-md-6">
                <label class="form-label small fw-bold text-muted mb-1">Date Range</label>
                <div class="input-group">
                    <input type="date" name="from_date" class="form-control form-control-lg" value="{{ request('from_date') }}" style="border-radius: 10px 0 0 10px;">
                    <input type="date" name="to_date" class="form-control form-control-lg" value="{{ request('to_date') }}" style="border-radius: 0 10px 10px 0; border-left: 0;">
                </div>
            </div>
            <div class="col-lg-2 col-md-12">
                <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>

    <!-- Inquiries Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4" style="width: 40px;">
                            <input type="checkbox" id="selectAll" class="form-check-input">
                        </th>
                        <th>Buyer</th>
                        <th>Seller</th>
                        <th>Product Interest</th>
                        <th class="text-center">Admin Status</th>
                        <th class="text-center">Seller Status</th>
                        <th>Date</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($inquiries as $inquiry)
                        <tr>
                            <td class="ps-4">
                                @if($inquiry->admin_status == 'pending')
                                    <input type="checkbox" class="form-check-input inquiry-checkbox" value="{{ $inquiry->id }}">
                                @endif
                            </td>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <div class="avatar-circle">
                                        {{ strtoupper(substr($inquiry->name ?? 'U', 0, 1)) }}
                                    </div>
                                    <div>
                                        <div class="fw-bold text-dark">{{ $inquiry->name }}</div>
                                        <small class="text-muted">{{ $inquiry->email }}</small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                @if($inquiry->seller)
                                    <div>
                                        <div class="fw-medium text-dark">{{ Str::limit($inquiry->seller->company_name, 20) }}</div>
                                        <div class="d-flex gap-1">
                                            @if($inquiry->seller->hasActiveSubscription())
                                                <span class="badge bg-light text-success border border-success px-1" style="font-size: 0.65rem;">Paid</span>
                                            @else
                                                <span class="badge bg-light text-muted border px-1" style="font-size: 0.65rem;">Free</span>
                                            @endif
                                            @if($inquiry->seller->notification_enabled)
                                                <i class="bi bi-bell-fill text-warning" style="font-size: 0.7rem;" title="Notifications On"></i>
                                            @endif
                                        </div>
                                    </div>
                                @else
                                    <span class="badge bg-danger bg-opacity-10 text-danger px-2">Deleted Seller</span>
                                @endif
                            </td>
                            <td>
                                @if($inquiry->product)
                                    <div class="product-chip" title="{{ $inquiry->product->name }}">
                                        {{ $inquiry->product->name }}
                                    </div>
                                @else
                                    <span class="text-muted small fst-italic">General Inquiry</span>
                                @endif
                            </td>
                            <td class="text-center">
                                @if($inquiry->admin_status == 'pending')
                                    <span class="status-badge pending"><i class="bi bi-hourglass-split me-1"></i> Pending</span>
                                @elseif($inquiry->admin_status == 'assigned')
                                    <span class="status-badge assigned"><i class="bi bi-check-circle me-1"></i> Assigned</span>
                                @else
                                    <span class="status-badge rejected"><i class="bi bi-x-circle me-1"></i> Rejected</span>
                                @endif
                            </td>
                            <td class="text-center">
                                @if($inquiry->status == 'new')
                                    <span class="status-badge new">New</span>
                                @else
                                    <span class="status-badge responded">Responded</span>
                                @endif
                            </td>
                            <td>
                                <div class="text-dark small fw-medium">{{ $inquiry->created_at->format('d M Y') }}</div>
                                <small class="text-muted">{{ $inquiry->created_at->format('H:i') }}</small>
                            </td>
                            <td class="text-end pe-4">
                                <div class="d-flex gap-1 justify-content-end">
                                    <a href="{{ route('admin.inquiries.show', $inquiry) }}" class="action-btn primary" title="View Details" data-bs-toggle="tooltip">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    @if($inquiry->admin_status == 'pending')
                                        <button type="button" class="action-btn success" title="Quick Assign" 
                                                onclick="quickAssign({{ $inquiry->id }})" data-bs-toggle="tooltip">
                                            <i class="bi bi-check-lg"></i>
                                        </button>
                                    @endif
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-envelope fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Inquiries Found</h5>
                                    <p class="text-muted small mb-0">Try different search criteria</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($inquiries->hasPages())
           <div class="p-3 border-top bg-light" style="border-radius: 0 0 16px 16px;">
                {{ $inquiries->links() }}
            </div>
        @endif
    </div>
</div>

<!-- Bulk Assign Modal -->
<div class="modal fade" id="bulkAssignModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <form action="{{ route('admin.inquiries.bulk-assign') }}" method="POST" class="modal-content border-0 shadow-lg" style="border-radius: 20px;">
            @csrf
            <input type="hidden" name="inquiry_ids" id="bulkInquiryIds">
            <div class="modal-header border-0 pb-0">
                <div>
                     <h5 class="modal-title fw-bold">Bulk Assign Inquiries</h5>
                     <p class="text-muted small mb-0">Assign checked inquiries to their sellers</p>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-4">
                <div class="alert alert-info border-0 d-flex align-items-center mb-3">
                    <i class="bi bi-info-circle-fill me-2 fs-5"></i>
                    <div>
                        You are about to assign <strong id="selectedCount" class="fs-5">0</strong> inquiries.
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label fw-bold small text-muted">Admin Notes (Optional)</label>
                    <textarea name="admin_notes" class="form-control form-control-lg rounded-3" rows="3" placeholder="Add any specific instructions..."></textarea>
                </div>
            </div>
            <div class="modal-footer border-0 pt-0">
                <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Cancel</button>
                <button type="submit" class="btn btn-success rounded-3 px-4 fw-bold">
                    <i class="bi bi-check2-all me-2"></i> Assign Selected
                </button>
            </div>
        </form>
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Select all checkbox
        const selectAll = document.getElementById('selectAll');
        if(selectAll) {
            selectAll.addEventListener('change', function() {
                const checkboxes = document.querySelectorAll('.inquiry-checkbox');
                checkboxes.forEach(cb => cb.checked = this.checked);
                updateBulkButton();
            });
        }
        
        // Individual checkbox change
        const checkboxes = document.querySelectorAll('.inquiry-checkbox');
        checkboxes.forEach(checkbox => {
            checkbox.addEventListener('change', updateBulkButton);
        });
        
        // Tooltips
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });

    function updateBulkButton() {
        const checked = document.querySelectorAll('.inquiry-checkbox:checked');
        const bulkBtn = document.getElementById('bulkAssignBtn');
        const selectedCount = document.getElementById('selectedCount');
        const bulkIds = document.getElementById('bulkInquiryIds');
        
        if (bulkBtn && selectedCount && bulkIds) {
            if (checked.length > 0) {
                bulkBtn.disabled = false;
                selectedCount.textContent = checked.length;
                bulkIds.value = JSON.stringify(Array.from(checked).map(cb => cb.value));
            } else {
                bulkBtn.disabled = true;
                selectedCount.textContent = '0';
                bulkIds.value = '';
            }
        }
    }

    function quickAssign(inquiryId) {
        if (confirm('Assign this inquiry to the seller?')) {
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = `/admin/inquiries/${inquiryId}/assign`;
            
            const csrf = document.createElement('input');
            csrf.type = 'hidden';
            csrf.name = '_token';
            csrf.value = '{{ csrf_token() }}';
            
            form.appendChild(csrf);
            document.body.appendChild(form);
            form.submit();
        }
    }
</script>
@endpush
@endsection
