<?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'){
        $busca=$pdo_sgt->prepare("SELECT * FROM tb_tipo_hd WHERE id=:id");
        $busca->bindValue(":id",$_GET['id']);
        $busca->execute();
        $dados=$busca->fetch(PDO::FETCH_ASSOC);
        $del = $dados['id'];
    }
    elseif($_SERVER['REQUEST_METHOD']=='POST'){
        $id = isset($_POST["id"]) ? $_POST["id"]: '';
        $id = ($id);
        $tipo = isset($_POST["tipo"]) ? $_POST["tipo"]: '';
        $select=$pdo_sgt->prepare("SELECT * FROM tb_tipo_hd WHERE tipo=:tipo");
        $select->bindValue(":tipo",$tipo);
        $select->execute();
        $array = $select->fetch(PDO::FETCH_ASSOC);
        if($array['tipo'] == $tipo){
            echo"<script language='javascript' type='text/javascript'>
                alert('Este Tipo já existe!');
                window.history.go(-1)>Volta;
            </script>";
            die();
        }
        else{
            $atualizar=$pdo_sgt->prepare("UPDATE tb_tipo_hd SET tipo=:tipo WHERE id=:id");
            $atualizar->bindValue(":id",$id);
            $atualizar->bindValue(":tipo",$tipo);
            $atualizar->execute();
            $mensagem = "Dados alterados na tabela: tb_tipo_hd - Para os campos: tipo - Com os seguintes valores: $tipo.";
            salvaLog($mensagem);
            echo"<script language='javascript' type='text/javascript'>
                window.location.href='list_tipo_hd.php';
            </script>";  
        }
    }
?>