<?php
include_once("postget.php");
include_once("common.php");

if ($c <> $config[37][1]) {
    echo _ACCESS_DENIED;
    exit;
}
?>
<html>
    <head>
        <TITLE><?php print($config[0][1]); ?></TITLE>
        <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1.0, maximum-scale=1">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <meta name="description" content="<?php echo $config[81][1] ?>" />
        <meta name="keywords" content="<?php echo $config[83][1] ?>" />
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />

        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>

        <link rel="stylesheet" type="text/css" href="content/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="content/css/bootstrap-responsive.css">
        <link rel="stylesheet" type="text/css" href="content/css/bootmetro.css">
        <link rel="stylesheet" type="text/css" href="content/css/bootmetro-tiles.css">
        <link rel="stylesheet" type="text/css" href="content/css/bootmetro-charms.css">
        <link rel="stylesheet" type="text/css" href="content/css/metro-ui-semilight.css">
        <link rel="stylesheet" type="text/css" href="content/css/icomoon.css">

        <link rel="shortcut icon" href="content/ico/favicon.ico">
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="content/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="content/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="content/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="content/ico/apple-touch-icon-57-precomposed.png">

        <script src="scripts/modernizr.min.js"></script>

    </head>

    <body data-accent="blue">

        <?php
        if ($_REQUEST['reset'] == 1) {
            mysql_query("UPDATE hd_config SET valor='0' WHERE ref='121'", $db_link);
            $config[121][1] = 0;
        }

        if ($_REQUEST['popretrieve'] == 'on') {
            mysql_query("UPDATE hd_config SET valor='1' WHERE ref='121'", $db_link);
            $config[121][1] = 1;
        } elseif ($_REQUEST['popretrieve'] == 'off') {
            mysql_query("UPDATE hd_config SET valor='0' WHERE ref='121'", $db_link);
            $config[121][1] = 0;
        }

        if ($config[121][1] == 1) {
            include_once("scan4tickets.php");
        }


        if ($_POST['department'] <> "" . _ALL . "" AND $_POST['department']) {
            $_SESSION['depart'] = $_POST['department'];
            $_SESSION['department'] = " AND depid=" . $_POST['department'] . " ";
        }

        if ($_POST['department'] == "" . _ALL . "") {
            $_SESSION['depart'] = "";
            $_SESSION['department'] = " ";
        }

        $query = "SELECT * FROM hd_tickets 
WHERE (status=1 OR status=2 OR status=4 OR followup=1) " . $_SESSION['department'] . " 
ORDER BY lastreply ASC";

        $result = mysql_query($query, $db_link);

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

        echo "<form method='POST' class='well form-inline'>";
        echo '<div class="control-group">';

        echo "" . strtoupper($config[0][1]) . "
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
" . mysql_affected_rows() . " " . strtolower(_TICKETS) . "
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
" . ucfirst(_UPDATED_EVERY) . " " . strtoupper($config[53][1]) . " " . strtolower(_SECONDS) . "
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
";

        print("<label class='control-label'>" . _EXTRACT_TICKETS_FROM_EMAIL_ACCOUNTS . "? </label>");

        if ($config[121][1] == 1) {
            print(" " . lang('_ENABLED') . "  <a href='tickets_panel.php?c=" . $config[37][1] . "&popretrieve=off' class='btn'>" . lang('_DISABLE') . "</a>");
        } else {
            print(" " . lang('_DISABLED') . "  <a href='tickets_panel.php?c=" . $config[37][1] . "&popretrieve=on' class='btn'>" . lang('_ENABLE') . "</a>");
        };


        echo '</div>';
        echo "</form>";
        echo '</div>';
        echo '</div>';


        echo '<div class="container">
    <div class="row">
    <table  class="table table-streped">

