<?php
include('includes/session.php');
error_reporting(0);
include('includes/dbconnection.php');

require_once 'vendor/autoload.php'; // For dotenv and other dependencies

$eid2 = $_SESSION['student_id'];

// Fetch student details
$query = "SELECT * FROM students WHERE id = $eid2";
$result = mysqli_query($con, $query);

if (!$result) {
    die("Query failed: " . mysqli_error($con));
}

if ($row = mysqli_fetch_assoc($result)) {
    $adno = $row['studentno'];
    $stdname = $row['studentName'];
    $stdid = $row['id'];
    $class = $row['class'];

    // Store details in session
    $_SESSION['studentno'] = $adno;
    $_SESSION['studentName'] = $stdname;
    $_SESSION['id'] = $stdid;
    $_SESSION['class'] = $class;
} else {
    echo "Invalid student ID.";
    exit;
}


// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Retrieve Paystack public key
$paystackPublicKey = $_ENV['PAYSTACK_PUBLIC_KEY'];

// Fetch all fees dynamically
$feesQuery = "SELECT * FROM fees";
$feesResult = mysqli_query($con, $feesQuery);

if (!$feesResult) {
    die("Fees query failed: " . mysqli_error($con));
}
?>
<!DOCTYPE html>
<html>
    <?php @include("includes/head.php"); ?>
    <script src="https://js.paystack.co/v1/inline.js"></script>
