<?php
    include 'head.php';
    session_start();
    $nameErr = $emailErr = $mobileErr = $passwordErr = "" ; 
    $username = $email = $mobile = $password = "" ;
    $result = array();
 //vendor register
if(isset($_POST['submit'])){
    // name
    if(empty($_POST['name'])){
        $nameErr = "Name is required" ;
        $result['name'] = "Name is required" ;
    }else{
        $username = $config->real_escape_string($_POST['name']);
    }
    // email
    if(empty($_POST['email'])){
        $emailErr = "Email is required" ;
        $result['email'] = "Email is required" ;
    }else{
        $check_email = $config->real_escape_string($_POST['email']);
        if(!filter_var($check_email,FILTER_VALIDATE_EMAIL)){
            $emailErr = "Invalid email format";
            $result['email'] = "Invalid email format" ;
        }else{
            $check = "SELECT * FROM master_login WHERE Master_Username = '$check_email' AND Master_Type = 2 ";
            $check_res = $config->query($check);
            if($check_res->num_rows > 0){
                $emailErr = "Email Already exists";
                $result['email'] = "Email Already exists" ;
            }else{
                $email = $check_email ;
            }
        }
    }
    // mobile
    if (empty($_POST["phone"])) {
        $phoneErr = "Mobile Number is required";
        $result['phone'] = "Mobile Number is required" ;
    } else {
        $check_phone = $config->real_escape_string($_POST['phone']);
        $phone_len = strlen($check_phone);
        if($phone_len == 10){
            $check = "SELECT * FROM master_login WHERE Master_phone = '$check_phone' AND Master_Type = 2 ";
            $check_res = $config->query($check);
            if($check_res->num_rows > 0){
                $phoneErr = "Mobile Number Already exists";
                $result['phone'] = "Mobile Number Already exists" ;
            }else{
                $phone = $check_phone ;
            }
        }else{
            $phoneErr = "Invalid Mobile number" ;
            $result['phone'] = "Invalid Mobile number" ;
        }
    }
    // password
    if (empty($_POST["password"])) {
        $passwordErr = "Password is required";
        $result['password'] = "Password is required" ;
    } else {
        $check_password = $config->real_escape_string($_POST['password']);
        // Validate password strength
        if(strlen($check_password) < 8) {
            $passwordErr = 'Password should be at least 8 characters in length.';
            $result['password'] = "Password is weak" ;
        }else{
            $password = password_hash($check_password,PASSWORD_BCRYPT);
        }
    }
    
    if(count($result) == 0 ){
        $sql = "INSERT INTO master_login(Master_Name, Master_Username, Master_phone, Master_Password, Master_Type, Master_Active_Status) VALUES('$username' ,'$email' ,'$phone' ,'$password' ,2 ,1) ";
        if($config->query($sql)){
            $v_id = $config->insert_id;
            $sub_sql = $config->query("INSERT INTO v_profile(v_id ,v_name) VALUES('$v_id' ,'$username') ;") ;
            $pur_sql .= $config->query("INSERT INTO ewallet_master(vendor_id) VALUES('$v_id') ") ;
            $username = $check_email = $check_phone = $check_password = "" ;
            // $_SESSION['status'] = "Registered Successfully.Our team will contact you soon.If you have any emergency contact admin." ;
            $_SESSION['status'] = "Registered Successfully." ;
            $_SESSION['alert'] = "success" ;
        }else{
            $_SESSION['status'] = "Registration Failed.Please try again." ;
            $_SESSION['alert'] = "error" ;
        }
    }
}

// driver_master

//  if(isset($_POST['driver_login'])){
//      $username = $config->real_escape_string($_POST['username']);
//      $password = $config->real_escape_string($_POST['password']);
//      $sql = "SELECT * FROM master_login WHERE d_mobile = '$username' ";
//      $res = $config->query($sql);
//      if($res->num_rows > 0){
//          $row = $res->fetch_assoc();
//          $pass = $row['d_password'];
//          if(password_verify($password,$pass)){
//              $_SESSION['d_id'] = $row['d_id'];
//              $_SESSION['d_name'] = $row['d_name'];
//              $_SESSION['d_mobile'] = $username;
//              $_SESSION['d_photo'] = $row['d_photo'];
//              header('Location:Main/d_dashboard.php');
//          }else{
//             $_SESSION['err'] = "Invalid mobile number or password" ;
//          }
//      }else{
//         $_SESSION['err'] = "Mobile Number does not exist " ;
//      }
//  }


