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

<?php $__template->section('title'); ?>قائمة الدخل<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/reports/income-statement" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">من تاريخ</label>
                <input type="date" name="date_from" class="form-input" value="<?= e($date_from) ?>">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
                <input type="date" name="date_to" class="form-input" value="<?= e($date_to) ?>">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الفرع</label>
                <select name="branch_id" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($branches as $b): ?>
                        <option value="<?= (int)$b['id'] ?>" <?= (int)($filters['branch_id'] ?? 0) === (int)$b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">مركز التكلفة</label>
                <select name="cost_center_id" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($cost_centers as $cc): ?>
                        <option value="<?= (int)$cc['id'] ?>" <?= (int)($filters['cost_center_id'] ?? 0) === (int)$cc['id'] ? 'selected' : '' ?>><?= e($cc['code'] . ' — ' . $cc['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">عرض</button>
        </form>
    </div>
</div>

<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;text-align:center;">
        <h2 style="margin:0;">قائمة الدخل</h2>
        <p style="color:#6B7280;margin:5px 0 0;">من <?= e($date_from) ?> إلى <?= e($date_to) ?></p>
    </div>
    <div style="padding:20px;">
        <!-- Revenue -->
        <div style="margin-bottom:25px;">
            <h3 style="color:#059669;border-bottom:2px solid #059669;padding-bottom:8px;">الإيرادات</h3>
            <table style="width:100%;">
                <?php foreach ($result['revenue'] as $r): ?>
                    <tr>
                        <td style="padding:6px 0;width:15%;direction:ltr;text-align:right;font-size:13px;color:#6B7280;"><?= e($r['account_code']) ?></td>
                        <td style="padding:6px 0;"><?= e($r['name_ar']) ?></td>
                        <td style="padding:6px 0;width:20%;direction:ltr;text-align:left;"><?= money($r['balance']) ?></td>
                    </tr>
                <?php endforeach; ?>
                <tr style="font-weight:700;border-top:2px solid #059669;">
                    <td style="padding:10px 0;" colspan="2">إجمالي الإيرادات</td>
                    <td style="padding:10px 0;direction:ltr;text-align:left;color:#059669;font-size:18px;"><?= money($result['total_revenue']) ?></td>
                </tr>
            </table>
        </div>

        <!-- Expenses -->
        <div style="margin-bottom:25px;">
            <h3 style="color:#DC2626;border-bottom:2px solid #DC2626;padding-bottom:8px;">المصروفات</h3>
            <table style="width:100%;">
                <?php foreach ($result['expenses'] as $exp): ?>
                    <tr>
                        <td style="padding:6px 0;width:15%;direction:ltr;text-align:right;font-size:13px;color:#6B7280;"><?= e($exp['account_code']) ?></td>
                        <td style="padding:6px 0;"><?= e($exp['name_ar']) ?></td>
                        <td style="padding:6px 0;width:20%;direction:ltr;text-align:left;"><?= money($exp['balance']) ?></td>
                    </tr>
                <?php endforeach; ?>
                <tr style="font-weight:700;border-top:2px solid #DC2626;">
                    <td style="padding:10px 0;" colspan="2">إجمالي المصروفات</td>
                    <td style="padding:10px 0;direction:ltr;text-align:left;color:#DC2626;font-size:18px;"><?= money($result['total_expenses']) ?></td>
                </tr>
            </table>
        </div>

        <!-- Net Income -->
        <div style="background:<?= bccomp($result['net_income'], '0.00', 2) >= 0 ? '#F0FDF4' : '#FEF2F2' ?>;padding:20px;border-radius:8px;text-align:center;">
            <div style="font-size:14px;color:#6B7280;">صافي الربح / (الخسارة)</div>
            <div style="font-size:32px;font-weight:700;color:<?= bccomp($result['net_income'], '0.00', 2) >= 0 ? '#059669' : '#DC2626' ?>;">
                <?= money($result['net_income']) ?> ج.م
            </div>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>
