<?php
session_start();
error_reporting(0);

include('includes/config.php');

$stmt = $dbh->prepare('SELECT * FROM control WHERE id = :id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

$id = 1;
$stmt->execute();

$data = $stmt->fetch(PDO::FETCH_ASSOC);
$status = $data['reg'];

if($status == 1){
    header('Location: regOff.php');
    die();
}

if (strlen($_SESSION['alogin']) == 0) {
    // header('location:index.php');
} else {
    header('Location: nid.php');
}

// Check if the email already exists
if (isset($_POST['submit'])) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = md5($_POST['password']);
    $gender = "male";
    $mobileno = $_POST['mobileno'];
    $image = "image.png";

    // Check if the email already exists in the database
    $sqlCheckEmail = "SELECT email FROM users WHERE email = :email";
    $queryCheckEmail = $dbh->prepare($sqlCheckEmail);
    $queryCheckEmail->bindParam(':email', $email, PDO::PARAM_STR);
    $queryCheckEmail->execute();

    // If email exists, show an error message
    if ($queryCheckEmail->rowCount() > 0) {
        $error = "This email is already registered. Please use a different email.";
    } else {
        // Proceed with registration if email doesn't exist
        $notitype = 'Create Account';
        $reciver = 'Admin';
        $sender = $email;

        $sqlnoti = "insert into notification (notiuser, notireciver, notitype) values (:notiuser, :notireciver, :notitype)";
        $querynoti = $dbh->prepare($sqlnoti);
        $querynoti->bindParam(':notiuser', $sender, PDO::PARAM_STR);
        $querynoti->bindParam(':notireciver', $reciver, PDO::PARAM_STR);
        $querynoti->bindParam(':notitype', $notitype, PDO::PARAM_STR);
        $querynoti->execute();

        $sql = "INSERT INTO users(name, email, password, gender, mobile, image, status) VALUES(:name, :email, :password, :gender, :mobileno, :image, 1)";
        $query = $dbh->prepare($sql);
        $query->bindParam(':name', $name, PDO::PARAM_STR);
        $query->bindParam(':email', $email, PDO::PARAM_STR);
        $query->bindParam(':password', $password, PDO::PARAM_STR);
        $query->bindParam(':gender', $gender, PDO::PARAM_STR);
        $query->bindParam(':mobileno', $mobileno, PDO::PARAM_STR);
        $query->bindParam(':image', $image, PDO::PARAM_STR);
        $query->execute();
        $lastInsertId = $dbh->lastInsertId();
        if ($lastInsertId) {
            echo "<script type='text/javascript'>alert('Registration Sucessfull!');</script>";
            echo "<script type='text/javascript'> document.location = 'login.php'; </script>";
        } else {
            $error = "Something went wrong. Please try again";
        }
    }
}
?>

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ডিজিটাল-সেবা</title>
    <link rel="shortcut icon" type="image/png" href="https://i.ibb.co/Ndwr1VLh/image.jpg" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        /* General Reset */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Nunito', sans-serif;
        }

        body {
            background: #F2F2F2;
        }

        h1 {
            padding: 0 0 30px;
        }

        .form-container {
            background: #ecf0f3;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 14px 14px 20px #cbced1, -14px -14px 20px white;
            width: 100%;
            max-width: 400px;
            margin: 50px auto;
        }

        .form-container .form-icon {
            color: #ac40ab;
            font-size: 55px;
            text-align: center;
            line-height: 100px;
            width: 100px;
            height: 100px;
            margin: 0 auto 15px;
            border-radius: 50px;
            box-shadow: 7px 7px 10px #cbced1, -7px -7px 10px #fff;
        }

        .form-container .title {
            color: #ac40ab;
            font-size: 25px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 1px;
            text-align: center;
            margin: 0 0 20px;
        }

        .form-container .form-horizontal .form-group {
            margin: 0 0 25px 0;
        }

        .form-container .form-horizontal .form-group label {
            font-size: 15px;
            font-weight: 600;
            text-transform: uppercase;
        }

        .form-container .form-horizontal .form-control {
            color: #333;
            background: #ecf0f3;
            font-size: 15px;
            height: 50px;
            padding: 10px 20px;
            letter-spacing: 1px;
            border: none;
            border-radius: 50px;
            box-shadow: inset 6px 6px 6px #cbced1, inset -6px -6px 6px #fff;
            display: inline-block;
            width: 100%;
            transition: all 0.3s ease 0s;
        }

        .form-container .form-horizontal .form-control:focus {
            box-shadow: inset 6px 6px 6px #cbced1, inset -6px -6px 6px #fff;
            outline: none;
        }

        .form-container .form-horizontal .form-control::placeholder {
            color: #808080;
            font-size: 14px;
        }

        .form-container .form-horizontal .btn {
            color: #000;
            background-color: #ac40ab;
            font-size: 15px;
            font-weight: bold;
            text-transform: uppercase;
            width: 100%;
            padding: 12px 15px 11px;
            border-radius: 20px;
            box-shadow: 6px 6px 6px #cbced1, -6px -6px 6px #fff;
            border: none;
            transition: all 0.5s ease 0s;
        }

        .form-container .form-horizontal .btn:hover,
        .form-container .form-horizontal .btn:focus {
            color: #fff;
            letter-spacing: 3px;
            box-shadow: none;
            outline: none;
        }

        /* Responsive Design */
        @media screen and (max-width: 768px) {
            .form-container {
                padding: 20px;
            }

            .form-container .form-icon {
                font-size: 45px;
                width: 80px;
                height: 80px;
            }

            .form-container .title {
                font-size: 22px;
            }
        }
    </style>
</head>

<body>
    <div class="form-container">
        <div class="form-icon"><i class="fa fa-user"></i></div>
        <h3 class="title">Register</h3>
        <form method="POST" class="form-horizontal">
            <div class="form-group">
                <label>Name</label>
                <input type="text" class="form-control" name="name" required placeholder="Full Name">
            </div>
            <div class="form-group">
                <label>Email</label>
                <input type="email" class="form-control" name="email" required placeholder="Email Address">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control" name="password" required placeholder="Password">
            </div>
            <div class="form-group">
                <label>Mobile Number</label>
                <input type="text" class="form-control" name="mobileno" required placeholder="Mobile Number">
            </div>
            <button type="submit" name="submit" class="btn">Register</button>
        </form>
    </div>
</body>

</html>
