<?php error_log('TRACE INDEX VIEW '. __FILE__); ?>
<?php
function ch_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$conference = $conference ?? [];
$policy = $policy ?? null;
$orgConferences = $orgConferences ?? [];
$blocks = $blocks ?? [];
$committees = $committees ?? [];
$templates = $templates ?? [];
$logs = $logs ?? [];

$title = ($conference['title_ar'] ?? '') ?: ($conference['title_en'] ?? 'Conference');
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body d-flex justify-content-between align-items-center flex-wrap gap-3 p-4">
            <div>
                <div class="opacity-75">Smart Conference Home Manager</div>
                <h3 class="fw-bold mb-1"><?= ch_h($title) ?></h3>
                <div class="opacity-75">
                    <?= ch_h($conference['short_name'] ?? '') ?>
                    | Org <?= (int)($conference['org_id'] ?? 0) ?>
                    | Conference <?= (int)($conference['id'] ?? 0) ?>
                </div>
            </div>

            <div class="d-flex gap-2 flex-wrap">
                <a class="btn btn-light rounded-pill fw-bold" target="_blank" href="/easychair/<?= htmlspecialchars($conference['slug'] ?? '') ?>">
                    عرض الصفحة
                </a>
                <a class="btn btn-outline-light rounded-pill fw-bold" href="/easychair/admin/dashboard">
                    لوحة التحكم
                </a>
            </div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold">سياسة مؤتمر واحد لكل مؤسسة</h5>
                    <div class="text-muted small mb-3">
                        يتم اعتماد مؤتمر واحد كمؤتمر نشط/حالي لكل مؤسسة. لا يتم حذف المؤتمرات القديمة، ولكن يتم ضبط المؤتمر الحالي للمؤسسة هنا.
                    </div>

                    <form method="post" action="/easychair/admin/conference-home/policy">
                        <select name="conference_id" class="form-select mb-2">
                            <?php foreach ($orgConferences as $c): ?>
                                <option value="<?= (int)$c['id'] ?>" <?= !empty($policy['conference_id']) && (int)$policy['conference_id'] === (int)$c['id'] ? 'selected' : '' ?>>
                                    <?= ch_h(($c['short_name'] ?? '') . ' - ' . (($c['title_ar'] ?? '') ?: ($c['title_en'] ?? ''))) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <input name="policy_note" class="form-control mb-2" placeholder="ملاحظة السياسة">
                        <button class="btn btn-primary w-100">حفظ المؤتمر الحالي للمؤسسة</button>
                    </form>

                    <?php if (count($orgConferences) > 1): ?>
                        <div class="alert alert-warning mt-3 small">
                            هذه المؤسسة لديها <?= count($orgConferences) ?> مؤتمرات. النظام سيعتمد المؤتمر المختار أعلاه كمؤتمر المؤسسة الحالي.
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="col-lg-8">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold">إضافة بلوك ذكي للصفحة الرئيسية</h5>

                    <form method="post" action="/easychair/admin/conference-home/block/store" enctype="multipart/form-data" class="row g-2">
                        <div class="col-md-3">
                            <select name="block_type" class="form-select">
                                <option value="hero_slide">Hero Slider</option>
                                <option value="panel">Panel/Card</option>
                                <option value="topic">Topic</option>
                                <option value="section_html">HTML Section</option>
                                <option value="important_date">Important Date</option>
                                <option value="sponsor">Sponsor</option>
                                <option value="announcement">Announcement</option>
                                <option value="custom">Custom</option>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <input name="title_ar" class="form-control" placeholder="العنوان العربي">
                        </div>
                        <div class="col-md-3">
                            <input name="title_en" class="form-control" placeholder="English title">
                        </div>
                        <div class="col-md-3">
                            <input name="sort_order" type="number" class="form-control" value="1" placeholder="Order">
                        </div>
                        <div class="col-md-6">
                            <textarea name="body_ar" class="form-control" rows="3" placeholder="المحتوى العربي"></textarea>
                        </div>
                        <div class="col-md-6">
                            <textarea name="body_en" class="form-control" rows="3" placeholder="English body"></textarea>
                        </div>
                        <div class="col-md-4">
                            <input type="file" name="image" class="form-control" accept=".png,.jpg,.jpeg,.webp">
                        </div>
                        <div class="col-md-5">
                            <input name="link_url" class="form-control" placeholder="Optional link URL">
                        </div>
                        <div class="col-md-3">
                            <button class="btn btn-success w-100">إضافة بلوك</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">

