<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);
$catid = filter_input(INPUT_GET, 'subid', FILTER_DEFAULT);

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

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

    WSErro($cadastra->getError()[0], $cadastra->getError()[1]);

endif;
?>
<div class="portlet">
    <div class="portlet-title">
        <div class="caption">
            <span class="caption-subject font-dark bold uppercase">Nova Subcategoria</span>
        </div>
    </div>
    <div class="portlet-body">
        <form name="formCat" role="form" method="POST" action="painel.php?exe=categoria/subcategoria" 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 $uf):
                                extract($uf);
                                ?>
                                <option 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" placeholder="Subcategoria"> 
                </div>
                <div class="col-md-2 col-sm-2 col-xs-2">  
                    <button type="submit" name="sendSub" value="enviar" class="btn btn-success pull-right"><i class="fa fa-plus-circle"></i> <span class="hidden-xs">Adicionar subcategoria</span></button>
                </div>
            </div>
        </form>
        <hr>
    </div>   
</div>
<?php
$delCat = filter_input(INPUT_GET, 'delete', FILTER_VALIDATE_INT);
if ($delCat):
    require ('_models/AdminSubcategoria.class.php');
    $deletar = new AdminSubcategoria;
    $deletar->ExeDelete($delCat);

    WSErro($deletar->getError()[0], $deletar->getError()[1]);
endif;
?>
<div class="portlet">
    <div class="portlet-title">
        <form class="form-inline pull-right" action="#">
            <div class="input-group input-medium">
                <input type="text" class="form-control" placeholder="Buscar" id="campo_busca" name="campo_busca">
                <span class="input-group-btn">
                    <button type="button" class="btn green" onclick="javascript:busca_orcamento(1);">
                        <i class="fa fa-search"></i>
                    </button>
                </span>
            </div>
        </form>
    </div>

    <div class="portlet-body">
        <table class="table table-striped table-bordered table-hover table-checkable order-column" id="sample_1">
            <thead>
                <tr>
                    <th> Categoria </th>
                    <th> Subcategoria </th>
                    <th> Data </th>
                    <th>
                    </th>
                </tr>
            </thead>
            <tbody>
                <?php
                $Read = new Read;
                $Read->ExeRead("app_subcategorias");
                if (!$Read->getResult()):
                    echo '<tr>';
                    echo '<td class="text-center" colspan="3">';
                    WSErro("Sem resultados!", WS_INFOR);
                    echo '</td>';
                    echo '</tr>';
                else:
                    foreach ($Read->getResult() as $subcategoria):
                        extract($subcategoria);
                        $ReadCat->FullRead("SELECT categoria_titulo FROM app_categorias WHERE categoria_id = :catId", "catId={$categoria_id}");
                        $categoria_titulo = $ReadCat->getResult()[0]['categoria_titulo'];
                        ?>
                        <tr class="odd gradeX">
                            <td>
                                <?= $categoria_titulo ?>
                            </td>
                            <td>
                                <?= $subcategoria_titulo ?>
                            </td>
                            <td>
                                <?= $data = date('d/m/Y', strtotime($subcategoria_data)); ?>
                            </td>
                            <td>
                                <a href="painel.php?exe=categoria/subcategoria_update&subid=<?= $subcategoria_id ?>" class="btn btn-xs green"><i class="fa fa-edit"></i></a>
                                <a href="painel.php?exe=categoria/subcategoria&delete=<?= $subcategoria_id ?>" class="btn btn-xs red"><i class="fa fa-times"></i></a>
                            </td>
                        </tr>
                        <?php
                    endforeach;
                endif;
                ?>
            </tbody>
        </table>
    </div>

</div>