<?php include'header.php';?>
<?php
if(isset($_SESSION['id'])){ ?>
<script type = "text/javascript">window.location = "home.php";</script>
<?php } ?>

<?php
if(isset($_POST['registration'])){
    $name     = $_POST['name'];
    $number   = $_POST['number'];
    $password = $_POST['password'];
    $check = $obj->sql("SELECT * FROM users Where number='$number'");
    $result = $obj->getResult();
    if($result[0]){
        echo "Number already registered!";
    } else {
        $obj->insert('users', [
            'name' => $name,
            'number' => $number,
            'password' => $password
        ]);

        if($obj->getResult()){
            $ret = $obj->login($number,$password);
            $num = mysqli_fetch_array($ret);
            if($num > 0) {
                $_SESSION['user']['login'] = TRUE;
                $_SESSION['id'] = $num['id'];
                header('location: home.php');
            }
        }
    }
}
?>  


<div class="card login-card">
    
    <div class="card-header">
        Registration
    </div>

    <div class="card-body p-4">
 
        <form action="" method="POST">
             <div class="mb-3">
                <label>Name</label>
                <input type="" name="name" class="form-control" placeholder="Enter Name" required>
            </div>


            <div class="mb-3">
                <label>Number</label>
                <input type="" name="number" class="form-control" placeholder="Enter Number" required>
            </div>

            <div class="mb-3">
                <label>Password</label>
                <input type="password" name="password" class="form-control" placeholder="Enter Password" required>
            </div>

            <div class="d-grid">
                <button type="submit" name="registration"  class="btn btn-login">
                    Registration
                </button>
            </div>

        </form>
        <hr>
        <center>I already have an account ? <a href="index.php">Log In</a></center>


    </div>

</div>


<?php include'footer.php';?>