<?php
/* REGISTER_CONFERENCE_CONTEXT_PACK801C */
$registerConference = null;

try {
    $key = trim($_GET['conference_id'] ?? ($_SESSION['submit_slug'] ?? ''));

    if ($key !== '') {
        $db = \App\Core\Database::getInstance();

        $stmt = $db->prepare("
            SELECT id, org_id, slug, short_name, title_en, title_ar, logo_path, banner_path
            FROM conferences
            WHERE slug = ?
               OR short_name = ?
               OR id = ?
            LIMIT 1
        ");

        $stmt->execute([
            $key,
            $key,
            ctype_digit($key) ? (int)$key : 0
        ]);

        $registerConference = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($registerConference) {
            $_SESSION['submit_conference_id'] = (int)$registerConference['id'];
            $_SESSION['conference_id'] = (int)$registerConference['id'];
            $_SESSION['org_id'] = (int)$registerConference['org_id'];
            $_SESSION['submit_slug'] = $registerConference['slug'] ?: $key;
        }
    }
} catch (Throwable $e) {
    $registerConference = null;
}
?>

<?php if (!empty($registerConference)): ?>
<div style="max-width:1100px;margin:24px auto 10px;padding:18px 24px;border-radius:24px;background:linear-gradient(135deg,#07112d,#1d4ed8);color:#fff;box-shadow:0 16px 45px rgba(15,23,42,.18);display:flex;gap:18px;align-items:center;">
    <?php if (!empty($registerConference['logo_path'])): ?>
        <img src="/easychair/<?= htmlspecialchars(ltrim($registerConference['logo_path'],'/'),ENT_QUOTES,'UTF-8') ?>" style="width:70px;height:70px;object-fit:contain;border-radius:18px;background:#fff;padding:6px;" onerror="this.style.display='none';">
    <?php endif; ?>

    <div>
        <div style="font-size:13px;opacity:.8;">Registering for selected conference

<?php
$__logo='';

if(!empty($registerConference['logo_path'])){

$__logo=
strpos($registerConference['logo_path'],'http')===0
?
$registerConference['logo_path']
:
'/easychair/public/'
.
ltrim(
$registerConference['logo_path'],
'/'
);

}
?>

<?php if($__logo): ?>

<img
class="register_conf_logo_pack805"
src="<?= htmlspecialchars($__logo,ENT_QUOTES,'UTF-8') ?>"
style="
width:72px;
height:72px;
object-fit:contain;
background:#fff;
padding:8px;
border-radius:18px;
box-shadow:0 8px 20px rgba(0,0,0,.15);
"
onerror="this.style.display='none';"
/>

<?php endif; ?>
</div>
        <div style="font-size:24px;font-weight:800;">
            <?= htmlspecialchars($registerConference['short_name'] ?: ($registerConference['title_en'] ?? 'Conference'),ENT_QUOTES,'UTF-8') ?>
        </div>
        <div style="opacity:.85;">
            <?= htmlspecialchars($registerConference['title_en'] ?? $registerConference['title_ar'] ?? '',ENT_QUOTES,'UTF-8') ?>
        </div>
    </div>
</div>
<?php endif; ?>

<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

require_once BASE_PATH . '/app/helpers/easychair_translate.php';

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

/*
------------------------------------------------------------
AUTO-DETECT CONFERENCE CONTEXT
------------------------------------------------------------
*/

$conference_id =
    $_SESSION['submit_conference_id']
    ?? $_POST['conference_id']
    ?? $_GET['conference_id']
    ?? null;

$preselectedConference =
    $preselectedConference
    ?? null;

$conferences =
    $conferences
    ?? [];

$error =
    $_SESSION['error']
    ?? '';

unset($_SESSION['error']);

/*
If only ID exists — build minimal object
*/

if (!$preselectedConference && $conference_id) {

    $preselectedConference = [
        'id' => (int)$conference_id,
        'short_name' => (($_SESSION['lang']??'ar')==='ar' ? ($registerConference['title_ar'] ?? $registerConference['title_en'] ?? $registerConference['short_name'] ?? '') : ($registerConference['title_en'] ?? $registerConference['title_ar'] ?? $registerConference['short_name'] ?? '')),
        'title_en' => '',
        'title_ar' => '',
        'org_name' => ''
    ];

}
?>

<!DOCTYPE html>
<html
    lang="<?= ec_lang() ?>"
    dir="<?= ec_dir() ?>">

<head>

<meta charset="UTF-8">

<title>Author Registration</title>

<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700;900&display=swap" rel="stylesheet">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">

<style>

body{
    font-family:Cairo,sans-serif;
    min-height:100vh;
    background:linear-gradient(135deg,#0f172a,#1d4ed8);
    padding:35px 12px;
}

.reg-card{
    max-width:980px;
    margin:auto;
    background:#fff;
    border-radius:28px;
    box-shadow:0 25px 70px rgba(0,0,0,.25);
    overflow:hidden;
}

.reg-head{
    background:linear-gradient(135deg,#0f172a,#2563eb);
    color:#fff;
    padding:34px;
}

.form-control,
.form-select{
    border-radius:14px;
    padding:12px 14px;
}

.btn-main{
    border:0;
    border-radius:18px;
    padding:13px 18px;
    font-weight:900;
    background:linear-gradient(135deg,#16a34a,#22c55e);
    color:#fff;
}

.required:after{
    content:" *";
    color:#dc2626;
}

</style>

</head>

<body>

<div class="reg-card">

<div class="reg-head">

<h2 class="fw-bold mb-1">
<title>
<?= t(
    'auth.author_registration',
    'تسجيل مؤلف جديد'
) ?>
</title>
</h2>

<div class="opacity-75">
<?= t(
    'auth.register_conf_note',
    'Your account will automatically be linked to the conference from which you started submission.'
) ?>
</div>

</div>

<div class="p-4 p-lg-5">

<?php if ($error): ?>

<div class="alert alert-danger">
<?= rg_h($error) ?>
</div>

<?php endif; ?>

<form method="post"
      action="/easychair/register"
      class="row g-3">

<div class="col-md-6">

<label class="form-label fw-bold required">
<?= t(
    'auth.first_name',
    'الاسم الثلاثي'
) ?>
</label>

<input name="first_name"
       class="form-control"
       required>

</div>

<div class="col-md-6">

<label class="form-label fw-bold required">
<?= t(
    'auth.last_name',
    'اسم العائلة'
) ?>
</label>

<input name="last_name"
       class="form-control"
       required>

</div>

<div class="col-md-6">

<label class="form-label fw-bold required">

<?= t(
    'auth.email',
    'Email'
) ?>
</label>
</label>

<input type="email"
       name="email"
       class="form-control"
       required>

</div>

<div class="col-md-6">

<label class="form-label fw-bold required">
<?= t(
    'auth.phone',
    'Phone / Mobile'
) ?>
</label>

<input name="phone"
       class="form-control" style="direction:ltr; text-align:left"
       required
       placeholder="+962779830983">

</div>

<div class="col-md-12">

<label class="form-label fw-bold">
<?= t(
    'auth.affiliation',
    'University / Affiliation'
) ?>
</label>

<input name="affiliation"
       class="form-control">

</div>

<div class="col-md-12">

<label class="form-label fw-bold required">
<?= t(
    'auth.conference',
    'المؤتمر'
) ?>
</label>

<?php if (!empty($preselectedConference)): ?>

<input type="hidden"
       name="conference_id"
       value="<?= (int)$preselectedConference['id'] ?>">

<div class="alert alert-primary mb-0">

<strong>
<?= rg_h(
    $preselectedConference['org_name']
    ?: ((($_SESSION['lang']??'ar')==='ar')?'المؤتمر المختار':'Selected Conference')
) ?>
</strong>

<br>

<?= rg_h(
    $preselectedConference['title_ar']
    ?: $preselectedConference['title_en']
    ?: $preselectedConference['short_name']
) ?>

</div>

<?php else: ?>

<select name="conference_id"
        class="form-select"
        required>

<option value="">
-- Select conference --
</option>

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

<option value="<?= (int)$c['id'] ?>">

<?= rg_h(
    ($c['org_name'] ?? '')
    . ' | '
    . ($c['short_name'] ?? '')
) ?>

</option>

<?php endforeach; ?>

</select>

<?php endif; ?>

</div>

<div class="col-md-6">

<label class="form-label fw-bold required">
<?= t(
    'auth.password',
    'كلمة المرور'
) ?>
</label>

<input type="password"
       name="password"
       class="form-control"
       required
       minlength="8">

</div>

<div class="col-md-6">

<label class="form-label fw-bold required">
<?= t(
    'auth.confirm_password',
    'Confirm Password'
) ?>
</label>

<input type="password"
       name="password_confirm"
       class="form-control"
       required
       minlength="8">

</div>

<div class="col-12 pt-2">

<button class="btn-main w-100">
<?= t(
    'auth.confirm_password',
    'Confirm Password'
) ?>
</button>

</div>

<div class="col-12 text-center">

<a href="/easychair/login">
<?= t(
    'auth.already_have_account',
    'Have Account? / Register'
) ?>
</a>

</div>

</form>

</div>

</div>

</body>

</html>
