<?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;">
            <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): ?>
<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/cost-centers">
            <?= 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="cost_center_id" class="form-select" required>
                        <option value="">— اختر —</option>
                        <?php foreach ($costCenters as $cc): ?>
                            <option value="<?= (int)$cc['id'] ?>"><?= e($cc['code']) ?> — <?= e($cc['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>

<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
                $ccMap = [];
                foreach ($costCenters as $cc) $ccMap[(int)$cc['id']] = $cc;
                ?>
                <?php if (!empty($budgets)): ?>
                    <?php foreach ($budgets as $ccId => $periods): ?>
                        <?php
                            $cc = $ccMap[$ccId] ?? null;
                            $total = array_sum(array_column($periods, 'budgeted_amount'));
                        ?>
                        <tr>
                            <td><?= $cc ? e($cc['code'] . ' — ' . $cc['name_ar']) : 'مركز #' . $ccId ?></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>
<?php endif; ?>
<?php $__template->endSection(); ?>
