<?php
    include "head.php" ;
    session_start();
    date_default_timezone_set("Asia/Kolkata");

    if(isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && $_SESSION['user_cate'] === "vendor"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $email = $_SESSION['user_email'] ;
        $phone = $_SESSION['user_phone'] ;
        $company = $_SESSION['company'] ;
        $vendor_image = $_SESSION['vendor_image'] ;
    }else{
        header("Location:vendor_login.php") ;
    }
?>
<?php
$nameErr = $emailErr = $mobileErr = $inErr= $schoolErr= $passwordErr = "" ; 
    $username = $email = $mobile = $school_name= $name_initial= $password = "" ;
    $result = array();
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']);
    }
    //   if(empty($_POST['name_initial'])){
    //     $inErr = "Initial is required" ;
    //     $result['name_initial'] = "Initial is required" ;
    // }else{
    //     $name_initial = $config->real_escape_string(strtoupper($_POST['name_initial']));
    // }
    // if(empty($_POST['school_name'])){
    //     $schoolErr = "School Name is required" ;
    //     $result['school_name'] = "School Name is required" ;
    // }else{
    //     $school_name = $config->real_escape_string(strtoupper($_POST['school_name']));
    // }
    // name
    
    $area = $config->real_escape_string($_POST['area']);
    $city = $config->real_escape_string($_POST['city']);
    $address = $config->real_escape_string($_POST['address']);

    // 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 = 3 ";
            $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 = 3 ";
            $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
        $uppercase = preg_match('@[A-Z]@', $check_password);
        $lowercase = preg_match('@[a-z]@', $check_password);
        $number    = preg_match('@[0-9]@', $check_password);
        $specialChars = preg_match('@[^\w]@', $check_password);
        if(!$uppercase || !$lowercase || !$number || !$specialChars ) {
            $passwordErr = 'Use at least one upper case letter, one number, and one special character.';
            $result['password'] = "Password is weak" ;
        }else{
            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' ,3 ,1) ";
         // print_r($config->query($sql));exit;
        if($config->query($sql)){
            $u_id = $config->insert_id;
            $sub_sql = $config->query("INSERT INTO user_profile(user_id ,user_name ,address ,area ,city ,ref_id) VALUES('$u_id' ,'$username' , '$address', '$area', '$city', '$user_id') ") ;
            $pur_sql .= $config->query("INSERT INTO ewallet_master(user_id) VALUES('$u_id') ") ;
            $username = $check_email = $check_phone = $check_password = $address = $area = $city = "" ;
            $_SESSION['status'] = "Registered Successfully." ;
            $_SESSION['alert'] = "success" ;
        }else{
        	  // print_r('d');exit;
            $_SESSION['status'] = "Registration Failed.Please try again." ;
            $_SESSION['alert'] = "error" ;
        }
    }
}

