<?php 
require_once("header.php");

if(isset($_COOKIE["USER_ID"]))
{
    header("location: service-details.php?category_id=".$_GET['category_id']."&subcategory_id=".$_GET['subcategory_id']);
}
?>

<?php
if(isset($_POST['send']))
{	
	$sql_check = "SELECT * FROM `sc_users` WHERE `phone` = '".$_REQUEST['login_phone']."'";
    $exe_check = mysqli_query($connection,$sql_check);
    $num_check = mysqli_num_rows($exe_check);
    if($num_check>0)
    {
        $arr_check = mysqli_fetch_array($exe_check);
        
        setcookie("USER_ID",$arr_check['user_id'],time()+ 3600*24*365,"/");
    		
		$sql_update = "UPDATE `sc_users` SET `full_name` = '".$_REQUEST['login_name']."' WHERE `user_id` = '".$arr_check['user_id']."'";
    	$exe_update = mysqli_query($connection,$sql_update);
    	
    	header("location: choose-pincode.php?category_id=".$_POST['hid_category_id']."&subcategory_id=".$_POST['hid_subcategory_id']);
    }
    else
    {
        $sql_insert = "INSERT INTO `sc_users` SET `full_name` = '".$_REQUEST['login_name']."',`phone` = '".$_REQUEST['login_phone']."',`registration_date` = '".date('Y-m-d H:i:s',time())."'";
    	$exe_insert = mysqli_query($connection,$sql_insert);
    	
    	$last_id = mysqli_insert_id($connection);
    
        setcookie("USER_ID",$last_id,time()+ 3600*24*365,"/");
        
        header("location: choose-pincode.php?category_id=".$_POST['hid_category_id']."&subcategory_id=".$_POST['hid_subcategory_id']);
    }
}
?>

            <script language="javascript" type="text/javascript">
			function form_validation()
			{
				if(document.getElementById('login_name').value == "")
				{
					alert("Please enter name");
					document.getElementById('login_name').focus();
					return false;
				}
				
				if(document.getElementById('login_phone').value == "")
				{
					alert("Please enter whatsapp no.");
					document.getElementById('login_phone').focus();
					return false;
				}
			}
			
			function opbu()
			{
                if(document.getElementById('agr').checked == true)
                {
                    $('#but1').hide();
                    $('#but2').show();
                }
                else
                {
                    $('#but1').show();
                    $('#but2').hide();
                }
			}
			</script>

			<!-- page template area start -->
			<section id="about">
				<div class="container mt-0 pb-0 pt-10">
				    <h1 class="title mt-0 mb-10" style="text-align:center;">Login</h1>
					<div class="row">
						<div class="col-md-12">
							<form id="contact_form" name="contact_form" class="" action="" method="post" onSubmit="return form_validation();">
							<div class="row">
							    <div class="col-sm-12">
									<div class="form-group">
										<input name="login_name" id="login_name" class="form-control" type="text" placeholder="Enter Name *" value="">
									</div>
								</div>
								<div class="col-sm-12">
									<div class="form-group">
										<input name="login_phone" id="login_phone" class="form-control" type="text" maxlength="10" placeholder="Enter Whatsapp No. *" value="">
									</div>
								</div>
								<!--<div class="col-sm-12">
									<div class="form-group">
										<input name="login_password" id="login_password" class="form-control" type="password" placeholder="Enter Password" value="<?php if(isset($_COOKIE["login_password"])) { echo $_COOKIE["login_password"]; } ?>">
									</div>
								</div>
								<div class="col-sm-12">
									<div class="form-group">
										<input type="checkbox" name="remember" id="remember" value="Yes" <?php if(isset($_COOKIE["remember"])) { ?>checked<?php } ?> /> Remember Me
									</div>
								</div>-->
								<div class="col-sm-12">
									<div class="form-group">
										<input type="checkbox" name="agr" id="agr" onclick="opbu();"> I accept the <a href="terms.php" style="color:#1565c0;">Terms & Conditions</a>
									</div>
								</div>
							</div>
							<div class="form-group">
								<input type="hidden" name="send" id="send" value="1" />
								<input type="hidden" name="hid_category_id" id="hid_category_id" value="<?php echo $_GET['category_id'];?>" />
								<input type="hidden" name="hid_subcategory_id" id="hid_subcategory_id" value="<?php echo $_GET['subcategory_id'];?>" />
								<p id="but1" style="display:block;"><button type="button" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px">Login</button></p>
								<p id="but2" style="display:none;"><button type="submit" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px">Login</button></p>
							</div>
							</form>
						</div>
					</div>
				</div>
			</section>
			<!-- page template area end -->

<?php require_once("footer.php");?>