<?php
//TITLE
$ref_title = $contactPageT;
// INCLUDE HEADER IN PAGE
include('other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
if($_POST['delete']) {
	$ticketid = $_POST['ticketid'];
	?>
		<div style="margin-top: -80px;" class="alert alert-success" role="alert">
			<?php echo $a232; ?>
		</div>
	<?php
}
if($_POST['submit']) {
					$q1 = "SELECT `tickets` FROM `Accounts_Classic_Advertisers` WHERE `username` = '$session_username'";
					$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
					$tickets = mysql_result($r1, 0);
					if($tickets < 5) {
					// GET DATA FROM FORM
					$title = mysql_real_escape_string($_POST['title']);
					$message = mysql_real_escape_string($_POST['message']);
					
					$query = "SELECT * FROM Accounts_Premium_Support ORDER BY rand() LIMIT 6";
					$result = mysql_query($query) or die('Error, query failed');
			
					if(mysql_num_rows($result)<1)
					{
   					echo '0 SUPPORTERS IN DATABASE';
   				}
   				else
   				{
     					while($row = mysql_fetch_array($result))
						{
							$assignedsupporter = $row['username'];
						}
					}
					// IF ALL DATA ARE OK
					if($title&&$message&&$assignedsupporter) {
						// IMPORT MESSAGE TO DB
						if($_SESSION['acctype'] == 0) {
							$accounttypenow = 'publisher';
						}elseif($_SESSION['acctype'] == 1) {
							$accounttypenow = 'advertiser';
						}
																							//ID   TITLE    CLIENT USERNAME     CLIENT TYPE       SUPPORT USERNAME    S TYPE     DATE    IP    POSTS  CLIENT READED    CLIENT ANSWERED    SUPPORT READED   SUPPORT ANSWERED
						$addmsg1 = mysql_query("INSERT INTO Tickets_All VALUES('','$title','$session_username','$accounttypenow','$assignedsupporter','support','$date','$uip','1','1','1','0','0')") or die("2<br/><br/>".mysql_error());
						$q1 = "SELECT `id` FROM `Tickets_All` WHERE `title` = '$title' AND `client_username` = '$session_username' AND `client_type` = '$accounttypenow' AND `support_username` = '$assignedsupporter' AND `support_type` = 'support' AND `date` = '$date' AND `createdbyip` = '$uip'";
						$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
						$id1 = mysql_result($r1, 0);
						$addmsg2 = mysql_query("INSERT INTO Tickets_Entries VALUES('','$id1','$session_username','$accounttypenow','1','1','0','0','$date','$uip','$message')") or die("3<br/><br/>".mysql_error());
						$ticketsnew = $tickets + 1;
						$addmsg = mysql_query("UPDATE `Accounts_Classic_Advertisers` SET `tickets` = '$ticketsnew' WHERE `username` = '$session_username'") or die("3<br/><br/>".mysql_error());
						$addmsg = mysql_query("UPDATE `Accounts_Classic_Publishers` SET `tickets` = '$ticketsnew' WHERE `username` = '$session_username'") or die("3<br/><br/>".mysql_error());
						?>
							<div style="margin-top: -80px;" class="alert alert-success" role="alert">
								<?php echo $a232; ?>
							</div>
						<?php
						}else{
						?>
							<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
								<?php echo $a238; ?>
							</div>
						<?php
						}
					}else{
						?>
							<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
								You can have only 5 tickets max.
							</div>
						<?php
						}						
}

// CSRF PROTECTION
if ( isset( $_POST[ 'field' ] ) )
{
    try
    {
        // Run CSRF check, on POST data, in exception mode, for 10 minutes, in one-time mode.
        NoCSRF::check( 'csrf_token', $_POST, true, 60*10, false );
        // form parsing, DB inserts, etc.
        // ...
        $result = 'CSRF check passed. Form parsed.';
    }
    catch ( Exception $e )
    {
        // CSRF attack detected
        $result = $e->getMessage() . ' Form ignored.';
    }
}
else
{
    $result = 'No post data yet.';
}
// Generate CSRF token to use in form hidden field
$token = NoCSRF::generate( 'csrf_token' );
?>

<div class="container">

    <div class="text-center">
      <h1><?php echo $title; ?> <small>/ Support / Tickets</small></h1>
    </div></br>

    <!-- BASE CSS   =========================================================== -->

    <!-- Tables -->

    <div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div class="row">
			<div class="col-md-8 col-md-offset-2">
			<?php
				if($_POST['enter']) {
					$ticketid = $_POST['ticketid'];
					?>
					enter
					</div>
					</div>
					</div>
					</div>
					<?php
				}else {
			?>
			<form name="csrf_form" method="POST">
				<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				<div class="input-group">
					<span style="width: 100px;" class="input-group-addon"><?php echo"$a004"; ?></span>
					<input style="width: 350px;" type="text" class="form-control" name="title">
				</div>
				<div class="input-group">
					<span style="width: 100px;" class="input-group-addon"><?php echo"$a109"; ?></span>
					<textarea name="message" style="width: 350px;" name="message" class="form-control" rows="5"></textarea>
				</div>
				<div class="input-group">
					<input name="submit" value="<?php echo"$a094"; ?>" class="btn btn-info btn-block btn-lg" type="submit">
				</div>
			</form>
			</div>
			</br>
			</br>
			</br>
        </div>
      </div>
    </div>
    <div class="text-center">
    	<?php
    		$q1 = "SELECT `tickets` FROM `Accounts_Classic_Advertisers` WHERE `username` = '$session_username'";
			$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
			$tickets = mysql_result($r1, 0);
		?>
      <h1>Tickets<small>/<?php echo $tickets; ?></small></h1>
    </div>
		<div class="col-sm-12">
          <table class="table table-bordered" style="width:100%;max-width:100%"  data-effect="fade">
										<thead>
											<tr>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>ID</th>
												<th style='max-width:140px;color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a004; ?></th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Assigned Supporter</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Creation date</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Viewed</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Answered</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Actions</th>
											</tr>
										</thead>
											<?php
			$query = " SELECT * FROM Tickets_All WHERE client_username='$session_username'";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td>
							</td>
							<td>
								You don't have created any tickets, if you need help please create one.
							</td>
							<td>
							</td>
							<td>
							</td>
							<td>
							</td>
							<td>
							</td>
							<td>
							</td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					if (empty($row['title'])) {
									$row['title'] = "---";
					};
					?>
						<tr>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<h5><?php echo $numberoflink; ?></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<h5><?php echo $title; ?></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<h5><a href="profile.support.<?php echo $row['support_username']; ?>"><?php echo $row['support_username']; ?></a></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<h5></a><?php echo $row['date']; ?></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<h5><?php if($row['client_readed'] == 0){$readed = "No";}else{$readed = "Yes";} echo $readed; ?></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<h5><?php if($row['client_answered'] == 0){$answered = "No";}else{$answered = "Yes";} echo $answered; ?></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
								<form style='display:inline-block' name='csrf_form' method='POST'>
									<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
									<input type="hidden" name="ticketid" value="<?php echo $row['id'];?>">
									<input name='enter' value='View' class='btn btn-default btn-sm' type='submit'>
								</form>
								<form style='display:inline-block' name='csrf_form' method='POST'>
									<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
									<input type="hidden" name="ticketid" value="<?php echo $row['id'];?>">
									<input name='delete' value='<?php echo $a111; ?>' class='btn btn-default btn-sm' type='submit'>
								</form>
							</td>
							</tr>
						<?php
				}   
      }
	?>
									</table>
				
        </div>
        <?php
				}   
				?>
</div>
</div>

<?php
include('other_footer.php');
?>