?>
	<body class="account-page">

		<!-- Main Wrapper -->
		<div class="main-wrapper">
			<!-- Header -->
			<?php include('./header.php') ?>
			<!-- /Header -->
			<!-- Page Content -->
			<div class="content">
				<div class="container-fluid">
					<div class="row">
						<div class="col-md-8 offset-md-2">
							<!-- Account Content -->
							<div class="account-content">
								<div class="row align-items-center justify-content-center">
									<div class="col-md-7 col-lg-6 login-left">
										<img src="assets/img/regis.jpg" class="img-fluid" alt="Login Banner">	
									</div>
									<div class="col-md-12 col-lg-6 login-right">
										<div class="login-header d-flex justify-content-center">
											<h3>Register 
												<!-- <a href="register.html">Not a Doctor?</a> -->
											</h3>
										</div>
										<!-- Register Form -->
										<form method="POST">
											<div class="form-group form-focus">
												<input type="text" name="name" class="form-control floating" value="<?= $username; ?>">
												<label class="focus-label">Name</label>
												<span class="text-danger py-2"><?= $nameErr ?></span>
											</div>
											<div class="form-group form-focus">
												<input type="email" name="email" class="form-control floating" value="<?= $check_email; ?>">
												<label class="focus-label">Email</label>
												<span class="text-danger py-2"><?= $emailErr ?></span>
											</div>
											<div class="form-group form-focus">
												<input type="number" name="phone" class="form-control floating" value="<?= $check_phone; ?>" maxlength="10" onkeypress="if(this.value.length == 10) return false;">
												<label class="focus-label">Mobile Number</label>
												<span class="text-danger py-2"><?= $phoneErr ?></span>
											</div>
											<div class="form-group form-focus">
												<input type="password" name="password" id="password" class="form-control floating" value="<?= $check_password; ?>">
												<i class="fas fa-eye-slash show-icon" id="pass-icon" onclick="showhide()"></i>
												<label class="focus-label">Create Password</label>
												<span class="text-danger py-2"><?= $passwordErr ?></span>
											</div>
											<div class="text-right">
												<a class="forgot-link" href="vendor_login.php">Already have an account?</a>
											</div>
											<button class="btn btn-primary btn-block btn-lg login-btn" name="submit" type="submit">Signup</button>
											<!-- <div class="login-or">
												<span class="or-line"></span>
												<span class="span-or">or</span>
											</div>
											<div class="row form-row social-login">
												<div class="col-6">
													<a href="#" class="btn btn-facebook btn-block"><i class="fab fa-facebook-f mr-1"></i> Login</a>
												</div>
												<div class="col-6">
													<a href="#" class="btn btn-google btn-block"><i class="fab fa-google mr-1"></i> Login</a>
												</div>
											</div> -->
										</form>
										<!-- /Register Form -->
									</div>
								</div>
							</div>
							<!-- /Account Content -->
						</div>
					</div>
				</div>
			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
			<?php include('./footer.php') ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
	    <!-- custom style -->
	    <style>
	        .show-icon{
                position: absolute;
                cursor: pointer;
                right: 10px;
                top: 50%;
                transform: translateY(-50%);
                font-size: 13px;
                color: #9d9b9b;
            }
	    </style>
	    <!-- custom script -->
	    <script>
	        const password = document.getElementById('password');
	        const icon = document.getElementById('pass-icon');
	        function showhide(){
	            if(password.type === "password"){
	                password.setAttribute("type","text");
	                icon.setAttribute("class","fas fa-eye show-icon");
	            }else{
	                password.setAttribute("type","password");
	                icon.setAttribute("class","fas fa-eye-slash show-icon");
	            }
	        }
	        
	    </script>
		<!-- jQuery -->
		<script src="assets/js/jquery.min.js"></script>
		<!-- Bootstrap Core JS -->
		<script src="assets/js/popper.min.js"></script>
		<script src="assets/js/bootstrap.min.js"></script>
		<!-- Custom JS -->
		<script src="assets/js/script.js"></script>
		<script>
    		<?php if(isset($_SESSION['status']) && !empty($_SESSION['status'])) { ?>
    		    swal({
                  title: "<?= $_SESSION['status'] ?>",
                  text: "",
                  icon: "<?= $_SESSION['alert'] ?>",
                  button: {
                    className: "agree",
                    },
                });
                $('.agree').on('click', function() {
                    window.location.href="vendor_login.php";
                });
            <?php unset($_SESSION['status']) ; } ?>
        </script>
	</body>
<!-- doccure/doctor-register.html  30 Nov 2019 04:12:16 GMT -->
</html>