@extends('admin.layouts.app')

@section('page-title', 'Customers')

@section('content')

<style>
    .glass-card {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(20px);
        border-radius: 15px;
        border: 1px solid rgba(255, 255, 255, 0.2);
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        overflow: hidden;
    }
    
    .stats-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 12px;
        padding: 15px;
        color: white;
        position: relative;
        overflow: hidden;
        transition: transform 0.3s ease;
        min-height: 80px;
    }
    
    .stats-card:hover {
        transform: translateY(-3px);
    }
    
    .stats-count {
        font-size: 1.4rem;
        font-weight: 700;
        margin-bottom: 2px;
    }
    
    .stats-label {
        font-size: 0.75rem;
        opacity: 0.9;
        font-weight: 500;
    }
    
    .stats-icon {
        position: absolute;
        top: 12px;
        right: 12px;
        font-size: 1.2rem;
        opacity: 0.7;
    }
    
    .search-container {
        background: rgba(255, 255, 255, 0.9);
        border-radius: 10px;
        padding: 8px 15px;
        border: 1px solid rgba(0, 0, 0, 0.1);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        width: 100%;
    }
    
    .search-input {
        border: none;
        background: transparent;
        padding: 5px 10px;
        width: 100%;
        font-size: 14px;
        outline: none;
    }
    
    .filter-btn {
        background: rgba(102, 126, 234, 0.1);
        border: 2px solid transparent;
        color: #667eea;
        padding: 6px 15px;
        border-radius: 8px;
        font-weight: 600;
        transition: all 0.3s ease;
        margin: 0 5px;
        text-decoration: none;
        display: inline-block;
        font-size: 0.8rem;
    }
    
    .filter-btn.active {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-color: #667eea;
    }
    
    .add-customer-btn {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        border-radius: 8px;
        padding: 8px 20px;
        color: white;
        font-weight: 600;
        box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
        transition: all 0.3s ease;
        text-decoration: none;
        display: inline-block;
        font-size: 0.8rem;
    }
    
    .table-glass {
        background: rgba(255, 255, 255, 0.9);
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
    }
    
    .table-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
        color: white !important;
        font-weight: 600;
        font-size: 0.9rem;
        padding: 12px 15px !important;
        position: relative;
        border: none;
    }
    
    /* Curved borders for table headers */
    .table-header:first-child {
        border-top-left-radius: 10px;
    }
    
    .table-header:last-child {
        border-top-right-radius: 10px;
    }
    
    .table > :not(caption) > * > * {
        padding: 12px 15px;
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    
    .customer-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-weight: 600;
        font-size: 0.9rem;
        box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
    }
    
    .premium-badge {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        color: white;
        padding: 3px 8px;
        border-radius: 8px;
        font-size: 0.7rem;
        font-weight: 600;
    }
    
    .regular-badge {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        color: white;
        padding: 3px 8px;
        border-radius: 8px;
        font-size: 0.7rem;
        font-weight: 600;
    }
    
    .status-badge {
        padding: 4px 10px;
        border-radius: 8px;
        font-size: 0.75rem;
        font-weight: 600;
    }
    
    .active-status {
        background: linear-gradient(135deg, #4cd964 0%, #5ac8fa 100%);
        color: white;
    }
    
    .inactive-status {
        background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
        color: white;
    }
    
    .action-btn {
        width: 30px;
        height: 30px;
        border-radius: 6px;
        border: none;
        background: rgba(102, 126, 234, 0.1);
        color: #667eea;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
        margin: 0 2px;
        text-decoration: none;
        font-size: 0.8rem;
    }
    
    .action-btn:hover {
        background: #667eea;
        color: white;
        transform: scale(1.05);
    }
    
    .stats-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: 12px;
        margin-bottom: 20px;
    }
    
    .customer-info-column {
        min-width: 300px;
    }
    
    .table tbody tr {
        transition: all 0.3s ease;
    }
    
    .table tbody tr:hover {
        background: rgba(102, 126, 234, 0.03);
        transform: translateX(5px);
    }
    
    .customer-details {
        font-size: 0.8rem;
        color: #6c757d;
    }
    
    .customer-stats {
        font-size: 0.75rem;
        color: #6c757d;
    }
    
    /* Enhanced table styling */
    .table-container {
        border-radius: 10px;
        overflow: hidden;
    }
    
    .table-header-container {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 10px 10px 0 0;
        overflow: hidden;
    }

    /* Remove horizontal scroll */
    .table-responsive {
        overflow-x: visible !important;
    }

    .container-fluid {
        overflow-x: hidden;
    }

    /* Adjust table layout to fit without scroll */
    .table {
        width: 100%;
        min-width: auto;
    }

    /* Ensure proper column sizing */
    .table th,
    .table td {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    /* Fix for search and filter section */
    .search-filter-container {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: nowrap;
        gap: 15px;
    }
    
    .search-section {
        flex: 1;
        min-width: 300px;
    }
    
    .filter-section {
        display: flex;
        align-items: center;
        flex-wrap: nowrap;
        gap: 8px;
    }
    
    @media (max-width: 768px) {
        .search-filter-container {
            flex-direction: column;
            align-items: stretch;
        }
        
        .search-section {
            min-width: auto;
        }
        
        .filter-section {
            justify-content: center;
            flex-wrap: wrap;
        }
    }
</style>

<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 style="color: #2c3e50; font-weight: 700;">Customer List</h4>
    </div>

    <!-- Stats Cards with REAL DATA -->
    <div class="stats-row">
        <div class="stats-card">
            <div class="stats-count">{{ $customers->count() }}</div>
            <div class="stats-label">Total Customers</div>
            <i class="bi bi-people-fill stats-icon"></i>
        </div>
        
        <div class="stats-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
            <div class="stats-count">
                @php
                    $newThisMonth = $customers->filter(function($customer) {
                        return $customer->created_at->format('Y-m') === now()->format('Y-m');
                    })->count();
                @endphp
                {{ $newThisMonth }}
            </div>
            <div class="stats-label">New This Month</div>
            <i class="bi bi-person-plus stats-icon"></i>
        </div>
        
        <div class="stats-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
            <div class="stats-count">
                @php
                    $activeCustomers = $customers->where('status', 'active')->count();
                @endphp
                {{ $activeCustomers }}
            </div>
            <div class="stats-label">Active Customers</div>
            <i class="bi bi-activity stats-icon"></i>
        </div>
        
        <div class="stats-card" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);">
            <div class="stats-count">
                @php
                    $premiumCount = $customers->where('customer_type', 'premium')->count();
                @endphp
                {{ $premiumCount }}
            </div>
            <div class="stats-label">Premium Customers</div>
            <i class="bi bi-award-fill stats-icon"></i>
        </div>
    </div>

    <!-- Search and Filter Section - FIXED LAYOUT -->
    <div class="glass-card p-3 mb-3">
        <div class="search-filter-container">
            <!-- Search Section -->
            <div class="search-section">
                <form method="GET" action="{{ route('admin.customers.index') }}" class="d-flex align-items-center">
                    <div class="search-container">
                        <i class="bi bi-search text-muted me-2"></i>
                        <input type="text" 
                               name="search"
                               value="{{ $search ?? '' }}"
                               class="search-input" 
                               placeholder="Search Customer Name, Email, or Phone...">
                        @if(!empty($status))
                            <input type="hidden" name="status" value="{{ $status }}">
                        @endif
                    </div>
                </form>
            </div>
            
            <!-- Filter Section -->
            <div class="filter-section">
                <a href="{{ route('admin.customers.index', ['status'=>'active', 'search'=>$search ?? '']) }}"
                    class="filter-btn {{ ($status ?? '') == 'active' ? 'active' : '' }}">
                    Active
                </a>

                <a href="{{ route('admin.customers.index', ['status'=>'inactive', 'search'=>$search ?? '']) }}"
                    class="filter-btn {{ ($status ?? '') == 'inactive' ? 'active' : '' }}">
                    Inactive
                </a>

                <a href="{{ route('admin.customers.index') }}"
                    class="filter-btn {{ empty($status) ? 'active' : '' }}">
                    All
                </a>

                <a href="{{ route('admin.customers.add') }}" class="add-customer-btn">
                    <i class="bi bi-plus-circle me-1"></i>Add Customer
                </a>
            </div>
        </div>
    </div>

    <!-- Customer List Table -->
    <div class="table-container">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead>
                    <tr>
                        <th class="table-header">Customer Info</th>
                        <th class="table-header">Customer ID</th>
                        <th class="table-header">Create Date</th>
                        <th class="table-header">Status</th>
                        <th class="table-header" width="120">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($customers as $c)
                    <tr>
                        <!-- Customer Info Column -->
                        <td class="customer-info-column">
                            <div class="d-flex align-items-start">
                                <div class="customer-avatar me-3">
                                    {{ strtoupper(substr($c->customer_name,0,1)) }}
                                </div>
                                <div>
                                    <div class="fw-bold">{{ $c->customer_name }}</div>
                                    @if($c->customer_type == 'premium')
                                        <span class="premium-badge">Premium Customer</span>
                                    @else
                                        <span class="regular-badge">Regular Customer</span>
                                    @endif
                                    <div class="customer-details mt-1">
                                        {{ $c->email }}<br>
                                        {{ $c->mobile_no }}
                                    </div>
                                    <div class="customer-stats mt-1">
                                        {{ $c->orders->count() }} orders • ₹{{ number_format($c->orders->sum('ttl_amount'), 2) }} total
                                    </div>
                                </div>
                            </div>
                        </td>

                        <!-- Customer ID -->
                        <td>
                            <span class="fw-bold text-primary">{{ $c->customer_id }}</span>
                        </td>

                        <!-- Create Date -->
                        <td>
                            <span class="text-muted">{{ $c->created_at ? $c->created_at->format('d-m-Y') : '-' }}</span>
                        </td>

                        <!-- Status -->
                        <td>
                            @if($c->status === 'active')
                                <span class="status-badge active-status">Active</span>
                            @else
                                <span class="status-badge inactive-status">Inactive</span>
                            @endif
                        </td>

                        <!-- Actions -->
                        <td>
                            <div class="d-flex">
                                <a href="{{ route('admin.customers.view', $c->id) }}"
                                   class="action-btn" title="View">
                                   <i class="bi bi-eye"></i>
                                </a>

                                <form action="{{ route('admin.customers.status', $c->id) }}" 
                                      method="POST" style="display:inline-block;">
                                    @csrf
                                    <button class="action-btn" title="Toggle Status" type="submit">
                                        <i class="bi bi-power"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="text-center py-4">
                            <i class="bi bi-people display-4 text-muted d-block mb-2"></i>
                            <span class="text-muted">No customers found.</span>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

@endsection