@extends('layouts.admin')

@section('title', 'Customers')

@section('content')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
    <h1 class="h2">Customers Management</h1>
</div>

<!-- KPI Cards -->
<div class="row mb-4">
    <div class="col-md-3">
        <div class="card text-center">
            <div class="card-body">
                <h5 class="card-title text-muted mb-2">
                    <i class="bi bi-calendar-day"></i> Today
                </h5>
                <h2 class="mb-0 text-primary">{{ number_format($todayRegistered) }}</h2>
                <small class="text-muted">New Registrations</small>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-center">
            <div class="card-body">
                <h5 class="card-title text-muted mb-2">
                    <i class="bi bi-calendar-week"></i> This Week
                </h5>
                <h2 class="mb-0 text-info">{{ number_format($thisWeekRegistered) }}</h2>
                <small class="text-muted">New Registrations</small>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-center">
            <div class="card-body">
                <h5 class="card-title text-muted mb-2">
                    <i class="bi bi-calendar-month"></i> This Month
                </h5>
                <h2 class="mb-0 text-success">{{ number_format($thisMonthRegistered) }}</h2>
                <small class="text-muted">New Registrations</small>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card text-center">
            <div class="card-body">
                <h5 class="card-title text-muted mb-2">
                    <i class="bi bi-calendar-year"></i> This Year
                </h5>
                <h2 class="mb-0 text-warning">{{ number_format($thisYearRegistered) }}</h2>
                <small class="text-muted">New Registrations</small>
            </div>
        </div>
    </div>
</div>

