<?php $__env->startSection('title',"Vérification du clé d'activation"); ?>
<?php $__env->startSection('content'); ?>
    <!-- Include jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <!-- Include SweetAlert -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

    <!-- CSRF Token setup for Laravel -->
    <meta name="csrf-token" content="<?php echo e(csrf_token()); ?>">
       <!-- [ Main Content ] start -->
       <div class="col-6">


        <div class="card">
                    <div class="card-header">
                        <h5>Vérification du clé d'activation</h5>
                    </div>
                    <div class="card-body">
                <div class="col-md-12">
                    <form id="kycRequestForm">
                        <?php echo csrf_field(); ?>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Numéro de téléphone (MSISDN)</label>
                                <input type="number" class="form-control" name="msisdn" id="msisdn" placeholder="Contact client" required>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Clé d'activation</label>
                                <input type="text" class="form-control" name="key" id="key" placeholder="Clé" required>
                            </div>

                            <button type="submit" class="btn btn-primary">Vérifier</button>


                        </form>
                </div>
                <!-- [ Main Content ] end -->
            </div>
        </div>
    </div>

    <!-- Form to submit the alias -->

 <!-- Hidden div to hold the printable content -->
 <div id="printableKYC" style="display:none;"></div>

 <script>
    $(document).ready(function() {
        // Handle form submission using AJAX
        $('#kycRequestForm').submit(function(e) {
            e.preventDefault();

            // Get form values
            var msisdn = $('#msisdn').val();
            var key = $('#key').val();

            // Make an AJAX POST request to the server
            $.ajax({
                url: "<?php echo e(asset('kyc-request')); ?>", // Route to handle the KYC request
                type: 'POST',
                data: {
                    _token: $('meta[name="csrf-token"]').attr('content'), // CSRF token
                    msisdn: msisdn,
                    key: key
                },
                success: function(response) {
                    // Build printable HTML content
                    var printableContent = '<strong>Status:</strong> ' + response.status +
                                           '<br><strong>Last Name:</strong> ' + response.lastName +
                                           '<br><strong>First Name:</strong> ' + response.firstName +
                                           '<br><strong>Date of Birth:</strong> ' + response.dob +
                                           '<br><strong>CIN:</strong> ' + response.cin;

                    // Place the content in a hidden div for printing
                    $('#printableKYC').html(printableContent);

                    // Show the result in SweetAlert with a print button
                    Swal.fire({
                        title: 'Résultat de la demande KYC',
                        html: printableContent +
                              '<br><br><button id="printKYCButton" class="swal2-confirm swal2-styled"><i class="fas fa-print"></i></button><a href="confirm_registration_form_link/'+msisdn+'/'+key+'" class="btn btn-primary">Validé l\'inscription</a>',
                        showConfirmButton: false,
                    });

                    // Add event listener for the print button
                    $('#printKYCButton').click(function() {
                        printDiv('printableKYC');
                    });
                },
                error: function(xhr, status, error) {
                // Handle validation errors or server-side errors
                var response = xhr.responseJSON;
                if (response && response.message) {
                    Swal.fire({
                        title: 'Erreur!',
                        text: response.message,
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                } else {
                    Swal.fire({
                        title: 'Erreur!',
                        text: 'Une erreur inattendue est survenue. Veuillez réessayer plus tard.',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                }
            }
            });
        });
    });

    // Function to print the content of a specific div
    function printDiv(divId) {
        var content = document.getElementById(divId).innerHTML;
        var myWindow = window.open('', 'Print', 'height=600,width=800');
        myWindow.document.write('<html><head><title>Print KYC Request</title>');
        myWindow.document.write('</head><body>');
        myWindow.document.write(content);
        myWindow.document.write('</body></html>');
        myWindow.document.close();
        myWindow.focus();
        myWindow.print();
        myWindow.close();
    }
</script>

<?php $__env->stopSection(); ?>


<?php echo $__env->make('admin.layout', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\laragon\www\api_web_orange_caece\resources\views/registration_module/check_key.blade.php ENDPATH**/ ?>