<?php
    include 'header.php';

	$err = "";
	$msg = "";

	// To udate user profile

	if (isset($_POST['edit_contact'])) {
		$_name = text_input($_POST['name']);
		$_phone = text_input($_POST['phone']);
		$_country = text_input($_POST['country']);
		$_city = text_input($_POST['city']);
		$_gender = text_input($_POST['gender']);
		$_address = text_input($_POST['address']);

		if (!empty($_fullname) || !empty($_phone) || !empty($_country) || !empty($_city) || !empty($_gender) || !empty($_address)) {

			$sql = mysqli_query($link, "UPDATE users SET fname = '$_name', phone = '$_phone', country = '$_country', city = '$_city', gender = '$_gender', address = '$_address' WHERE email = '$user' ");

			if ($sql) {
				$msg = "Profile Updated successfully";
			}
		}
   }


   // TO update user photo
   
  if (isset($_POST['upload_photo']) && isset($_FILES['photo'])) {
	  $file_name = $_FILES['photo']['name'];
      $file_size =$_FILES['photo']['size'];
      $file_tmp =$_FILES['photo']['tmp_name'];
      $file_type=$_FILES['photo']['type'];
	  $file_ext=strtolower(end(explode('.',$_FILES['photo']['name'])));
	  $extensions= array("jpeg","jpg","png");
	  
		if(in_array($file_ext, $extensions) === false){
			$err = "Extension not allowed, please choose a JPEG or PNG file.";
		}

		if(empty($err)){
			$newImageName = uniqid().".png";
			
			$sql = mysqli_query($link, "UPDATE users SET photo = '$newImageName' WHERE email = '$user' ");

			move_uploaded_file($file_tmp, "upload/".$newImageName);
			$msg = "Profile Image Updated successfully";
	    }
  }

  // 	To change user password
  if (isset($_POST['change_password'])) {

    $check_password = mysqli_query($link, "SELECT * FROM users WHERE email = '$email'  ");
    $user = mysqli_fetch_assoc($check_password);

    if (mysqli_num_rows($check_password) == 0) {
        $err = "User not found";
    } else {
        if (empty($_POST['old_password']) || empty($_POST['new_password']) || empty($_POST['confirm_password'])) {
            $err = "All fields are required";
        } elseif ($_POST['old_password'] != $user['password']) {
            $err = "Old password is incorrect";
        } elseif ($_POST['new_password'] != $_POST['confirm_password']) {
            $err = "Passwords do not match";
        } else {
            $password2 = text_input($_POST['new_password']);

            // Update the password in the users table
            $update_password = mysqli_query($link, "UPDATE users SET password = '$password2' WHERE email = '$email' ");

            if ($update_password) {
                $msg = "Password Updated Successfully";
            } else {
                $err = "Failed to update password";
            }
        }
    }
}

?>

<?php

	if($err != ""){
		echo customAlert('error', $err);
		// echo pageRedirect('3', 'register.php');
	}

	if ($msg != "") {
		echo customAlert('success', $msg);
		// echo pageRedirect('3', '../wallet/index.html');
	}

?>

<div class="appHeader" style="background-color:#161129">
        <div class="left">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#sidebarPanel"> <i class="fas fa-bars text-white"></i> </a>
        </div>
        <div class="pageTitle text-white">
            Profile
        </div>
        <div class="right">
				<a href="Notifications.php" class="headerButton"> <i class="fas fa-bell text-white"></i> <span class="badge badge-danger">0</span> </a>
				<a href="logout.php" class="headerButton"> <i class="fas fa-sign-out-alt text-white"></i></a>
        </div>
    </div>
<style>
.wallet-card-section:before {
   
    background: #000000 !important;}
body.dark-mode .appHeader {
    border-bottom-color: #000000 !important;
}
path {
    color: #f8f9fa;
}

.appBottomMenu .item.active .icon, .appBottomMenu .item.active ion-icon, .appBottomMenu .item.active strong {
color:white !important;}

.appHeader {
    background-color: #000000 !important;
}
div#appCapsule {
    background-color: white;
    color: black;
}
ul.listview.image-listview.text.inset {
    background: #f8f9fa;
    box-shadow: 0 1px 19px 0 #dcdce9;
}
.in {
    color: black;
}



