require "includes/profile_management_function.php";
require "includes/session.php";
require "includes/util.php";
require "includes/country_list.php";
redirectToHTTPS();
if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    if (isset($_POST['profileSubmit'])) {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $gender = $_POST['gender'];
        $address1 = $_POST['address1'];
        $address2 = $_POST['address2'];
        $country = $_POST['country'];
        $contactnumber = $_POST['contactnumber'];
        $updateProfileResponse = updateProdileDetails($username, $firstname, $lastname, $gender, $address1, $address2, $country, $contactnumber);
    }
    $userProfileDetails = getProfileDetailsByUsername($username);
    if (isset($userProfileDetails)) {
        $userFirstName = $userProfileDetails["first_name"];
        $userLastName = $userProfileDetails["last_name"];
        $gender = $userProfileDetails["gender"];
        $userEmail = $userProfileDetails["email"];
        if (isset($userProfileDetails["contact_number"])) {
            $userContactNumber = $userProfileDetails["contact_number"];
        } else {
            $userContactNumber = "";
        }
        $address1 = $userProfileDetails["address_line1"];
        if (isset($userProfileDetails["address_line2"])) {
            $address2 = $userProfileDetails["address_line2"];
             $selectedUserContactNumber = "";
         }
     }
 }
 if (isset($_POST['profileSubmit'])) {
     if (isset($_SESSION['selectedUsername'])) {
         $selectedUsername = $_SESSION['selectedUsername'];
         $selectedUserFirstName = $_POST['firstname'];
         $selectedUserLastName = $_POST['lastname'];
         $selectedUserGender = $_POST['gender'];
         $selectedUserAddress1 = $_POST['address1'];
         $selectedUserAddress2 = $_POST['address2'];
         $selectedUserCountry = $_POST['country'];
         $selectedUserContactNumber = $_POST['contactnumber'];
         if (hasProfileDetailsByUsername($selectedUsername)) {
             $selectedUserUpdateProfileResponse = updateProdileDetails($selectedUsername, $selectedUserFirstName, $selectedUserLastName, $selectedUserGender, $selectedUserAddress1, $selectedUserAddress2, $selectedUserCountry, $selectedUserContactNumber);
             $selectedUserProfileDetails = getProfileDetailsByUsername($selectedUsername);
             if (isset($selectedUserProfileDetails)) {
                 $selectedUserFirstName = $selectedUserProfileDetails["first_name"];
                 $selectedUserLastName = $selectedUserProfileDetails["last_name"];
                 $selectedUserContactNumber = $selectedUserProfileDetails["contact_number"];
                 $selectedUserEmail = $selectedUserProfileDetails["email"];
             }
             $selectedUserProfilePicturePath = getProfilePicturePathByUsername($selectedUsername);
         } else {
             $selectUserFirstMsg = "User does not exists";
         }
     } else {
         $selectedUserUpdateProfileResponse = "";
         $selectUserFirstMsg = "You have to select a user first";
     }