<?php
require("config.php");
require("includes/debug.php");
require("includes/functions.php");
require("includes/logincheck.php");
require("includes/settings.php");

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//$aid = $_SESSION['adminid'];
$aid = null;
if ($_SESSION['adminlevel'] == "1") {
    $aid = $_SESSION['adminid'];
}
if (isset($_GET["p"])) {

    $param = json_decode(base64_decode($_GET["p"]), true);
    if (key_exists("id", $param)) {
        $aid = $param["id"];
    }
}

//var_dump($_GET);

$SQL = "SELECT us.*,(SELECT username FROM cmum_admins AS ad WHERE ad.id = us.addedby) AS revendedor,DATEDIFF(us.expiredate,NOW()) as data_dif FROM cmum_udb AS us WHERE 1=1";
if (isset($aid)) {
    $SQL .= " AND addedby = '{$aid}'";
}

/*if (isset($_GET['listopt'])) {
    if ($_GET['listopt'] == 'ativo') {
        $SQL .= " AND (us.enabled = 'true' AND us.expiredate > NOW() AND DATEDIFF(us.expiredate,us.added) > 1)";
    } elseif ($_GET['listopt'] == 'desativado') {
        $SQL .= " AND (us.enabled = 'false' OR us.expiredate < NOW())";
    } elseif ($_GET['listopt'] == 'teste') {
        $SQL .= " AND (us.enabled = 'true' AND us.expiredate > NOW() AND DATEDIFF(us.expiredate,us.added) = 1)";
    }
}*/

if (isset($_GET['listopt'])) {
    if ($_GET['listopt'] == 'ativo') {
        $SQL .= " AND (us.enabled = 'true' AND us.expiredate > NOW() AND DATEDIFF(us.expiredate,us.added) > 1)";
    } elseif ($_GET['listopt'] == 'desativado') {
        $SQL .= " AND (us.enabled = 'false')";
    } elseif ($_GET['listopt'] == 'teste') {
        $SQL .= " AND (us.enabled = 'true' AND us.expiredate > NOW() AND DATEDIFF(us.expiredate,us.added) = 1)";
    } 
    elseif ($_GET['listopt'] == 'vencido') {
        $SQL .= " AND us.expiredate < NOW() AND (us.enabled <> 'false')";
    }
}

if (isset($_GET["inptbusca"])) {
    $_GET["inptbusca"] = addslashes($_GET["inptbusca"]);
    $SQL .= " AND (us.name LIKE '%{$_GET["inptbusca"]}%' OR us.email LIKE '%{$_GET["inptbusca"]}%' OR us.displayname LIKE '%{$_GET["inptbusca"]}%' OR ad.username LIKE '%{$_GET["inptbusca"]}%')";
}
//Ordenaçãoes
if (isset($_GET['orderby'])) {
    if ($_GET['orderby'] == "id") {
        $SQL .= " ORDER BY us.id";
    }
    if ($_GET['orderby'] == "status") {
        $SQL .= " ORDER BY us.enabled";
    }
    if ($_GET['orderby'] == "nome") {
        $SQL .= " ORDER BY us.displayname";
    }
    if ($_GET['orderby'] == "login") {
        $SQL .= " ORDER BY us.name";
    }
    if ($_GET['orderby'] == "dias_restante") {
        $SQL .= " ORDER BY data_dif";
    }
    if ($_GET['orderby'] == "vencimento") {
        $SQL .= " ORDER BY us.expiredate";
    }
    if (isset($_GET['class']) && $_GET['class'] == "a") {
        $SQL .= " ASC";
    }
    if (isset($_GET['class']) && $_GET['class'] == "d") {
        $SQL .= " DESC";
    }
} else {
    $SQL .= " ORDER BY us.name ASC";
}

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

$logins = array();

if ($rs = $conn->query($SQL)) {
    while ($login = $rs->fetch_assoc()) {
        $logins[] = $login;
    }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title><?php echo $longtitle; ?></title>
        <link rel="shortcut icon" href="favicon.ico"/>
        <link rel="apple-touch-icon" href="favicon.png"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <!--<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />-->
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <!--<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>-->
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
        <style>
            body{
                width: 980px;
                margin: auto;
            }
            @media print {
                .label-success {
                    background-color: green !important;
                    -webkit-print-color-adjust: exact; 
                    color: white !important;
                    border: none;   
                }
                 .label-warning {
                    background-color: orange !important;
                    -webkit-print-color-adjust: exact; 
                    color: white !important;
                    border: none;   
                }
                 .label-danger {
                    background-color: red !important;
                    -webkit-print-color-adjust: exact; 
                    color: white !important;
                    border: none;   
                }
            }
            
            
            </style>
        </head>
        <body>
            <h4><?php echo $longtitle; ?></h4>
            <p>Página de Impressão</p>
            <!-- Tabela-->
            <table class="table table-bordered table-condensed" style="font-size: 8pt">
            <thead>
                <th>ID</th>
                <th>Status</th>
                <th>Nome</th>
                <th>Login</th>
                <th>Senha</th>
                <th>Email</th>
                <th>Revendedor</th>
                <th>Vencimento</th>
                <th>Restam</th>
                <th>Observaçoes</th>
                <th>Valor Cobrado</th>
            </thead>
            <tbody>
                <?php
                foreach ($logins as $login) :
                    if ($login['enabled'] == 'true') {
                        $status = '<span class="label label-success">ativo</span>';
                    }
                    if (strtotime($login['expiredate']) < time()) {
                        $status = '<span class="label label-warning">vencido</span>';
                    }
                    if ($login['enabled'] == 'false') {
                        $status = '<span class="label label-danger">desativado</span>';
                    }

                    $agora = new DateTime(date('Y-m-d H:i:s'));
                    $vencimento = new DateTime($login['expiredate']);
                    $dias_restantes = $agora->diff($vencimento);
                    $d_restante = $dias_restantes->days;
                    //var_dump($dias_restantes);
                    if ($dias_restantes->invert == 1) {
                        $d_restante = 0;
                    }
                    ?>
                    <?php //for ($i = 0; $i < 100; $i++): ?>
                    <tr>
                        <td><?php echo $login['id']; ?></td>
                        <td><?php echo $status; ?></td>
                        <td><?php echo $login['displayname']; ?></td>
                        <td><?php echo $login['name']; ?></td>
                        <td><?php echo $login['password']; ?></td>
                        <td><?php echo $login['email']; ?></td>
                        <td><?php echo $login['revendedor']; ?></td>
                        <td><?php echo $vencimento->format('d/m/Y H:i:s'); ?></td>
                        <td><?php echo $d_restante ?> dias</td>
                        <td><?php echo $login['obs_user']; ?></td>
                        <td><?php echo $login['valor_cob']; ?></td>
                    </tr>
                    <?php //endfor;  ?>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="11">Total da Listagem: <?php echo $rs->num_rows ?></td>
                </tr>
            </tfoot>
        </table>
    </body>
</html>