<?php
$regerrors=array();
include('../includes/validations.php');
include('../includes/functions.php');
if(isset($_POST['regsubmit']))
{
 $name=$_POST['name'];
 $regno=$_POST['regno'];
 $phone=$_POST['phone'];
 $block=$_POST['block'];

 
$password=$_POST['password'];
$passwordconfirm=$_POST['passwordconfirm'];
//$regerrors=validate_signup($name,$regno,$phone,$password,$passwordconfirm);

if(empty($regerrors))
{
    //success in checking bad fields
    make_connection();
    $success=register_user($name,$regno,$phone,$password,$block);
    if(!$success)
    {
        $regerrors['submiterror']="Some error occured. Please try again.";
    }
    else
    {
        //successfully registered information in SQL
        session_start();
        $_SESSION['login']=$regno;//**********ID for recognizing user*********
        redirect_to("home.php");
    }
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
 <?php include('../includes/head.php');?>
 <title>Utopia - Smarter City</title>
 <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css">
 <link rel="stylesheet" type="text/css" href="materialize/css/materialize.css">
 <link rel="stylesheet" type="text/css" href="css/common.css">
 <link rel="stylesheet" type="text/css" href="css/signup.css">
 <style type="text/css">
 .main{
   display:table;
   width:100%;
   background-size: cover !important;
   background-repeat: no-repeat !important;
   background: linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ), url('images/smartcity.png');
   background-position:center top;
   min-height:100vh;
}
.white-color{
  color: #fff;
}
</style>
</head>
<body>
    <div class="main">
        <div class="fbdiv">
            <span class="pull-left"><a href="index.php"><i class="fa fa-home fa-2x" aria-hidden="true"></i></a></span>
            <a class="facebook" href="https://www.facebook.com/vitbookshare/"><span></span><i class="fa fa-facebook fa-2x" aria-hidden="true"></i></a>
        </div>
        <h1 class="green-color center">Utopia - Smarter City</h1>
        <hr class="underlinehr">
        <main>
          <h3 class="center signuph2 white-color">Signup</h3>
          <?php
          if(isset($regerrors['submiterror']) && !empty($regerrors['submiterror']))
            echo "<h3 class='center redcolor'>{$regerrors['submiterror']}</h3>";
        ?>

        <h4 class="center red-text"></h4>
        <div class="container">
            <div class="card z-depth-3">
              <div class="container">
                <div class="row">
                  <form id="reg_form" action="signup.php" method="post" autofocus>
                    <div class="col s12 l12 m12 input-field">
                        <input type="text" required class="" value="<?php if(isset($name)) echo $name;?>" name="name" id="name" />
                        <label class="active" for="name">Name</label>
                        <span class="regerror">
                            <?php
                            if(isset($regerrors) && isset($regerrors['name']))
                                echo $regerrors['name'];
                            ?>
                        </span>
                    </div>
                    <div class="col s12 l6 m6 input-field">
                        <input type="email" name="regno" class="validate" value="<?php if(isset($regno)) echo $regno;?>" id="email" />
                        <label data-error="wrong email" <?php if(isset($email)) echo " class='active' ";?> for="email">Email</label>
                        <span class="regerror">
                            <?php
                            if(isset($regerrors) && isset($regerrors['email']))
                                echo $regerrors['email'];
                            ?>
                        </span>
                    </div>
                    <div class="col s12 l6 m6 input-field">
                        <input type="number" name="phone" class="validate" value="<?php if(isset($phone)) echo $phone;?>" id="phone" />
                        <label <?php if(isset($phone)) echo " class='active' ";?> for="phone">Phone no.</label>
                        <span class="regerror">
                            <?php
                            if(isset($regerrors) && isset($regerrors['phone']))
                                echo $regerrors['phone'];
                            ?>
                        </span>
                        <?php
                        if(isset($regerrors['email']) && !empty($regerrors['email']))
                            echo "<br><br>";
                        ?>
                    </div>
                    <div class="col s12 l12 m12 input-field">
                        <input required type="text" name="block" id="block" />
                        <label for="block">Your Location</label>
                        <span class="regerror">
                            <?php
                            if(isset($regerrors) && isset($regerrors['block']))
                                echo $regerrors['block'];
                            ?>
                        </span>
                    </div>
                    <div class="col s12 l6 m6 input-field">
                        <input required type="password" name="password" id="password" />
                        <label for="password">Password</label>
                        <span class="regerror">
                            <?php
                            if(isset($regerrors) && isset($regerrors['password']))
                                echo $regerrors['password'];
                            ?>
                        </span>
                    </div>
                    <div class="col s12 l6 m6 input-field">
                        <input required type="password" name="passwordconfirm" id="passwordconfirm" />
                        <label for="passwordconfirm">Password Confirmation</label>
                        <span class="regerror">
                            <?php
                            if(isset($regerrors) && isset($regerrors['passwordconfirm']))
                                echo $regerrors['passwordconfirm'];
                            ?>
                        </span>
                    </div>
                    <a class="waves-effect waves-light btn right">
                        <input type="submit" name="regsubmit" value="Register" />
                       <i class="material-icons right">send</i>
                    </a>

                </form>
                <div class="waves-effect waves-light btn left white">
                    <a href="login.php">
                        Login <i class="material-icons right">perm_identity</i>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
</main>
<br><br>
</div>
<script src="js/jquery.js"></script>
<script src="materialize/js/materialize.min.js"></script>
<script>
    $(document).ready(function() {
        $('select').material_select();
    });
</script>
<script>
    var options=document.getElementsByTagName('option');
    <?php 
    if(isset($block))
    {
        ?>
        options[<?php echo (int)$block;?>].selected=true;
        $('select').material_select();
        <?php
    }
    else
    {
        ?>
        options[0].selected=true;
        $('select').material_select();
        <?php
    }
    ?>
</script>
<script>
    var dayscholar=document.getElementById('dayscholar');
    var block=document.getElementById('block');
    var roomno=document.getElementById('roomno');
    function dayscholarclicked(){
        if(dayscholar.checked)
        {
            block.disabled=true;
            $('select').material_select();
            roomno.disabled=true;
        }
        else
        {
         block.disabled=false;
         $('select').material_select();
         roomno.disabled=false;
     }
 }
</script>
</body>
</html>