<?php
//print_r($_POST);
require_once('dbconnection.php');
session_start();


        $user_id = $_SESSION['emp_id'];
        

        // Decode arrays from JSON
        $course_type_arr = json_decode($_POST['course_type']);
        $t_name_arr = json_decode($_POST['t_name']);
        $class_time_arr = json_decode($_POST['class_time']);
        $main_amount_arr = json_decode($_POST['main_amount']);
        $discount_arr = json_decode($_POST['discount']);
        $amount_arr = json_decode($_POST['amount']);
        $receive_amount_arr = json_decode($_POST['receive_amount']);
        $borrow_amount_arr = json_decode($_POST['borrow_amount']);
        $end_date_arr = json_decode($_POST['end_date']);
        $p_status_arr = json_decode($_POST['p_status']);

        // Other data
        $date = $_POST['date'];
        $bill_no = $_POST['bill_no'];
        $cash_id = $_POST['cash_id'];
        $student_id = $_POST['student_id'];
    

for ($i = 0; $i < count($course_type_arr); $i++) {
        
        /* not allowing empty values and the row which has been removed. */
    $sql="INSERT INTO payments (class_time, p_main_amount, p_amount, p_status, end_date, start_date, s_id, cp_id, c_id,t_id) Values 
    ('$class_time_arr[$i]','$main_amount_arr[$i]','$amount_arr[$i]','$p_status_arr[$i]','$end_date_arr[$i]','$date','$student_id', '$cash_id', '$course_type_arr[$i]','$t_name_arr[$i]')";
    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    
    $sqlloan = "INSERT INTO loan (total_price, received_amount, borrow_amount, s_id, c_id,t_id) Values
    ('$amount_arr[$i]', '$receive_amount_arr[$i]', '$borrow_amount_arr[$i]', '$student_id', '$course_type_arr[$i]','$t_name_arr[$i]')";
    mysqli_query($con,$sqlloan);
    
    $sqlloan_details = "INSERT INTO loan_r_details (p_received_amount, p_borrow_amount, bill_no, p_date, s_id, cp_id, c_id,t_id) Values
    ('$receive_amount_arr[$i]', '$borrow_amount_arr[$i]', '$bill_no', '$date', '$student_id', '$cash_id', '$course_type_arr[$i]','$t_name_arr[$i]')";
    mysqli_query($con,$sqlloan_details);

    $sqlcashpoint = "INSERT INTO cash_point (cp_id, deposit_amount, details, bill_no, emp_id, date) Values
    ('$cash_id', '$receive_amount_arr[$i]', '$course_type_arr[$i]', '$bill_no', '$user_id','$date')";
    mysqli_query($con,$sqlcashpoint);


    $sqldiscount = "INSERT INTO discount (d_amount, c_id, s_id,t_id) Values
    ('$discount_arr[$i]','$course_type_arr[$i]','$student_id','$t_name_arr[$i]')";
    mysqli_query($con,$sqldiscount);

    $sqlcourse_std = "INSERT INTO course_std (c_id, s_id,t_id) Values 
    ('$course_type_arr[$i]','$student_id','$t_name_arr[$i]')";
    mysqli_query($con,$sqlcourse_std);

   

    
}



mysqli_close($con);
?>