<?php
header('Content-Type: application/json');
require_once __DIR__ . '/db.php';
session_start();

if (!isset($_SESSION['user_id'])) {
  echo json_encode(['success' => false, 'error' => 'Not authenticated']);
  exit;
}

try {
  $pdo = getDB();
  $stmt = $pdo->prepare("SELECT * FROM loans WHERE user_id = :uid ORDER BY created_at DESC");
  $stmt->execute([':uid' => $_SESSION['user_id']]);
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  echo json_encode(['success' => true, 'data' => $rows]);
} catch (Throwable $e) {
  echo json_encode(['success' => false, 'error' => 'Server error']);
}
