<?php
try {
  error_reporting(E_ALL); ini_set('display_errors', 1);
  require_once "./pineapple.php";
  require_once "./mysql/helper.php";
  pineapple_html_begin("Pineapple PNP", "Promotional Credits");

  $db = db_connect("pontech_pineapplepnp");

  //print("<pre>\n");
  //print_r($_POST);
  //print_r($_SESSION);  // Warning, printing session vars could lead to security issues
  //print("</pre>\n");
	
  function db_table_prepared_test($db, $statement, $prepared_array, $id, $link)
{	
try
{
	$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
  $sth = $db->prepare ($statement);
  $sth->execute($prepared_array);

  print("<table id=\"$id\">\n");
  print("<tr>\n"); 
  foreach(range(0, $sth->columnCount() - 1) as $column_index)
  {
    print("<th>" . $sth->getColumnMeta($column_index)["name"] . "</th>\n");
  }
  print("</tr>\n");

	$row_count = 0;
  while($row = $sth->fetch(PDO::FETCH_BOTH))
  {
		$row_count++;
		print("<tr>\n");
    for($i = 0; $i < $sth->columnCount(); $i++)
    {
			if(isset($link[$i]["link"]))
			{
				$link_str = $link[$i]["link"];
				if(!($link_str == ""))
				{
					$get_string = "";
					foreach ($link[$i][0] as $column=>$value) {
						if(!($get_string == ""))
							$get_string = $get_string . "&";
						$get_string = $get_string . "$value=" . htmlspecialchars($row[$column]);
					}
					$href = "<a href=\"$link_str?$get_string\">";
					$href_prime = "</a>";
				}
			}
			else
			{
				$href = "";
				$href_prime = "";
			}
      print("<td>$href" . htmlspecialchars($row[$i]) . "$href_prime</td>\n");
    }
    ?>
    <!--td>
      <form name="edit_credit" action="pineapple_promotional_credit_detail_action.php" method="POST">
        <input type="hidden" name="transaction_id" value="<?php //echo $row[0]; ?>"/>
        <input type="submit" name="transaction_action" value="Edit"/>
      </form>
    </td-->
    <td>
      <form name="edit_credit" action="pineapple_promotional_credit_detail.php" method="POST">
        <input type="hidden" name="transaction_id" value="<?php echo $row[0]; ?>"/>
        <input type="hidden" name="customer_email" value="<?php echo $row[1]; ?>"/>
        <input type="submit" name="transaction_action" value="Delete"/>
      </form>
    </td>
    <?php
    print("</tr>\n");
  }
  print("</table>\n");
}
catch (PDOException $e)
{
	print "<br />";
	print_r("\n" . $e . "\n");
}
}
  
function db_table_test($db, $statement, $id, $link)
{
	try
	{
		db_table_prepared_test($db, $statement, NULL, $id, $link);
	}
	catch(PDOException $e)
	{
		# Print error information from exception object
		print (" getCode value: " . $e-> getCode () . "\ n");
		print (" getMessage value: " . $e-> getMessage () . "\ n");
		# Print error information from database handle
		print (" errorCode value: " . $db-> errorCode () . "\ n");
		print (" errorInfo value: " . join (",", $db-> errorInfo ()) . "\ n");

		// DuBois, Paul (2013-03-28). MySQL (5th Edition) (Developer's Library) (Kindle Locations 16308-16311). Pearson Education. Kindle Edition. 		
	}
}
  
	if(!isset($_SESSION['email']))
  {
		echo "<br>You must sign in to view this page<br>";
    goto exit_page;
  }
  
  if($_SESSION['admin'] != '1') {
    ?>
      <h1>This account does not have priveliges for viewing this page</h1>
    <?php
    goto exit_page;
  }
  
  $email = $_SESSION['email'];
  $customer_email = $_POST['customer_email'];
  
  // entered email account check
  $sql = "SELECT * FROM user WHERE email = '$customer_email'";
  $result = $db->query($sql);
  $num_rows = $result->rowCount();
  if ($num_rows == 0){
    ?>
		<p>The entered email is not registered. Please click <a href="pineapple_promotional_credit.php">here</a> to go back.</p>
		<?php
    goto exit_page;
  }
  
  if(isset($_POST['select_transaction_type'])){
    $statement =  "INSERT INTO user_account (email, transaction_type, transaction_notes, transaction_amount) " .
                  "VALUES (?, ?, ?, ?)";
    $prepared_array = array($customer_email, $_POST['select_transaction_type'], $_POST['transaction_notes'], $_POST['transaction_amount']);
    $stmt = $db->prepare($statement);
    $count = $stmt->execute($prepared_array);
  }
  
  if(isset($_POST['transaction_action'])){
    if($_POST['transaction_action']=='Delete'){
      $sql = "DELETE FROM user_account WHERE transaction_id = ?";
      $sth = $db->prepare($sql);
      $sth->execute(array($_POST['transaction_id']));
    }
  }
  
  // display all credits
  $statement = "SELECT transaction_id, email, transaction_date, transaction_type, transaction_notes, transaction_amount FROM user_account WHERE email='$customer_email'";
  db_table_test($db, $statement, "t01", NULL);
  
  $db = NULL;
  
  ?>
  <button id="opener-button">Update Promotional Credits</button>
  <?php
  // Create the dialog, it's display type is set to none so it doesn't appear when
  // the page is loading.
  ?>
  <div id="dialog1" title="Promotional Credits" style="display: none;">
  <div class="login-form">
    <form method="post" action="pineapple_promotional_credit_detail.php" name="update_order_status">
      <p>Transaction Type</p>
      <select name = "select_transaction_type">
        <option value="Sign Up Bonus">Sign Up Bonus</option>
        <option value="Referral Bonus">Referral Bonus</option>
        <option value="Applied Credit">Applied Credit</option>
      </select>
        <p>Notes</p>
        <textarea type="text" name="transaction_notes" cols="80" rows="10"></textarea>
        <input type="number" name="transaction_amount"></a>
        <input type="hidden" name="customer_email" value="<?php echo $customer_email; ?>"/>
        <input type="submit" onclick="myFunction()" value="Submit" > <a href="#" class=" icon arrow"></a>                                                                                                                                                                                                                                 </h4>
    </form>
  </div>
  </div>
  <?php
  
exit_page:

	pineapple_script_end();
  
  ?>
  <script>
  // Turn the <div> with id = 'dialog' into a jQuery dialog
  $('#dialog1').dialog({
    autoOpen: false,
    width: 625,
    height: 450
  });
  // If you press the button open the dialog
  $('#opener-button').click(function() {
      $('#dialog1').dialog('open');
      return false;
  });
  </script>
  <?php
  
	pineapple_html_end();
}
catch (PDOException $e)
{
  print ("<br />$e");
}
?>