<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>تقرير الانحرافات<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
    <h2 style="margin:0;">تقرير انحرافات الموازنة</h2>
    <a href="/accounting/budgets<?= $selectedYearId ? '?fiscal_year_id='.$selectedYearId : '' ?>" class="btn btn-outline">العودة للموازنات</a>
</div>

<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
            <div>
                <label class="form-label" style="font-size:12px;">السنة المالية</label>
                <select name="fiscal_year_id" class="form-select">
                    <?php foreach ($fiscalYears as $fy): ?>
                        <option value="<?= (int)$fy['id'] ?>" <?= (int)$fy['id'] === $selectedYearId ? 'selected' : '' ?>><?= e($fy['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">من فترة</label>
                <input type="month" name="from_period" class="form-input" value="<?= e($fromPeriod ?? '') ?>" dir="ltr">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">إلى فترة</label>
                <input type="month" name="to_period" class="form-input" value="<?= e($toPeriod ?? '') ?>" dir="ltr">
            </div>
            <div>
                <button type="submit" class="btn btn-primary">عرض</button>
            </div>
        </form>
    </div>
</div>

<?php if (!empty($report)): ?>
<div class="card">
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>كود الحساب</th>
                    <th>اسم الحساب</th>
                    <th>النوع</th>
                    <th style="text-align:center;">الموازنة</th>
                    <th style="text-align:center;">الفعلي</th>
                    <th style="text-align:center;">الانحراف</th>
                    <th style="text-align:center;">النسبة %</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $totalBudget = 0;
                $totalActual = 0;
                $totalVariance = 0;
                $typeLabels = ['expense' => 'مصروف', 'revenue' => 'إيراد', 'asset' => 'أصل', 'liability' => 'التزام', 'equity' => 'حقوق ملكية'];
                ?>
                <?php foreach ($report as $row): ?>
                    <?php
                    $totalBudget += $row['budgeted'];
                    $totalActual += $row['actual'];
                    $totalVariance += $row['variance'];
                    $isOverrun = $row['variance'] < 0;
                    ?>
                    <tr>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= e($row['account_code']) ?></td>
                        <td><?= e($row['account_name']) ?></td>
                        <td><?= $typeLabels[$row['account_type']] ?? $row['account_type'] ?></td>
                        <td style="text-align:center;"><?= number_format($row['budgeted'], 2) ?></td>
                        <td style="text-align:center;"><?= number_format($row['actual'], 2) ?></td>
                        <td style="text-align:center;color:<?= $isOverrun ? '#DC2626' : '#059669' ?>;font-weight:600;">
                            <?= number_format($row['variance'], 2) ?>
                        </td>
                        <td style="text-align:center;color:<?= $isOverrun ? '#DC2626' : '#059669' ?>;">
                            <?= $row['variance_pct'] ?>%
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="font-weight:700;background:#F9FAFB;">
                    <td colspan="3">الإجمالي</td>
                    <td style="text-align:center;"><?= number_format($totalBudget, 2) ?></td>
                    <td style="text-align:center;"><?= number_format($totalActual, 2) ?></td>
                    <td style="text-align:center;color:<?= $totalVariance < 0 ? '#DC2626' : '#059669' ?>;"><?= number_format($totalVariance, 2) ?></td>
                    <td style="text-align:center;">—</td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php elseif ($selectedYearId): ?>
<div class="card">
    <div style="padding:30px;text-align:center;color:#6B7280;">
        لا توجد بيانات موازنة لهذه السنة المالية
    </div>
</div>
<?php endif; ?>
<?php $__template->endSection(); ?>
