<?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(isset($_POST['deleteprocess'])) {
	$ticketid = $_POST['ticketid'];
	$ticketid = AntiXSS::setEncoding($ticketid, "UTF-8");
	$ticketid = str_replace('"','',$ticketid);
	$ticketid = str_replace("'","",$ticketid);
	$ticketid = str_replace(">","",$ticketid);
	$ticketid = str_replace("<","",$ticketid);
	$ticketid = str_replace("</","",$ticketid);
	$ticketid = str_replace("&%","",$ticketid);
	$ticketid = str_replace("script","",$ticketid);
	$ticketid = str_replace("(","",$ticketid);
	$ticketid = str_replace(")","",$ticketid);
	$question = $_POST['question'];
	$question = AntiXSS::setEncoding($question, "UTF-8");
	$question = str_replace('"','',$question);
	$question = str_replace("'","",$question);
	$question = str_replace(">","",$question);
	$question = str_replace("<","",$question);
	$question = str_replace("</","",$question);
	$question = str_replace("&%","",$question);
	$question = str_replace("script","",$question);
	$question = str_replace("(","",$question);
	$question = str_replace(")","",$question);
	if($question == '1') {
	$delete = mysql_query("DELETE FROM Tickets_All WHERE id='$ticketid'");
	$query = "SELECT * FROM Tickets_Entries WHERE ticketid='$ticketid'";
	$result = mysql_query($query) or die('Error, query failed');
	while($row = mysql_fetch_array($result))
	{
		$actualid = $row['id'];
		$delete = mysql_query("DELETE FROM Tickets_Entries WHERE id='$actualid'");
	}
	?>
		<div style="margin-top: -80px;" class="alert alert-success" role="alert">
			<strong>Well done!</strong> Ticket deleted.
		</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>
<?php
	if(isset($_POST['enter'])) {
		$ticketid = $_POST['ticketid'];
		if($_SESSION['acctype'] == 0) {
				$accounttypenow = 'publisher';
			}elseif($_SESSION['acctype'] == 1) {
				$accounttypenow = 'advertiser';
			}
		mysql_query("UPDATE Tickets_All SET client_readed='1' WHERE id='$ticketid'") or die(mysql_error());
		if(isset($_POST['message'])) {
			mysql_query("UPDATE Tickets_All SET client_answered='1' WHERE id='$ticketid'") or die(mysql_error());
			mysql_query("UPDATE Tickets_All SET support_answered='0' WHERE id='$ticketid'") or die(mysql_error());
			mysql_query("UPDATE Tickets_All SET support_readed='0' WHERE id='$ticketid'") or die(mysql_error());
			$insertmessage = $_POST['message'];
			mysql_query("INSERT INTO Tickets_Entries VALUES('','$ticketid','$session_username','$accounttypenow','1','1','0','0','$date','$ip','$insertmessage')");
		}
		?>
	<div class="row">
      <div class="col-md-8 col-md-offset-2">
        <div class="well">
			<div class="row">
				<center>
				<form name="send" id="send" action="" method="POST">
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
							<input class="form-control" placeholder="Message..." name="message" type="text">
							<span class="input-group-btn">
								<button type="submit" class="btn btn-default" form="send" name="enter" value="Send"><i class="fa fa-paper-plane"></i> Send</button>
							</span>
					</div>
					<input type="hidden" name="ticketid" value="<?php echo $ticketid; ?>">
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				<form name="refresh" id="refresh" method="POST">
					<span class="input-group-btn">
						<button type="submit" class="btn btn-default" form="refresh" name="enter" value="Submit"><i class="fa fa-refresh"></i></button>
					</span>
					<input type="hidden" name="ticketid" value="<?php echo $ticketid; ?>">
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
				</form>
				</center>
			</div>
        </div>
      </div>
    </div>
		<?php
		$query = "SELECT * FROM Tickets_Entries WHERE ticketid='$ticketid' ORDER BY id DESC";
		$result = mysql_query($query) or die('Error, query failed');
    	while($row = mysql_fetch_array($result))
		{
			mysql_query("UPDATE Tickets_Entries SET client_readed='1' WHERE ticketid='$ticketid'") or die(mysql_error());
			if($row['writertype'] == 'support') {
				$class = 'pull-right';
				$classtext = 'class="text-danger"';
				if($row['client_readed'] == '1') {
					$class2 =	'border-right: 5px solid #5CB85C;';
				}
			}else {
				$class =	'';
				$classtext =	'class="text-success;"';
				if($row['support_readed'] == '1') {
					$class2 =	'border-left: 5px solid #5CB85C;';
				}
			}
		?>
			<blockquote class="<?php echo $class;?>" style="width:100%;<?php echo $class2;?>">
  				<p <?php echo $classtext;?>><?php echo $row['message'];?></p>
  				<small><?php echo $row['writertype'];?> :<cite title="Source Title"><?php echo $row['writerusername'];?></cite></small>
			</blockquote>
		<?php
		}
	}elseif(isset($_POST['delete'])) {
		$ticketid = $_POST['ticketid'];
	?>		
	<div class="row">
      <div class="col-md-8 col-md-offset-2">
        <div class="well">
			<div class="row">
				<center>
				<form name="deleteee" id="deleteee" action="" method="POST">
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
							<center>
							<span class="input-group-btn">
								<div class="form-group has-success">
										<div class="input-group">
               						<span class="input-group-addon">Are you sure?</span>
               						<input id="q1" class="toggle toggle-left" name="question" value="1" type="radio"><label style="min-width:30.2%;width:auto" class="btn btn-default" for="q1" class="btn"><i class="fa fa-plus-circle"></i> Yes</label>
											<input id="q2" class="toggle toggle-right" name="question" value="0" type="radio"><label style="min-width:30.2%;width:auto" class="btn btn-default" for="q2" class="btn"><i class="fa fa-minus-circle"></i> No</label>
           							</div>
								</div>
								<button onclick="history.go(-1)" class="btn btn-default"><i class="fa fa-refresh"></i></button>
								<button type="submit" class="btn btn-default" form="deleteee" name="deleteprocess" value="Submit"><i class="fa fa-trash"></i> Continue</button>
							</span>	
							</center>				
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
					<input type="hidden" name="ticketid" value="<?php echo $ticketid; ?>">
				</form>
				</br>
				</center>
			</div>
        </div>
      </div>
    </div>
    <?php
	}else {
?>     
   <div class="row">
      <div class="col-md-8 col-md-offset-2">
		<div class="row">
			<div class="col-md-8 col-md-offset-2">
			<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">
      <h1><small>List of your 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;'>#</th>

						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Title</th>

						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Supporter</th>

						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Posts</th>

						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Status</th>

						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a061; ?></th>

					</tr>

				</thead>

	<?php

		$query = "SELECT * FROM Tickets_All WHERE client_username='$session_username' ORDER BY id ASC";

		$result = mysql_query($query) or die('Error, query failed');

		if(mysql_num_rows($result)<1){

   	}else{

			$numberoflink = 0;

    		while($row = mysql_fetch_array($result))

			{

				$numberoflink = $numberoflink + 1;
				$tickettitle = $row['title'];
				$supportusername = $row['support_username'];
				$posts = $row['posts'];
				$readed = $row['client_readed'];
				$answered = $row['client_answered'];
				if($posts == $readed) {
					$statusreaded = '<span class="label label-success">READED</span>';
				}else {
					$statusreaded = '<span class="label label-danger">UNREADED</span>';
				}
				if($posts == $answered) {
					$statusanswered = '<span class="label label-success">ANSWERED</span>';
				}else {
					$statusanswered = '<span class="label label-danger">UNANSWERED</span>';
				}
				?>
					<tr>
						<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
							<?php echo $numberoflink; ?>
						</td>
						<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
							<?php echo $tickettitle; ?>
						</td>
						<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
							<?php echo $supportusername; ?>
						</td>
						<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
							<?php echo $posts; ?>
						</td>
						<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
							<center>
								<?php echo $statusreaded; ?> <?php echo $statusanswered; ?>
							</center>
						</td>
						<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
							<center>
								<form method='POST'>
									<input type='hidden' name='ticketid' value='<?php echo $row['id'];?>'>
									<button type="submit" name="enter" class="btn btn-success"> <i class="fa fa-plus-circle"></i> Enter</button> 
									<button type="submit" name="delete" class="btn btn-danger"> <i class="fa fa-minus-circle"></i> Delete</button>
								</form>
							</center>
						</td>
					</tr>
				<?php

			}

    	}

	?>

				</table>
        </div>
<?php
	}
?>
       
</div>

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