<?php

session_start();

include "../includes/admin_auth.php";
include "../config/database.php";

if(!isset($_GET['id'])){

    $_SESSION['error']="Invalid request.";

    header("Location: registrations.php");
    exit();

}

$paymentId=(int)$_GET['id'];

/*
Get Payment
*/

$sql="SELECT * FROM registration_payments WHERE id=?";

$stmt=$pdo->prepare($sql);

$stmt->execute([$paymentId]);

$payment=$stmt->fetch(PDO::FETCH_ASSOC);

if(!$payment){

    $_SESSION['error']="Registration not found.";

    header("Location: registrations.php");
    exit();

}

/*
Update Registration Payment
*/

$sql="UPDATE registration_payments
SET status='approved'
WHERE id=?";

$stmt=$pdo->prepare($sql);

$stmt->execute([$paymentId]);

/*
Activate User
*/

$sql="UPDATE users
SET status='active'
WHERE id=?";

$stmt=$pdo->prepare($sql);

$stmt->execute([$payment['user_id']]);

$_SESSION['success']="Registration approved successfully.";

header("Location: registrations.php");

exit();

?>