<?php
if (isset($_POST['approve_expense_btn'])) {
    $expense_id = $_POST['expense_id'];

    $stmt = $connection->prepare("UPDATE `expenses` SET `status` = 'approved' WHERE `id` = ?");
    $stmt->bind_param("i", $expense_id);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "✅ Expense approved successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "❌ Failed to approve expense.";
        $_SESSION['type'] = "danger";
    }
    $stmt->close();
    header("Location: pending_expenses");
    exit();
}

if (isset($_POST['reject_expense_btn'])) {
    $expense_id = $_POST['expense_id'];

    $stmt = $connection->prepare("UPDATE `expenses` SET `status` = 'rejected' WHERE `id` = ?");
    $stmt->bind_param("i", $expense_id);

    if ($stmt->execute()) {
        $_SESSION['msg'] = "⚠️ Expense rejected successfully.";
        $_SESSION['type'] = "warning";
    } else {
        $_SESSION['msg'] = "❌ Failed to reject expense.";
        $_SESSION['type'] = "danger";
    }
    $stmt->close();
        header("Location: pending_expenses");
    exit();
}
?>

<div class='page-title'>
  <div>
    <h1><i class='fa fa-money'></i> Manage Expenses</h1>
    <p>Pending Expenses</p>
  </div>  
</div>
<div class='card'>
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h4 class="text-center">Pending Expenses</h4>
		</div>
		<div class="panel-body">
			<div class="table-responsive">
				<table class="table table-bordered">
					<thead>
						<tr>
							<th>S/N</th>
							<th>Receiver</th>
							<th>Reason</th>
							<th>Amount</th>
							<th>Phone</th>
							<th>Date</th>
							<th>Action</th>
						</tr>
					</thead>
					<tbody>
						<?php
						$query = "SELECT * FROM `expenses` WHERE `status` = 'pending'";
						$run_query = mysqli_query($connection, $query);

						if (mysqli_num_rows($run_query) > 0) {
							$i = 0;
							while ($result = mysqli_fetch_assoc($run_query)) {
								$i++;
								echo "
								<tr>
									<td>{$i}</td>
									<td>{$result['paid_to']}</td>
									<td>{$result['paid_for']}</td>
									<td>{$result['amount_paid']}</td>
									<td>{$result['phone']}</td>
									<td>{$result['date']}</td>
									<td>
										<form method='POST' style='display: inline;'>
											<input type='hidden' name='expense_id' value='{$result['id']}' />
											<button type='submit' name='approve_expense_btn' class='btn btn-success btn-sm'>Approve</button>
										</form>
										<form method='POST' style='display: inline;'>
											<input type='hidden' name='expense_id' value='{$result['id']}' />
											<button type='submit' name='reject_expense_btn' class='btn btn-danger btn-sm'>Reject</button>
										</form>
									</td>
								</tr>
								";
							}
						} else {
							echo "
							<tr>
								<td colspan='7' class='text-center text-danger'><b>No expenses for now!!!!</b></td>
							</tr>
							";
						}
						?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
