<?php // pineapple_user_settings.php  

	require_once "./pineapple.php";		// this file has the menu, header and css code for the site
	require_once "./mysql/helper.php";	// this is general purpose mysql helper code, code work anywhere

	// This line renders the menu and header for the site
	pineapple_html_begin("Pineapple PNP", "Pineapple User Settings");

	if( isset($_SESSION['email']) )
		$email = $_SESSION['email'];

	if(!isset($email)) {
		?>
		<p>Please click <a href="pineapple_login.php">here</a> to login.</p>
		<?php
		goto exit_page;
	}

	$db = db_connect("pontech_pineapplepnp");
  
  // Enjoying pineapples since
  $sql = "SELECT date(creation_date) AS creation_date FROM pontech_pineapplepnp.user WHERE email = ?";
  $prepared_array = array($email);
  $sth = $db->prepare($sql);
  $success = $sth->execute($prepared_array);
  $row = $sth->fetch (PDO::FETCH_NUM);
  echo "Enjoying pineapples since " . $row[0] . "!";
  
  // Customer information
  print "<h1>Personal Information</h1>";
  $sql = "SELECT fullname, country, phone_num FROM pontech_pineapplepnp.user WHERE email = ? limit 1 ";
  $prepared_array = array($email);
  
  db_edit_row($db, $sql, $prepared_array, "pineapple_personal_information_edit_action.php",
              array("country" => "select country_code, country from phone_num_country_code order by country asc")
              );
  echo "Please enter your phone number without any special characters or a country code. Ex: 1234567890";
  
  // Addresses
  $sql = "SELECT address_label, street, city, state, zip, country FROM user_address WHERE email = ?";
	$prepared_array = array($email);
  $sth = $db->prepare($sql);
  $success = $sth->execute($prepared_array);

  $row_count = $sth->rowCount();
  
  if ($row_count == 0)
  {
    print "<h1>No addresss found, please add a shipping address</h1>";
    $sql = "SELECT address_label, street, city, state, zip, country FROM user_address WHERE email = ? limit 1 ";
    $prepared_array = array($email);
    
    db_edit_row($db, $sql, $prepared_array, "pineapple_address_edit_action.php",
                array()
                );
    goto exit_page;
  }
  else
  {
    if ($row_count > 1)
    {
      print "<h1>Found $row_count addresses in our database</h1>";
    }
    else
    {
      print "<h1>Found $row_count address in our database</h1>";
    }
    while ($row = $sth->fetch (PDO::FETCH_NUM))
    {
      $sql = "SELECT address_label, street, city, state, zip, country FROM user_address WHERE email = ? AND address_label = ?";
      $prepared_array = array($email, $row[0]);
      db_edit_row($db, $sql, $prepared_array, "pineapple_address_edit_action.php",
                  array()
                  );
      print "<br>";
    }
  }
  ?>
  
  <form action="pineapple_user_new_address.php" method="post">
  <input type="submit" value="Add Address"></form>
  
  <?php
  
exit_page:
  // This is the end of the HTML body, scripting starts after this.
  pineapple_script_end();
	// This is the end of the HTML file, scripting starts after this.
  pineapple_html_end();

//$username = $result->fetchColumn(3);
?>