<div class="card border-0 shadow-sm mb-4"
     style="border-radius:24px;">

    <div class="card-body">

        <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">

            <div>

                <h4 class="fw-bold mb-1">
                    Homepage Sections Governance
                </h4>

                <div class="text-muted small">
                    Live orchestration and visibility management for homepage sections.
                </div>

            </div>

        </div>

        <div class="table-responsive">

            <table class="table table-bordered align-middle">

                <thead class="table-dark">

                <tr>

                    <th>ID</th>

                    <th>Section</th>

                    <th>Title</th>

                    <th>Theme</th>

                    <th>Order</th>

                    <th>Status</th>

                </tr>

                </thead>

                <tbody>

                <?php foreach ($sections as $s): ?>

                    <tr>

                        <td>
                            <?= (int)$s['id'] ?>
                        </td>

                        <td>

                            <span class="badge bg-primary">

                                <?= ch_h($s['section_key']) ?>

                            </span>

                        </td>

                        <td>

                            <strong>

                                <?= ch_h(
                                    $s['title_ar']
                                    ?: $s['title_en']
                                    ?: $s['section_title_ar']
                                    ?: $s['section_title_en']
                                    ?: $s['section_key']
                                ) ?>

                            </strong>

                        </td>

                        <td>

                            <span class="badge bg-info text-dark">

                                <?= ch_h(
                                    $s['theme_name']
                                    ?? 'default'
                                ) ?>

                            </span>

                        </td>

                        <td>

                            <?= (int)(
                                $s['sort_order']
                                ?? 0
                            ) ?>

                        </td>

<td>

    <?php if (!empty($s['enabled'])): ?>

        <span class="badge bg-success px-3 py-2">
            LIVE
        </span>

    <?php else: ?>

        <span class="badge bg-danger px-3 py-2">
            DISABLED
        </span>

    <?php endif; ?>

    <div class="mt-2">

        <a
            class="btn btn-sm <?= !empty($s['enabled'])
                ? 'btn-outline-danger'
                : 'btn-outline-success' ?>"

            href="/easychair/admin/conference-home/section/toggle?id=<?= (int)$s['id'] ?>">

            <?= !empty($s['enabled'])
                ? 'Disable'
                : 'Enable' ?>

        </a>

    </div>

</td>     

                    </tr>

                <?php endforeach; ?>

                <?php if (!$sections): ?>

                    <tr>

                        <td colspan="6"
                            class="text-center text-muted py-4">

                            No homepage sections found.

                        </td>

                    </tr>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>            

