<?php

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

//include("paystack/config.php"); 

//$currency = "NGN";

$balance = $user[0]['balance'];
$savings_balance = $user[0]['savings_balance'];

$saving_plans = $db_handle->runBaseQuery("SELECT * FROM savings_plans WHERE userID = $id ORDER BY id DESC");

$action = ($saving_plans[0]['status'] ==  "PENDING")? "disabled" : "";


if(isset($_POST['save'])){
    
    if (!isset($_POST['csrf_token']) || !$csrf->validateToken($_POST['csrf_token'])) {
        die("CSRF token validation failed!");
    }
    
   
$mode = "BANK TRANSFER";

   
    $confirmed = 0;
    $date = date("Y-m-d h:i:sa");
    $amount = $db_handle->sanitize($_POST['amount']);
    $duration = (isset($_POST["duration"])) ? $db_handle->sanitize($_POST["duration"]) : "";
    

    $t_id = "OVOH".mt_rand(10000,99999);
    $status = 'SUCCESSFUL';
    
    
    if($amount < $balance){
        
        $balance -= $amount;
        
        $savings_balance += $amount;
        
       $update = $db_handle->update("update users SET balance = ?, savings_balance = ? WHERE id = ? ","ssi",array($balance,$savings_balance,$id));
       
       if($update){
    

if($duration != ""){
    
    $start_date = date('Y-m-d');
$end_date = date('Y-m-d', strtotime("+$duration months"));
    
    $insert = $db_handle->insertRecord("insert into savings_plans(userID,duration_months,start_date,end_date,created_at,status) VALUES(?,?,?,?,?,?)","isssss",array($id,$duration,$start_date,$end_date,$date,"ONGOING"));
    
    $plan_id = $db_handle->lastID();
    

}else{
   
$saving_plans = $db_handle->runBaseQuery("SELECT * FROM savings_plans WHERE userID = $id AND status = 'ONGOING' ORDER BY id DESC LIMIT 1");

 $plan_id = $saving_plans[0]['id'];
 
}
    
    $insert = $db_handle->insertRecord("insert into savings(userID,transactionID,amount,method,date,status,planID) VALUES(?,?,?,?,?,?,?)","isisssi",array($id,$t_id,$amount,$mode,$date,$status,$plan_id));

    if($insert){
        
        
     $msg =  "<div class='alert alert-danger mx-auto'>
    
    <strong>Savings updated successfully.</strong>
  </div>";
}
}

}else{
    
    $msg =  "<div class='alert alert-danger mx-auto'>
    
    <strong>Insufficient funds, please fund your account.</strong>
  </div>";
}

}

?>
 
 <div class="main-content app-content">
<div class="container">
                            
                            <!--Modal: order-modal-->
                            
                            <div class="d-flex justify-content-between align-items-center mb-3 mt-3 p-4 rounded-4 shadow-sm text-white" 
     style="background: #C81D25;">
  
  <div>
    <h5 class="fw-bold mb-1 text-light">Save Money</h5>
    <p class="mb-0 opacity-75">Grow your savings with secure and guaranteed returns. Earn up to 15% Interest on your savings.</p>
  </div>

  <div>
    <i class="bi bi-piggy-bank-fill" style="font-size: 2.5rem;"></i>
  </div>
</div>
                           
                               
    <div class="row settings mt-3">
       
        <div class="col-xl-12">
            <div id="settingmenuContent">
                
                                <div id="editprofile">
                    <div class="row mb-5">
                        <div class="col-12">
                            <div class="card border-0 box-shadow">
                                <div class="card-body pb-0">
                                    <div class="d-flex align-items-center mb-3">
                                        
                                    </div>
                                    
                                        <form method="POST" action="" >
                                       <?php echo $csrf->csrfInput(); ?>  
                                       <input type="hidden" name="action" value="save">
                                       <?php echo $msg ?>
                                       <div class="row">
                                            <div class="form-group col-sm-12">
                                                <label class="form-label">Amount<span
                                                        class="text-danger"> * </span></label>
                                                <input type="number" class="form-control modern-input" name="amount" id="amount"
                                                    value=""
                                                    placeholder="Enter Amount" required>

                                            </div>
                                            <?php
                                            if($saving_plans[0]['status'] != "ONGOING"){ ?>
                                            
                                            <div class="mb-3">
                <label class="form-label fw-semibold">Duration <span class="text-danger">*</span></label>
                <select class="form-select modern-input" name="duration" id="duration" required>
                  <option value="">-- Select duration --</option>
                  <option value="3">3 Months</option>
                  <option value="6">6 Months</option>
                  <option value="12">12 Months</option>
                </select>
              </div>
              <?php } ?>
                                                                                                                                    <div class="form-group col-md-6">
                                                    
                                                </div>
                                                
                                                                                        <div class="form-group text-end">
                                                                                            <br>
                                                <button
                                                    class="btn btn-red modern-btn"
                                                     type="submit" name="save"  <?php echo $action; ?>>Start Saving </button>
                                            </div>
                                        </div>
                                    </form>
                                 
                                    
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                

                          
                    </div>
                  
                               

            </div>
        </div>
        
        
        
        
        
        
        
        
    </div>
    
    
                        </div>
                  
  <?php

include("includes/footer.php"); 

 ?>
 