@extends('layouts.app')

@section('title', __('customers'))

@push('styles')
<style>
    .customer-card {
        transition: transform 0.2s, box-shadow 0.2s;
        border-radius: 12px;
        overflow: hidden;
    }
    .customer-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.1) !important;
    }
    .status-badge {
        font-size: 0.75rem;
        padding: 0.25rem 0.5rem;
        border-radius: 4px;
        font-weight: 600;
    }
    .status-overdue {
        background-color: #ffebee;
        color: #c62828;
    }
    .status-due-soon {
        background-color: #fff3e0;
        color: #ef6c00;
    }
    .status-upcoming {
        background-color: #e8f5e9;
        color: #2e7d32;
    }
    
    /* Dark Mode Support */
    body.dark-mode .customer-card {
        background-color: #1e293b;
        border-color: #334155;
    }
    body.dark-mode .text-muted {
        color: #94a3b8 !important;
    }
    body.dark-mode .status-overdue {
        background-color: rgba(198, 40, 40, 0.2);
        color: #ef9a9a;
    }
    body.dark-mode .status-due-soon {
        background-color: rgba(239, 108, 0, 0.2);
        color: #ffcc80;
    }
    body.dark-mode .status-upcoming {
        background-color: rgba(46, 125, 50, 0.2);
        color: #a5d6a7;
    }
</style>
@endpush

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold m-0">{{ __('customers') }}</h4>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCustomerModal">
            <i class="bi bi-plus-lg me-2"></i> {{ __('add_customer') }}
        </button>
    </div>
    
    <!-- Search -->
    <div class="row mb-4">
        <div class="col-12">
            <form action="{{ route('customers.index') }}" method="GET">
                <div class="input-group shadow-sm">
                    <span class="input-group-text bg-white border-end-0"><i class="bi bi-search text-muted"></i></span>
                    <input type="search" class="form-control border-start-0 ps-0" name="search" placeholder="{{ __('subscriptions.search_placeholder') }}" value="{{ request('search') }}">
                </div>
            </form>
        </div>
    </div>

    <div class="row g-4">
        @forelse($customers as $customer)
            @php
                $subscriptions = $customer->serviceSubscriptions;
                $activeCount = $subscriptions->count();
                // Find the most urgent renewal date
                $nearestDue = $subscriptions->sortBy('renewal_due_date')->first();
                
                $statusClass = 'bg-light text-muted'; // Default for no services
                $statusText = __('no_active_services');
                $dueDateStr = '-';

                if ($nearestDue) {
                    $dueDate = \Carbon\Carbon::parse($nearestDue->renewal_due_date);
                    $now = \Carbon\Carbon::now();
                    $daysDiff = $now->diffInDays($dueDate, false);
                    $dueDateStr = $dueDate->format('Y-m-d');
                    
                    if ($daysDiff < 0) {
                        $statusClass = 'status-overdue';
                        $statusText = __('status_overdue');
                    } elseif ($daysDiff <= 30) {
                        $statusClass = 'status-due-soon';
                        $statusText = __('status_due_soon');
                    } else {
                        $statusClass = 'status-upcoming';
                        $statusText = __('status_upcoming');
                    }
                }
            @endphp
            <div class="col-md-6 col-lg-4 col-xl-3">
                <a href="{{ route('customers.show', $customer->id) }}" class="text-decoration-none text-dark">
                    <div class="card h-100 border-0 shadow-sm customer-card">
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-start mb-3">
                                <h5 class="card-title fw-bold mb-0 text-truncate" title="{{ $customer->name }}">
                                    {{ $customer->name }}
                                </h5>
                                <span class="status-badge {{ $statusClass }}">
                                    {{ $statusText }}
                                </span>
                            </div>
                            
                            <div class="mb-3">
                                <div class="text-muted small mb-1">
                                    <i class="bi bi-telephone me-2"></i> {{ $customer->phone ?? 'N/A' }}
                                </div>
                                <div class="text-muted small">
                                    <i class="bi bi-briefcase me-2"></i> 
                                    {{ $customer->business_name ?? '-' }}
                                </div>
                                <div class="text-muted small mt-1">
                                    <i class="bi bi-collection me-2"></i> 
                                    {{ $activeCount }} {{ __('services') }}
                                </div>
                            </div>
                            
                            <div class="d-flex justify-content-between align-items-center mt-auto pt-3 border-top">
                                <small class="text-muted">
                                    {{ __('next_renewal') }}: <span class="fw-bold">{{ $dueDateStr }}</span>
                                </small>
                                <i class="bi bi-arrow-right text-primary"></i>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
        @empty
            <div class="col-12">
                <div class="text-center py-5 text-muted">
                    <i class="bi bi-people fs-1 mb-3 d-block"></i>
                    {{ __('no_customers_found') }}
                </div>
            </div>
        @endforelse
    </div>

    @if($customers->hasPages())
        <div class="d-flex justify-content-center mt-4">
            {{ $customers->links() }}
        </div>
    @endif
</div>

<!-- Add Customer Modal -->
<div class="modal fade" id="addCustomerModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('customers.store') }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title">{{ __('add_customer') }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">{{ __('customer_name') }} <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ __('phone_number') }} <span class="text-danger">*</span></label>
                        <input type="text" name="phone" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ __('whatsapp_number') }}</label>
                        <input type="text" name="whatsapp" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ __('address') }}</label>
                        <textarea name="address" class="form-control" rows="2"></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ __('business_name') }}</label>
                        <input type="text" name="business_name" class="form-control">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">{{ __('business_link') }}</label>
                        <input type="url" name="business_link" class="form-control">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('close') }}</button>
                    <button type="submit" class="btn btn-primary">{{ __('save') }}</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
