<?php
require '../config/db.php';
require '../config/function/function.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['giftAction'])) {
    $id = intval($_POST['id']);
    $email = cleanInput($_POST['email']);

    $location = "./profile";
    $message ="";
    $type = "";
    $noError = true;

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $message = 'Invalid email address.';
        $type = 'error';
        header("Location: profile");
        exit;
    }

    $query = "UPDATE admin SET email = ? WHERE id = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param('si', $email, $id);

    if ($stmt->execute()) {
        $message = 'Profile updated successfully.';
        $type = 'success';
    } else {
        $message = 'Failed to update profile. Please try again.';
        $type = 'error';
    }

    redirectWithMessage($location, $message, $type);
    exit;
}
