<?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_aux_unid.php");
    protegePagina();
    include "../conectar.php";
    include "../log_.php";
    $id = isset($_POST["id"]) ? $_POST["id"]: '';
    $id = ($id);
    $mac = $_POST['mac'];
    if($mac != "" || $mac != null){
        $busca_ip_=$pdo_sgt->prepare("SELECT * FROM tb_map_ip WHERE id=:id");
        $busca_ip_->bindValue(":id",$id);
        $busca_ip_->execute();
        $dados_ip_ = $busca_ip_->fetch(PDO::FETCH_ASSOC);
        $ip_unidade = $dados_ip_['unidade'];
        $ip_rede = $dados_ip_['rede'];
        $busca = $pdo_sgt->prepare("SELECT * FROM tb_map_ip WHERE mac='$mac' AND unidade='$ip_unidade' AND rede='$ip_rede'");
        $busca->bindValue(":mac",$mac);
        $busca->bindValue(":unidade",$ip_unidade);
        $busca->bindValue(":rede",$ip_rede);
        $busca->execute();
        $dados = $busca->fetch(PDO::FETCH_ASSOC);
        if($dados['mac'] == $mac){
            echo"<script language='javascript' type='text/javascript'>
                alert('Este ".$mac." já existe!');
                window.history.go(-1)>Volta;
            </script>";
            die();
        } else {
            $atualizar=$pdo_sgt->prepare("UPDATE tb_map_ip SET mac=:mac WHERE id=:id");
            $atualizar->bindValue(":mac",$mac);
            $atualizar->bindValue(":id",$id);
            $atualizar->execute();
            $mensagem = "Dado alterado na tabela: tb_map_ip - "
            . "Para o campo: mac - "
            . "Com o seguinte valore: $mac "
            . "Do id: $id.";
            salvaLog($mensagem);
        }
    }else{
        $atualizar=$pdo_sgt->prepare("UPDATE tb_map_ip SET mac=:mac WHERE id=:id");
        $atualizar->bindValue(":mac",$mac);
        $atualizar->bindValue(":id",$id);
        $atualizar->execute();
        $mensagem = "Dado alterado na tabela: tb_map_ip - "
        . "Para o campo: mac - "
        . "Com o seguinte valore: $mac "
        . "Do id: $id.";
        salvaLog($mensagem);
    }
    echo"<script language='javascript' type='text/javascript'>
        window.location.href='list_ip_unid.php#id".$id."';
    </script>";
?>