<?php

// fetch_districts.php

require_once('lib/dbclass.php');

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

$id = $data['id'];

// 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;
}

unset($data['croppedImage']);

// Save the form data to the database (or update)
$id = saveArray('tbl_candidates', $data, $id);

// After saving the data, display SweetAlert using JavaScript
echo "<script src='sweetalert.js'></script>";
echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
            swal({
                icon: 'success',
                title: 'Updated Successfully!',
                text: 'Your registration has been successfully updated.',
                Button: false,   // Hides the OK button
                timer: 3000                // Set timer in milliseconds (3000ms = 3 seconds)
            }).then(function() {
                window.location = 'viewdata.php';  // Redirect after the timer expires
            });
        });
      </script>";
