<?php
require('../setClassPath.php');
require_once('object/User.php');
require_once('object/Picture.php');
require_once('db/UserDB.php');
require_once('db/TransManager.php');
require_once('ui/Component.php');
require_once('util/InputHelper.php');

$helper     = InputHelper::getInstance();
$error      = (intval($_GET['err']) == 1);
$uswitch    = intval($_GET['u']);

session_start();
$user = $_SESSION['user'];
if (!$user)
    exit();

$trans = TransManager::getInstance();
$trans->beginTrans();
$userdb = new UserDB();

$headline = $userdb->getHeadline($user->getUserId(), $trans->getPDO());
$location = $user->getLocation();
if (isset($_SESSION['tmpTags'])) {
    $tags = $_SESSION['tmpTags'];
    unset($_SESSION['tmpTags']);
}
else 
    $tags     = implode(', ', $userdb->getUserInterestTags($user->getUserId(), $trans->getPDO()));
if (isset($_SESSION['tmpDescription'])) {
    $description = $_SESSION['tmpDescription'];
    unset($_SESSION['tmpDescription']);
}
else {
    if ($details = $userdb->getProfileDetails($user->getUserId(), $trans->getPDO()))
        $description = $details->getDescription();
    
}

$trans->commit();
$trans->disconnect();


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Update Your Profile</title>
<link type="text/css" rel="stylesheet" href="/css/main.css">
<style type="text/css">
.hd { font-weight: bold; margin: 10px 0 8px 0; }
</style>
</head>
<body>
<div id="body">
<div id="widetop">
<h1>Update Your Profile</h1>

<?php
if ($error) {
    switch ($uswitch) {
    case 1:
        echo '<p class="lg"><b>ERROR!</b> Sorry, there was a problem locating that zip code. Please try your zip code again, or enter a nearby zip code.</p>';
        break;
    case 2:
        echo '<p class="lg"><b>ERROR!</b> Sorry, there was a problem with your interest tags. Please make sure you use only alphanumeric characters (no special symbols).</p>';
        break;
    case 3:
        echo '<p class="lg"><b>ERROR!</b> Sorry, there was a problem with your description. Please make sure it is at least 200 characters long.</p>';
        break;
    }
}
else {
    switch ($uswitch) {
    case 1:
        echo '<p class="lg"><b>UPDATE:</b> Your location has been updated.  If there is anything else you want to change, go right ahead.</p>';
        break;
    case 2:
        echo '<p class="lg"><b>UPDATE:</b> Your interest tags have been updated.  If there is anything else you want to change, go right ahead.</p>';
        break;
    case 3:
        echo '<p class="lg"><b>UPDATE:</b> Your description has been updated.  If there is anything else you want to change, go right ahead.</p>';
        break;
    case 4:
        echo '<p class="lg"><b>UPDATE:</b> Your profile details have been updated.  If there is anything else you want to change, go right ahead.</p>';
        break;
    default:
        echo '<p class="lg">From here you can make any changes you like to your profile.  Just follow the instructions below.</p>';
    }
}
?>

</div>

<?php
if ($location) {
?>
  <form action="uprofile1_update.html" method="post">
    <div class="hd">Do you no longer live in <?= $location->getCity() .', '. $location->getState() ?>?</div>
    <label for="zipcode">Enter new zip code:</label>
    <input type="text" name="zipcode" size="5" maxlength="5">
    <input type="submit" value="Make Change"><br><br>
  </form>
<?php
}
?>
  <form action="uprofile2_update.html" method="post">
    <div class="hd">Would you like to update your interst tags?</div>
    <label for="tags">Your interest tags:</label>
    <input type="text" name="tags" size="60" maxlength="300" value="<?= $tags ?>">
    <input type="submit" value="Make Change"><br><br>
  </form>

  <form action="uprofile3_update.html" method="post">
    <div class="hd">Would you like to update your description?</div>
    <label for="description">Your description:</label><br>
    <textarea name="description" cols="60" rows="11"><?= $description ?></textarea><br>
    <input type="submit" value="Make Change"><br><br>
  </form>

  <div class="hd">Would you like to update your profile details?</div>
  If you want to change details such as education, income, or religion 
  <a href="uprofile2.html">click here</a> to do so.

<?php
$ui = Component::getInstance();
$ui->drawHeader($user);
$ui->drawFooter();
?>
</div>
</body>
</html>
