<?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'] ;
        $user_image = $_SESSION['user_image'] ;
        echo $user_image;
    }else{
        header("Location:login.php") ;
    }
?>

<?php   
    if(isset($_POST['profile_edit'])){
        $name = $_POST['name'] ;
        $address = $_POST['address'] ;
        $area = $_POST['area'] ;
        $city = $_POST['city'] ;
        
         // user profile 
        $profile = $_FILES['profile']['name'];
        $old_profile = $_POST['old_profile'];
        $update_filename = "";
        if($profile != NULL){
            $extension = pathinfo($profile,PATHINFO_EXTENSION);
            $filename = $name.time().'.'.$extension;
            $update_filename = $filename;
        }else{
            $update_filename = $old_profile;
        }

        
        $sql = "UPDATE user_profile SET address = '$address' , area = '$area' , city = '$city' , up_image = '$update_filename' WHERE  user_id = '$user_id' " ;
        if($config->query($sql) === TRUE){
            // check profile
            if($profile != NULL){
                if(file_exists('assets/img/user/profile/'.$old_profile)){
                    unlink('assets/img/user/profile/'.$old_profile);
                }
                move_uploaded_file($_FILES['profile']['tmp_name'],'assets/img/user/profile/'.$update_filename);
            }
            
            $_SESSION['status'] = "Profile Uploaded Successfully";
		    $_SESSION['alert'] = "success";
        } else {
            $_SESSION['status'] = "Upload Failed";
		    $_SESSION['alert'] = "error";
        }
    }
    
    
?>

	<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">
						    <?php
						        $pro_sql = "SELECT * FROM user_profile WHERE user_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">
    									<h4 class="card-title">Basic Information</h4>
        								<div class="row form-row">
    										<div class="col-md-6">
    											<div class="form-group">
    												<div class="change-avatar">
    													<div class="profile-img">
    														<img src="assets/img/<?php echo empty($pro['up_image']) ? 'no-avatar.png' : 'user/profile/'.$pro['up_image'] ;?>" alt="<?= $username ?> Image">
    													</div>
    													<div class="upload-img">
    														<div class="change-photo-btn">
    															<span><i class="fa fa-upload"></i> Upload Photo</span>
    															<input type="hidden" class="upload" name="old_profile" value="<?= $pro['up_image'] ?>">
    															<input type="file" class="upload" name="profile">
    														</div>
    														<small class="form-text text-muted">Allowed JPG, GIF or PNG. Max size of 2MB</small>
    													</div>
    												</div>
    											</div>
    										</div>
    										
    										<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 ?>" readonly>
    											</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="<?= $email ?>" readonly>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label>Phone Number</label>
    												<input type="text" class="form-control" name="phone" id="phone" value="<?= $phone ?>" readonly>
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="com_map" >Address</label>
    												<input type="text" class="form-control" name="address" id="address"  value="<?= $pro['address'] ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="com_map" class="control-label">Area</label>
    												<input type="text" class="form-control" name="area" id="area"  value="<?= $pro['area'] ?>">
    											</div>
    										</div>
    										<div class="col-md-6">
    											<div class="form-group">
    												<label for="com_map" class="control-label">City</label>
    												<input type="text" class="form-control" name="city" id="city"  value="<?= $pro['city'] ?>">
    											</div>
    										</div>
    									</div>
    							    </div>
    						    </div>
        							<!-- /Basic Information -->
        							
    							<div class="submit-section submit-btn-bottom">
    								<button type="submit" class="btn btn-primary submit-btn" name="profile_edit">Save Changes</button>
    							</div>
							</form>
						</div>
					</div>

				</div>

			</div>		
			<!-- /Page Content -->
   
			<!-- Footer -->
		    <?php include "footer.php" ; ?>
			<!-- /Footer -->
		   
		</div>
		<!-- /Main Wrapper -->
	  
		<!-- 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>