?>

	<body>

		<!-- Main Wrapper -->
		<div class="main-wrapper">
		
			<!-- Header -->
			<?php include "header.php" ; ?>
			<!-- /Header -->
			
			<!-- Breadcrumb -->
			<?php include "breadcrumb.php" ; ?>
			<!-- /Breadcrumb -->
			
			<!-- Page Content -->
			<div class="content">
				<div class="container-fluid">

					<div class="row">
						<div class="col-md-5 col-lg-4 col-xl-3 theiaStickySidebar">
						
							<!-- Profile Sidebar -->
							<?php include "vendor_sidebar.php" ; ?>
							<!-- /Profile Sidebar -->
							
						</div>
						<div class="col-md-7 col-lg-8 col-xl-9">
						    <div class="d-flex justify-content-between align-items-start">
							    <h4 class="card-title">Add User</h4>
							    <a href="added_user.php" class="btn btn-sm bg-info-light">View</a>
							</div>
						    <?php
						        $pro_sql = "SELECT * FROM v_profile WHERE v_id = '$user_id' " ;
						        $pro_res = $config->query($pro_sql);
						        $pro = $pro_res->fetch_assoc();
						    ?>
						    <form method="post" enctype="multipart/form-data">
    							<!-- Basic Information -->
    							<div class="card">
    								<div class="card-body">
    									
        								<div class="row form-row">
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="name">Name<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="name" id="name" value="<?= $username ?>" >
    											    <span class="text-danger py-2"><?= $nameErr ?></span>
    											</div>
    										</div>
    										<!-- <div class="col-md-6">
    											<div class="form-group">
    												<label for="name">Name Initial<span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="name_initial" id="initial" value="<?= $name_initial ?>" >
    											    <span class="text-danger py-2"><?= $inErr ?></span>
    											</div>
    										</div> -->
    										<!-- <div class="col-md-6">
    											<div class="form-group">
    												<label for="name">School Name<span class="text-danger"></span></label>
    												<input type="text" class="form-control" name="school_name" id="s_name" value="<?= $school_name ?>" >
    											    <span class="text-danger py-2"><?= $schoolErr ?></span>
    											</div>
    										</div> -->
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="com_map">Email <span class="text-danger">*</span></label>
    												<input type="email" class="form-control" name="email" id="email" value="<?= $check_email ?>" >
    												<span class="text-danger py-2"><?= $emailErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label>Mobile Number <span class="text-danger">*</span></label>
    												<input type="text" class="form-control" name="phone" id="phone" value="<?= $check_phone ?>" maxlength="10" onkeypress="if(this.value.length == 10) return false;">
    												<span class="text-danger py-2"><?= $phoneErr ?></span>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="address" >Address</label>
    												<input type="text" class="form-control" name="address" id="address"  value="<?= $address ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="area" class="control-label">Area</label>
    												<input type="text" class="form-control" name="area" id="area"  value="<?= $area ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="city" class="control-label">City</label>
    												<input type="text" class="form-control" name="city" id="city"  value="<?= $city ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="password" class="control-label">Password <span class="text-danger">*</span></label>
    												<i class="fas fa-eye-slash show-icon" id="pass-icon" onclick="showhide()"></i>
    												<input type="password" class="form-control" name="password" id="password"  value="<?= $check_password ?>">
    												<span class="text-danger py-2"><?= $passwordErr ?></span>
    											</div>
    										</div>
    									</div>
    							    </div>
    						    </div>
        							<!-- /Basic Information -->
        							
    							<div class="submit-section submit-btn-bottom">
    								<button type="submit" class="btn btn-primary submit-btn" name="submit">Submit</button>
    							</div>
							</form>
						</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: 15px;
                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>
		
		<!-- Sticky Sidebar JS -->
        <script src="assets/plugins/theia-sticky-sidebar/ResizeSensor.js"></script>
        <script src="assets/plugins/theia-sticky-sidebar/theia-sticky-sidebar.js"></script>
		
		<!-- Select2 JS -->
		<script src="assets/plugins/select2/js/select2.min.js"></script>
		
		<!-- Dropzone JS -->
		<script src="assets/plugins/dropzone/dropzone.min.js"></script>
		
		<!-- Bootstrap Tagsinput JS -->
		<script src="assets/plugins/bootstrap-tagsinput/js/bootstrap-tagsinput.js"></script>
		
		<!-- Profile Settings JS -->
		<script src="assets/js/profile-settings.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'] ?>",
                });
            <?php unset($_SESSION['status']) ; } ?>
        </script>
        <script>
    		<?php if(isset($_SESSION['check']) && !empty($_SESSION['check'])) { ?>
    		    swal({
                    title: "<?= $_SESSION['check'] ?>", 
                    text: "",
                    icon: "<?= $_SESSION['alert'] ?>",
                });
            <?php unset($_SESSION['check']) ; } ?>
        </script>
	</body>

<!-- doccure/doctor-profile-settings.html  30 Nov 2019 04:12:15 GMT -->
</html>