<?php

include("postget.php");
include_once("common.php");

$pagetitle = lang("_TICKETS");

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

////////////////////// revertir suspension o eliminación //////////////////////////////////
if ($_REQUEST[reverse] AND $_SESSION['hd_accesslvl'] > 1) {
    if ($_REQUEST[reverse] == 'suspension') {
        $query = "UPDATE hd_clients SET suspended='0' WHERE clientid='" . $_SESSION['hd_clientid'] . "'";
        if (!mysql_query($query, $db_link)) {
            messagetop(_ERROR, mysql_error(), 'main.php', 0);
        } else {
            logevent('_SUSPENSION_HAS_BEEN_REVERSED', $_SESSION['hd_clientid'], $_SESSION['hd_idc']);
        }
    }
    if ($_REQUEST[reverse] == 'elimination') {
        $query = "UPDATE hd_clients SET active='1' WHERE clientid='" . $_SESSION['hd_clientid'] . "'";
        if (!mysql_query($query, $db_link)) {
            messagetop(_ERROR, mysql_error(), 'main.php', 0);
        } else {
            logevent('_ELIMINATION_HAS_BEEN_REVERSED', $_SESSION['hd_clientid'], $_SESSION['hd_idc']);
        }
    }
}



moduleaccess(64);

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


$thisclient = 1;


if ($_SESSION['hd_accesslvl'] == 1) {
    $where = " WHERE hd_tickets.clientid=" . $_SESSION['hd_clientid'] . "";
} else {
    if (is_numeric($_SESSION['hd_clientid'])) {
        $where = " WHERE hd_tickets.clientid=" . $_SESSION['hd_clientid'] . "";
    } else {
        $where = " ";
    }
}


$query = "SELECT * FROM hd_tickets $where ORDER BY $orderby";

include("head_template.php");

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

$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();

normalbutton("ticketsnew.php?clientid=" . $_SESSION['hd_clientid'] . "", _OPEN_NEW_TICKET);

echo '<br>

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






print("<tr><td colspan='9' align='center'>");

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




print("</td></tr>");


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



$bgc = true;
$now = time() + $config[31][1] * 3600;
while ($tickets = $paging->fetchResultado()) {

    if ($bgc) {
        print("<TR class='row1'>");
        $bgc = !$bgc;
    } else {
        print("<TR class='row2'>");
        $bgc = !$bgc;
    }
    if ($tickets[followup] == '1') {
        $fondo = 'bgcolor=gray';
    } else {
        $fondo = '';
    }

    if ($tickets[followup] == '1') {
        $img_followup = "&nbsp;&nbsp;&nbsp;<img class='sprites followup' src='images/transparent.gif' title='" . ucfirst(_FOLLOW_UP) . "'>";
    } else {
        $img_followup = '';
    }


    if ($tickets[status] == '1') {
        print("<td><b><font color='red'>" . strtoupper(_NEW) . "</font></b>$img_followup</td>");
    } elseif ($tickets[status] == '2') {
        print("<td><b><font color='yellow'>" . strtoupper(_PENDING) . "</font></b>$img_followup</td>");
    } elseif ($tickets[status] == '3') {
        print("<td><b><font color='green'>" . strtoupper(_ANSWERED) . "</font></b>$img_followup</td>");
    } elseif ($tickets[status] == '4') {
        print("<td><b><font color='orange'>" . strtoupper(_CUSTOMER_REPLY) . "</font></b>$img_followup</td>");
    } elseif ($tickets[status] == '5') {
        print("<td><b><font color='gray'>" . strtoupper(_CLOSED) . "</font></b>$img_followup</td>");
    } else {
        print("<td nowrap>&nbsp;</td>");
    }

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

    //	print("<td align='left'>".date($df.' H:i:s',$tickets[date])."</td>");
    print("<td>");
    print(TimeAgo($tickets[lastreply], $now));
    print("</td>");



    print("<td>$tickets[ticketid]</td>");
    print("<td>");
    switch (strtolower($tickets[priority])) {
        case 'low':
            echo lang('_' . $tickets[priority]);
            break;
        case 'medium':
            echo lang('_' . $tickets[priority]);
            break;
        case 'high':
            echo lang('_' . $tickets[priority]);
            break;
    }
    print("</td>");

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


    print("<td>$tickets[title]</td>");
    print("<td>" . substr(strip_tags($tickets[message]), 0, 200) . "...</td>");



    print("</tr>");
}

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


echo '</div></div>';
include("foot_template.php");
?> 