<?php
$conference = $conference ?? [];
$settings = $settings ?? [];
$workflowStages = $workflowStages ?? [];

function wf_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$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:22px;">
        <div class="card-body d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <h3 class="mb-1 fw-bold">إدارة سير العمل</h3>
                <div class="text-muted"><?= wf_h($title) ?></div>
            </div>

            <a href="/easychair/conference/home?id=<?= (int)($conference['id'] ?? 0) ?>" target="_blank" class="btn btn-outline-primary">
                عرض الصفحة الذكية للمؤتمر
            </a>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:18px;">
                <div class="card-body">
                    <div class="text-muted">عدد جولات التحكيم</div>
                    <div class="display-6 fw-bold"><?= wf_h($settings['review_rounds'] ?? 1) ?></div>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:18px;">
                <div class="card-body">
                    <div class="text-muted">إتاحة التعديلات</div>
                    <div class="mt-2">
                        <?php if (!empty($settings['allow_revision'])): ?>
                            <span class="badge bg-success p-2">مفعلة</span>
                        <?php else: ?>
                            <span class="badge bg-secondary p-2">معطلة</span>
                        <?php endif; ?>
                    </div>
                    <a class="btn btn-sm btn-outline-dark mt-3" href="/easychair/admin/workflow/toggle?type=revision">
                        تبديل الحالة
                    </a>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:18px;">
                <div class="card-body">
                    <div class="text-muted">القرار الآلي</div>
                    <div class="mt-2">
                        <?php if (!empty($settings['auto_decision'])): ?>
                            <span class="badge bg-success p-2">مفعل</span>
                        <?php else: ?>
                            <span class="badge bg-secondary p-2">معطل</span>
                        <?php endif; ?>
                    </div>
                    <a class="btn btn-sm btn-outline-dark mt-3" href="/easychair/admin/workflow/toggle?type=auto_decision">
                        تبديل الحالة
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body">
            <h5 class="fw-bold mb-3">مراحل سير العمل</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>المرحلة</th>
                            <th>الحالة</th>
                            <th>إجراء</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($workflowStages as $i => $stage): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td class="fw-bold"><?= wf_h($stage['stage'] ?? '') ?></td>
                            <td>
                                <?php if (!empty($stage['is_enabled'])): ?>
                                    <span class="badge bg-success">مفعلة</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">معطلة</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a class="btn btn-sm btn-outline-primary"
                                   href="/easychair/admin/workflow/toggle?type=stage&id=<?= (int)$stage['id'] ?>">
                                    تبديل
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$workflowStages): ?>
                        <tr>
                            <td colspan="4" class="text-center text-muted py-4">
                                لا توجد مراحل معرفة لهذا المؤتمر.
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>
