<?php
if (!isset($_SESSION)) {
    session_start();
}
require_once('../lib/dbclass.php');


unset($_POST['declaration']);
$data = $_POST;


// Handle cropped image
$imageFile = null;



if (isset($_POST['croppedImage']) && $_POST['croppedImage'] != '') {
    $folderPath   = '../uploads/';
    $image_parts  = explode(';base64,', $_POST['croppedImage']);
    $image_base64 = base64_decode($image_parts[1]);
    $picname      = uniqid() . '.png';
    $imageFile    = $folderPath . $picname;
    file_put_contents($imageFile, $image_base64);
    $data['photo'] = $picname;
} else {
    echo "<script src='sweetalert.js'></script>";
    echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
            swal({
                icon: 'error',
                title: 'Image upload failed!',
                text: 'Image upload failed,Please try again.',
                Button: false,   // Hides the OK button
                timer: 3000                // Set timer in milliseconds (3000ms = 3 seconds)
            }).then(function() {
                window.location = 'index.html';  // Redirect after the timer expires
            });
        });
      </script>";

    exit;
}

unset($data['croppedImage']);


$data['paymentid'] = '';


$data['date'] = date('Y-m-d H:i:s');

if ($data['referal'] == '') {
    $data['referal'] = 31;
}

$id = saveArray('tbl_candidates', $data, 0);

$Regno = 'AKNME/2025/' . $id;

updateQuery("update tbl_candidates set Regno='$Regno' where id=$id");

$_POST['customer_id'] = $Regno;




$modifiedUrl = 'https://keralaneetmodelexam.in/payments/share.php';

// Creating a hidden form to POST the data
echo '<html><body>';
echo '<form id="redirectForm" action="' . $modifiedUrl . '" method="POST">';

// Loop through POST values and create hidden inputs for each
foreach ($_POST as $key => $value) {
    echo '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($value) . '" />';
}

echo '</form>';

// JavaScript to automatically submit the form
echo '<script type="text/javascript">
            document.getElementById("redirectForm").submit();
          </script>';
echo '</body></html>';

exit();  // Ensure no further code is executed





?>

