<?php
session_start();
include 'include/config.php';

if(!isset($_SESSION['id'])){
 echo "<script> window.location.href = 'index.php' </script>";
  }
?>

<?php  include 'include/head.php'; ?>
                    
                      <!-- Start Content-->
                      <div class="container-fluid">
                        
                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box">
                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                            <li class="breadcrumb-item active">Loan Details</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title"> Loan Details</h4>
                                </div>
                            </div>
                        </div>
                         <!--end page title -->
                          <div>
                              <a href="apply_loan.php"  class="btn btn-success">  Apply For Loan</a>
                          </div>

                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-body">
                                        
                                        <?php
                                        if(isset($_GET['user'])){
                                            $id = $_GET['user'];
                                        
                                            $sql = "DELETE FROM `loan` WHERE id='$id'";
                                           // print_r($sql);
                                            if(mysqli_query($conn,$sql)){
                                             echo "<script>alert('Your Data deleted successfully');</script>";
                                            // echo "<script>document.location('user.php');</script>";
                                            echo "<script>window.location.href='loan.php';</script>";
                                            }
                                           
                                        }
                                        ?>
                                        <div class="table-responsive">
                                            <div class="tab-pane show active" id="basic-datatable-preview">
                                                <table id="datatable" class="table table-striped dt-responsive nowrap w-100">
                                                    <thead>
                                                        <tr>
                                                            <th>Sr. No.</th>
                                                            <th>User ID</th>
                                                            <th>Name</th>
                                                             <th>Loan Amount</th>
                                                             <th>Forst Socity Found Amount</th>
                                                             <th>Monthly Intrest</th>
                                                             <th>Monthly Penalty</th>
                                                             <th>CD Deduction Amount:</th>
                                                             <th>Share Money Amount:</th>
                                                             <th>Total Deduct Amount:</th>
                                                             <th>Loan Returan:</th>
                                                             <th>Available Amount:</th>
                                                             <th>Loan Date:</th>
                                                             <th>Loan Month:</th>
                                                             <th>User Taking Amount:</th>
                                                             <th>First Gurantor ID:</th>
                                                             <th>First Gurantor Name:</th>
                                                             <th>Second Gurantor ID:</th>
                                                             <th>Second Gurantor Name:</th>
                                                             <th>Third Gurantor ID:</th>
                                                             <th>Third Gurantor Name:</th>
                                                             <th>Fourth Gurantor ID:</th>
                                                             <th>Fourth Gurantor Name:</th>
                                                             <th>Fifth Gurantor ID:</th>
                                                             <th>Fifth Gurantor Name:</th>
                                                             <th>Sixth Gurantor ID:</th>
                                                             <th>Sixth Gurantor Name:</th>
                                                             <th>Seventh Gurantor ID:</th>
                                                             <th>Seventh Gurantor Name:</th>
                                                             <th>Payment Mode:</th>
                                                             <th>Payment Mode Number:</th>
                                                               <th>Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                    <?php
                                                    $sql = "SELECT * FROM `loan` ORDER BY ID DESC";
                                                    $res = mysqli_query($conn, $sql);
                                                    $i = 1;
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                    ?>
                                                        <tr>
                                                            <td><?= $i; ?></td>
                                                            <td><?= $row['userid'] ?></td>
                                                            <?php
                                                                 $select_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM `user` WHERE `id`='$row[userid]'"));
                                                             ?>
                                                            <td><?= $select_name['first_name']. ' ' .$select_name['last_name'] ?></td>
                                                            <td><?= $row['loan_amount'] ?></td>
                                                            <td><?= $row['first_society_fund'] ?></td>
                                                            <td><?= $row['monthly_Interest'] ?></td>
                                                            <td><?= $row['monthly_Penalty'] ?></td>
                                                            <td><?= $row['cd_deduction'] ?></td>
                                                            <td><?= $row['share_money'] ?></td>
                                                            <td><?= $row['total_deduct_amount'] ?></td>
                                                            
                                                           
                                                            <td><?= $row['loan_return'] ?></td>
                                                            <td><?= $row['available_balance'] ?></td>
                                                            <td><?= $row['date'] ?></td>
                                                            <td><?= $row['month_name'] ?></td>
                                                            <td><?= $row['user_give_amount'] ?></td>
                                                             <td><?= $row['first_guarantor_id'] ?></td>
                                                            <?php
                                                              $guarantor_id = mysqli_real_escape_string($conn, $row['first_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$guarantor_id'"));
                                                              ?>
                                                            <td><?= $select_gur['first_name']. ' ' .$select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['second_guarantor_id'] ?></td>
                                                            
                                                             <?php
                                                              $second_guarantor_id = mysqli_real_escape_string($conn, $row['second_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $second_select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$second_guarantor_id'"));
                                                              ?>
                                                            <td><?= $second_select_gur['first_name']. ' ' .$second_select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['third_guarantor_id'] ?></td>
                                                            
                                                             <?php
                                                              $third_guarantor_id = mysqli_real_escape_string($conn, $row['third_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $third_select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$third_guarantor_id'"));
                                                              ?>
                                                            <td><?= $third_select_gur['first_name']. ' ' .$third_select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['fourth_guarantor_id'] ?></td>
                                                            
                                                             <?php
                                                              $fourth_guarantor_id = mysqli_real_escape_string($conn, $row['fourth_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $fourth_select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$fourth_guarantor_id'"));
                                                              ?>
                                                            <td><?= $fourth_select_gur['first_name']. ' ' .$fourth_select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['fifth_guarantor_id'] ?></td>
                                                            
                                                             <?php
                                                              $fifth_guarantor_id = mysqli_real_escape_string($conn, $row['fifth_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $fifth_select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$fifth_guarantor_id'"));
                                                              ?>
                                                            <td><?= $fifth_select_gur['first_name']. ' ' .$fifth_select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['sixth_guarantor_id'] ?></td>
                                                            
                                                             <?php
                                                              $sisxth_guarantor_id = mysqli_real_escape_string($conn, $row['sixth_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $sisxth_select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$sisxth_guarantor_id'"));
                                                              ?>
                                                            <td><?= $sisxth_select_gur['first_name']. ' ' .$sisxth_select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['seventh_guarantor_id'] ?></td>
                                                            
                                                             <?php
                                                              $seventh_guarantor_id = mysqli_real_escape_string($conn, $row['seventh_guarantor_id']);
                                                             // print_r($guarantor_id);
                                                              $seventh_select_gur = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `user` WHERE `id`='$seventh_guarantor_id'"));
                                                              ?>
                                                            <td><?= $seventh_select_gur['first_name']. ' ' .$seventh_select_gur['last_name'] ?></td>
                                                            
                                                            <td><?= $row['payment_mod'] ?></td>
                                                            <td><?= $row['payment_mode_number'] ?></td>
                                                            
                                                            <td>
                                                                <!--<a href="useredit.php?id=<?=$row['id']?>" class="action-icon"> <i class="mdi mdi-square-edit-outline"></i></a>-->
                                                                <a href="loan.php?user=<?= $row['id'] ?>" onclick="return confirm('Are You Sure Want To Delete This?');" class="action-icon"> <i class="mdi mdi-delete"></i></a>

                                                            </td>
                                                         </tr>
                                                        <?php
                                                       $i++;  } 
                                                        ?>
                                                    </tbody>
                                                </table>                                           
                                            </div> <!-- end preview-->
                                        
                                            
                                        </div> <!-- end tab-content-->

                                    </div> <!-- end card body-->
                                </div> <!-- end card -->
                            </div><!-- end col-->
                        </div> <!-- end row-->

                    
                    </div> <!-- container -->


<?php  
include 'include/footer.php'; 
?>