<?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";
    $unidade = $_POST['unidade'];
    $func = $_POST['func'];
    $pin = $_POST['pin'];
    $part = (isset($_POST['part'])) ? 1 : 0;
    $select_pin=$pdo_sgt->prepare("SELECT * FROM tb_voip_pin WHERE unidade=:unidade AND pin=:pin");
    $select_pin->bindValue(":unidade",$unidade);
    $select_pin->bindValue(":pin",$pin);
    $select_pin->execute();
    $array_pin = $select_pin->fetch(PDO::FETCH_ASSOC);
    $select_p=$pdo_sgt->prepare("SELECT * FROM tb_voip_pin WHERE unidade=:unidade AND func=:func AND part=:part");
    $select_p->bindValue(":unidade",$unidade);
    $select_p->bindValue(":func",$func);
    $select_p->bindValue(":part",$part);
    $select_p->execute();
    $array_p = $select_p->fetch(PDO::FETCH_ASSOC);
    if($array_pin['pin'] == $pin){
        echo"<script language='javascript' type='text/javascript'>
            alert('Este PIN para esta Unidade já existe!');
            window.history.go(-1)>Volta;
        </script>";
    }else{
        if($array_p['part'] == $part && $part != 0){
            echo"<script language='javascript' type='text/javascript'>
                alert('PIN Particular para esta Funcionário já existe!');
                window.history.go(-1)>Volta;
            </script>";
        }else{
            $query=$pdo_sgt->prepare("INSERT INTO tb_voip_pin (unidade,func,pin,part) VALUES (:unidade,:func,:pin,:part)");
            $query->bindValue(":unidade",$unidade);
            $query->bindValue(":func",$func);
            $query->bindValue(":pin",$pin);
            $query->bindValue(":part",$part);
            $query->execute();
            $mensagem = "Dados inseridos na tabela: tb_voip_pin - Para os campos: unidade, func, pin e part - Com os seguintes valores: $unidade, $func, $pin e $part.";
            salvaLog($mensagem);
            echo"<script language='javascript' type='text/javascript'>
                window.history.go(-1)>Volta;
            </script>";
        }
    }
?>