<?php
use Core\System\AdminAuthTheme;
use Core\System\Csrf;

$theme = AdminAuthTheme::get();

$token = $_GET['token'] ?? '';
$error = $_GET['error'] ?? '';
?>
<!doctype html>
<html lang="<?= htmlspecialchars($theme['lang']) ?>"
      dir="<?= htmlspecialchars($theme['dir']) ?>">

<head>
<meta charset="utf-8">
<title><?= __('admin.reset.new_title', 'Set New Password') ?></title>

<meta name="viewport" content="width=device-width, initial-scale=1">

<?php if (!empty($theme['font_css'])): ?>
<link rel="stylesheet" href="<?= $theme['font_css'] ?>">
<?php endif; ?>

<?php if ($theme['dir'] === 'rtl'): ?>
<link rel="stylesheet" href="/admin/assets/css/adminlte.rtl.css">
<?php else: ?>
<link rel="stylesheet" href="/admin/assets/css/adminlte.css">
<?php endif; ?>

<style>
/* =========================
   Direction
========================= */
body.rtl { direction: rtl; text-align: right; }
body.ltr { direction: ltr; text-align: left; }

body.rtl input::placeholder { direction: rtl; text-align: right; }
body.rtl input {
    direction: rtl;
    text-align: right;
}

/* =========================
   Font
========================= */
<?php if (!empty($theme['font_family'])): ?>
body,
.login-box,
.login-card-body,
input,
button,
p,
a,
h1,h2,h3,h4,h5 {
    font-family: <?= $theme['font_family'] ?> !important;
}
<?php endif; ?>

/* =========================
   Background – SVG Pattern
========================= */
body.login-page {
    min-height: 100vh;
    background-color: <?= !empty($theme['background']) ? $theme['background'] : '#f4f6f9' ?> !important;
    background-image: url("/admin/assets/img/bg-tech.svg");
    background-repeat: repeat;
    background-size: 360px 360px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 14px;
    position: relative;
}

body.login-page::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(255,255,255,0.0);
    pointer-events: none;
    z-index: 0;
}

.login-box,
.login-logo,
.card {
    position: relative;
    z-index: 1;
}

/* =========================
   Layout
========================= */
.login-box {
    width: 100%;
    max-width: 430px;
    margin: 0 !important;
}

/* =========================
   Logo
========================= */
.login-logo {
    margin-bottom: 18px !important;
}
.login-logo img {
    max-height: 70px;
}

/* =========================
   Card
========================= */
.card {
    border: 0 !important;
    border-radius: 10px !important;
    box-shadow: 0 24px 60px rgba(0,0,0,.12) !important;
}
.login-card-body {
    padding: 26px !important;
}

/* =========================
   Text
========================= */
.auth-title {
    margin: 0 0 8px 0;
    font-size: 22px;
    font-weight: 700;
}
.auth-desc {
    margin: 0 0 18px 0;
    color: #6c757d;
    font-size: 14px;
    line-height: 1.6;
}

/* =========================
   Inputs
========================= */
.form-control {
    height: 44px;
    border-radius: 6px;
}

/* =========================
   Button
========================= */
<?php if (!empty($theme['primary'])): ?>
.btn-primary {
    background-color: <?= $theme['primary'] ?> !important;
    border-color: <?= $theme['primary'] ?> !important;
}
<?php endif; ?>
.btn {
    height: 44px;
    border-radius: 6px;
    font-weight: 700;
}
</style>
</head>

<body class="login-page <?= $theme['dir'] === 'rtl' ? 'rtl' : 'ltr' ?>">

<div class="login-box">

    <div class="login-logo text-center">
        <?php if (!empty($theme['logo'])): ?>
            <img src="<?= htmlspecialchars($theme['logo']) ?>" alt="Logo">
        <?php else: ?>
            <b>MTEngine</b>
        <?php endif; ?>
    </div>

    <div class="card">
        <div class="card-body login-card-body">

            <?php if ($error === 'invalid'): ?>
                <div class="alert alert-danger">
                    <?= __('admin.reset.invalid', 'Invalid or expired reset link.') ?>
                </div>
            <?php elseif ($error === 'weak'): ?>
                <div class="alert alert-warning">
                    <?= __('admin.reset.weak', 'Password must be at least 8 characters.') ?>
                </div>
            <?php endif; ?>

            <div class="auth-title">
                <?= __('admin.reset.new_heading', 'Choose a new password') ?>
            </div>

            <div class="auth-desc">
                <?= __('admin.reset.new_desc', 'Please enter a strong new password for your account.') ?>
            </div>

            <form method="post" action="/admin/reset-password">
                <?= Csrf::field(); ?>

                <input type="hidden"
                       name="token"
                       value="<?= htmlspecialchars($token, ENT_QUOTES, 'UTF-8') ?>">

                <div class="mb-3">
                    <input type="password"
                           name="password"
                           class="form-control"
                           placeholder="<?= __('admin.reset.new_password', 'New password') ?>"
                           minlength="8"
                           required>
                </div>

                <button type="submit" class="btn btn-primary w-100">
                    <?= __('admin.reset.save', 'Reset password') ?>
                </button>
            </form>

        </div>
    </div>

</div>

</body>
</html>
