<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <span>Ferramentas</span>
            <i class="fa fa-circle"></i>
        </li>
        <li>
            <span>Categorias</span>
            <i class="fa fa-circle"></i>
        </li>
        <li>
            <span>Sub-Categoria</span>
        </li>
    </ul>
</div>
<?php
$data = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$subid = filter_input(INPUT_GET, 'subid', FILTER_DEFAULT);

if (!empty($data['sendSubUp'])):
    unset($data['sendSubUp']);

    require('_models/AdminSubcategoria.class.php');
    $cadastra = new AdminSubcategoria;
    $cadastra->ExeUpdate($subid, $data);

    WSErro($cadastra->getError()[0], $cadastra->getError()[1]);
else:
    $read = new Read;
    $read->ExeRead("app_subcategorias", "WHERE subcategoria_id = :id", "id={$subid}");

    if (!$read->getResult()):
        header('Location: painel.php?exe=categoria/categoria&empty=true');
    else:
        $data = $read->getResult()[0];
    endif;
endif;
?>
<div class="portlet">
    <div class="portlet-title">
        <div class="caption">
            <span class="caption-subject font-dark bold uppercase">Atualizar Subcategoria</span>
        </div>
    </div>
    <div class="portlet-body">
        <form name="formCat" role="form" method="POST" action="" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-5 col-sm-5 col-xs-5">
                    <select class="form-control" name="categoria_id">
                        <option selected="selected" value="">Selecione uma categoria...</option>
                        <?php
                        $ReadCat = new Read;
                        $ReadCat->ExeRead("app_categorias", "ORDER BY categoria_nome ASC");

                        if ($ReadCat->getResult()):
                            foreach ($ReadCat->getResult() as $cat):
                                extract($cat);
                                $selected = ($data['categoria_id'] == $categoria_id ? "selected=\"selected\" " : " ");
                                ?>
                                <option <?= $selected ?> value="<?= $categoria_id ?>"><?= $categoria_nome ?></option>
                                <?php
                            endforeach;
                        endif;
                        ?>
                    </select>
                    <div class="form-control-focus"> </div>
                </div>
                <div class="col-md-5 col-sm-5 col-xs-5">
                    <input type="text" class="form-control" name="subcategoria_titulo" value="<?php if (isset($data)) echo $data['subcategoria_titulo']; ?>" placeholder="Subcategoria"> 
                </div>
                <div class="col-md-2 col-sm-2 col-xs-2">  
                    <button type="submit" name="sendSubUp" value="enviar" class="btn btn-success pull-right"><i class="fa fa-plus-circle"></i> <span class="hidden-xs">Atualizar subcategoria</span></button>
                </div>
            </div>
        </form>
        <hr>
    </div>   
</div>