</style>
<!-- App Capsule -->
<div id="appCapsule">

	<div class="section mt-3 text-center">
		<div class="avatar-section">
			<a href="#" data-bs-toggle="modal" data-bs-target="#photo"><img src="upload/<?php echo $photo  ?>" alt="avatar" class="imaged w100 rounded">
			<span class="button"><i class="fas fa-camera"></i></span>
			</a>
		</div>
	</div>

	<div class="listview-title mt-1">&nbsp;</div>
	<ul class="listview image-listview text inset">
		<li>
			<a href="#" class="item" data-bs-toggle="modal" data-bs-target="#profile">
				<div class="in">
					<div>
						<?php echo $fullname ?>					</div>
					<span class="text-primary" style="color:white">
						<i class="fas fa-edit"></i> EDIT PROFILE
					</span>
				



				</div>
			</a>
		</li>
		<li>
			<a href="#" class="item">
				<div class="in">
				
				</div>
			</a>
		</li>
	</ul>
	<div class="listview-title mt-1">&nbsp;</div>
	<ul class="listview image-listview text inset">
		<li>
			<a href="#" class="item">
				<div class="in">
					<div>Email</div>
					<span class="text-primary">
						<?php echo $email ?>			</span>
				</div>
			</a>
		</li>
		<li>
			<a href="#" class="item">
				<div class="in">
					<div>Phone</div>
					<span class="text-primary">
							<?php echo $phone ?>				</span>
				</div>
			</a>
		</li>
		<li>
			<a href="#" class="item">
				<div class="in">
					<div>Country</div>
					<span class="text-primary">
							<?php echo $country  ?>				</span>
				</div>
			</a>
		</li>
		<li>
			<a href="#" class="item">
				<div class="in">
					<div>City</div>
					<span class="text-primary">
				      	<?php echo $city  ?>					</span>
				</div>
			</a>
		</li>
		<li>
			<a href="#" class="item">
				<div class="in">
					<div>Gender</div>
					<span class="text-primary">
					      <?php echo $gender  ?>						</span>
				</div>
			</a>
		</li>
		<li>
			<a href="#" class="item">
				<div class="in">
					<div>Address</div>
					<span class="text-primary">
					     <?php echo $address  ?>					</span>
				</div>
			</a>
		</li>
	</ul>
	<div class="listview-title mt-1">Security</div>
	<ul class="listview image-listview text inset" id="Password">
		<li>
			<a href="#" class="item" data-bs-toggle="modal" data-bs-target="#pass">
				<div class="in">
					<div class="text-danger">Change Password</div>
					<span class="text-primary">
						<i class="fas fa-user-shield"></i>
					</span>
				



				</div>
			</a>
		</li>
	</ul>

	<div class="listview-title mt-1">&nbsp;</div>

</div>
<!-- * App Capsule -->
<!-- Dialog Form -->
<div class="modal fade dialogbox" id="photo" data-bs-backdrop="static" tabindex="-1" role="dialog">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Change Profile Photo</h5>
			</div>
			<form action="profile.php" enctype="multipart/form-data" method="POST">
				<div class="modal-body text-start mb-2">
					<div class="form-group basic">
						<div class="input-wrapper">
							<input type="file" class="form-control" name="photo" required>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<div class="btn-inline">
						<button type="button" class="btn btn-text-secondary" data-bs-dismiss="modal">CANCEL</button>

						<input type="submit" class="btn btn-text-primary" value="UPLOAD" name="upload_photo">
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<!-- * Dialog Form -->
<!-- Dialog Form -->
<div class="modal fade dialogbox" id="profile" data-bs-backdrop="static" tabindex="-1" role="dialog">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Profile</h5>
			</div>
			<form action="profile.php" method="POST">

				<div class="modal-body text-start mb-2">

					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Name</label>
							<input type="text" class="form-control" name="name" value="<?php echo $fullname ?>" >
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Phone</label>
							<input type="number" class="form-control" name="phone" value="<?php echo $phone ?>">
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Country</label>
							<select name="country" class="form-control" id="country"  >
								<option value="<?php echo $country ?>"></option>
							</select>
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">City</label>
							<select name="city" class="form-control" id="state">
								<option value="<?php echo $city ?>"></option>
							</select>
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Gender</label>
							<select class="form-control" name="gender">
								<option value="<?php  echo $gender?>">-Select-</option>
								<option value="Male">Male</option>
								<option value="Female">Female</option>
								<option value="Other">Other</option>
							</select>
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Address</label>
							<textarea class="form-control" rows="3" name="address"></textarea>
						</div>
					</div>

				</div>
				<div class="modal-footer">
					<div class="btn-inline">
						<button type="button" class="btn btn-text-secondary" data-bs-dismiss="modal">CANCEL</button>

						<input type="submit" class="btn btn-text-primary" value="PROCEED" name="edit_contact">
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<!-- * Dialog Form -->
<!-- Dialog Form -->
<div class="modal fade dialogbox" id="pass" data-bs-backdrop="static" tabindex="-1" role="dialog">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Change Password</h5>
			</div>
			<form method="POST" action="profile.php">
				<div class="modal-body text-start mb-2">

					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Old Password</label>
							<input type="password" class="form-control" name="old_password" required>
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">New Password</label>
							<input type="password" class="form-control" name="new_password" required>
						</div>
					</div>
					<div class="form-group basic">
						<div class="input-wrapper">
							<label class="label" for="text1">Confirm New Password</label>
							<input type="password" class="form-control" name="confirm_password" required>
						</div>
					</div>

				</div>
				<div class="modal-footer">
					<div class="btn-inline">
						<button type="button" class="btn btn-text-secondary" data-bs-dismiss="modal">CANCEL</button>

						<input type="submit" class="btn btn-text-primary" value="PROCEED" name="change_password">
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<!-- * Dialog Form -->
<style>
.sidebar-balance {
    background: #000000;
}.action-group {
    background: #000000;
}.modal-body.p-0 {
    background: white;
}ul.listview.flush.transparent.image-listview {
    background: white;
    box-shadow: 0 1px 3px 0 rgb(0 0 0 / 9%);
}body.dark-mode .image-listview>li a.item {
    color: #000000 !important;
}.icon-box.bg-primary {
    background: #000000 !important;
}span.badge.badge-primary {
    background: #000000 !important;
}



</style>


<?php
    include 'footer.php';
?>