<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>خطة تقسيط #<?= (int) $plan['id'] ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/members/<?= (int) $plan['member_id'] ?>" class="btn btn-outline">← العضو</a>
<a href="/installments" class="btn btn-outline">← كل الأقساط</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<!-- Plan Summary -->
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(150px, 1fr));gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #0D7377;">
        <div style="font-size:20px;font-weight:700;color:#0D7377;"><?= money($plan['total_amount']) ?></div>
        <div style="font-size:11px;color:#6B7280;">المبلغ الأصلي</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #059669;">
        <div style="font-size:20px;font-weight:700;color:#059669;"><?= money($plan['down_payment']) ?></div>
        <div style="font-size:11px;color:#6B7280;">المقدم</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #D97706;">
        <div style="font-size:20px;font-weight:700;color:#D97706;"><?= money($plan['total_interest']) ?></div>
        <div style="font-size:11px;color:#6B7280;">الفائدة (<?= e($plan['interest_rate']) ?>%)</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #0284C7;">
        <div style="font-size:20px;font-weight:700;color:#0284C7;"><?= money($plan['monthly_payment']) ?></div>
        <div style="font-size:11px;color:#6B7280;">القسط الشهري</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid <?= $plan['status'] === 'active' ? '#059669' : '#6B7280' ?>;">
        <div style="font-size:20px;font-weight:700;color:<?= $plan['status'] === 'active' ? '#059669' : '#6B7280' ?>;">
            <?= $plan['status'] === 'active' ? '● نشط' : ($plan['status'] === 'completed' ? '✅ مكتمل' : $plan['status']) ?>
        </div>
        <div style="font-size:11px;color:#6B7280;">الحالة</div>
    </div>
</div>

<!-- Schedule Table -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;color:#0D7377;">📅 جدول الأقساط (<?= (int) $plan['number_of_months'] ?> شهر)</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr><th>#</th><th>تاريخ الاستحقاق</th><th>المبلغ</th><th>أصل الدين</th><th>الفائدة</th><th>المتبقي</th><th>الحالة</th><th>الإجراءات</th></tr>
            </thead>
            <tbody>
                <?php foreach ($schedule as $s): ?>
                <?php
                $isOverdue = ($s['status'] === 'pending' && $s['due_date'] < date('Y-m-d'));
                $isPaid = ($s['status'] === 'paid');
                ?>
                <tr style="<?= $isOverdue ? 'background:#FEF2F2;' : '' ?><?= $isPaid ? 'background:#F0FDF4;' : '' ?>">
                    <td style="font-weight:600;"><?= (int) $s['installment_number'] ?></td>
                    <td style="white-space:nowrap;<?= $isOverdue ? 'color:#DC2626;font-weight:700;' : '' ?>"><?= e($s['due_date']) ?></td>
                    <td style="font-weight:600;"><?= money($s['amount']) ?></td>
                    <td style="font-size:13px;"><?= money($s['principal']) ?></td>
                    <td style="font-size:13px;color:#D97706;"><?= money($s['interest']) ?></td>
                    <td style="font-size:13px;"><?= money($s['remaining_after']) ?></td>
                    <td>
                        <?php if ($isPaid): ?>
                            <span style="color:#059669;font-weight:700;">✅ مدفوع</span>
                            <div style="font-size:11px;color:#9CA3AF;"><?= e(substr($s['paid_at'] ?? '', 0, 10)) ?></div>
                        <?php elseif ($isOverdue): ?>
                            <span style="color:#DC2626;font-weight:700;">⚠️ متأخر</span>
                        <?php else: ?>
                            <span style="color:#D97706;font-weight:600;">⏳ معلق</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($s['status'] === 'pending'): ?>
                        <form method="POST" action="/installments/<?= (int) $plan['id'] ?>/pay/<?= (int) $s['id'] ?>" style="display:flex;gap:5px;">
                            <?= csrf_field() ?>
                            <input type="hidden" name="amount" value="<?= e($s['amount']) ?>">
                            <select name="payment_method" class="form-select" style="width:auto;font-size:11px;padding:3px 6px;">
                                <option value="cash">نقدي</option>
                                <option value="visa">فيزا</option>
                            </select>
                            <button type="submit" class="btn btn-sm btn-primary" onclick="return confirm('دفع قسط <?= money($s['amount']) ?>؟')">💰 ادفع</button>
                        </form>
                        <?php elseif ($isPaid): ?>
                            <span style="font-size:11px;color:#9CA3AF;">—</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>