<?php

include("postget.php");
include_once("common.php");
$pagetitle = _SUPPORT_TICKETS;

checklevel(2, $_REQUEST['clientid']);

$paging = new pagination;
if ($_REQUEST['orderby']) {
    $orderby = $_REQUEST['orderby'];
} else {
    $orderby = 'date DESC';
}
$paging->mantenerVar('orderby');


if ($_REQUEST['title'] <> "") {
    $where = "
	WHERE (`title` LIKE '%" . $_REQUEST['title'] . "%' OR `message` LIKE '%" . $_REQUEST['title'] . "%')
	";
} else {
    $where = "";
}

if ($_SESSION['hd_userdept'] > 0) {
    if ($where == "") {
        $depto = "WHERE depid=" . $_SESSION['hd_userdept'];
    } else {
        $depto = "AND depid=" . $_SESSION['hd_userdept'];
    }
} else {
    $depto = "";
}


$search = $_REQUEST['title'];


$query = "SELECT * FROM hd_tickets $where $depto ORDER BY " . $orderby . "";
$paging->agregarConsulta($query);
$paging->porPagina($config[32][1]);
$paging->paginasAntes(3, 10, 20, 50);
$paging->paginasDespues(3, 10, 20, 50);
$paging->linkSeparador('&nbsp;');
$paging->linkSeparadorEspecial('...');
$paging->linkClase('pag');
$paging->mostrarActual("<span class=\"pagthis\">{n}</span>");

$paging->ejecutar();


include("head_template.php");

echo '
<div class="container">
	<div class="row">
';

print("
<form method='POST'>
<b>" . ucfirst(_SUBJECT) . " " . strtolower(_OR) . " " . strtolower(_MESSAGE) . ":</b> <input name='title' size=30> 
<input type='submit' name='submittitle' value='" . _SEARCH . "' class='btnform'>
</form>
");



if ($paging->numTotalPaginas() > 1) {
    echo "<div class='pagination'><b>" . ucfirst(_PAGES) . "</b>: " . $paging->fetchNavegacion() . "</div>";
}

echo '<table class="table table-striped">';

print("<thead><tr>
	<th><a href='" . $PHP_SELF . "?orderby=status'>" . ucfirst(_STATUS) . "</a></th> 
	<th>" . _VIEW . "</th> 
	<th><a href='" . $PHP_SELF . "?orderby=lastreply'>" . ucfirst(_LAST_REPLY) . "</a></th> 
	<th><a href='" . $PHP_SELF . "?orderby=ticketid'>" . ucfirst(_ID) . "</a></th> 
	<th><a href='" . $PHP_SELF . "?orderby=depid'>" . ucfirst(_DEPARTMENT) . "</a></th> 
	<th><a href='" . $PHP_SELF . "?orderby=priority'>" . ucfirst(_PRIORITY) . "</a></th> 
	<th><a href='" . $PHP_SELF . "?orderby=title'>" . ucfirst(_SUBJECT) . "</a></th> 
	<th><a href='" . $PHP_SELF . "?orderby=message'>" . ucfirst(_MESSAGE) . "</a></th> 
	</tr></thead><tbody>");



$bgc = true;
$now = time() + $config[31][1] * 3600;
while ($datos = $paging->fetchResultado()) {
    echo '<tr>';
    if ($datos[status] == '1') {
        print("<td><b><font color='red'>" . strtoupper(_NEW) . "</font></b></td>");
    } elseif ($datos[status] == '2') {
        print("<td><b><font color='yellow'>" . strtoupper(_PENDING) . "</font></b></td>");
    } elseif ($datos[status] == '3') {
        print("<td><b><font color='green'>" . strtoupper(_ANSWERED) . "</font></b></td>");
    } elseif ($datos[status] == '4') {
        print("<td><b><font color='orange'>" . strtoupper(_CUSTOMER_REPLY) . "</font></b></td>");
    } elseif ($datos[status] == '5') {
        print("<td><b><font color='gray'>" . strtoupper(_CLOSED) . "</font></b></td>");
    } else {
        print("<td></td>");
    }

    print("<td align='center'>
	<a href='ticketsview.php?ticketid=$datos[ticketid]&clientid=$datos[clientid]&reference=$datos[reference]'><b>" . _VIEW . "</b></a>
	</td>");

    print("<td align='center'>");
    print(TimeAgo($datos[lastreply], $now));
    print("</td>");



    print("<td align='center'>$datos[ticketid]</td>");
    $depto = query('SELECT name FROM hd_tickets_dep WHERE hd_tickets_dep.depid = ' . $datos[depid], 1);
    print("<td align='center'>$depto[0]</td>");

    print("<td align='center'>" . constant('_' . strtoupper($datos[priority])) . "</td>");

    print("<td align='left'>$datos[title]</td>");
    print("<td align='left'>" . substr(strip_tags($datos[message]), 0, 300) . "...</td>");

    print("</tr>");
}

echo '
		</td>
		</tr></tbody>
		</table>
</div>
</div>
';

include("foot_template.php");
?> 
