<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__.'/../../../includes/auth.php';
requireLogin();
requirePermission('view_statements');

require_once __DIR__.'/../../../includes/layout.php';

/* ===============================
INPUT
================================ */

$accountId = $_GET['account_id'] ?? 0;
$from = $_GET['from'] ?? '';
$to   = $_GET['to'] ?? '';

if(!$accountId){
    die("Account missing");
}

/* ===============================
CALL API
================================ */

$_GET['account_id'] = $accountId;
$_GET['from'] = $from;
$_GET['to']   = $to;

$data = require __DIR__ . '/../../../../api/statements/get.php';

/* ===============================
VALIDATION
================================ */

if(!$data || ($data['status'] ?? '') !== 'success'){
    die("Failed to load statement");
}

$account = $data['account'];
$transactions = $data['transactions'];
$closingBalance = $data['closing_balance'] ?? 0;

/* ===============================
CALCULATE TOTALS
================================ */

$totalDebit = 0;
$totalCredit = 0;

foreach($transactions as $t){
    if($t['type'] !== 'Opening'){
        $totalDebit += $t['debit'];
        $totalCredit += $t['credit'];
    }
}
?>

<h2>Account Statement</h2>

<div style="margin-bottom:15px;">
    <button onclick="window.print()" class="btn btn-primary">
        🖨️ Print Statement
    </button>
</div>

<!-- FILTER -->
<form method="GET" style="margin-bottom:15px; display:flex; gap:10px; align-items:end;">

    <input type="hidden" name="account_id" value="<?= $accountId ?>">

    <div>
        <label>From</label><br>
        <input type="date" name="from" value="<?= htmlspecialchars($from) ?>">
    </div>

    <div>
        <label>To</label><br>
        <input type="date" name="to" value="<?= htmlspecialchars($to) ?>">
    </div>

    <div>
        <button type="submit" class="btn btn-primary">Filter</button>
    </div>

    <div>
        <a href="?account_id=<?= $accountId ?>" class="btn btn-secondary">Reset</a>
    </div>

</form>

<div class="card">
    <div class="card-body">

        <!-- ACCOUNT INFO -->
        <p>
            <strong>Customer:</strong> <?= htmlspecialchars($account['customer_name'] ?? '') ?><br>
            <strong>Account:</strong> <?= htmlspecialchars($account['account_name'] ?? '') ?>
        </p>

        <!-- TOTALS -->
        <div style="margin-bottom:15px;">
            <strong>Total Invoices:</strong> <?= number_format($totalDebit,2) ?> |
            <strong>Total Payments:</strong> <?= number_format($totalCredit,2) ?> |
            <strong>Closing Balance:</strong> <?= number_format($closingBalance,2) ?>
        </div>

        <!-- TABLE -->
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Type</th>
                    <th>Reference</th>
                    <th>Description</th>
                    <th>Debit</th>
                    <th>Credit</th>
                    <th>Balance</th>
                </tr>
            </thead>

            <tbody>
                <?php if(!empty($transactions)): ?>
                    <?php foreach($transactions as $t): ?>
                    <tr>
                        <td><?= htmlspecialchars($t['date']) ?></td>
                        <td><?= htmlspecialchars($t['type']) ?></td>
                        <td><?= htmlspecialchars($t['reference']) ?></td>
                        <td><?= htmlspecialchars($t['description']) ?></td>
                        <td><?= number_format($t['debit'],2) ?></td>
                        <td><?= number_format($t['credit'],2) ?></td>
                        <td><?= number_format($t['balance'],2) ?></td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7" style="text-align:center;">
                            No transactions found
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>

        </table>

    </div>
</div>