<?php
require_once 'db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) || empty($password)) {
        echo "<script>";
        echo "alert('Username dan password tidak boleh kosong!')";
        echo "</script>";
        exit();
    }

    $sql = "SELECT * FROM Account WHERE username = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        echo "<script>";
        echo "alert('Username sudah terdaftar. Silakan pilih username lain.')";
        echo "</script>";
    } else {
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);

        $sql = "INSERT INTO Account (username, password) VALUES (?, ?)";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("ss", $username, $hashed_password);

        if ($stmt->execute()) {
            echo "Registrasi berhasil!";
            header("Location: index.php");
            echo "<script>";
            echo "window.location.href = '/';";
            echo "</script>";
            exit();
        } else {
            echo "Error: " . $stmt->error;
        }
    }

    $stmt->close();
}
?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <meta name="description" content="" />
    <meta name="author" content="" />

    <title>Infra Track - Registrasi</title>

    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css" />
    <link
      href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
      rel="stylesheet" />

    <link href="css/style.min.css" rel="stylesheet" />
  </head>

  <body class="bg-gradient-primary">
    <div class="container">
      <div class="d-flex justify-content-center align-items-center">
        <div class="col-xl-10 col-lg-12 col-md-9">
          <div class="card o-hidden border-0 shadow-lg my-5">
            <div class="card-body p-0">
              <div class="p-5">
                <div class="text-center">
                  <h1 class="h4 text-gray-900 mb-4">Selamat Datang</h1>
                </div>
                <form class="user" method="post" action="register.php">
                  <div class="form-group">
                    <input
                      type="text"
                      class="form-control form-control-user"
                      id="username"
                      name="username"
                      placeholder="Masukan username Anda... (Contoh: habibilham)"
                      required />
                  </div>
                  <div class="form-group">
                    <input
                      type="password"
                      class="form-control form-control-user"
                      id="password"
                      name="password"
                      placeholder="Masukan kata sandi Anda..."
                      required />
                  </div>
                  <button type="submit" class="btn btn-primary btn-user btn-block">Daftar</button>
                </form>
                <hr />
                <div class="text-center">
                  <a class="small" href="login.php">Saya sudah punya akun</a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <script src="js/script.min.js"></script>
  </body>
</html>
