<?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.php");
    protegePagina();
    include "../conectar.php";
    include "../log_.php";
    if($_SERVER['REQUEST_METHOD']=='GET'){
        $id_get = $_GET['id'];
        $busca=$pdo_sgt->prepare("SELECT * FROM tb_unidades WHERE id=:id");
        $busca->bindValue(":id",$id_get);
        $busca->execute();
        $dados=$busca->fetch(PDO::FETCH_ASSOC);
        $del = $dados['id'];
    }
    elseif($_SERVER['REQUEST_METHOD']=='POST'){
        $id = isset($_POST["id"]) ? $_POST["id"]: '';
        $id = ($id);
        $unidade = isset($_POST["unidade"]) ? $_POST["unidade"]: '';
        $dominio = isset($_POST["dominio"]) ? $_POST["dominio"]: '';
        $ddd = isset($_POST["ddd"]) ? $_POST["ddd"]: '';
        $nome_pt1 = isset($_POST["nome_pt1"]) ? $_POST["nome_pt1"]: '';
        $email_pt1 = isset($_POST["email_pt1"]) ? $_POST["email_pt1"]: '';
        $nome_pt2 = isset($_POST["nome_pt2"]) ? $_POST["nome_pt2"]: '';
        $email_pt2 = isset($_POST["email_pt2"]) ? $_POST["email_pt2"]: '';
        $des_unid = isset($_POST["des_unid"]) ? $_POST["des_unid"]: '';
        $atualizar=$pdo_sgt->prepare("UPDATE tb_unidades
            SET unidade=:unidade,dominio=:dominio,ddd=:ddd,nome_pt1=:nome_pt1,email_pt1=:email_pt1,nome_pt2=:nome_pt2,email_pt2=:email_pt2,des_unid=:des_unid
            WHERE id=:id");
        $atualizar->bindValue(":id",$id);
        $atualizar->bindValue(":unidade",$unidade);
        $atualizar->bindValue(":dominio",$dominio);
        $atualizar->bindValue(":ddd",$ddd);
        $atualizar->bindValue(":nome_pt1",$nome_pt1);
        $atualizar->bindValue(":email_pt1",$email_pt1);
        $atualizar->bindValue(":nome_pt2",$nome_pt2);
        $atualizar->bindValue(":email_pt2",$email_pt2);
        $atualizar->bindValue(":des_unid",$des_unid);
        $atualizar->execute();
        $mensagem = "Dados alterados na tabela: tb_unidades - "
            . "Para os campos: unidade, dominio, ddd, nome_pt1, email_pt1, nome_pt2, email_pt2, des_unid - "
            . "Com os seguintes valores: $unidade, $dominio, $ddd, $nome_pt1, $email_pt1, $nome_pt2, $email_pt2, $des_unid.";
        salvaLog($mensagem);
        echo"<script language='javascript' type='text/javascript'>
            window.location.href='list_unid.php';
        </script>";
    }
?>