<?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/>.

    ini_set('default_charset','UTF-8');
    include "../seguranca_aux_unid.php" ;
    protegePagina();
    include "../conectar.php";
    include "../log_.php";
    $id = isset($_POST["id"]) ? $_POST["id"]: '';
    $id = ($id);
    $setor = isset($_POST["setor"]) ? $_POST["setor"]: '';
    $select=$pdo_sgt->prepare("SELECT * FROM tb_setores WHERE setor=:setor");
    $select->bindValue(":setor",$setor);
    $select->execute();
    $array = $select->fetch(PDO::FETCH_ASSOC);
    if($array['setor'] == $setor && $array['id'] != $id){
        echo"<script language='javascript' type='text/javascript'>
            alert('O setor: ".$setor." já existe!');
            window.location.href='list_local.php#id".$id."';
        </script>";
    }
    else{
        $atualizar=$pdo_sgt->prepare("UPDATE tb_setores SET setor=:setor WHERE id=:id");
        $atualizar->bindValue(":id",$id);
        $atualizar->bindValue(":setor",$setor);
        $atualizar->execute();
        $mensagem = "Dados alterados na tabela: tb_setores - Para o campo: setor - Com o seguinte valore: $setor.";
        salvaLog($mensagem);
        echo"<script language='javascript' type='text/javascript'>
            window.location.href='list_setor.php#id".$id."';
        </script>";
    }
?>