

<?php $__env->startSection('title', 'Customers'); ?>

<?php $__env->startSection('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"><?php echo e(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"><?php echo e(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"><?php echo e(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"><?php echo e(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="<?php echo e(route('admin.users.customers')); ?>"
                class="d-flex align-items-end justify-content-end gap-2 flex-wrap">

                <div class="customers-filter-field">
                    <label for="filter_name" class="admin-form-label">Filter by Name</label>
                    <input type="text"
                        class="form-control admin-form-control"
                        id="filter_name"
                        name="filter_name"
                        value="<?php echo e(request('filter_name')); ?>"
                        placeholder="Enter name to search...">
                </div>

                <div class="customers-filter-field customers-filter-mobile">
                    <label for="filter_mobile" class="admin-form-label">Filter by Mobile</label>
                    <div class="input-group">
                        <span class="input-group-text">+91</span>
                        <input type="text"
                            class="form-control admin-form-control"
                            id="filter_mobile"
                            name="filter_mobile"
                            value="<?php echo e(request('filter_mobile')); ?>"
                            placeholder="10-digit mobile"
                            maxlength="10"
                            inputmode="numeric"
                            autocomplete="tel-national"
                            pattern="[6-9]\d{9}">
                    </div>
                </div>

                <div>
                    <label class="admin-form-label">&nbsp;</label>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-search"></i> Search
                        </button>

                        <?php if(request()->has('filter_name') || request()->has('filter_mobile')): ?>
                            <a href="<?php echo e(route('admin.users.customers')); ?>"
                            title="Clear filter"
                            class="btn btn-outline-secondary">
                                <i class="bi bi-arrow-counterclockwise"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </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>
                    <?php $__empty_1 = true; $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>                      
                        <tr>
                            <td>#<?php echo e($user->id); ?></td>
                            <td><?php echo e($user->name); ?></td>
                            <td><?php echo e($user->mobile); ?></td>
                            <td><?php echo e($user->email ?? '-'); ?></td>
                            <td>
                                <?php if($user->banned_at): ?>
                                    <span class="badge bg-danger">
                                        <i class="bi bi-ban"></i> Banned
                                    </span>
                                <?php else: ?>
                                    <span class="badge bg-success">
                                        <i class="bi bi-check-circle"></i> Active
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo e($user->created_at->format('M d, Y')); ?></td>
                            <td><?php echo e($user->last_login_at?->format('M d, Y H:i') ?? 'Never'); ?></td>
                            <td>
                                <div class="d-flex gap-2">
                                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('customers.update')): ?>
                                        <a href="<?php echo e(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>
                                        <?php if($user->banned_at): ?>
                                            <form action="<?php echo e(route('admin.users.unban', $user->id)); ?>" method="POST" class="d-inline">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('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>
                                        <?php else: ?>
                                            <form action="<?php echo e(route('admin.users.ban', $user->id)); ?>" method="POST" class="d-inline">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('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>
                                        <?php endif; ?>

                                        
                                        <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<?php echo e($user->id); ?>" title="Change Password">
                                            <i class="bi bi-key"></i>
                                        </button>

                                        
                                        <div class="modal fade mt-5" id="changePasswordModal<?php echo e($user->id); ?>" tabindex="-1"
                                            aria-labelledby="changePasswordModalLabel<?php echo e($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<?php echo e($user->id); ?>">
                                                            <i class="bi bi-key"></i> Change Password for <?php echo e($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="<?php echo e(route('admin.users.change-password', $user->id)); ?>" method="POST">
                                                        <?php echo csrf_field(); ?>
                                                        <?php echo method_field('PUT'); ?>
                                                        <div class="modal-body">
                                                            <div class="mb-3">
                                                                <label for="password<?php echo e($user->id); ?>" class="form-label">New Password</label>
                                                                <input type="password" class="form-control"
                                                                    id="password<?php echo e($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<?php echo e($user->id); ?>" class="form-label">Confirm Password</label>
                                                                <input type="password" class="form-control"
                                                                    id="password_confirmation<?php echo e($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>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <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>
                    <?php endif; ?>
                </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><?php echo e($users->firstItem() ?? 0); ?></strong>–<strong><?php echo e($users->lastItem() ?? 0); ?></strong> 
                    of <strong><?php echo e($users->total()); ?></strong> customers
                </div>
                <?php if($users->hasPages()): ?>
                <div class="pagination-controls">
                    <?php echo e($users->links('vendor.pagination.admin-categories')); ?>

                </div>
                <?php 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>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/html/shivajewellers_uat/resources/views/admin/users/customers.blade.php ENDPATH**/ ?>