<?php
require_once __DIR__ . '/../config/config.php';
require_permission('accounting.reports_view');

$from = $_GET['from'] ?? date('Y-m-01');
$to = $_GET['to'] ?? date('Y-m-d');

$stmt = $pdo->prepare("SELECT je.*, u.username created_by_name,
                        (SELECT SUM(debit) FROM journal_entry_lines WHERE entry_id = je.id) total
                        FROM journal_entries je JOIN users u ON u.id = je.created_by
                        WHERE je.entry_date BETWEEN ? AND ?
                        ORDER BY je.entry_date DESC, je.id DESC");
$stmt->execute([$from, $to]);
$entries = $stmt->fetchAll();

$canPost = has_permission($pdo, current_user(), 'accounting.journal_post');

$pageTitle = 'Journal Entries';
include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="flex items-center justify-between mb-6">
  <h1 class="text-2xl font-bold">Journal Entries</h1>
  <?php if ($canPost): ?><a href="create.php" class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">+ New Journal Entry</a><?php endif; ?>
</div>

<form method="GET" class="flex flex-wrap items-end gap-3 mb-6 bg-white rounded-xl shadow-sm border border-slate-200 p-4">
  <div>
    <label class="text-xs text-slate-500 block mb-1">From</label>
    <input type="date" name="from" value="<?= e($from) ?>" class="border border-slate-300 rounded-lg px-3 py-2 text-sm">
  </div>
  <div>
    <label class="text-xs text-slate-500 block mb-1">To</label>
    <input type="date" name="to" value="<?= e($to) ?>" class="border border-slate-300 rounded-lg px-3 py-2 text-sm">
  </div>
  <button class="bg-blue-700 hover:bg-blue-800 text-white text-sm font-medium px-4 py-2 rounded-lg">Filter</button>
</form>

<div class="overflow-x-auto">
<div class="bg-white rounded-xl shadow-sm border border-slate-200">
  <table class="w-full text-sm">
    <thead class="bg-slate-50 text-slate-500 text-xs uppercase"><tr>
      <th class="text-left px-5 py-2">Entry No.</th><th class="text-left px-5 py-2">Date</th>
      <th class="text-left px-5 py-2">Description</th><th class="text-left px-5 py-2">Reference</th>
      <th class="text-left px-5 py-2">Amount</th><th class="text-left px-5 py-2">Status</th>
      <th class="text-right px-5 py-2">Action</th></tr></thead>
    <tbody>
    <?php foreach ($entries as $e): ?>
      <tr class="border-t border-slate-100 <?= $e['status']==='voided' ? 'opacity-50' : '' ?>">
        <td class="px-5 py-2.5 font-medium"><?= e($e['entry_number']) ?></td>
        <td class="px-5 py-2.5"><?= date('M j, Y', strtotime($e['entry_date'])) ?></td>
        <td class="px-5 py-2.5"><?= e($e['description']) ?></td>
        <td class="px-5 py-2.5 text-slate-500"><?= e($e['reference_type']) ?></td>
        <td class="px-5 py-2.5">₦<?= number_format($e['total'],2) ?></td>
        <td class="px-5 py-2.5"><span class="px-2 py-0.5 rounded-full text-xs <?= $e['status']==='posted' ? 'bg-emerald-100 text-emerald-700' : 'bg-red-100 text-red-700' ?> capitalize"><?= $e['status'] ?></span></td>
        <td class="px-5 py-2.5 text-right"><a href="view.php?id=<?= $e['id'] ?>" class="text-blue-600 hover:underline">View →</a></td>
      </tr>
    <?php endforeach; ?>
    <?php if (empty($entries)): ?><tr><td colspan="7" class="px-5 py-6 text-center text-slate-400">No journal entries in this period.</td></tr><?php endif; ?>
    </tbody>
  </table>
</div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
