<?php
session_start();
require_once "config/database.php";

$database = new Database();
$db = $database->getConnection();

if (!$db) {
    die("Database connection failed.");
}

$message = "";
$message_type = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $full_name = trim($_POST['full_name'] ?? '');
    $username  = trim($_POST['username'] ?? '');
    $password  = trim($_POST['password'] ?? '');
    $email     = trim($_POST['email'] ?? '');
    $tel       = trim($_POST['tel'] ?? '');

    if ($full_name === '' || $username === '' || $password === '' || $email === '') {
        $message = "Please fill all required fields.";
        $message_type = "error";
    } else {
        $check = $db->prepare("SELECT id FROM users WHERE username = :username OR email = :email LIMIT 1");
        $check->bindParam(":username", $username);
        $check->bindParam(":email", $email);
        $check->execute();

        if ($check->fetch(PDO::FETCH_ASSOC)) {
            $message = "Username or email already exists.";
            $message_type = "error";
        } else {
            $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

            $stmt = $db->prepare("
                INSERT INTO users (full_name, username, password, email, tel, role, status)
                VALUES (:full_name, :username, :password, :email, :tel, 'user', 'active')
            ");

            $stmt->bindParam(":full_name", $full_name);
            $stmt->bindParam(":username", $username);
            $stmt->bindParam(":password", $hashedPassword);
            $stmt->bindParam(":email", $email);
            $stmt->bindParam(":tel", $tel);

            if ($stmt->execute()) {
                $message = "Registration successful. You can login now.";
                $message_type = "success";
            } else {
                $message = "Registration failed.";
                $message_type = "error";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - DocuTrack</title>
    <link rel="stylesheet" href="assets/css/app.css">
    <style>
        body {
            margin: 0;
            background: linear-gradient(135deg, #f4f7fb 0%, #e9eef6 100%);
            font-family: Arial, sans-serif;
        }

        .auth-page {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 24px;
        }

        .auth-wrap {
            width: 100%;
            max-width: 980px;
            display: grid;
            grid-template-columns: 1fr 420px;
            background: #fff;
            border-radius: 24px;
            overflow: hidden;
            box-shadow: 0 20px 50px rgba(16, 24, 40, 0.12);
        }

        .auth-left {
            background: linear-gradient(135deg, #1d4ed8, #2563eb);
            color: #fff;
            padding: 48px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .auth-left .logo {
            font-size: 32px;
            font-weight: 800;
            margin-bottom: 24px;
        }

        .auth-left .logo span {
            color: #bfdbfe;
        }

        .auth-left h1 {
            font-size: 36px;
            margin: 0 0 16px;
            line-height: 1.2;
        }

        .auth-left p {
            margin: 0;
            font-size: 16px;
            line-height: 1.7;
            color: rgba(255,255,255,0.9);
        }

        .auth-right {
            padding: 40px 32px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #ffffff;
        }

        .register-card {
            width: 100%;
        }

        .register-card .section-title {
            font-size: 28px;
            margin-bottom: 8px;
        }

        .register-card .section-subtext {
            margin-bottom: 24px;
            color: #667085;
        }

        .form-group {
            margin-bottom: 18px;
        }

        .form-group label {
            display: block;
            font-weight: 600;
            margin-bottom: 8px;
            color: #344054;
        }

        .form-group input {
            width: 100%;
            box-sizing: border-box;
            padding: 12px 14px;
            border: 1px solid #d0d5dd;
            border-radius: 12px;
            font-size: 15px;
            background: #fff;
        }

        .form-group input:focus {
            outline: none;
            border-color: #2563eb;
            box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
        }

        .alert-box {
            margin-bottom: 18px;
            padding: 12px 14px;
            border-radius: 12px;
            font-size: 14px;
        }

        .alert-danger {
            background: #fef3f2;
            color: #b42318;
            border: 1px solid #fecdca;
        }

        .alert-success {
            background: #ecfdf3;
            color: #166534;
            border: 1px solid #abefc6;
        }

        .auth-actions {
            display: flex;
            flex-direction: column;
            gap: 12px;
            margin-top: 8px;
        }

        .auth-actions .btn {
            width: 100%;
            text-align: center;
            text-decoration: none;
            box-sizing: border-box;
        }

        .auth-links {
            margin-top: 20px;
            text-align: center;
            font-size: 14px;
            color: #667085;
        }

        .auth-links a {
            color: #2563eb;
            text-decoration: none;
            font-weight: 600;
        }

        .logo {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }

        .logo-img {
            width: 60px;
            height: auto;
            margin-bottom: 10px;
        }
        .logo {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }

        .logo-box {
            background: #ffffff;
            padding: 10px 14px;
            border-radius: 10px;
            margin-bottom: 12px;
            display: inline-block;
        }

        .logo-box img {
            width: 60px;
            height: auto;
            display: block;
        }

        @media (max-width: 900px) {
            .auth-wrap {
                grid-template-columns: 1fr;
            }

            .auth-left {
                padding: 32px 24px;
            }

            .auth-right {
                padding: 32px 24px;
            }
        }

    </style>
</head>
<body>
    <div class="auth-page">
        <div class="auth-wrap">
            <div class="auth-left">
                <div class="logo">
                    <div class="logo-box">
                        <img src="assets/images/logo.png" alt="Logo">
                    </div>
                    <div>Docu<span>Track</span></div>
                </div>
                
                <h1>Create your account</h1>
                <p>
                    Register to manage your documents, contracts, reminders,
                    and vehicle records in one secure and simple system.
                </p>
            </div>

            <div class="auth-right">
                <div class="register-card">
                    <div class="section-title">Register</div>
                    <div class="section-subtext">Create a new account to get started.</div>

                    <?php if ($message !== ""): ?>
                        <div class="alert-box <?php echo $message_type === 'success' ? 'alert-success' : 'alert-danger'; ?>">
                            <?php echo htmlspecialchars($message); ?>
                        </div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="form-group">
                            <label for="full_name">Full Name</label>
                            <input
                                type="text"
                                name="full_name"
                                id="full_name"
                                value="<?php echo htmlspecialchars($_POST['full_name'] ?? ''); ?>"
                                placeholder="Enter full name"
                                required
                            >
                        </div>

                        <div class="form-group">
                            <label for="username">Username</label>
                            <input
                                type="text"
                                name="username"
                                id="username"
                                value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"
                                placeholder="Enter username"
                                required
                            >
                        </div>

                        <div class="form-group">
                            <label for="password">Password</label>
                            <input
                                type="password"
                                name="password"
                                id="password"
                                placeholder="Enter password"
                                required
                            >
                        </div>

                        <div class="form-group">
                            <label for="email">Email</label>
                            <input
                                type="email"
                                name="email"
                                id="email"
                                value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>"
                                placeholder="Enter email address"
                                required
                            >
                        </div>

                        <div class="form-group">
                            <label for="tel">Tel</label>
                            <input
                                type="text"
                                name="tel"
                                id="tel"
                                value="<?php echo htmlspecialchars($_POST['tel'] ?? ''); ?>"
                                placeholder="Enter phone number"
                            >
                        </div>

                        <div class="auth-actions">
                            <button type="submit" class="btn btn-primary">Register</button>
                            <a href="login.php" class="btn btn-secondary">Back to Login</a>
                        </div>
                    </form>

                    <div class="auth-links">
                        Already have an account?
                        <a href="login.php">Login</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>