<?php

include 'config/koneksi.php';

if(isset($_POST['register'])){

    $nama =
    mysqli_real_escape_string(
    $conn,
    $_POST['nama']
    );

    $username =
    mysqli_real_escape_string(
    $conn,
    $_POST['username']
    );
    $password = md5($_POST['password']);

    $cek = mysqli_query($conn,
    "SELECT * FROM users WHERE username='$username'");

    if(mysqli_num_rows($cek) > 0){

        echo "<script>
        alert('Username sudah digunakan');
        window.location='register.php';
        </script>";

        exit;
    }

    mysqli_query($conn,

    "INSERT INTO users
    (nama,username,password,role)

    VALUES

    (
    '$nama',
    '$username',
    '$password',
    'warga'
    )"

    );

    header("Location: index.php");
    }

?>

<!DOCTYPE html>
<html>

<head>

    <title>Register Warga</title>

    <script src="https://cdn.tailwindcss.com"></script>

</head>

<?php

include 'config/koneksi.php';

if(isset($_POST['register'])){

    $nama = mysqli_real_escape_string(
        $conn,
        trim($_POST['nama'])
    );

    $username = mysqli_real_escape_string(
        $conn,
        trim($_POST['username'])
    );

    $password = $_POST['password'];
    $konfirmasi = $_POST['konfirmasi_password'];

    if(strlen($password) < 6){

        echo "
        <script>
        alert('Password minimal 6 karakter');
        window.location='register.php';
        </script>
        ";

        exit;
    }

    if($password != $konfirmasi){

        echo "
        <script>
        alert('Konfirmasi password tidak sesuai');
        window.location='register.php';
        </script>
        ";

        exit;
    }

    $cek = mysqli_query(
        $conn,
        "SELECT * FROM users WHERE username='$username'"
    );

    if(mysqli_num_rows($cek) > 0){

        echo "
        <script>
        alert('Username sudah digunakan');
        window.location='register.php';
        </script>
        ";

        exit;
    }

    $password_hash = password_hash(
        $password,
        PASSWORD_DEFAULT
    );

    mysqli_query(
        $conn,
        "INSERT INTO users
        (
            nama,
            username,
            password,
            role
        )
        VALUES
        (
            '$nama',
            '$username',
            '$password_hash',
            'warga'
        )"
    );

    echo "
    <script>
    alert('Registrasi berhasil');
    window.location='index.php';
    </script>
    ";

    exit;
}

?>

<!DOCTYPE html>

<html>

<head>


    <title>Register Warga</title>

    <meta charset="utf-8">

    <script src="https://cdn.tailwindcss.com"></script>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">


</head>

<body
    class="min-h-screen bg-gradient-to-br from-blue-600 via-blue-500 to-cyan-500 flex items-center justify-center p-5">


    <div class="w-full max-w-md">

        <div class="bg-white rounded-3xl shadow-2xl overflow-hidden">

            <div class="bg-blue-600 text-white p-8 text-center">

                <i class="fas fa-user-plus text-5xl mb-4"></i>

                <h1 class="text-3xl font-bold">
                    Register Warga
                </h1>

                <p class="mt-2 text-blue-100">
                    Buat akun untuk mengirim laporan kemiskinan
                </p>

            </div>

            <form method="POST" class="p-8 space-y-5">

                <div>

                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        Nama Lengkap
                    </label>

                    <input type="text" name="nama" required placeholder="Masukkan nama lengkap"
                        class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                </div>

                <div>

                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        Username
                    </label>

                    <input type="text" name="username" required placeholder="Masukkan username"
                        class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                </div>

                <div>

                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        Password
                    </label>

                    <input type="password" id="password" name="password" required placeholder="Minimal 6 karakter"
                        class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                </div>

                <div>

                    <label class="block text-sm font-medium text-gray-700 mb-2">
                        Konfirmasi Password
                    </label>

                    <input type="password" id="konfirmasi" name="konfirmasi_password" required
                        placeholder="Ulangi password"
                        class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-blue-500 focus:outline-none">

                </div>

                <button type="submit" name="register"
                    class="w-full bg-blue-600 hover:bg-blue-700 text-white py-3 rounded-xl font-semibold transition">

                    <i class="fas fa-user-plus mr-2"></i>

                    Register

                </button>

            </form>

            <div class="px-8 pb-8 text-center">

                <a href="index.php" class="text-blue-600 hover:text-blue-800 font-medium">

                    <i class="fas fa-arrow-left mr-1"></i>

                    Sudah punya akun? Login di sini

                </a>

            </div>

        </div>

    </div>


</body>

</html>


?>