<?php
session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');


if (isset($_POST['user_id']) && isset($_POST['auth']) && isset($_POST['application_id'])) {
    include 'config.php';

    $user_id = $_POST['user_id'];
    $application_id = $_POST['application_id'];
    $auth = $_POST['auth'];

    $user_id = stripslashes($user_id);
    $user_id = mysqli_escape_string($con, $user_id);

    $application_id = stripslashes($application_id);
    $application_id = mysqli_escape_string($con, $application_id);

    $auth = stripslashes($auth);
    $auth = mysqli_escape_string($con, $auth);
    
    $passport_url = $p_url."/passport/";
    $id_url = $p_url."/id_docs/";
      

    $sql = $con->query("SELECT * FROM applications WHERE application_id = '$application_id' AND user_id = '$user_id'") 
        or die("Error: " . mysqli_error($con));

    if ($sql->num_rows > 0) {
        $data = [];
        while ($rows = mysqli_fetch_assoc($sql)) {
            
            $data[] = $rows;
        }

        $suc = "Success";
        $status = true;
        
           $sqlx = $con->query("SELECT * FROM applications WHERE application_id = '$application_id' AND user_id = '$user_id'") 
        or die("Error: " . mysqli_error($con));
         $rowx = mysqli_fetch_assoc($sqlx);
        $status = $rowx['status'];
        $application_fee = $rowx['application_fee'];
        $admission_fee = $rowx['admission_fee'];
       $visa_fee = $rowx['visa_fee'];
        
        if ($application_fee !== "Paid")
        {
           $sql2="SELECT * FROM fees where fee_type = 'Application'";
           $resulta = mysqli_query($con,$sql2) or die(mysqli_error($con));
           $rowa = mysqli_fetch_array($resulta);
           $amount = $rowa['amount'];
          $fee_type = $rowa['fee_type'];
           $payment_status = 'Due';
           $payment_type = $fee_type;
    
    
        }
        
        else if ($status == "Admitted" && $admission_fee !== "Paid")
        {
           $sqla="SELECT * FROM fees where fee_type = 'Admission'";
           $resulta = mysqli_query($con,$sqla) or die(mysqli_error($con));
           $rowa = mysqli_fetch_array($resulta);
           $amount = $rowa['amount'];
           $fee_type = $rowa['fee_type'];
           $payment_status = 'Due';
           $payment_type = $fee_type;
    
        }

        else if ($status == "Admitted" && $admission_fee == "Paid" && $visa_fee !== "Paid")
        {
           $sqla="SELECT * FROM fees where fee_type = 'Visa'";
           $resulta = mysqli_query($con,$sqla) or die(mysqli_error($con));
           $rowa = mysqli_fetch_array($resulta);
           $amount = $rowa['amount'];
           $fee_type = $rowa['fee_type'];
           $payment_status = 'Due';
           $payment_type = $fee_type;
            }
        else
        {
           $payment_status = '';
           $payment_type = '';
 
        }
        
        
    } else {
        $data = [];
        $suc = "No record found";
        $status = false;
    }

    $con->close();


    $output = array('success' => $suc, 'status' => $status, 'payment_status' => $payment_status, 'payment_type' => $payment_type,   'amount' =>  $amount,    'data' => $data);
    echo json_encode($output); // Output JSON data
} else {
    echo json_encode(array('success' => "Failure", 'status' => false, 'message' => "Missing required fields"));
}
?>