<h5 class="fw-bold mb-3">البلوكات الحالية</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Type</th>
                        <th>Title</th>
                        <th>Order</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($blocks as $b): ?>
                        <tr>
                            <td><?= (int)$b['id'] ?></td>
                            <td><span class="badge bg-primary"><?= ch_h($b['block_type']) ?></span></td>
                            <td>
                                <strong><?= ch_h($b['title_ar'] ?: $b['title_en']) ?></strong>
                                <div class="text-muted small"><?= ch_h(mb_substr((string)($b['body_ar'] ?: $b['body_en']), 0, 120)) ?></div>
                            </td>
                            <td><?= (int)$b['sort_order'] ?></td>
                            <td>
                                <?php if (!empty($b['is_active'])): ?>
                                    <span class="badge bg-success">active</span>
                                <?php else: ?>
                                    <span class="badge bg-secondary">inactive</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a class="btn btn-sm btn-warning" href="/easychair/admin/conference-home/block/toggle?id=<?= (int)$b['id'] ?>">Toggle</a>
                                <a class="btn btn-sm btn-danger" onclick="return confirm('Delete?')" href="/easychair/admin/conference-home/block/delete?id=<?= (int)$b['id'] ?>">Delete</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (!$blocks): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No blocks yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div id="imports" class="row g-3 mb-4">
        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold">Bulk Upload Reviewers CSV</h5>
                    <div class="text-muted small mb-2">
                        Columns: email, first_name, last_name, mobile, job_title, expertise, password
                    </div>
                    <form method="post" action="/easychair/admin/conference-home/reviewers/upload" enctype="multipart/form-data">
                        <input type="file" name="csv" class="form-control mb-2" accept=".csv" required>
                        <button class="btn btn-primary">Upload Reviewers</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body">
                    <h5 class="fw-bold">Bulk Upload Committees / TPC CSV</h5>
                    <div class="text-muted small mb-2">
                        Columns: committee_type, full_name_ar, full_name_en, title_ar, title_en, affiliation_ar, affiliation_en, email, sort_order
                    </div>
                    <form method="post" action="/easychair/admin/conference-home/committees/upload" enctype="multipart/form-data">
                        <input type="file" name="csv" class="form-control mb-2" accept=".csv" required>
                        <button class="btn btn-primary">Upload Committees/TPC</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Committees / TPC</h5>
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>Type</th>
                        <th>Name</th>
                        <th>Title</th>
                        <th>Email</th>
                        <th>Order</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($committees as $m): ?>
                        <tr>
                            <td><span class="badge bg-primary"><?= ch_h($m['committee_type']) ?></span></td>
                            <td><?= ch_h($m['full_name_ar'] ?: $m['full_name_en']) ?></td>
                            <td><?= ch_h($m['title_ar'] ?: $m['title_en']) ?></td>
                            <td><?= ch_h($m['email']) ?></td>
                            <td><?= (int)$m['sort_order'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (!$committees): ?>
                        <tr><td colspan="5" class="text-center text-muted py-4">No committees/TPC yet.</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    
    <div id="email-templates" class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Email Templates Full Control</h5>

            <div class="alert alert-info small">
                Variables: {conference_short_name}, {author_name}, {reviewer_name}, {submission_no}, {submission_title}, {decision}
            </div>

            <form method="post" action="/easychair/admin/conference-home/email/add" class="border rounded-4 p-3 mb-4 bg-light">
                <h6 class="fw-bold">Add / Update Template</h6>
                <div class="row g-2">
                    <div class="col-md-3">
                        <input name="template_key" class="form-control" placeholder="template_key" required>
                    </div>
                    <div class="col-md-4">
                        <input name="subject_ar" class="form-control" placeholder="Arabic subject" required>
                    </div>
                    <div class="col-md-5">
                        <input name="subject_en" class="form-control" placeholder="English subject" required>
                    </div>
                    <div class="col-md-6">
                        <textarea name="body_ar" rows="4" class="form-control" placeholder="Arabic body" required></textarea>
                    </div>
                    <div class="col-md-6">
                        <textarea name="body_en" rows="4" class="form-control" placeholder="English body" required></textarea>
                    </div>
                    <div class="col-md-2">
                        <button class="btn btn-success w-100">Save New</button>
                    </div>
                </div>
            </form>

            <?php foreach ($templates as $t): ?>
                <form method="post" action="/easychair/admin/conference-home/email/save" class="border rounded-4 p-3 mb-3">
                    <input type="hidden" name="template_key" value="<?= ch_h($t['template_key'] ?: $t['event_key']) ?>">

                    <div class="d-flex justify-content-between flex-wrap gap-2 mb-2">
                        <h6 class="fw-bold">
                            <?= ch_h($t['template_key'] ?: $t['event_key']) ?>
                            <?php if (!empty($t['is_active'])): ?>
                                <span class="badge bg-success">active</span>
                            <?php else: ?>
                                <span class="badge bg-secondary">inactive</span>
                            <?php endif; ?>
                        </h6>

                        <div class="d-flex gap-2">
                            <a class="btn btn-sm btn-warning" href="/easychair/admin/conference-home/email/toggle?id=<?= (int)$t['id'] ?>">Toggle</a>
                            <a class="btn btn-sm btn-danger" onclick="return confirm('Delete template?')" href="/easychair/admin/conference-home/email/delete?id=<?= (int)$t['id'] ?>">Delete</a>
                        </div>
                    </div>

                    <div class="row g-2">
                        <div class="col-md-6">
                            <input name="subject_ar" class="form-control" value="<?= ch_h($t['subject_ar']) ?>" placeholder="Arabic subject">
                        </div>
                        <div class="col-md-6">
                            <input name="subject_en" class="form-control" value="<?= ch_h($t['subject_en']) ?>" placeholder="English subject">
                        </div>
                        <div class="col-md-6">
                            <textarea name="body_ar" rows="4" class="form-control"><?= ch_h($t['body_ar']) ?></textarea>
                        </div>
                        <div class="col-md-6">
                            <textarea name="body_en" rows="4" class="form-control"><?= ch_h($t['body_en']) ?></textarea>
                        </div>
                        <div class="col-md-2">
                            <button class="btn btn-primary w-100">Update</button>
                        </div>
                    </div>
                </form>
            <?php endforeach; ?>
        </div>
    </div>

<div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Recent Import Logs</h5>
            <table class="table table-bordered">
                <thead class="table-light">
                <tr>
                    <th>Type</th>
                    <th>File</th>
                    <th>Total</th>
                    <th>Inserted</th>
                    <th>Updated</th>
                    <th>Skipped</th>
                    <th>Date</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($logs as $log): ?>
                    <tr>
                        <td><?= ch_h($log['import_type']) ?></td>
                        <td><?= ch_h($log['filename']) ?></td>
                        <td><?= (int)$log['total_rows'] ?></td>
                        <td><?= (int)$log['inserted_rows'] ?></td>
                        <td><?= (int)$log['updated_rows'] ?></td>
                        <td><?= (int)$log['skipped_rows'] ?></td>
                        <td><?= ch_h($log['created_at']) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$logs): ?>
                    <tr><td colspan="7" class="text-center text-muted py-3">No import logs yet.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>
