<?php

include("includes/header.php"); 
include("includes/sidebar.php"); 

$saving_plans = $db_handle->runBaseQuery("SELECT * FROM savings_plans WHERE status = 'PENDING'");

if (isset($_GET['confirm'])) {
    
    $plan_id = $_GET['confirm'];
    
       $update =  $db_handle->update("UPDATE savings_plans SET status = 'ONGOING' WHERE id= ?", "i",array($plan_id));

       if($update){
           
        $url  = "pending-savings-plans?page=pending-savings-plans";
        
        echo '<META HTTP-EQUIV="refresh" content="3; URL='.$url.'">';  
        $msg = "<div class='alert alert-success text-center'> Saving Plan activated successfully</div>";
        # code...
       }
        
    }

if(isset($_GET['decline'])){
    $plan_id = $_GET['decline'];
    
    $update =  $db_handle->update("UPDATE savings_plans SET status = 'CANCELED' WHERE id= ?", "i",array($plan_id));
      
     $url  = "pending-savings-plans?page=pending-savings-plans";
    echo '<META HTTP-EQUIV="refresh" content="3; URL='.$url.'">';  
    $msg = "<div class='alert alert-success text-center'> Saving plan has been canceled.</div>";
}



 ?>
 <div class="main-content app-content">
 	<div class="container-fluid">
 	    
 	   
<div class="row mt-3">
            <div class="col-xl-12">
                 
                <div class="card custom-card">
                    <div class="card-header justify-content-between border-bottom-0">
                        <div class="card-title"> All Savings Plans </div>
                       
                    </div>
                    <?php echo $msg; ?>
                    <div class="card-body p-2">
                        <div class="table-responsive">
                            <table class="table text-nowrap table-bordered" id="TableTab">
                                <thead>
                                    <tr>
                                        <th scope="col">#</th>
                                       <th scope="col">Name</th>
                                        <th scope="col">Duration</th>
                                       
                                        <th scope="col">Start Date</th>
                                        <th scope="col">End Date</th>
                                        <th scope="col">Date Created</th>
                                       
                                        <th scope="col">Action</th>
                                       
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    if($saving_plans){
                                        $i = 1;
                                    foreach($saving_plans as $saving_plan){
                                        $userID = ($saving_plan['userID']);

                                                    $user = $db_handle->runBaseQuery("SELECT * FROM users WHERE id = $userID");

                                                    $name = $user[0]['first_name']." ".$user[0]['surname'];
                                               
                                   echo "<tr>";
                                    echo '<td class="fw-medium">'.$i.'</td>';
                                    echo '<td class="fw-medium">'.$name.'</td>';
                                      
                                        echo '<td> <span class="fw-medium">'.$saving_plan['duration_months'].' Months</span></td>';
                                       
                                        echo '<td> <span class="">'.$saving_plan['start_date'].'</span> </td>';
                                        echo '<td> <span class="">'.$saving_plan['end_date'].'</span> </td>';
                                        echo '<td> <span class="">'.$saving_plan['created_at'].'</span> </td>';
                                        
                                        

                                        echo "<td class='tdSpace'>";
                                    echo "<a href=". "pending-savings-plans?confirm=". $saving_plan['id'] ."&user=".$userID."&page=pending-savings-plans>";
                                    echo "<button class='bg-secondary text-white' style='font-size:0.85em;padding:5px 30px 2px 30px;outline:none;border:none'>";
                                    echo "<i class='fa fa-check-circle'></i> Approve</button>";
                                    echo "</a>";
                                    echo " ";
                                    echo "<a href=". "pending-savings-plans?decline=". $saving_plan['id']."&page=pending-savings-plans>";
                                    echo "<button class='bg-danger text-white' style='font-size:0.85em;padding:5px 30px 2px 30px;outline:none;border:none'>";
                                    echo "<i class='fa fa-times-circle'></i> Decline</button>";
                                    echo "</td>";
                                       
                                    echo '</tr>';

                                    $i++;
                                    
                                    }

                                }

                                ?>
                                    
                                   
                                   
                                </tbody>
                            </table>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
 </div>
  <?php

include("includes/footer.php"); 
 ?>