<?php
session_name('mini_admin_session');
session_start();
include('includes/dbconnection.php');

if (isset($_GET['id'])) {
    $id = $_GET['id'];

    // Fetch the message by ID
    $query = "SELECT * FROM message WHERE id = ?";
    $stmt = $con->prepare($query);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $messageData = $result->fetch_assoc();

        // Save message data to session
        $_SESSION['message_data'] = $messageData;

        // Determine the corresponding approval page
        $approvalPage = '';

        // Example of combining multiple conditions for redirection
        if (
            strpos($messageData['purpose'], 'SCHOOL FEES') !== false) {
            // Redirect to School Fees page if both "SCHOOL FEES" and "2024 SESSION" are found
            $approvalPage = 'pay_tuition1.php';
        } elseif (
            strpos($messageData['purpose'], 'NEW STUDENT FEE') !== false) {
            // Redirect to Boarding Fees page if either "BOARDING FEES" or "HOSTEL" is found
            $approvalPage = 'pay11.php';
        } elseif (
            strpos($messageData['purpose'], 'ENTRANCE FORM') !== false) {
            // Redirect to Lesson Fees page if both "Lesson Fees" and "EXTRA CLASSES" are found
            $approvalPage = 'entrancepay111.php';
        }elseif (
            strpos($messageData['purpose'], 'Lesson Fees') !== false) {
            // Redirect to Lesson Fees page if both "Lesson Fees" and "EXTRA CLASSES" are found
            $approvalPage = 'acec1.php';
        }
    elseif (
        strpos($messageData['purpose'], 'JSS 1 (External Lesson)') !== false ||  
        strpos($messageData['purpose'], 'JSS 2 (External Lesson)') !== false || 
        strpos($messageData['purpose'], 'JSS 3 (External Lesson)') !== false || 
        strpos($messageData['purpose'], 'SS 1 (External Lesson)') !== false || 
        strpos($messageData['purpose'], 'SS 2 (External Lesson)') !== false || 
        strpos($messageData['purpose'], 'SS 3 (External Lesson)') !== false || 
        strpos($messageData['purpose'], 'GCE Class Fees') !== false || 
        strpos($messageData['purpose'], 'JAMB Class Fees') !== false||
        strpos($messageData['purpose'], 'JSS 1-3 SUMMER SCHOOL') !== false ||
        strpos($messageData['purpose'], 'SS 1-3 SUMMER SCHOOL') !== false) {
        // Redirect to Lesson Fees page if both "Lesson Fees" and "EXTRA CLASSES" are found
        $approvalPage = 'entrancepay11.php';
    }

    elseif (

        strpos($messageData['purpose'], 'Testimonial') !== false ||  
        strpos($messageData['purpose'], 'Certificate (JSCE)') !== false || 
        strpos($messageData['purpose'], 'Certificate (SSCE)') !== false || 
        strpos($messageData['purpose'], 'Transcript (Physical)') !== false || 
        strpos($messageData['purpose'], 'Transcript (Soft Copy)') !== false || 
        strpos($messageData['purpose'], 'OFFICIAL DOCUMENT') !== false ) {
        // Redirect to Lesson Fees page if both "Lesson Fees" and "EXTRA CLASSES" are found
        $approvalPage = 'entrancepay21.php';
    }
         else {
            // Default to Other Services page if no specific purpose matches
            $approvalPage = 'otherpay1.php';
        }

        // Redirect to the corresponding page
        header("Location: $approvalPage?id=$id");
        exit();
    } else {
        echo "Message not found.";
    }
} else {
    echo "Invalid request.";
}
?>
