<?php
declare(strict_types=1);
require_once __DIR__ . '/../../../app/Config/functions.php';
require_once __DIR__ . '/../../../app/Models/Journal.php';

require_login();

$journal = new Journal();
$success = $journal->add([
    'reference_no' => $_POST['reference_no'] ?? null,
    'journal_date' => $_POST['journal_date'] ?? date('Y-m-d'),
    'description'  => $_POST['description'] ?? '',
    'status'       => $_POST['status'] ?? 'draft',
    'created_by'   => $_SESSION['user_id']
]);

echo json_encode([
    'success' => $success,
    'message' => $success ? 'Journal entry created successfully.' : 'Failed to create journal entry.'
]);