<!-- Filters for Customers -->
<div class="card">
    <div class="card-body pb-0">
        <div class="row mb-3">
            <div class="col-md-12">
                <form method="GET" action="{{ route('admin.users.customers') }}" class="row g-2 justify-content-end">
                    <div class="col-md-3">
                        <label for="filter_name" class="form-label">Filter by Name</label>
                        <input type="text" 
                               class="form-control" 
                               id="filter_name" 
                               name="filter_name" 
                               value="{{ request('filter_name') }}" 
                               placeholder="Enter name to search...">
                    </div>
                    <div class="col-md-3">
                        <label for="filter_mobile" class="form-label">Filter by Mobile</label>
                        <div class="input-group">
                            <span class="input-group-text">+91</span>
                            <input type="text" 
                                   class="form-control" 
                                   id="filter_mobile" 
                                   name="filter_mobile" 
                                   value="{{ request('filter_mobile') }}" 
                                   placeholder="Enter mobile number..."
                                   maxlength="10"
                                   pattern="[6-9]\d{9}">
                        </div>
                    </div>
                    <div class="col-md-2 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary me-2">
                            <i class="bi bi-search"></i> Search
                        </button>
                        @if(request()->has('filter_name') || request()->has('filter_mobile'))
                        <a href="{{ route('admin.users.customers') }}" title="clear filter" class="btn btn-outline-secondary">
                           <i class="bi bi-arrow-counterclockwise"></i> 
                        </a>
                        @endif
                    </div>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Mobile</th>
                        <th>Email</th>
                        <th>Status</th>
                        <th>Registered</th>
                        <th>Last Login</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($users as $user)                      
                        <tr>
                            <td>#{{ $user->id }}</td>
                            <td>{{ $user->name }}</td>
                            <td>{{ $user->mobile }}</td>
                            <td>{{ $user->email ?? '-' }}</td>
                            <td>
                                @if($user->banned_at)
                                    <span class="badge bg-danger">
                                        <i class="bi bi-ban"></i> Banned
                                    </span>
                                @else
                                    <span class="badge bg-success">
                                        <i class="bi bi-check-circle"></i> Active
                                    </span>
                                @endif
                            </td>
                            <td>{{ $user->created_at->format('M d, Y') }}</td>
                            <td>{{ $user->last_login_at?->format('M d, Y H:i') ?? 'Never' }}</td>
                            <td>
                                <div class="d-flex gap-2">
                                    @can('customers.update')
                                        <a href="{{ route('admin.users.edit', $user->id) }}" class="admin-action-btn admin-action-btn-sm admin-btn-primary" title="View User Details">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                        @if($user->banned_at)
                                            <form action="{{ route('admin.users.unban', $user->id) }}" method="POST" class="d-inline">
                                                @csrf
                                                @method('PUT')
                                                <button type="submit" class="admin-action-btn admin-action-btn-sm admin-btn-primary" style="background: green; color: white;"
                                                        onclick="return confirm('Are you sure you want to unban this user?')" title="Unban User">
                                                    <i class="bi bi-person-gear"></i> 
                                                </button>
                                            </form>
                                        @else
                                            <form action="{{ route('admin.users.ban', $user->id) }}" method="POST" class="d-inline">
                                                @csrf
                                                @method('PUT')
                                                <button type="submit" class="admin-action-btn admin-action-btn-sm admin-btn-danger"  style="background: #ef4444; height: -webkit-fill-available; color: white;"
                                                        onclick="return confirm('Are you sure you want to ban this user? They will not be able to login.')" title="Ban User">
                                                    <i class="bi bi-person-gear"></i>
                                                </button>
                                            </form>
                                        @endif

                                        {{-- Change Password Action --}}
                                        <button type="button" class="admin-action-btn admin-action-btn-sm admin-btn-primary" style="background: #f59e0b; color: white;"
                                            data-bs-toggle="modal" data-bs-target="#changePasswordModal{{ $user->id }}" title="Change Password">
                                            <i class="bi bi-key"></i>
                                        </button>

                                        {{-- Change Password Modal --}}
                                        <div class="modal fade mt-5" id="changePasswordModal{{ $user->id }}" tabindex="-1"
                                            aria-labelledby="changePasswordModalLabel{{ $user->id }}" aria-hidden="true" style="margin-top: 3rem;">
                                            <div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="changePasswordModalLabel{{ $user->id }}">
                                                            <i class="bi bi-key"></i> Change Password for {{ $user->name }}
                                                        </h5>
                                                        <button type="button" class="btn p-0 text-white" data-bs-dismiss="modal" aria-label="Close"><i class="bi-x-lg"></i></button>
                                                    </div>
                                                    <form action="{{ route('admin.users.change-password', $user->id) }}" method="POST">
                                                        @csrf
                                                        @method('PUT')
                                                        <div class="modal-body">
                                                            <div class="mb-3">
                                                                <label for="password{{ $user->id }}" class="form-label">New Password</label>
                                                                <input type="password" class="form-control"
                                                                    id="password{{ $user->id }}" name="password"
                                                                    required minlength="8" placeholder="Enter new password">
                                                                <div class="form-text">Minimum 8 characters</div>
                                                            </div>
                                                            <div class="mb-3">
                                                                <label for="password_confirmation{{ $user->id }}" class="form-label">Confirm Password</label>
                                                                <input type="password" class="form-control"
                                                                    id="password_confirmation{{ $user->id }}" name="password_confirmation"
                                                                    required minlength="8" placeholder="Confirm new password">
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                                            <button type="submit" class="btn btn-warning"
                                                                    onclick="return confirm('Are you sure you want to change this user\'s password?')">
                                                                <i class="bi bi-key"></i> Change Password
                                                            </button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="bi bi-inbox" style="font-size: 2rem;"></i>
                                <p class="mt-2">No customers found.</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- Pagination - Right Aligned -->
        <div class="pagination-wrapper">
            <div class="d-flex justify-content-end align-items-center gap-3 flex-wrap">
                <div class="pagination-info-text">
                    Showing <strong>{{ $users->firstItem() ?? 0 }}</strong>–<strong>{{ $users->lastItem() ?? 0 }}</strong> 
                    of <strong>{{ $users->total() }}</strong> customers
                </div>
                @if($users->hasPages())
                <div class="pagination-controls">
                    {{ $users->links('vendor.pagination.admin-categories') }}
                </div>
                @endif
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const mobileFilterInput = document.getElementById('filter_mobile');
    if (mobileFilterInput) {
        // Only allow digits, max 10
        mobileFilterInput.addEventListener('input', function(e) {
            let value = this.value.replace(/\D/g, '').substring(0, 10);
            if (this.value !== value) {
                this.value = value;
            }
        });
        
        // Strip +91 if pasted
        mobileFilterInput.addEventListener('paste', function(e) {
            setTimeout(() => {
                let value = this.value.replace(/^\+91/, '').replace(/\D/g, '').substring(0, 10);
                if (value) {
                    this.value = value;
                }
            }, 0);
        });
    }
});
</script>
@endsection
