<?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">Monthly Loan Payments</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Monthly Loan Payments</h4>
                                </div>
                            </div>
                        </div>
                         <!--end page title -->
                         
                         <div class="row">
        <div class="card">
            <div class="card-body">
                <!-- Form start -->
                <form method="post" action="">
                    <!-- Dropdown and Input field -->
                    <div class="row">
                        <!--<div class="col-lg-4">-->
                        <!--    <div class="form-group">-->
                        <!--        <label for="exampleFormControlSelect1">Select Spa</label>-->
                        <!--        <select name="spa" class="form-control" id="exampleFormControlSelect1">-->
                        <!--            <option>Select..</option>-->
                        <!--            <?php while($data = mysqli_fetch_assoc($select_spa)){?>-->
                        <!--            <option value="<?php echo $data['unique_id'];?>"><?php echo $data['unique_id'] . ' = ' .$data['name'];?></option>-->
                        <!--            <?php }?>-->
                                    
                        <!--        </select>-->
                        <!--    </div>-->
                        <!--</div>-->
                        <div class="col-lg-4">
                           <div class="form-group">
                               <label for="schedule">User ID/Ac No /Phone No</label>
                               <input type="text" name="userid" class="form-control" id="schedule" placeholder="Enter User ID">
                           </div>
                       </div>
                       
                       <!--<div class="col-lg-4">-->
                       <!--    <div class="form-group">-->
                       <!--        <label for="schedule">From Date</label>-->
                       <!--        <input type="date" name="from_date" class="form-control" id="schedule">-->
                       <!--    </div>-->
                       <!--</div>-->
                        <div class="col-lg-4" style="margin-top:20px;">
                         <button type="submit" name="submittt" class="btn btn-primary">Submit</button>
                         </div>
                    </div>
                    <!-- End of Dropdown and Input field -->
                    <!-- Submit button -->
                   
                </form>
                
                <?php 
                 if(isset($_POST['submittt'])){
                    $userid = $_POST['userid'];
                    $sqll = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM `user` where `id`='$userid' OR `a_c_number`='$userid' OR `phone`='$userid'"));
                     $iddd = $sqll['id'];
                   $sql = "SELECT * FROM `loan_desc` where `userid`='$iddd'";
                 }
                ?>
                <!-- Form end -->
            </div>
        </div>
</div>
                          <div>
                              <a href="Add_loann.php"  class="btn btn-success"> Add Monthly 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_desc` WHERE id='$id'";
                                           // print_r($sql);
                                            if(mysqli_query($conn,$sql)){
                                             echo "<script>alert('Your Data deleted successfully');</script>";
                                            echo "<script>window.location.href='m_loan_p.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>Month Amount</th>
                                                             <th>Monthly Intrest</th>
                                                             <th>Monthly Penalty</th>
                                                             <th>CD Deduction Amount:</th>
                                                             <th>Total Deduct Amount:</th>
                                                             <th>Loan Returan:</th>
                                                             <th>Available Amount:</th>
                                                             <th>Loan Date:</th>
                                                             <th>Loan Month:</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['available_balance'] ?></td>
                                                            <td><?= $row['month_payment'] ?></td>
                                                            <td><?= $row['monthly_Interest'] ?></td>
                                                            <td><?= $row['monthly_Penalty'] ?></td>
                                                            <td><?= $row['cd_deduction'] ?></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['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="m_loan_p.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'; 
?>