<?php
function cert_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function cert_t($key, $fallback) {
    if (function_exists('ec_t')) return ec_t($key, $fallback);
    if (function_exists('t')) return t($key, $fallback);
    return $fallback;
}
$certificates = $certificates ?? [];
$eligible = $eligible ?? [];
$success = $_SESSION['flash_success'] ?? '';
$error = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_error']);
?>

<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 p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-certificate"></i>
                <?= cert_h(cert_t('admin.certificates.title','Certificates')) ?>
            </h3>
            <div class="opacity-75">
                <?= cert_h(cert_t('admin.certificates.generate','Generate certificates for authors, reviewers, chairs, TPC and committees.')) ?>
            </div>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= cert_h($success) ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= cert_h($error) ?></div>
    <?php endif; ?>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3"><?= cert_h(cert_t('admin.certificates.generate','Generate Certificate')) ?></h5>

            <form method="post" action="/easychair/admin/certificates/generate" class="row g-3 align-items-end">
                <div class="col-md-5">
                    <label class="form-label fw-bold"><?= cert_h(cert_t('submission.title','Submission')) ?></label>
                    <select name="submission_id" class="form-select">
                        <option value="0">--</option>
                        <?php foreach ($eligible as $e): ?>
                            <option value="<?= (int)$e['submission_id'] ?>"
                                    data-user="<?= (int)$e['user_id'] ?>">
                                <?= cert_h(($e['submission_no'] ?? '') . ' - ' . (($e['title'] ?? '') ?: ($e['title_ar'] ?? ''))) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold"><?= cert_h(cert_t('admin.certificates.recipient','Recipient')) ?></label>
                    <select name="user_id" class="form-select" required>
                        <option value="">--</option>
                        <?php foreach ($eligible as $e): ?>
                            <option value="<?= (int)$e['user_id'] ?>">
                                <?= cert_h(trim(($e['first_name'] ?? '') . ' ' . ($e['last_name'] ?? '')) ?: ($e['email'] ?? '')) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-2">
                    <label class="form-label fw-bold"><?= cert_h(cert_t('admin.certificates.certificate_type','Certificate Type')) ?></label>
                    <select name="certificate_type" class="form-select">
                        <option value="author"><?= cert_h(cert_t('admin.certificates.author','Author Certificate')) ?></option>
                        <option value="reviewer"><?= cert_h(cert_t('admin.certificates.reviewer','Reviewer Certificate')) ?></option>
                        <option value="chair"><?= cert_h(cert_t('admin.certificates.chair','Chair Certificate')) ?></option>
                        <option value="tpc"><?= cert_h(cert_t('admin.certificates.tpc','TPC Certificate')) ?></option>
                        <option value="committee">Committee</option>
                    </select>
                </div>

                <div class="col-md-2">
                    <button class="btn btn-success w-100 rounded-pill">
                        <i class="fa fa-certificate"></i>
                        <?= cert_h(cert_t('action.create','Create')) ?>
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3"><?= cert_h(cert_t('admin.certificates.title','Certificates')) ?></h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th><?= cert_h(cert_t('admin.certificates.recipient','Recipient')) ?></th>
                        <th><?= cert_h(cert_t('admin.certificates.certificate_type','Certificate Type')) ?></th>
                        <th><?= cert_h(cert_t('submission.no','Submission No.')) ?></th>
                        <th><?= cert_h(cert_t('admin.certificates.issue_date','Issue Date')) ?></th>
                        <th><?= cert_h(cert_t('submission.status','Status')) ?></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($certificates as $c): ?>
                        <tr>
                            <td><?= (int)$c['id'] ?></td>
                            <td>
                                <strong><?= cert_h(trim(($c['first_name'] ?? '') . ' ' . ($c['last_name'] ?? '')) ?: ($c['email'] ?? '')) ?></strong>
                                <div class="text-muted small"><?= cert_h($c['email'] ?? '') ?></div>
                            </td>
                            <td><span class="badge bg-primary"><?= cert_h($c['certificate_type'] ?? '') ?></span></td>
                            <td><?= cert_h($c['submission_no'] ?? '') ?></td>
                            <td><?= cert_h($c['issued_at'] ?? $c['created_at'] ?? '') ?></td>
                            <td><span class="badge bg-success"><?= cert_h($c['status'] ?? 'issued') ?></span></td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$certificates): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">
                                <?= cert_h(cert_t('msg.no_records','No records found.')) ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
