<?php
session_start();

require("../includes/db_connect.php");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    if ((!empty($_POST['fullname'])) && (!empty($_POST['phone_number'])) && isset($_POST['address_1'])  && isset($_POST['address_2'])  && isset($_POST['city'])  && isset($_POST['postal_code'])  && isset($_POST['region'])  && isset($_POST['website']) )   {

        if ($conn) {
            try {

                $fullname = $_POST['fullname'];
                $phone_number = $_POST['phone_number'];
                $address_1 = $_POST['address_1'];
                $address_2 = $_POST['address_2'];
                $city = $_POST['city'];
                $region = $_POST['region'];
                $postal_code = $_POST['postal_code'];
                $website = $_POST['website'];

                $profile_sql = "UPDATE users SET fullname=?, phone_number=?, address_1=?, address_2=?, city=?, region=?, postal_code=?, website=? WHERE accountId=?";

                $update_profile = $conn->prepare($profile_sql)->execute([$fullname, $phone_number, $address_1, $address_2, $city, $postal_code, $region, $website, $_SESSION['usr_accountId']]);

                die("success");
            } catch (PDOException $e) {
                die('An unexpected error occurred. Connection failed');
            }
        } else {
            die('An unexpected error occurred. Connection failed.');
        }
    } else {
        die('Invalid user information.');
    }
} else {
    die('Invalid request.');
}