<?php
require_once '../config/db.php';
require_once '../config/auth.php';
require_once '../config/functions.php';

if (isLoggedIn()) {
    header("Location: /lostfound_relink/user/dashboard.php");
    exit;
}

$error   = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name     = sanitize($conn, $_POST['name']);
    $email    = sanitize($conn, $_POST['email']);
    $password = $_POST['password'];
    $confirm  = $_POST['confirm_password'];
    $gender   = sanitize($conn, $_POST['gender']);
    $phone    = sanitize($conn, $_POST['phone']);

    if (strlen($name) < 2) {
        $error = 'Name must be at least 2 characters.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Invalid email address.';
    } elseif (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters.';
    } elseif ($password !== $confirm) {
        $error = 'Passwords do not match.';
    } else {
        $check = $conn->query("SELECT id FROM users WHERE email = '$email'");
        if ($check->num_rows > 0) {
            $error = 'Email already registered.';
        } else {
            $hashed = password_hash($password, PASSWORD_DEFAULT);
            $conn->query("INSERT INTO users (name, email, password, gender, phone, account_status) VALUES ('$name','$email','$hashed','$gender','$phone','pending')");
            $success = 'Registration submitted! Your account is pending admin approval. You will be notified once activated.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - RELINK</title>
<link rel="stylesheet" href="/lostfound_relink/assets/css/style.css?v=2">
</head>
<body>
<div class="auth-wrapper auth-bg">
<div class="auth-card" style="max-width:520px;">
        <div class="auth-logo">
            <div style="display:flex;align-items:center;justify-content:center;gap:12px;margin-bottom:12px">
                <img src="/lostfound_relink/cyn_logo.png" alt="Cauayan City Hall" style="width:52px;height:52px;object-fit:contain;flex-shrink:0">
                <div style="text-align:left">
                    <h1 style="margin:0">🔗 RELINK</h1>
                    <p style="margin:0;font-size:12px">Lost & Found Management System</p>
                    <p style="margin:0;font-size:11px;color:rgba(255,255,255,.55)">Cauayan City Hall</p>
                </div>
            </div>
        </div>
        <?php if($error): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?>
        <?php if($success): ?><div class="alert alert-success"><?= $success ?></div><?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label>Full Name</label>
                <input type="text" name="name" class="form-control" placeholder="Enter your full name" required
                       value="<?= htmlspecialchars($_POST['name'] ?? '') ?>">
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Email Address</label>
                    <input type="email" name="email" class="form-control" placeholder="Email" required
                           value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
                </div>
                <div class="form-group">
                    <label>Phone Number</label>
                    <input type="text" name="phone" class="form-control" placeholder="09XXXXXXXXX"
                           value="<?= htmlspecialchars($_POST['phone'] ?? '') ?>">
                </div>
            </div>
            <div class="form-group">
                <label>Gender</label>
                <select name="gender" class="form-control" required>
                    <option value="">Select gender</option>
                    <option value="male"   <?= ($_POST['gender']??'')==='male'  ?'selected':'' ?>>Male</option>
                    <option value="female" <?= ($_POST['gender']??'')==='female'?'selected':'' ?>>Female</option>
                </select>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Min 6 characters" required>
                </div>
                <div class="form-group">
                    <label>Confirm Password</label>
                    <input type="password" name="confirm_password" class="form-control" placeholder="Repeat password" required>
                </div>
            </div>
            <button type="submit" class="btn btn-primary btn-block">Create Account</button>
        </form>
        <p style="text-align:center;margin-top:16px;font-size:13px;color:#fff">
            Already have an account? <a href="/lostfound_relink/auth/login.php" style="color:#d4f06a;font-weight:600">Sign In</a>
        </p>
    </div>
</div>
</body>
</html>
