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

    if(isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && $_SESSION['user_cate'] === "user"){
        $user_id = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $email = $_SESSION['user_email'] ;
        $phone = $_SESSION['user_phone'] ;
        // $vendor_image = $_SESSION['vendor_image'] ;
    }else{
        header("Location:login.php") ;
    }
?>
<?php
    $passwordErr = $newpasswordErr = "" ; 
    $result = array();
if(isset($_POST['changepass'])){
    // password
    if(empty($_POST["password"])) {
        $passwordErr = "Password is required";
        $result['password'] = "Password is required" ;
    } else {
        $check_password = $config->real_escape_string($_POST['password']);
        $sql = "SELECT * FROM master_login WHERE Master_Username = '$email' AND Master_Type = 3 ";
        $res = $config->query($sql);
        if($res->num_rows > 0){
            $row = $res->fetch_assoc();
            $pass = $row['Master_Password'];
            if(!password_verify($check_password,$pass)){
                $passwordErr = "Invalid Password";
                $result['password'] = "Invalid Password" ;
            }
        }
    }
    
    // new password
    if(empty($_POST["newpassword"])) {
        $newpasswordErr = "New Password is required";
        $result['newpassword'] = "New Password is required" ;
    }else{
        $check_new_password = $config->real_escape_string($_POST['newpassword']);
        $uppercase = preg_match('@[A-Z]@', $check_new_password);
        $lowercase = preg_match('@[a-z]@', $check_new_password);
        $number    = preg_match('@[0-9]@', $check_new_password);
        $specialChars = preg_match('@[^\w]@', $check_new_password);
        if(!$uppercase || !$lowercase || !$number || !$specialChars ) {
            $newpasswordErr = 'Use at least one upper case letter, one number, and one special character.';
            $result['newpassword'] = "Password is weak" ;
        }else{
            if(strlen($check_new_password) < 8) {
                $newpasswordErr = 'Password should be at least 8 characters in length.';
                $result['newpassword'] = "Password is weak" ;
            }else{
                $cpassword = $config->real_escape_string($_POST['cpassword']);
                if($check_new_password == $cpassword){
                    $newpassword = password_hash($check_new_password,PASSWORD_BCRYPT);
                }else{
                    $cpasswordErr = 'Password not match';
                    $result['cpassword'] = "Password not match" ;
                }
            }
        }

    }
    
    
    if(count($result) == 0 ){
        $sql = "UPDATE master_login SET Master_Password = '$newpassword' WHERE Master_id = '$user_id' AND Master_Type = 3 ";
        if($config->query($sql)){
            // $pur_sql .= $config->query("INSERT INTO ewallet_master(vendor_id) VALUES('$v_id') ") ;
            $check_password = $check_new_password = "" ;
            $_SESSION['status'] = "Password Changed Successfully." ;
            $_SESSION['alert'] = "success" ;
        }else{
            $_SESSION['status'] = "Failed.Please try again." ;
            $_SESSION['alert'] = "error" ;
        }
    }
}


?>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" ></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" ></script>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" >
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" ></script>

    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.js"></script>

	<link rel="stylesheet" href="assets/css/style.css">
	<body>

		<!-- Main Wrapper -->
		<div class="main-wrapper">
		
			<!-- Header -->
			<?php include "header.php" ; ?>
			<!-- /Header -->
			
			<!-- Breadcrumb -->
			<?php include "breadcrumbuser.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 "sidebar.php" ; ?>
							<!-- /Profile Sidebar -->
							
						</div>
						<div class="col-md-7 col-lg-8 col-xl-9">
							<div class="card">
								<div class="card-body">
									<div class="row">
										<div class="col-md-12 col-lg-6">
										
											<!-- Change Password Form -->
											<form method="post">
												<div class="form-group">
													<label for="password">Old Password</label>
													<div style="position:relative;">
													    <input type="password" class="form-control" name="password" id="password" value="<?= $check_password; ?>">
													    <i class="fas fa-eye-slash show-icon" id="pass-icon" onclick="showhide()"></i>
													</div>
													<span class="text-danger"><?= $passwordErr; ?></span>
												</div>
												<div class="form-group">
													<label for="newpassword">New Password</label>
													<div style="position:relative;">
													    <input type="password" class="form-control" name="newpassword" id="newpassword" value="<?= $check_new_password ?>">
													    <i class="fas fa-eye-slash show-icon" id="newpass-icon" onclick="showhide1()"></i>
													</div>
													<span class="text-danger"><?= $newpasswordErr; ?></span>
												</div>
												<div class="form-group">
													<label for="cpassword">Confirm Password</label>
													<input type="password" class="form-control" name="cpassword" id="cpassword">
													<span class="text-danger"><?= $cpasswordErr; ?></span>
												</div>
												<div class="submit-section">
													<button type="submit" class="btn btn-primary submit-btn" name="changepass">Save Changes</button>
												</div>
											</form>
											<!-- /Change Password Form -->
											
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>

				</div>

			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
			<?php include "footer.php" ; ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
		<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 newpassword = document.getElementById('newpassword');
            const icon = document.getElementById('pass-icon');
            const icon1 = document.getElementById('newpass-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");
                }
            }
            function showhide1(){
                if(newpassword.type === "password"){
                    newpassword.setAttribute("type","text");
                    icon1.setAttribute("class","fas fa-eye show-icon");
                }else{
                    newpassword.setAttribute("type","password");
                    icon1.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>
	</body>

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