<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <?php @include("includes/headerstd.php"); ?>
        <?php @include("includes/sidebaroldstdboard.php"); ?>

        <div class="content-wrapper">
            <section class="content-header">
                <h1>Fees Payment</h1>
            </section>
            <section class="content">
                <div class="container-fluid">
                    

                   
                    <div class="card mt-4">
                        <div class="card-body">
                            <h4>Confirm Payment Details</h4>
                            <p><strong>Name:</strong> <?php echo $_SESSION['studentName']; ?></p>
                            <p><strong>Admission No | Registration No:</strong> <?php echo $_SESSION['studentno']; ?></p>
                            <p><strong>Class:</strong> <?php echo $_SESSION['class']; ?></p>
                            
                            <form method="POST" id="paymentForm">
                                <div class="form-group">
                                    <label for="email">Enter Your Email</label>
                                    <input type="email" id="email" class="form-control" placeholder="Enter your email" required>
                                    <label for="feeType">Purpose of Payment</label>
                                        <select class="form-control" id="feeType" name="feeType" required>
                                            <option value=""></option>
                                            <option value = "JSS1 FULL TERM SCHOOL FEES">JSS1 FULL TERM SCHOOL FEES</option>
                                            <option value = "JSS1 PART PAYMENT SCHOOL FEES">JSS1 PART PAYMENT SCHOOL FEES</option>
                                            <option value = "JSS1 2 TERM SCHOOL FEES ">JSS1  2 TERM SCHOOL FEES</option>
                                            <option value = "JSS1 FULL SESSION SCHOOL FEES ">JSS1 FULL SESSION SCHOOL FEES</option>

                                            <option value = "JSS2 FULL TERM SCHOOL FEES ">JSS2 FULL TERM SCHOOL FEES</option>
                                            <option value = "JSS2 PART PAYMENT SCHOOL FEES">JSS2 PART PAYMENT SCHOOL FEES</option>
                                            <option value = "JSS2 2 TERM SCHOOL FEES ">JSS2  2 TERM SCHOOL FEES</option>
                                            <option value = "JSS2 FULL SESSION SCHOOL FEES ">JSS2 FULL SESSION SCHOOL FEES</option>

                                            <option value = "JSS3 FULL SESSION SCHOOL FEES ">JSS3 FULL SESSION SCHOOL FEES</option>
                                            <option value = "JSS3 SESSION FEES 2 INSTALMENTS">JSS3 SESSION FEES 2 INSTALMENTS</option>
                                            <option value = "JSS3 SESSION FEES 3 INSTALMENTS ">JSS3 SESSION FEES 3 INSTALMENTS</option>
                                            <option value = "JSS3 SESSION FEES 4 INSTALMENTS ">JSS3 SESSION FEES 4 INSTALMENTS</option>

                                            <option value = "SSS1 FULL TERM SCHOOL FEES ">SSS1 FULL TERM SCHOOL FEES</option>
                                            <option value = "SSS1 PART PAYMENT SCHOOL FEES">SSS1 PART PAYMENT SCHOOL FEES</option>
                                            <option value = "SSS1 2 TERM SCHOOL FEES ">SSS1  2 TERM SCHOOL FEES</option>
                                            <option value = "SSS1 FULL SESSION SCHOOL FEES ">SSS1 FULL SESSION SCHOOL FEES</option>

                                            <option value = "SSS2 FULL TERM SCHOOL FEES ">SSS2 FULL TERM SCHOOL FEES</option>
                                            <option value = "SSS2 PART PAYMENT SCHOOL FEES">SSS2 PART PAYMENT SCHOOL FEES</option>
                                            <option value = "SSS2 2 TERM SCHOOL FEES ">SSS2  2 TERM SCHOOL FEES</option>
                                            <option value = "SSS2 FULL SESSION SCHOOL FEES ">SSS2 FULL SESSION SCHOOL FEES</option>

                                            <option value = "SS3 FULL SESSION SCHOOL FEES ">SS3 FULL SESSION SCHOOL FEES</option>
                                            <option value = "SS3 SESSION FEES 2 INSTALMENTS">SS3 SESSION FEES 2 INSTALMENTS</option>
                                            <option value = "SS3 SESSION FEES 3 INSTALMENTS ">SS3 SESSION FEES 3 INSTALMENTS</option>
                                            <option value = "SS3 SESSION FEES 4 INSTALMENTS ">SS3 SESSION FEES 4 INSTALMENTS</option>
                                        </select>
                                        <label for="feeType">Amount</label>
                                    <input type="number" id="amount" name="amount" class="form-control" placeholder="Amount" required>
                                    <input type="hidden" id="studentno" value="<?php echo $_SESSION['studentno']; ?>" required>
                                    <input type="hidden" id="studentName" value="<?php echo $_SESSION['studentName']; ?>"required>
                                    
                                    <input type="hidden" id="stdid" value="<?php echo $_SESSION['id']; ?>"required>

                                </div>
                                <center><button type="button" class="btn btn-primary" onclick="payWithPaystack()"> Pay  <i class="fa fa-credit-card"></i></button><center>
                            </form>
                        </div>
                    </div>
                    
                </div>
            </section>
        </div>
        
        <?php @include("includes/footer.php"); ?>
    </div>

    <script>
        const PAYSTACK_PUBLIC_KEY = "<?php echo $paystackPublicKey; ?>";

        function payWithPaystack() {
            let email = document.getElementById("email").value;
            let studentno = document.getElementById("studentno").value;
            let amount = document.getElementById("amount").value;
            let feeType = document.getElementById("feeType").value;
            let studentName = document.getElementById("studentName").value;
            let stdid = document.getElementById("stdid").value;

            if (!email || !studentno || !amount || !feeType || !studentName ||!stdid) {
                alert("Email, student number, or amount is missing!");
                return;
            }

            console.log("Email: " + email);
            console.log("Studentno: " + studentno);
            console.log("Amount: " + amount);
            console.log("feeType: " + feeType);
            console.log("studentName: " + studentName);
            console.log("studentid: " + stdid);

            let handler = PaystackPop.setup({
                key: PAYSTACK_PUBLIC_KEY,
                email: email,
                amount: amount * 100, // Convert to kobo
                currency: "NGN",
                metadata: {
                    custom_fields: [
                        {
                            display_name: "Fees Type",
                            variable_name: "feeType",
                            value: feeType
                        }
                    ]
                },
                callback: function(response) {
                    let reference = response.reference;
                    console.log("Payment reference: " + reference);
                    window.location.href = `verify_paymentb.php?reference=${reference}&studentno=${studentno}&email=${email}&studentName=${studentName}&stdid=${stdid}`;
                },
                onClose: function() {
                    alert('Transaction was not completed, window closed.');
                }
            });
            handler.openIframe();
        }
    </script>
   
    <?php @include("includes/foot.php"); ?>
    <script src ="dist/js/select2.min.js"></script>
    <script>
      // In your Javascript (external .js resource or <script> tag)
$(document).ready(function() {
    $('#feeType').select2();
});
    </script>
</body>
</html>
