<?php

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

$criteria   = $criteria ?? [];
$editing    = $editing ?? false;
$criterion  = $criterion ?? [];

?>

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

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body d-flex justify-content-between align-items-center">

            <div>
                <h4 class="fw-bold mb-1">
                    <i class="fa fa-clipboard-list"></i>
                    Review Form Criteria
                </h4>

                <div class="text-muted small">
                    Manage scoring criteria for reviewers
                </div>
            </div>

            <a href="/easychair/admin/dashboard"
               class="btn btn-outline-secondary">

                <i class="fa fa-arrow-left"></i>
                Back

            </a>

        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body">

            <h5 class="fw-bold mb-3">
                <?= $editing ? 'Edit Criterion' : 'Add Criterion' ?>
            </h5>

            <form method="post"
                  action="<?= $editing
                        ? '/easychair/admin/review-form/save'
                        : '/easychair/admin/review-form/store' ?>">

                <?php if ($editing): ?>
                    <input type="hidden"
                           name="id"
                           value="<?= (int)$criterion['id'] ?>">
                <?php endif; ?>

                <div class="row g-3">

                    <div class="col-md-4">
                        <label class="form-label">Arabic Label</label>

                        <input name="label_ar"
                               class="form-control"
                               value="<?= rf_h($criterion['label_ar'] ?? '') ?>"
                               required>
                    </div>

                    <div class="col-md-4">
                        <label class="form-label">English Label</label>

                        <input name="label_en"
                               class="form-control"
                               value="<?= rf_h($criterion['label_en'] ?? '') ?>">
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Max Score</label>

                        <input name="max_score"
                               type="number"
                               step="0.01"
                               class="form-control"
                               value="<?= rf_h($criterion['max_score'] ?? 10) ?>">
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Weight</label>

                        <input name="weight"
                               type="number"
                               step="0.01"
                               class="form-control"
                               value="<?= rf_h($criterion['weight'] ?? 1) ?>">
                    </div>

                    <div class="col-md-2">
                        <label class="form-label">Order</label>

                        <input name="sort_order"
                               type="number"
                               class="form-control"
                               value="<?= rf_h($criterion['sort_order'] ?? 1) ?>">
                    </div>

                    <div class="col-md-5">
                        <label class="form-label">Arabic Description</label>

                        <textarea name="description_ar"
                                  class="form-control"><?= rf_h($criterion['description_ar'] ?? '') ?></textarea>
                    </div>

                    <div class="col-md-5">
                        <label class="form-label">English Description</label>

                        <textarea name="description_en"
                                  class="form-control"><?= rf_h($criterion['description_en'] ?? '') ?></textarea>
                    </div>

                    <div class="col-md-2 d-flex align-items-end">

                        <button class="btn btn-primary w-100">

                            <?= $editing ? 'Update' : 'Add' ?>

                        </button>

                    </div>

                </div>

            </form>

        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">

            <h5 class="fw-bold mb-3">

                Existing Criteria

            </h5>

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

                <thead class="table-dark">

                    <tr>

                        <th>ID</th>
                        <th>Criterion</th>
                        <th>Max</th>
                        <th>Weight</th>
                        <th>Order</th>
                        <th>Status</th>
                        <th>Actions</th>

<td>
<form method="post" action="/easychair/admin/review-form/toggle">
<input type="hidden" name="id" value="<?= (int)$c["id"] ?>">
<button class="btn btn-sm <?= (int)$c["is_active"] ? "btn-success" : "btn-secondary" ?>">
<?= (int)$c["is_active"] ? "Active" : "Inactive" ?>
</button>
</form>
</td>
                    </tr>

                </thead>

                <tbody>

                <?php foreach ($criteria as $c): ?>

                    <tr>

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

                        <td>

                            <strong>
                                <?= rf_h($c['label_ar']) ?>
                            </strong>

                            <div class="text-muted small">
                                <?= rf_h($c['label_en']) ?>
                            </div>

                        </td>

                        <td><?= rf_h($c['max_score']) ?></td>
                        <td><?= rf_h($c['weight']) ?></td>
                        <td><?= (int)$c['sort_order'] ?></td>

                        <td>

                            <?php if (!empty($c['is_active'])): ?>

                                <span class="badge bg-success">
                                    Active
                                </span>

                            <?php else: ?>

                                <span class="badge bg-secondary">
                                    Inactive
                                </span>

                            <?php endif; ?>

                        </td>

                        <td>

                            <a href="/easychair/admin/review-form/edit?id=<?= (int)$c['id'] ?>"
                               class="btn btn-sm btn-primary">

                                Edit

                            </a>

                            <a href="/easychair/admin/review-form/delete?id=<?= (int)$c['id'] ?>"
                               class="btn btn-sm btn-danger"
                               onclick="return confirm('Delete?')">

                                Delete

                            </a>

                        </td>

<td>
<form method="post" action="/easychair/admin/review-form/toggle">
<input type="hidden" name="id" value="<?= (int)$c["id"] ?>">
<button class="btn btn-sm <?= (int)$c["is_active"] ? "btn-success" : "btn-secondary" ?>">
<?= (int)$c["is_active"] ? "Active" : "Inactive" ?>
</button>
</form>
</td>
                    </tr>

                <?php endforeach; ?>

                </tbody>

            </table>

        </div>
    </div>

</div>
