<form
    action="<?php if(!empty($packages)): ?> <?php echo e(route('batchUserPackageChange')); ?> <?php else: ?> <?php echo e(route('batchUserBillingCycleChange')); ?> <?php endif; ?>"
    method="POST" class="flex flex-wrap" id="batchUserPackageChange">
    <?php echo csrf_field(); ?>

    <!-- Changed from client_ids[] to client_ids (single input) -->
    <input type="hidden" name="client_ids" id="client_ids">
    <input type="hidden" id="uuid" name="uuid" value="<?php echo e(createUUID()); ?>">

    <div class="table-responsive">
        <table class="table table-bordered">
            <thead style="background: #78716C;color:white;">
                <tr>
                    <th class="text-center">
                        <input type="checkbox" id="allSelect">
                    </th>
                    <th scope="col" class="">
                        ID
                    </th>

                    <th scope="col" class="">
                        User name
                    </th>

                    <th scope="col" class="">
                        Expire Date
                    </th>

                    <th scope="col" class="">
                        Bill Cycle
                    </th>

                    <?php if(!auth()->check() || ! auth()->user()->hasRole('Sub Reseller')): ?>
                        <th scope="col" class="">
                            Package
                        </th>
                    <?php endif; ?>
                    <th scope="col" class="">
                        Sub Package
                    </th>

                    <th scope="col" class="">
                        Contact No
                    </th>
                </tr>
            </thead>
            <tbody class="">
                <?php $__empty_1 = true; $__currentLoopData = $r; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td class="text-center">
                            <!-- Removed name attribute - will be handled by JavaScript -->
                            <input type="checkbox" class="checkbox new_class_for_client_id_of_pending"
                                data-id="<?php echo e($row->id); ?>" value="<?php echo e($row->id); ?>">
                        </td>
                        <td>
                            <?php echo e($row->id); ?>

                        </td>
                        <td>
                            <?php echo e($row->userid); ?>

                        </td>

                        <td class="whitespace-nowrap">
                            <?php echo e(date('d-M-Y', strtotime($row->expire_date))); ?>

                        </td>

                        <td>
                            <?php echo e($row->billing_cycle); ?>

                        </td>

                        <?php if(!auth()->check() || ! auth()->user()->hasRole('Sub Reseller')): ?>
                            <td>
                                <?php echo e($row->packages->package_name); ?>

                            </td>
                        <?php endif; ?>

                        <td>
                            <?php if($row->subpack): ?>
                                <?php echo e($row->subpack->name); ?>

                            <?php endif; ?>
                        </td>

                        <td>
                            <?php echo e($row->clientsinfo->contact_no ?? ''); ?>

                        </td>
                    </tr>

                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="8" class="text-center">No records found</td>
                    </tr>
                <?php endif; ?>

                <?php if(!empty($packages)): ?>
                    <tr>
                        <td colspan="8">
                            <div class="row">
                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('batch-package-change-without-cost')): ?>
                                    <div class="col-sm-2">
                                        <label class="flex-1 block text-gray-700 text-sm font-bold mb-2"
                                            for="change_pack_without_cost">
                                            Change Package Without Cost
                                        </label>
                                    </div>
                                    <div class="col-sm-2">
                                        <select name="change_pack_without_cost" id="change_pack_without_cost"
                                            class="form-control">
                                            <option value="no">No</option>
                                            <option value="yes">Yes</option>
                                        </select>
                                    </div>
                                <?php endif; ?>
                                <div class="col-sm-4"></div>
                                <div class="col-sm-2">

                                    <?php if($row->pop->subreseller == 'yes'): ?>

                                        <select name="package_id" class="form-control" required>
                                            <option value="">Select</option>
                                            <?php $__currentLoopData = $packages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($p->id); ?>">
                                                    (<?php echo e($p->id); ?>)
                                                    - <?php echo e($p->name); ?> <?php if(globalPermission('package-change-price-show')): ?>
                                                        (<?php echo e($p->rate); ?> TK) (s)
                                                    <?php endif; ?>
                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                        </select>
                                    <?php else: ?>
                                        <select name="package_id" class="form-control" required>
                                            <option value="">Select</option>
                                            <?php $__currentLoopData = $packages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($p->id); ?>">
                                                    (<?php echo e($p->id); ?>)
                                                    - <?php echo e($p->package_name); ?>

                                                    <?php if(globalPermission('package-change-price-show')): ?>
                                                        (<?php echo e($p->package_rate); ?> TK)
                                                    <?php endif; ?>
                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                                        </select>

                                    <?php endif; ?>

                                </div>

                                <div class="col-sm-2">
                                    <button type="button" class="btn btn-primary" id="changePack">Change Package</button>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php else: ?>
                    <tr>
                        <td colspan="8">

                            <div class="container-fluid " style="background: #d8b4fe;">
                                <div class="row border  p-2">
                                    <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('change-billing-cycle-without-cost')): ?>
                                        <div class="col-sm-2">
                                            <label class="flex-1 block text-gray-700 text-sm font-bold mb-2"
                                                for="change_without_cost">
                                                Change Without Cost
                                            </label>
                                        </div>
                                        <div class="col-sm-2">
                                            <select name="change_without_cost" id="change_without_cost"
                                                class="form-control">
                                                <option value="no">No</option>
                                                <option value="yes">Yes</option>
                                            </select>
                                        </div>
                                    <?php endif; ?>
                                    <div class="col-sm-4"></div>
                                    <div class="col-sm-2 border " id="billCycle">
                                        <select name="billCycle" class="form-control">
                                            <option value="">Select Cycle</option>
                                            <?php $__currentLoopData = explode(',', $confday); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($item); ?>">
                                                    <?php echo e($item); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                    <div class="col-sm-2" id="expireDate" style="display: none">
                                        <input type="date" class="form-control" name="expire_date">
                                    </div>

                                    <div class="col-sm-2">
                                        <button type="button" class="btn btn-primary" id="changePack">Billing Cycle Change</button>
                                    </div>
                                </div>
                            </div>

                        </td>
                    </tr>

                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <!-- Selected Count Display -->
    <div class="mt-3">
        <span id="selectedCount" class="badge badge-info">Selected: 0</span>
    </div>
