<?php
#   SGT - The SGT for Internet - http://sgt.eti.br
#   Copyright (c) 2016-2018, Jean Azevedo. All rights reserved.

#   This file is part of SGT.

#   SGT is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   any later version.

#   SGT is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

    include("../seguranca.php");
    protegePagina();
    include "../var_sub.php";
    $qry = $pdo_sgt->query("SELECT * FROM tb_usuarios WHERE id='".$id_temp."'");
    $temp = $qry->fetch(PDO::FETCH_ASSOC);
    if($unid_master != $temp['unid_temp']){
       $unid_master = $temp['unid_temp'];
    }
    $cont = 0;
?>
<!DOCTYPE HTML>
<html>
<head>
    <?php
        include '../css.html';
    ?>
    <meta charset="UTF-8">
    <title>lista de Monitores - SGT</title>
</head>
<body>
    <div class="main_sgt">
        <?php
            include '../menu.html';
        ?>
        <div class="container-fluid">
            <div class="table-responsive-sm">
                <table class="table-responsive-xl table-striped table-hover table-condensed table-bordered">
                    <thead>
                        <tr align="center" bgcolor=#B8DAFF>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Cód.
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Editar
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Arquivo
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Unidade
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Marca
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Modelo
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Número de Série
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Data de Aquisição
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Término da Garantia
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    N. do Imobilizado
                                </div>
                            </th>
                            <th scope="col">
                                <div class="mx-sm-1">
                                    Computador
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                        include "../conectar.php";
                        if($unid_master == 1){
                           $buscar_query = ("SELECT * FROM tb_monitores WHERE ativo='1' ORDER BY unidade, cod_m");
                        } else {
                           $buscar_query = ("SELECT * FROM tb_monitores WHERE unidade='".$unid_master."' AND ativo='1' ORDER BY unidade, cod_m");
                        }
                        $buscar = $pdo_sgt->query($buscar_query);
                        $cont = $buscar->rowCount();
                        if($cont > 0){
                            while($ver = $buscar->fetch(PDO::FETCH_ASSOC)){
                                $valor_marc = "";
                                $valor_mod = "";
                                $busca_unid=$pdo_sgt->query("SELECT * FROM tb_unidades WHERE id='".$ver['unidade']."'");
                                $dados_unid = $busca_unid->fetch(PDO::FETCH_ASSOC);
                                $qry_mod = $pdo_sgt->query("SELECT * FROM tb_modelos WHERE id='".$ver['modelo']."'");
                                while ($linha_mod = $qry_mod->fetch(PDO::FETCH_ASSOC)){
                                    $valor_mod = $linha_mod['modelo'];
                                    $sql_marc = "SELECT * FROM tb_marcas WHERE id='".$linha_mod['marca']."'";
                                    $qry_marc = $pdo_sgt->query($sql_marc);                    
                                    while ($linha_marc = $qry_marc->fetch(PDO::FETCH_ASSOC)){
                                        $valor_marc = $linha_marc['marca'];   
                                    }  
                                }
                                if($sgturl.$ver['f_url']== ""){
                                    $url= '';
                                } else {
                                    $url= '<a href="'.$sgturl.$ver['f_url'].'" target="_blank"><img title="Editar - '.$ver['cod_m'].'" src="../img/doc.png" height="15" width="15"></a>';
                                }
                                $busca_comp=$pdo_sgt->query("SELECT * FROM tb_computadores WHERE cod_mon1='".$ver['id']."' OR cod_mon2='".$ver['id']."'");
                                $dados_comp = $busca_comp->fetch(PDO::FETCH_ASSOC);
                                echo'<tr align="center">'
                                    . '<td class="align-middle" scope="row" title="Código - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$ver['cod_m']
                                        . '</div>'
                                    . '</td>'
                                    . '<td class="align-middle" title="Editar - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            . '<a href="unid_alt_mon.php?id='.$ver['id'].'">'
                                                . '<img title="Editar - '.$ver['cod_m'].'" src="../img/editar.png" height="15" width="15">'
                                            . '</a>'
                                        . '</div>'
                                    . '</td>'
                                    . '<td class="align-middle" title="Arquivo da Nota Fiscal - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$url
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Data de Unidade - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$dados_unid['unidade']
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Data de Marca - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$valor_marc
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Data de Modelo - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$valor_mod
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Data de Número de Série - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$ver['nserie']
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Data de Data de Aquisição - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                        .date('d/m/Y', strtotime($ver['daquise']))
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Data de Término da Garantia - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .date('d/m/Y', strtotime($ver['garantia']))
                                        . '</div>'
                                    .'</td>'
                                    . '<td class="align-middle" title="Número do Imobilizado - '.$ver['cod_m'].'">'
                                        . '<div class="mx-sm-1">'
                                            .$ver['nptri']
                                        . '</div>'
                                    .'</td>'
                                        . '<td class="align-middle" title="Computador">'
                                        . '<div class="mx-sm-1">'
                                            .$dados_comp['cod_c']
                                        . '</div>'
                                    .'</td>'
                                . '</tr>';
                            }
                        }else{
                            echo'Sem dados cadastrais<br>';
                        }
                        echo'</tbody>'
                        . '<tfoot>Contagem de monitores '.$cont.'</table>'
                    ?>
                </table>
            </div>
        </div>
        <?php
            include '../roda_pe.html';
        ?>
    </div>
</body>
</html>