<?php
session_start();
include '../include/config.php'; 
if (isset($_POST['pay_one'])) {
    if (!empty($_POST['studentid']) && !empty($_POST['amount']) && !empty($_POST['intake']) && !empty($_POST['mobile'])  && !empty($_POST['year']) && !empty($_POST['fee_type'])) {

        $payid = 'IFEE' . strtoupper(uniqid());

   $studentid = trim($_POST['studentid']);
   $fee = trim($_POST['fee_type']);
        $intake = trim($_POST['intake']);
        $amount = trim($_POST['amount']);
  $mobile = trim($_POST['mobile']);
  $year = trim($_POST['year']);

        $check_sql = "SELECT payid FROM payments WHERE studentid = '$studentid' AND status = 'Pending'";
        $check_result = $conn->query($check_sql);

        if ($check_result && $check_result->num_rows > 0) {
            $row = $check_result->fetch_assoc();
            header("Location: dashboard.php?payid=you have a pending FeeID alraedy");
            exit();
        }else{

        $sql = "INSERT INTO `payments`(`payid`, `fee_type`, `studentid`, `amount`, `intake`, `mobile`, `year`)  VALUES (?, ?, ?, ?,?,?,?)";
        $stmt = $conn->prepare($sql);

        if (!$stmt) {
            die("Error preparing statement: " . $conn->error);
        }

    
        $stmt->bind_param("sssssss", $payid,$fee, $studentid, $amount, $intake,$mobile,$year);

        if ($stmt->execute()) {
            
            $_SESSION['payid'] = $payid;

            header("Location: dashboard.php?payid=FeeID created procced to 2. payment");
            exit();
        } else {
            
            die("Error executing statement: " . $stmt->error);
        }

    }
     
        $stmt->close();
    } else {
        die("Error: Missing required form fields.");
    }

    
    $conn->close();
} else {
    die("Error: Form not submitted correctly.");
}
?>
