<?php
include 'dbclass/dbclass.php';
// Establish connection to the database
$con = new mysqli('localhost', 'gdhhsxmy_mlavallikkunnu', 'mlavallikkunnumlavallikkunnu', 'gdhhsxmy_mlavallikkunnu');

// Check connection
if ($con->connect_error) {
    die('Connection failed: ' . $con->connect_error);
}

// Get form data and convert to uppercase
$regnumber  = strtoupper($_POST['regnumber']);
$name       = strtoupper($_POST['name']);
$exam       = strtoupper($_POST['exam']);
$fathername = strtoupper($_POST['fathername']);
$number     = strtoupper($_POST['number']);
$address    = strtoupper($_POST['address']);
$panchayath = ($_POST['panchayath']);
$ward       = strtoupper($_POST['ward']);
$cnsno      = ($_POST['school']);

$id = $_POST['editid'];



// Handle cropped image
$imageFile = null;



if (isset($_POST['croppedImage']) && !empty($_POST['croppedImage'])) {
    $folderPath   = 'uploads/';
    $image_parts  = explode(';base64,', $_POST['croppedImage']);
    $image_base64 = base64_decode($image_parts[1]);
    $imageFile    = $folderPath . uniqid() . '.x';
    file_put_contents($imageFile, $image_base64);
}

// Check file formats and upload mark list
$marklistPath = uploadFile('marklist', 'uploads/', ['pdf', 'jpg', 'jpeg', 'png']);

$query  = "SELECT * FROM students WHERE id=$id";
$result = $con->query($query);



if ($result->num_rows > 0) {
    // If a record exists, update it
    $sql = "UPDATE students SET 
        name='$name', 
        exam='$exam', 
        fathername='$fathername', 
        number='$number', 
        address='$address', 
        regnumber='$regnumber',
        panchayath='$panchayath', 
        school='$cnsno',
       
        ward='$ward'";

    // Append photo to the SQL if a new image was uploaded
    if ($imageFile) {
        $sql .= ", photo='$imageFile'";
    }

    // Append marklist to the SQL if a new marklist was uploaded
    if ($marklistPath) {
        $sql .= ", marklist='$marklistPath'";
    }

    $sql .= " WHERE id='$id'";



    if ($con->query($sql) == true) {
        ?>
        <script>
            //alert(window.screen.width);
                window.location.href = 'view.php';
        </script>
        <?php
    } else {
        echo 'Error: ' . $sql . '<br>' . $con->error;
    }
}

// Function to upload file with random name
function uploadFile($fileInputName, $targetDir, $allowedFormats)
{
    if (isset($_FILES[$fileInputName]) && $_FILES[$fileInputName]['error'] == UPLOAD_ERR_OK) {
        $fileType = pathinfo($_FILES[$fileInputName]['name'], PATHINFO_EXTENSION);

        if (!in_array(strtolower($fileType), $allowedFormats)) {
            return null;
        }
        $randomFileName = uniqid() . '_' . basename($_FILES[$fileInputName]['name']);
        $targetFile     = $targetDir . $randomFileName;

        // Move the uploaded file to the specified directory
        if (move_uploaded_file($_FILES[$fileInputName]['tmp_name'], $targetFile)) {
            return $targetFile;
        }
    }

    return null;
}

// Close database connection
$con->close();
?>
