<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= e($parent->full_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<?php $relationships = \App\Modules\ParentPortal\Models\ParentAccount::getRelationshipTypes(); ?>
<div style="display:grid;grid-template-columns:1fr 300px;gap:20px;">
    <div>
        <!-- Linked Players -->
        <div class="card" style="padding:20px;margin-bottom:20px;">
            <h4 style="margin-bottom:15px;">اللاعبون المرتبطون (<?= count($players) ?>)</h4>
            <?php if (empty($players)): ?>
            <p style="color:#6B7280;text-align:center;padding:20px;">لا يوجد لاعبون مرتبطون</p>
            <?php else: ?>
            <div style="display:grid;gap:10px;">
                <?php foreach ($players as $p): ?>
                <div style="display:flex;align-items:center;justify-content:space-between;padding:12px;background:#F9FAFB;border-radius:8px;">
                    <div>
                        <a href="/parents/<?= (int) $parent->id ?>/player/<?= (int) $p['id'] ?>" style="font-weight:600;color:#0D7377;text-decoration:none;"><?= e($p['full_name_ar'] ?? '') ?></a>
                        <div style="font-size:12px;color:#6B7280;">
                            <?= e($relationships[$p['relationship'] ?? ''] ?? '') ?>
                            <?php if ($p['is_primary_contact']): ?><span style="color:#059669;margin-right:5px;">● جهة اتصال رئيسية</span><?php endif; ?>
                        </div>
                    </div>
                    <div style="display:flex;gap:5px;">
                        <a href="/parents/<?= (int) $parent->id ?>/player/<?= (int) $p['id'] ?>" class="btn btn-sm btn-outline">لوحة اللاعب</a>
                        <form method="POST" action="/parents/<?= (int) $parent->id ?>/unlink/<?= (int) $p['id'] ?>" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-sm btn-outline" style="color:#DC2626;" onclick="return confirm('فك الارتباط؟')">فك</button></form>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>

            <!-- Link new player -->
            <form method="POST" action="/parents/<?= (int) $parent->id ?>/link-player" style="margin-top:15px;display:flex;gap:8px;align-items:end;">
                <?= csrf_field() ?>
                <div style="flex:1;"><label class="form-label" style="font-size:12px;">ربط لاعب جديد</label><input type="number" name="player_id" placeholder="رقم اللاعب" class="form-input" style="direction:ltr;text-align:right;" required></div>
                <input type="hidden" name="relationship" value="<?= e($parent->relationship) ?>">
                <button type="submit" class="btn btn-sm btn-primary">ربط</button>
            </form>
        </div>

        <!-- Recent Notifications -->
        <div class="card" style="padding:20px;">
            <h4 style="margin-bottom:15px;">آخر الإشعارات</h4>
            <?php if (empty($notifications)): ?>
            <p style="color:#6B7280;text-align:center;">لا توجد إشعارات</p>
            <?php else: ?>
            <div style="max-height:400px;overflow-y:auto;">
                <?php foreach ($notifications as $n): ?>
                <div style="padding:10px;border-bottom:1px solid #F3F4F6;<?= !$n['is_read'] ? 'background:#F0F9FF;' : '' ?>">
                    <div style="display:flex;justify-content:space-between;">
                        <span style="font-weight:600;font-size:13px;"><?= e($n['title_ar']) ?></span>
                        <span style="font-size:11px;color:#6B7280;"><?= e(substr($n['created_at'], 0, 16)) ?></span>
                    </div>
                    <p style="margin:4px 0 0;font-size:12px;color:#4B5563;"><?= e($n['body_ar']) ?></p>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Profile Sidebar -->
    <div>
        <div class="card" style="padding:20px;">
            <h5 style="margin-bottom:12px;color:#0D7377;">بيانات ولي الأمر</h5>
            <table style="width:100%;font-size:13px;">
                <tr><td style="color:#6B7280;padding:5px 0;">الاسم</td><td style="font-weight:600;"><?= e($parent->full_name_ar) ?></td></tr>
                <tr><td style="color:#6B7280;padding:5px 0;">الهاتف</td><td style="direction:ltr;text-align:right;"><?= e($parent->phone) ?></td></tr>
                <?php if ($parent->secondary_phone): ?><tr><td style="color:#6B7280;padding:5px 0;">هاتف بديل</td><td style="direction:ltr;text-align:right;"><?= e($parent->secondary_phone) ?></td></tr><?php endif; ?>
                <tr><td style="color:#6B7280;padding:5px 0;">الصلة</td><td><?= e($relationships[$parent->relationship] ?? '') ?></td></tr>
                <?php if ($parent->national_id): ?><tr><td style="color:#6B7280;padding:5px 0;">الرقم القومي</td><td style="direction:ltr;text-align:right;"><?= e($parent->national_id) ?></td></tr><?php endif; ?>
                <?php if ($parent->email): ?><tr><td style="color:#6B7280;padding:5px 0;">البريد</td><td style="direction:ltr;text-align:right;font-size:12px;"><?= e($parent->email) ?></td></tr><?php endif; ?>
            </table>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>
