<?php


 function formatDate($data){
    return date('jS M, Y', strtotime($data));
 };


 function formatDateTime($data){
    $timestamp = strtotime($data);

    return date('jS M, Y - g:i A', strtotime($data));
 };


 function log_activity($activity){
 	global $conn;
 	$sql = "INSERT INTO site_log (activity) VALUES (?)";
 	$stmt = $conn->prepare($sql);
 	$stmt->bind_param("s", $activity);
 	$stmt->execute();
 	$stmt->close();
 }
 
 function insertRecord($conn, $table, $data) {
    // Build the placeholders string
    $placeholders = implode(', ', array_fill(0, count($data), '?'));

    // Extract the values to be bound
    $values = array_column($data, 'value');

    // Prepare the SQL statement
    $sql = "INSERT INTO $table (" . implode(', ', array_column($data, 'key')) . ") VALUES ($placeholders)";
    $stmt = $conn->prepare($sql);

    // Check for errors in preparing the statement
    if (!$stmt) {
        // Log the error or display a user-friendly message
        echo "Error in preparing the statement. Please check your SQL.";
        return;
    }

    // Bind parameters dynamically
    $types = str_repeat('s', count($values)); // Assuming all values are strings
    $stmt->bind_param($types, ...$values);

    // Execute the statement
    if ($stmt->execute()) {
        
        // You can add any additional logic here after successful execution
    } else {
        // Log the error or display a user-friendly message
        echo "Error submitting data to $table. Please try again later.";
    }

    // Close the statement
    $stmt->close();
}


function updateRecord($conn, $table, $column, $data, $id) {
   
    // Prepare the SQL statement
    $sql = "UPDATE $table SET $column = $data WHERE id = $id";
    $stmt = $conn->prepare($sql);

    // Execute the statement
    if ($stmt->execute()) {
        echo "Updated Successfully";

    } else {
        
        echo "Error Updating Record";
    }

    // Close the statement
    $stmt->close();
}


function deleteRecord($conn, $table, $id) {
   
    // Prepare the SQL statement
    $sql = "DELETE FROM $table WHERE id = $id";
    $stmt = $conn->prepare($sql);

    // Execute the statement
    if ($stmt->execute()) {
        echo "Success";

    } else {
        
        echo "Error Deleting File";
    }

    // Close the statement
    $stmt->close();
}



