<?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);
    $unidade = $_POST['unidade'];
    $pt = $_POST['pt'];
    $local = $_POST['local'];
    $sw = $_POST['sw'];
    $sw_pt = $_POST['sw_pt'];
    $busca_sw_pt=$pdo_sgt->prepare("SELECT * FROM tb_map_canal WHERE unidade=:unidade AND local=:local AND sw=:sw AND sw_pt=:sw_pt");
    $busca_sw_pt->bindValue(":unidade",$unidade);
    $busca_sw_pt->bindValue(":local",$local);
    $busca_sw_pt->bindValue(":sw",$sw);
    $busca_sw_pt->bindValue(":sw_pt",$sw_pt);
    $busca_sw_pt->execute();
    $dados_sw_pt=$busca_sw_pt->fetch(PDO::FETCH_ASSOC);
    if($dados_sw_pt['sw_pt'] == $sw_pt){
        echo"<script language='javascript' type='text/javascript'>
            alert('A porta do switch desta Sala já está em uso!');
            window.location.href='list_canal.php';
        </script>";
    }else{
        $atualizar=$pdo_sgt->prepare("UPDATE tb_map_canal SET sw_pt=:sw_pt WHERE id=:id");
        $atualizar->bindValue(":sw_pt",$sw_pt);
        $atualizar->bindValue(":id",$id);
        $atualizar->execute();
        $mensagem = "Dado alterado na tabela: tb_map_canal - "
            . "Para os campos: sw_pt - Com o seguinte valore: $sw_pt - Do id: $id.";
        salvaLog($mensagem);
        echo"<script language='javascript' type='text/javascript'>
            window.location.href='list_canal.php#id".$pt."';
        </script>";
    }
?>