<?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/cost-centers<?= $selectedYearId ? '?fiscal_year_id='.$selectedYearId : '' ?>" class="btn btn-outline">موازنات مراكز التكلفة</a>
</div>

<!-- Year Selector -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;gap:10px;align-items:end;">
            <div style="flex:1;">
                <label class="form-label" style="font-size:12px;">السنة المالية</label>
                <select name="fiscal_year_id" class="form-select" onchange="this.form.submit()">
                    <option value="">— اختر —</option>
                    <?php foreach ($fiscalYears as $fy): ?>
                        <option value="<?= (int)$fy['id'] ?>" <?= (int)$fy['id'] === $selectedYearId ? 'selected' : '' ?>><?= e($fy['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </form>
    </div>
</div>

<?php if ($selectedYearId): ?>
<!-- Budget Entry Form -->
<?php if (can('accounting.budget.manage')): ?>
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">إدخال موازنة لحساب</h3>
    </div>
    <div style="padding:20px;">
        <form method="POST" action="/accounting/budgets">
            <?= csrf_field() ?>
            <input type="hidden" name="fiscal_year_id" value="<?= $selectedYearId ?>">
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
                <div>
                    <label class="form-label">الحساب <span style="color:#DC2626;">*</span></label>
                    <select name="account_id" class="form-select" required>
                        <option value="">— اختر حساب —</option>
                        <?php foreach ($accounts as $acc): ?>
                            <option value="<?= (int)$acc['id'] ?>"><?= e($acc['account_code']) ?> — <?= e($acc['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">ملاحظات</label>
                    <input type="text" name="budget_notes" class="form-input" placeholder="ملاحظات اختيارية">
                </div>
            </div>
            <div style="overflow-x:auto;">
                <table class="data-table" style="width:100%;">
                    <thead>
                        <tr>
                            <?php for ($m = 1; $m <= 12; $m++): ?>
                                <?php $period = date('Y') . '-' . str_pad((string)$m, 2, '0', STR_PAD_LEFT); ?>
                                <th style="text-align:center;font-size:12px;"><?= $period ?></th>
                            <?php endfor; ?>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <?php for ($m = 1; $m <= 12; $m++): ?>
                                <?php $period = date('Y') . '-' . str_pad((string)$m, 2, '0', STR_PAD_LEFT); ?>
                                <td><input type="number" name="amounts[<?= $period ?>]" class="form-input" style="width:100px;text-align:center;" step="0.01" min="0" value="0"></td>
                            <?php endfor; ?>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div style="margin-top:15px;">
                <button type="submit" class="btn btn-primary">حفظ الموازنة</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

<!-- Existing Budgets Summary -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">الموازنات المسجلة</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>الحساب</th>
                    <th style="text-align:center;">إجمالي الموازنة</th>
                    <th style="text-align:center;">عدد الشهور</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $accountMap = [];
                foreach ($accounts as $acc) $accountMap[(int)$acc['id']] = $acc;
                ?>
                <?php if (!empty($budgets)): ?>
                    <?php foreach ($budgets as $accountId => $periods): ?>
                        <?php
                            $acc = $accountMap[$accountId] ?? null;
                            $total = array_sum(array_column($periods, 'budgeted_amount'));
                        ?>
                        <tr>
                            <td><?= $acc ? e($acc['account_code'] . ' — ' . $acc['name_ar']) : 'حساب #' . $accountId ?></td>
                            <td style="text-align:center;font-weight:600;"><?= number_format($total, 2) ?></td>
                            <td style="text-align:center;"><?= count($periods) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="3" style="text-align:center;color:#6B7280;padding:30px;">لا توجد موازنات مسجلة لهذه السنة</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div style="margin-top:15px;">
    <a href="/accounting/budgets/variance<?= $selectedYearId ? '?fiscal_year_id='.$selectedYearId : '' ?>" class="btn btn-outline">تقرير الانحرافات</a>
</div>
<?php endif; ?>
<?php $__template->endSection(); ?>
