<?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_adm_av.php" ;
    protegePagina();
    include "../conectar.php";
    include "../log_.php";
    if($_SERVER['REQUEST_METHOD']=='POST'){
        $id = isset($_POST["id"]) ? $_POST["id"]: '';
        $id = ($id);
        $pin = isset($_POST["pin"]) ? $_POST["pin"]: '';
        $select_u=$pdo_sgt->prepare("SELECT * FROM tb_voip_pin WHERE id=:id");
        $select_u->bindValue(":id",$id);
        $select_u->execute();
        $dados_u = $select_u->fetch(PDO::FETCH_ASSOC);
        $select_p=$pdo_sgt->prepare("SELECT * FROM tb_voip_pin WHERE unidade=:unidade AND pin=:pin");
        $select_p->bindValue(":unidade",$dados_u['unidade']);
        $select_p->bindValue(":pin",$pin);
        $select_p->execute();
        $dados_p = $select_p->fetch(PDO::FETCH_ASSOC);
        if($pin === $dados_p['pin'] && $id != $dados_p['id']){
            echo"<script language='javascript' type='text/javascript'>
                alert('Este PIN para esta Unidade já existe!');
                window.history.go(-1)>Volta;
            </script>";
        }else{
            $atualizar=$pdo_sgt->prepare("UPDATE tb_voip_pin SET pin=:pin WHERE id=:id");
            $atualizar->bindValue(":pin",$pin);
            $atualizar->bindValue(":id",$id);
            $atualizar->execute();
            $mensagem = "Dados alterados na tabela: tb_voip_pin - Para o campo: pin - Com o seguinte valore: $pin.";
            salvaLog($mensagem);
            echo"<script language='javascript' type='text/javascript'>
                window.location.href='list_voip_pin.php';
            </script>";
        }
    }
?>