';


        if ($config[100][1] == 1) {
            $hrmin = _MINUTES;
        } else {
            $hrmin = _HOURS;
        }

        print("<thead><tr>
	<th>" . ucfirst(_STATUS) . "</th> 
	<th>" . ucfirst(_LAST_REPLY) . "</th> 
	<th>" . ucfirst(_ID) . "</th> 
	<th>" . ucfirst(_PRIORITY) . "</th> 
	<th>" . ucfirst(_DEPARTMENT) . "</th> 
	<th>" . ucfirst(_SUBJECT) . "</th> 
	<th>" . ucfirst(_MESSAGE) . "</th> 
	<th>" . ucfirst($hrmin) . "</th> 
	</tr></thead><tbody>");



        $bgc = true;
        $now = time() + $config[31][1] * 3600;
        while ($tickets = mysql_fetch_array($result)) {
            print("<tr><td colspan='8' align='center' height='10'></td></tr>");
            if ($bgc) {
                print("<TR class='row1'>");
                $bgc = !$bgc;
            } else {
                print("<TR class='row2'>");
                $bgc = !$bgc;
            }

            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') {
                $fondo = 'red';
                $letras = 'white';
                print("<td class='frame-title' align='center' bgcolor='$fondo'><b><font color='$letras'>" . strtoupper(_NEW) . "</font></b>$img_followup</td>");
            } elseif ($tickets[status] == '2') {
                $fondo = 'yellow';
                $letras = 'black';
                print("<td class='frame-title' align='center' bgcolor='$fondo'><b><font color='$letras'>" . strtoupper(_PENDING) . "</font></b>$img_followup</td>");
            } elseif ($tickets[status] == '3') {
                $fondo = 'green';
                $letras = 'white';
                print("<td class='frame-title' align='center' bgcolor='$fondo'><b><font color='$letras'>" . strtoupper(_ANSWERED) . "</font></b>$img_followup</td>");
            } elseif ($tickets[status] == '4') {
                $fondo = 'orange';
                $letras = 'white';
                print("<td class='frame-title' align='center' bgcolor='$fondo'><b><font color='$letras'>" . strtoupper(_CUSTOMER_REPLY) . "</font></b>$img_followup</td>");
            } elseif ($tickets[status] == '5') {
                $fondo = 'gray';
                $letras = 'white';
                print("<td class='frame-title' align='center' bgcolor='$fondo'><b><font color='$letras'>" . strtoupper(_CLOSED) . "</font></b>$img_followup</td>");
            } else {
                print("<td></td>");
            }

            if ($tickets[followup] == '1') {
                $fondo = 'gray';
                $letras = 'white';
            }

            print("<td align='center' bgcolor='$fondo'><font color='$letras'>");

            print(TimeAgo($tickets[lastreply], $now));

            if ($tickets[lastreplier] <> '') {
                print("<br>(" . strtolower(_BY) . " " . $tickets[lastreplier] . ")");
            }

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

            print("<td align='center' bgcolor='$fondo'><font color='$letras'>$tickets[ticketid]</font></td>");
            print("<td align='center' bgcolor='$fondo'><font color='$letras'>" . lang('_' . $tickets[priority]) . "</font></td>");

            $depto = query('SELECT name FROM hd_tickets_dep WHERE hd_tickets_dep.depid = ' . $tickets[depid], 1);
            print("<td align='center' bgcolor='$fondo'><font color='$letras'>$depto[0]</font></td>");

            print("<td align='left' bgcolor='$fondo'><font color='$letras'>" . substr(strip_tags($tickets[title]), 0, 100) . "...</font></td>");
            print("<td align='left' bgcolor='$fondo'><font color='$letras'>" . substr(strip_tags($tickets[message]), 0, 300) . "...</font></td>");
            if ($config[100][1] == 1) {
                $hoursbefore = ($now - $tickets[lastreply]) / (60);
            } else {
                $hoursbefore = ($now - $tickets[lastreply]) / (60 * 60);
            }
            print("<td class=tickets_time align='right' bgcolor='$fondo'><font color='$letras'>" . intval($hoursbefore) . "</font></td>");

            print("</tr>");
        }


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


        print("<form name='department' method='POST' style='margin-bottom:0;'>
" . ucfirst(_DEPARTMENT) . ": <select name='department' onChange='this.form.submit()'>");
        $query = "SELECT depid,name FROM hd_tickets_dep ORDER BY orderby";
        $Deptos = mysql_query($query, $db_link);
        print ("<option value='" . _ALL . "'>" . _ALL . "</option>");
        while ($depart = mysql_fetch_array($Deptos)) {
            print ("<option value='" . $depart[depid] . "' " . sele($_SESSION['depart'], $depart[depid]) . ">" . $depart[name] . "</option>");
        };
        print("</select></form>");

        echo $log;

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


        include 'foot_template.php';
        ?>
