<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Laybuy.php';
require_once __DIR__ . '/../../classes/Branch.php';

$record_id	= $_GET['record_id'];
$record_id	= $_GET['record_id'];

$database = new Database();
$conn = $database->getConnection();


//$auth = new Auth($conn);


if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}
//$auth->enforceSuperAdminPrivileges($_SESSION['user_id']);
//echo "$_SESSION['user_id']";



if (isset($_POST['btn_assign_code'])) {
    $payment_date=$_POST['payment_date'];
	$unique_code=$_POST['unique_code'];
	$branch_id=$_GET['branch_id'];
	$user_id=$_SESSION['user_id'];
	$payment_date=$_GET['payment_date'];
	
	$laybuy = new Laybuy($conn);
	//$laybuy->AssignCode($record_id, $unique_code, $branch_id, $user_id, $payment_date);
   if  ($laybuy->AssignCode($record_id, $unique_code, $branch_id, $user_id, $payment_date)) {
       header('Location: laybuy_and_loans.php');
      exit();
		} else { 
        echo "<div class='alert alert-danger'>Failed to assign code.</div>";
	 header('Location: laybuy_and_loans.php');
       
	}	
	}
	
?>
<?php include __DIR__ . '/../../includes/header.php'; ?>
    <div class="container mt-5">
        <h1 class="mb-4">Assign Code to Laybuy </h1>
        <form method="POST" action="#">
			
            <div class="form-group">
                <label for="payment_date">Last Payment date </label>
                <input type="date" class="form-control" id="payment_date" name="payment_date"  required>
            </div>
			<div class="form-group">
                <label for="payment_date">Enter Code</label>
                <input type="text" class="form-control" id="unique_code" name="unique_code"  required>
            </div>
			           
             <button type="submit" class="btn btn-primary" name="btn_assign_code">Assign Code</button>
            <a href="laybuy_and_loans.php" class="btn btn-secondary">Cancel</a>
        </form>
    </div>
<script>
	function updateNewBal(){
		const balance = "<?php echo $balance ?>";
		const top_up_amount = document.getElementById('top_up_amount').value;
		document.getElementById('new_balance').value=balance-top_up_amount;
	}

</script>
<?php include __DIR__ . '/../../includes/footer.php'; ?>