<?php
require_once 'includes/auth.php';

$auth = new Auth();

$error = '';
$success = '';

if ($auth->isLoggedIn()) {
    header("Location: user/dashboard.php");
    exit();
}

// Check for referral code in URL
$referral_code = isset($_GET['ref']) ? $_GET['ref'] : '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $result = $auth->register($_POST);
    if ($result['success']) {
        $success = $result['message'];
        // Redirect after 2 seconds
        header("refresh:2;url=user/dashboard.php");
    } else {
        $error = $result['message'];
    }
}

$page_title = "Register";
include 'includes/header.php';
?>

<link rel="stylesheet" href="home/style/style_log.css">

<style>
    .logincontainer {
        margin: 40px auto 100px auto;
        padding: 0 10px;
        max-width: 500px
    }
</style>

<script>
function checkform() {
    var f = document.regform;
    if (!f) return true;
    if (f.fullname && f.fullname.value.trim()=='') { alert('Please enter your full name'); f.fullname.focus(); return false; }
    if (f.username && f.username.value.trim()=='') { alert('Please enter your username'); f.username.focus(); return false; }
    if (f.password && f.password.value=='') { alert('Please enter your password'); f.password.focus(); return false; }
    if (f.email && f.email.value.trim()=='') { alert('Please enter your email'); f.email.focus(); return false; }
    return true;
}
</script>

<section id="main-body">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-lg-6">
        <div class="bg-white p-5 rounded-lg shadow">

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

<?php if (defined('SHOW_DEBUG_ERRORS') && SHOW_DEBUG_ERRORS): ?>
  <div class="alert alert-info small">
    <strong>Debug info</strong><br>
    Session ID: <?php echo htmlspecialchars(session_id() ?: '(none)'); ?><br>
    Check server log: <code>error_log</code> in the project root.
  </div>
<?php endif; ?>
<?php if (!empty($success)): ?>
  <div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div>
<?php endif; ?>

          <h2 class="text-warning mb-4">Create an account</h2>

          <form method="post" onsubmit="return checkform()" name="regform">
            <input type="hidden" name="action" value="signup">

            <div class="form-group mb-3">
              <label class="form-label">Full Name</label>
              <input type="text" name="fullname" value="<?php echo isset($_POST['fullname']) ? htmlspecialchars($_POST['fullname'], ENT_QUOTES) : ''; ?>" class="form-control">
            </div>

            <div class="form-group mb-3">
              <label class="form-label">Username</label>
              <input type="text" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username'], ENT_QUOTES) : ''; ?>" class="form-control">
            </div>

            <div class="row">
              <div class="col-md-6 mb-3">
                <label class="form-label">Password</label>
                <input type="password" name="password" class="form-control">
              </div>
              <div class="col-md-6 mb-3">
                <label class="form-label">Retype Password</label>
                <input type="password" name="password2" class="form-control">
              </div>
            </div>

            <div class="form-group mb-3">
              <label class="form-label">E-mail</label>
              <input type="email" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : ''; ?>" class="form-control">
            </div>

            <div class="form-row mb-3">
              <div class="col-md-6 mb-3">
                <label class="form-label">Secret question</label>
                <input type="text" name="sq" value="<?php echo isset($_POST['sq']) ? htmlspecialchars($_POST['sq'], ENT_QUOTES) : ''; ?>" class="form-control">
              </div>
              <div class="col-md-6 mb-3">
                <label class="form-label">Secret answer</label>
                <input type="text" name="sa" value="<?php echo isset($_POST['sa']) ? htmlspecialchars($_POST['sa'], ENT_QUOTES) : ''; ?>" class="form-control">
              </div>
            </div>

            <?php // include 'includes/captcha.php'; ?>
            <?php if (!empty($referral_code)): ?>
              <input type="hidden" name="ref" value="<?php echo htmlspecialchars($referral_code); ?>">
            <?php endif; ?>

            <div class="form-check mb-3">
              <input class="form-check-input" type="checkbox" name="agree" value="1" id="agree" <?php echo isset($_POST['agree']) ? 'checked' : ''; ?>>
              <label class="form-check-label" for="agree">I agree with <a href="?a=rules">Terms and conditions</a></label>
            </div>

            <div class="d-flex justify-content-between align-items-center">
              <a href="login" class="text-decoration-none">Already have an account? Sign-In</a>
              <button type="submit" class="btn btn-warning jj">Register</button>
            </div>

          </form>

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

<?php include 'includes/footerm.php'; ?>
