<?php

if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = base64_decode($_GET['id']); 
    $crud = new crud('tickets'); // Table
    $crud->excluir("id = $id"); // ID
    
    header("Location: index.php?mod=TicketsClosed&y=3");
    }
    
    $cmd1 = mysql_query("SELECT * FROM tickets WHERE status = '1'");
	$ticketsopen = mysql_num_rows($cmd1);
    $cmd2 = mysql_query("SELECT * FROM tickets WHERE status = '2'");
	$ticketsanswed = mysql_num_rows($cmd2);
	$cmd3 = mysql_query("SELECT * FROM tickets WHERE status = '3'");
	$ticketsprocess = mysql_num_rows($cmd3);
	$cmd4 = mysql_query("SELECT * FROM tickets WHERE status = '4'");
	$ticketsclosed = mysql_num_rows($cmd4);
    
?>

<!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            <?php echo $lang['TITLE_TICKETS']; ?>
          </h1>
          
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="row">
			  
            <div class="col-md-3">
              <a href="?mod=NewTicket" class="btn btn-primary btn-block margin-bottom"><?php echo $lang['TITLE_NEW']; ?></a>
              <div class="box box-solid">
                <div class="box-header with-border">
                  <h3 class="box-title">Tickets</h3>
                  <div class="box-tools">
                    <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                  </div>
                </div>
                <div class="box-body no-padding">
                  <ul class="nav nav-pills nav-stacked">
                    <li><a href="?mod=Tickets"><i class="fa fa-circle-o text-green"></i> <?php echo $lang['TICKET_OPEN']; ?> <span class="label label-primary pull-right"><?php echo $ticketsopen; ?></span></a></li>
                    <li><a href="?mod=TicketsAnswered"><i class="fa fa-circle-o text-light-blue"></i> <?php echo $lang['TICKET_ANSWERED']; ?> <span class="label label-primary pull-right"><?php echo $ticketsanswed; ?></span></a></li>
                    <li><a href="?mod=TicketsProcess"><i class="fa fa-circle-o text-yellow"></i> <?php echo $lang['TICKET_PROCESS']; ?> <span class="label label-primary pull-right"><?php echo $ticketsprocess; ?></span></a></li>
                    <li class="active"><a href="?mod=TicketsClosed"><i class="fa fa-circle-o text-red"></i> <?php echo $lang['TICKET_CLOSED']; ?> <span class="label label-primary pull-right"><?php echo $ticketsclosed; ?></span></a></li>
                  </ul>
                </div><!-- /.box-body -->
              </div><!-- /. box -->
              
             
              
            </div><!-- /.col -->
            
            
            <div class="col-md-9">
				
				<?php if($_GET['y'] == '3') { ?>
				<div class="callout callout-success">
                    <h4><?php echo $lang['ALERT_TICKET_DELETE']; ?></h4>
                    <p><?php echo $lang['ALERT_TICKET_DELETE_TXT']; ?></p>
                  </div>
				<?php } ?>
				<?php if($_GET['y'] == '4') { ?>
				<div class="callout callout-warning">
                    <h4><?php echo $lang['ALERT_TICKET_CLOSED']; ?></h4>
                    <p><?php echo $lang['ALERT_TICKET_CLOSED_TXT']; ?></p>
                  </div>
				<?php } ?>
				
              <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title">Inbox - <?php echo $lang['TICKET_CLOSED']; ?></h3>
                  <div class="box-tools pull-right">
                    <div class="has-feedback">
                     
                    </div>
                  </div><!-- /.box-tools -->
                </div><!-- /.box-header -->
                
                <div class="box-body no-padding">
                  <div class="mailbox-controls">
                   
                  <div class="table-responsive mailbox-messages">
					  
                    <table class="table table-hover table-striped">
					<thead>
                      <tr>
                        <th><?php echo $lang['TICKET_CUSTOMER']; ?></th>
                        <th><?php echo $lang['TICKET_SUBJECT']; ?></th>
                        <th><?php echo $lang['TICKET_DEPARTMENT']; ?></th>
                        <th><?php echo $lang['TICKET_DATE']; ?></th>
                        <th><?php echo $lang['TICKET_STATUS']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </thead>	
						
                      <tbody>
						  
					   <?php
				        $pagina = (isset($_GET['pg']))? $_GET['pg'] : 1;
						$cmd = "SELECT * FROM tickets WHERE status = '4' order by id DESC";
						$tickets = mysql_query($cmd);
						$total = mysql_num_rows($tickets);
						$registros = 20;
						$numPaginas = ceil($total/$registros);
						$inicio = ($registros*$pagina)-$registros;
						$tk = mysql_query("SELECT p.*, c.name AS name, c.email AS email, d.name_department AS name_department FROM tickets AS p INNER JOIN customers AS c ON p.customer = c.id INNER JOIN departments AS d ON p.department = d.id WHERE p.status = '4' order by p.date, p.hour DESC limit $inicio,$registros");
						$totalregistros = mysql_num_rows($tk);
						while ($ticket = mysql_fetch_array($tk)) {
							
						?>  
                        <tr class="<?php if($ticket['priority'] == 'A') { ?>danger<?php } ?><?php if($ticket['priority'] == 'M') { ?>warning<?php } ?><?php if($ticket['priority'] == 'B') { ?>default<?php } ?>">
						  <td class="mailbox-name"><?php echo $ticket['name']; ?><br><small><?php echo $ticket['email']; ?></small></td>
                          <td class="mailbox-subject"><?php echo limitText($ticket['subject'],40); ?></td>
                          <td class="mailbox-department"><b><?php echo $ticket['name_department']; ?></b></td>
                          <td class="mailbox-date"><?php if($lang['DATE_FORMAT'] == 'PT') { ?><?php echo data_br($ticket['date']); ?><?php } ?><?php if($lang['DATE_FORMAT'] == 'EN') { ?><?php echo data_en($ticket['date']); ?><?php } ?><br><?php echo $ticket['hour']; ?></td>
						  <td class="mailbox-success">
							  <?php if($ticket['status'] == '1') { ?><span class="label label-success"><?php echo $lang['TICKET_OPEN']; ?></span><?php } ?>
							  <?php if($ticket['status'] == '2') { ?><span class="label label-info"><?php echo $lang['TICKET_ANSWERED']; ?></span><?php } ?>
							  <?php if($ticket['status'] == '3') { ?><span class="label label-warning"><?php echo $lang['TICKET_PROCESS']; ?></span><?php } ?>
							  <?php if($ticket['status'] == '4') { ?><span class="label label-danger"><?php echo $lang['TICKET_CLOSED']; ?></span><?php } ?>
							  </td>
						  <td class="mailbox-actions">
						  <a class="btn btn-success btn-sm" href="?mod=TicketResume&id=<?php echo base64_encode($ticket['id']); ?>"><i class="fa fa-search"></i></a>
							  
							  
					      <a class="btn btn-danger btn-sm" href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_DELETE']; ?>')) { window.location.href='?mod=TicketsClosed&id=<?php echo base64_encode($ticket['id']); ?>&action=delete' } else { void('') };"><i class="fa fa-remove"></i></a>
							  </td>
                        </tr> 
                        <?php } ?>  
                      </tbody>
                    </table><!-- /.table -->
                    
                    
                  </div><!-- /.mail-box-messages -->
                </div><!-- /.box-body -->
                
                
                <div class="box-footer no-padding">
                  <div class="mailbox-controls">
                    
                    <button class="btn btn-default btn-sm" onClick="window.location.reload()"><i class="fa fa-refresh"></i></button>
                    <div class="pull-right">
                     <?php echo $total; ?> <?php echo $lang['COUNT_REGISTER']; ?> / 
                      <div class="btn-group">
				      <?php
				      for($i = 1; $i < $numPaginas + 1; $i++) {
					  ?>
					  <a class="btn btn-default btn-sm <?php if($_GET['pg'] == $i) { echo 'active'; } ?>" href="?mod=Tickets&pg=<?php echo $i; ?>"><?php echo $i; ?></a>
                      <?php } ?> 
                      
                      </div><!-- /.btn-group -->
                    </div><!-- /.pull-right -->
                  </div>
                </div>
              </div><!-- /. box -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
