<?php
// Fix pagination
$page_num = isset($_GET['p']) && is_numeric($_GET['p']) ? (int)$_GET['p'] : 1;
$limit = 50;
$offset = ($page_num - 1) * $limit;

// Count total entries for pagination
$total_entries = db()->query("SELECT COUNT(*) FROM acc_journal_entries")->fetchColumn();
$data['total_pages'] = ceil($total_entries / $limit);
$data['current_page'] = $page_num;

$data['journal_entries'] = db()->query("SELECT je.*, 
                                        (SELECT SUM(debit) FROM acc_ledger WHERE journal_entry_id = je.id) as total_debit 
                                        FROM acc_journal_entries je 
                                        ORDER BY je.entry_date DESC, je.id DESC LIMIT $limit OFFSET $offset")->fetchAll();
$data['accounts'] = db()->query("SELECT * FROM acc_accounts ORDER BY code ASC")->fetchAll();

if (isset($_GET['action']) && $_GET['action'] === 'get_entry_details') {
    header('Content-Type: application/json');
    $id = (int)$_GET['id'];
    $stmt = db()->prepare("SELECT l.*, a.name_en, a.code FROM acc_ledger l JOIN acc_accounts a ON l.account_id = a.id WHERE l.journal_entry_id = ?");
    $stmt->execute([$id]);
    echo json_encode($stmt->fetchAll());
    exit;
}

if (isset($_GET['view']) && $_GET['view'] === 'trial_balance') {
    $data['trial_balance'] = db()->query("SELECT a.code, a.name_en, SUM(l.debit) as total_debit, SUM(l.credit) as total_credit 
                                          FROM acc_accounts a 
                                          LEFT JOIN acc_ledger l ON a.id = l.account_id 
                                          GROUP BY a.id 
                                          HAVING total_debit > 0 OR total_credit > 0 
                                          ORDER BY a.code ASC")->fetchAll();
}

if (isset($_GET['view']) && $_GET['view'] === 'profit_loss') {
    $data['revenue_accounts'] = db()->query("SELECT code, name_en, name_ar FROM acc_accounts WHERE type = 'revenue' AND parent_id IS NOT NULL ORDER BY code ASC")->fetchAll();
    $data['expense_accounts'] = db()->query("SELECT code, name_en, name_ar FROM acc_accounts WHERE type = 'expense' AND parent_id IS NOT NULL ORDER BY code ASC")->fetchAll();
}

if (isset($_GET['view']) && $_GET['view'] === 'balance_sheet') {
    $data['asset_accounts'] = db()->query("SELECT code, name_en, name_ar FROM acc_accounts WHERE type = 'asset' AND parent_id IS NOT NULL ORDER BY code ASC")->fetchAll();
    $data['liability_accounts'] = db()->query("SELECT code, name_en, name_ar FROM acc_accounts WHERE type = 'liability' AND parent_id IS NOT NULL ORDER BY code ASC")->fetchAll();
    $data['equity_accounts'] = db()->query("SELECT code, name_en, name_ar FROM acc_accounts WHERE type = 'equity' AND parent_id IS NOT NULL ORDER BY code ASC")->fetchAll();
}

if (isset($_GET['view']) && $_GET['view'] === 'vat_report') {
    $start = $_GET['start_date'] ?? date('Y-m-01');
    $end = $_GET['end_date'] ?? date('Y-m-d');
    $data['vat_report'] = getVatReport($start, $end);
    $data['start_date'] = $start;
    $data['end_date'] = $end;
}

if (isset($_GET['view']) && $_GET['view'] === 'coa') {
    $data['coa'] = db()->query("SELECT a.*, p.name_en as parent_name 
                                FROM acc_accounts a 
                                LEFT JOIN acc_accounts p ON a.parent_id = p.id 
                                ORDER BY a.code ASC")->fetchAll();
}