</form>

<script>
    $(function() {
        // Update selected count
        function updateSelectedCount() {
            var count = $('.new_class_for_client_id_of_pending:checked').length;
            $('#selectedCount').text('Selected: ' + count);
        }

        // Select/Deselect All
        $('#allSelect').on('click', function() {
            var isChecked = this.checked;
            $('.new_class_for_client_id_of_pending').prop('checked', isChecked);
            updateSelectedCount();
        });

        // Update count when individual checkbox is clicked
        $('.new_class_for_client_id_of_pending').on('change', function() {
            updateSelectedCount();

            // Update "select all" checkbox state
            var totalCheckboxes = $('.new_class_for_client_id_of_pending').length;
            var checkedCheckboxes = $('.new_class_for_client_id_of_pending:checked').length;
            $('#allSelect').prop('checked', totalCheckboxes === checkedCheckboxes);
        });

        // Handle form submission
        $('#changePack').on('click', function(e) {
            e.preventDefault();

            // Collect all checked IDs
            var ids = [];
            $('.new_class_for_client_id_of_pending:checked').each(function() {
                ids.push($(this).val());
            });

            // Validate that at least one checkbox is selected
            if (ids.length === 0) {
                alert('Please select at least one user to proceed.');
                return false;
            }

            // Additional validation for package/billing cycle
            <?php if(!empty($packages)): ?>
                var packageId = $('select[name="package_id"]').val();
                if (!packageId) {
                    alert('Please select a package.');
                    return false;
                }
            <?php else: ?>
                // Check if expire date field is visible
                if ($('#expireDate').is(':visible')) {
                    var expireDate = $('#expireDate input[name="expire_date"]').val();
                    console.log('Expire Date Value:', expireDate); // Debug
                    if (!expireDate || expireDate.trim() === '') {
                        alert('Please select an expire date.');
                        return false;
                    }
                } else {
                    // Otherwise validate billing cycle
                    var billCycle = $('#billCycle select[name="billCycle"]').val();
                    console.log('Bill Cycle Value:', billCycle); // Debug
                    if (!billCycle || billCycle === '') {
                        alert('Please select a billing cycle.');
                        return false;
                    }
                }
            <?php endif; ?>

            // Confirm action
            var confirmMessage = 'Are you sure you want to change ' + ids.length + ' user(s)?';
            if (!confirm(confirmMessage)) {
                return false;
            }

            // Disable button to prevent double submission
            $('#changePack').prop('disabled', true).text('Processing...');

            // Store IDs as comma-separated string in hidden input
            $('#client_ids').val(ids.join(','));

            // Submit the form
            $('#batchUserPackageChange').submit();
        });

        // Toggle between billing cycle dropdown and date picker
        $('#change_without_cost').on('change', function() {
            let type_val = $(this).val();

            if (type_val == 'yes') {
                $('#billCycle').hide();
                $('#billCycle select').prop('required', false);
                $('#expireDate').show();
                $('#expireDate input').prop('required', true);
            } else {
                $('#billCycle').show();
                $('#billCycle select').prop('required', true);
                $('#expireDate').hide();
                $('#expireDate input').prop('required', false);
            }
        });

        // Initialize selected count on page load
        updateSelectedCount();
    });
</script>
<?php /**PATH /var/www/html/netlink/radius-circle/resources/views/clients/result_all.blade.php ENDPATH**/ ?>