<?php

include 'config/koneksi.php';

/* AMBIL RUMAH IBADAH */

$rumah_ibadah = mysqli_query($conn,
"SELECT * FROM rumah_ibadah");

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

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

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

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

    $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_cs.php';
        </script>";

        exit;
    }

    /* INSERT USERS */

    mysqli_query($conn,

    "INSERT INTO users

    (
    nama,
    username,
    password,
    role
    )

    VALUES

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

    );

    /* AMBIL USER ID */

    $user_id = mysqli_insert_id($conn);

    /* INSERT CS */

    mysqli_query($conn,

    "INSERT INTO cs_rumah_ibadah

    (
    nama,
    rumah_ibadah_id,
    no_hp,
    user_id
    )

    VALUES

    (
    '$nama',
    '$rumah_ibadah_id',
    '$no_hp',
    '$user_id'
    )"

    );

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

?>

<!DOCTYPE html>
<?php

include 'config/koneksi.php';

$rumah_ibadah = mysqli_query(
    $conn,
    "SELECT * FROM rumah_ibadah ORDER BY nama ASC"
);

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

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

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

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

    $rumah_ibadah_id = intval(
        $_POST['rumah_ibadah_id']
    );

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

    if(strlen($password) < 6){

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

        exit;
    }

    if($password != $konfirmasi){

        echo "
        <script>
        alert('Konfirmasi password tidak sesuai');
        window.location='register_cs.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_cs.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',
            'cs'
        )"
    );

    $user_id = mysqli_insert_id($conn);

    mysqli_query(
        $conn,
        "INSERT INTO cs_rumah_ibadah
        (
            nama,
            rumah_ibadah_id,
            no_hp,
            user_id
        )
        VALUES
        (
            '$nama',
            '$rumah_ibadah_id',
            '$no_hp',
            '$user_id'
        )"
    );

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

    exit;
}

?>

<!DOCTYPE html>

<html>

<head>


    <title>Register CS Rumah Ibadah</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-green-600 via-emerald-500 to-teal-500 flex items-center justify-center p-5">


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

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

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

                <i class="fas fa-hand-holding-heart text-5xl mb-4"></i>

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

                <p class="mt-2 text-green-100">
                    Daftarkan Customer Service Rumah Ibadah
                </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-green-500 focus:outline-none">

                </div>

                <div>

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

                    <select name="rumah_ibadah_id" required
                        class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-green-500 focus:outline-none">

                        <option value="">
                            -- Pilih Rumah Ibadah --
                        </option>

                        <?php while($r = mysqli_fetch_assoc($rumah_ibadah)){ ?>

                        <option value="<?= $r['id'] ?>">

                            <?= htmlspecialchars($r['nama']) ?>

                        </option>

                        <?php } ?>

                    </select>

                </div>

                <div>

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

                    <input type="text" name="no_hp" required placeholder="08xxxxxxxxxx"
                        class="w-full border rounded-xl p-3 focus:ring-2 focus:ring-green-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-green-500 focus:outline-none">

                </div>

                <div>

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

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

                </div>

                <div>

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

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

                </div>

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

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

                    Daftar CS

                </button>

            </form>

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

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

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

                    Kembali ke Login

                </a>

            </div>

        </div>

    </div>


</body>

</html>