<?php
ob_start();
require './login/connect.php';
$pizza = $_GET['q'];
$pieces = explode("-", $pizza);
//$class = $pieces[0]; // piece1
$cardID= $pieces[0]; // piece2


$tenant = $conn->query("SELECT * FROM employee_details where cardID='$cardID'");
while($row=$tenant->fetch_assoc()):

    $student_id = $row['EmployeeId'];
    $date_in =$row['date_in'];
    $class = $row['class'];
    $EmployeeName = $row['EmployeeName']; 
    $father = $row['father'];
    //$months = abs(strtotime(date('Y-m-d')." 23:59:59") - strtotime($date_in." 23:59:59"));
    //$months = floor(($months) / (30*60*60*24));
    
    
    $date1 = new \DateTime("$date_in");
    $date2 = new \DateTime("24-01-2024");

$date1->modify('first day of this month');  //2021-07-01
$date2->modify('first day of next month');  //2021-09-01

$interval = $date1->diff($date2);
$countMonth = ($interval->y * 12) + $interval->m;  // Corrected line

$months = $countMonth;
   
    
                $tenantc = $conn->query("SELECT * FROM class where class_id='$class'");
                while($rowc=$tenantc->fetch_assoc()):
                $price = $rowc['fees'];
                $exam_fees = $rowc['exam_fees'];
                $payable= abs($price * $months);
                $payable_amount = number_format($payable,2);
                
                //$data['payable'] = number_format($payable,2);
                
                //$tenantp = $conn->query("SELECT SUM(amount) as paid FROM payments where EmployeeId ='$student_id'");
                
                $paid = $conn->query("SELECT SUM(amount) as paid FROM payments where EmployeeId ='$student_id' and payment_type=2");
		$last_payment = $conn->query("SELECT * FROM payments where  EmployeeId ='$student_id' order by unix_timestamp(date_created) desc limit 1");
		$paid = $paid->num_rows > 0 ? $paid->fetch_array()['paid'] : 0;
		$data['paid'] = number_format($paid,2);
		$data['last_payment'] = $last_payment->num_rows > 0 ? date("M d, Y",strtotime($last_payment->fetch_array()['date_created'])) : 'N/A';
		$du = $payable - $paid;
                
                $due = number_format($payable - $paid,2);
                
                
		if($paid !=0){
                    ?>
                    <input type="text" hidden  name="EmployeeId" value="<?php echo $row['EmployeeId']; ?>" class="form-control" />
                    <input type="text" hidden  name="cardID" value="<?php echo $cardID; ?>" class="form-control" />
                       <input type="text" hidden name="dt" value="<?php echo date('d-m-Y');?>" class="form-control" />
                      
                                
                        <div class=""style="float: left; width: 100%; height: 150px; margin-top: 30px;font-size: 18px;">
                            <div class=""style="float: left;width: 100%; height: 35px;">
                                <div class=""style="float: left;width: 40%; height: 25px;">Total Due </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $due; ?></div>
                            </div>
                             <div class=""style="width: 100%; height: 50px;">
                                 <div class=""style="float: left;width: 40%; height: 25px;">Total Payable Month </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $du/number_format($price,2); ?></div>
                            </div>
                             <div class=""style="width: 100%; height: 50px;">
                                 <div class=""style="float: left;width: 40%; height: 25px;">Student Name </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $EmployeeName; ?></div>
                            </div>
                            <div class=""style="width: 100%; height: 50px;">
                                 <div class=""style="float: left;width: 40%; height: 25px;">Father's Name </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $father; ?></div>
                            </div>
                        </div>
                    
                    <?php

                        $exam = $conn->query("SELECT * FROM admission_fee where EmployeeId='$cardID'");
                        while($rowx=$exam ->fetch_assoc()):
                           $exam_one = $rowx['exam_one'];$exam_two = $rowx['exam_two'];$exam_three = $rowx['exam_three']; 

                           if(empty($exam_one)){
                               ?>
                                        <div class="row g-2" style="margin-top: 130px;">
                                                <div class="col mb-0">
                                                    <label for="emailLarge" class="form-label"><h3>Amount</h3> </label>
                                                    <input type="number" id="input1" value="0" oninput="calculateSum()" required name="amount" id="emailLarge" />

                                                </div>
                                                <div class="col mb-0">

                                                    <label for="showInputCheckbox">First Temporary Exam:</label>
                                                    <input type="checkbox" id="showInputCheckbox" onchange="toggleInputField()">

                                                    <div id="inputContainer">
                                                        <input type="number" id="input2" value="0" oninput="calculateSum()" id="additionalInput"  name="examfees">
                                                    </div>

                                                </div>
                                            </div>
                            <?php
                           } else {
                               if(empty($exam_two)){
                               ?>
                                        <div class="row g-2" style="margin-top: 130px;">
                                                <div class="col mb-0">
                                                    <label for="emailLarge" class="form-label"><h3>Amount</h3> </label>
                                                    <input type="number" id="input1" value="0" oninput="calculateSum()" required name="amount" id="emailLarge" />

                                                </div>
                                                <div class="col mb-0">

                                                    <label for="showInputCheckbox">Second Temporary Exam:</label>
                                                    <input type="checkbox" id="showInputCheckbox" onchange="toggleInputField()">

                                                    <div id="inputContainer">
                                                        <input type="number" id="input2" value="0" oninput="calculateSum()" id="additionalInput"  name="examfees">
                                                    </div>

                                                </div>
                                            </div>
                            <?php
                           } else {
                               ?>
                                        <div class="row g-2" style="margin-top: 130px;">
                                                <div class="col mb-0">
                                                    <label for="emailLarge" class="form-label"><h3>Amount</h3> </label>
                                                    <input type="number" id="input1" value="0" oninput="calculateSum()" required name="amount" id="emailLarge" />

                                                </div>
                                                <div class="col mb-0">

                                                    <label for="showInputCheckbox">Final Exam:</label>
                                                    <input type="checkbox" id="showInputCheckbox" onchange="toggleInputField()">

                                                    <div id="inputContainer">
                                                        <input type="number" id="input2" value="0" oninput="calculateSum()" id="additionalInput"  name="examfees">
                                                    </div>

                                                </div>
                                            </div>
                            <?php
                           }
                           }
                           
                           endwhile;
                    ?>
                                            
                    <?php 
               
                } else {
                    
                     ?>
                    <input type="text" hidden  name="EmployeeId" value="<?php echo $row['EmployeeId']; ?>" class="form-control" />
                    <input type="text" hidden  name="cardID" value="<?php echo $cardID; ?>" class="form-control" />
                    <input type="text" hidden name="dt" value="<?php echo date('d-m-Y');?>" class="form-control" />
                    
                       <div class=""style="float: left; width: 100%; height: 150px; margin-top: 30px;font-size: 18px;">
                            
                           <div class=""style="float: left;width: 100%; height: 50px;">
                                <div class=""style="float: left;width: 40%; height: 25px;">Total Due  </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $due; ?></div>
                            </div>
                             <div class=""style="width: 100%; height: 50px;">
                                 <div class=""style="float: left;width: 40%; height: 25px;">Total Payable Month </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $months; ?></div>
                            </div>
                             
                            <div class=""style="width: 100%; height: 50px;">
                                 <div class=""style="float: left;width: 40%; height: 25px;">Student Name </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $EmployeeName; ?></div>
                            </div>
                            <div class=""style="width: 100%; height: 50px;">
                                 <div class=""style="float: left;width: 40%; height: 25px;">Father's Name </div>
                                <div class=""style="float: left;width: 59%; height: 25px;">: <?php echo $father; ?></div>
                            </div>
                        </div>
                
                   
                    <?php

                        $exam = $conn->query("SELECT * FROM admission_fee where EmployeeId='$cardID'");
                        while($rowx=$exam ->fetch_assoc()):
                           $exam_one = $rowx['exam_one'];$exam_two = $rowx['exam_two'];$exam_three = $rowx['exam_three']; 

                           if(empty($exam_one)){
                               ?>
                                        <div class="row g-2" style="margin-top: 130px;">
                                                <div class="col mb-0">
                                                    <label for="emailLarge" class="form-label"><h3>Amount</h3> </label>
                                                    <input type="number" id="input1" value="0" oninput="calculateSum()" required name="amount" id="emailLarge" />

                                                </div>
                                                <div class="col mb-0">

                                                    <label for="showInputCheckbox">First Temporary Exam:</label>
                                                    <input type="checkbox" id="showInputCheckbox" onchange="toggleInputField()">

                                                    <div id="inputContainer">
                                                        <input type="number" id="input2" value="0" oninput="calculateSum()" id="additionalInput"  name="examfees">
                                                    </div>

                                                </div>
                                            </div>
                            <?php
                           } else {
                               if(empty($exam_two)){
                               ?>
                                        <div class="row g-2" style="margin-top: 130px;">
                                                <div class="col mb-0">
                                                    <label for="emailLarge" class="form-label"><h3>Amount</h3> </label>
                                                    <input type="number" id="input1" value="0" oninput="calculateSum()" required name="amount" id="emailLarge" />

                                                </div>
                                                <div class="col mb-0">

                                                    <label for="showInputCheckbox">Second Temporary Exam:</label>
                                                    <input type="checkbox" id="showInputCheckbox" onchange="toggleInputField()">

                                                    <div id="inputContainer">
                                                        <input type="number" id="input2" value="0" oninput="calculateSum()" id="additionalInput"  name="examfees">
                                                    </div>

                                                </div>
                                            </div>
                            <?php
                           } else {
                               ?>
                                        <div class="row g-2" style="margin-top: 130px;">
                                                <div class="col mb-0">
                                                    <label for="emailLarge" class="form-label"><h3>Amount</h3> </label>
                                                    <input type="number" id="input1" value="0" oninput="calculateSum()" required name="amount" id="emailLarge" />

                                                </div>
                                                <div class="col mb-0">

                                                    <label for="showInputCheckbox">Final Exam:</label>
                                                    <input type="checkbox" id="showInputCheckbox" onchange="toggleInputField()">

                                                    <div id="inputContainer">
                                                        <input type="number" id="input2" value="0" oninput="calculateSum()" id="additionalInput"  name="examfees">
                                                    </div>

                                                </div>
                                            </div>
                            <?php
                           }
                           }
                           
                           endwhile;
                    ?>                  
                    <?php
                }
                
                
		//$data['name'] = ucwords($name);
		$data['rent_started'] = date('M d, Y',strtotime($date_in));
                
		
                
                
                
                endwhile;
                
endwhile;
           ?>

