<?php
declare(strict_types=1);
@header('Content-Type: text/plain; charset=utf-8');
if (session_status()!==PHP_SESSION_ACTIVE) session_start();

require_once __DIR__ . '/../includes/db.php';
$db = $conn ?? new mysqli('localhost','root','','edencamp_edencampus');
$db->set_charset('utf8mb4');

$student_id = (int)($_POST['student_id'] ?? 0);
$amount     = (float)($_POST['amount'] ?? 0);
$reason     = trim((string)($_POST['reason'] ?? ''));

if ($student_id<=0 || $amount<=0) { http_response_code(400); exit('Bad input'); }

$requested_by = isset($_SESSION['admin']['id']) ? (int)$_SESSION['admin']['id'] : null;
$payload = json_encode(['amount_lkr'=>$amount,'reason'=>$reason], JSON_UNESCAPED_UNICODE);

$st = $db->prepare("
  INSERT INTO approval_requests
    (type,status,student_id,requested_by,payload,requested_at)
  VALUES ('extra_fee','pending',?,?,?,NOW())
");
$st->bind_param('iis', $student_id, $requested_by, $payload);
$st->execute(); $st->close();

echo "Extra fee request sent for approval.";
