<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <span>Catalogo</span>
            <i class="fa fa-circle"></i>
        </li>
        <li>
            <span>Categoria</span>
            <i class="fa fa-circle"></i>
        </li>
        <li>
            <span>Editar</span>
        </li>
    </ul>
</div>
<?php
$data = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$categoriaid = filter_input(INPUT_GET, 'categoriaid', FILTER_VALIDATE_INT);
if (!empty($data['sendForm'])):
    //$data['categoria_status'] = (isset($data['categoria_status']) ? '0' : '1');
    unset($data['sendForm']);

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


    WSErro($cadastra->getError()[0] . '. <a href="painel.php?exe=categoria/index">Voltar pra lista</a>', $cadastra->getError()[1]);

else:
    $read = new Read;
    $read->ExeRead("tp_categorias", "WHERE categoria_id = :id", "id={$categoriaid}");

    if (!$read->getResult()):
        header('Location: painel.php?exe=categoria/index&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">Editar Categoria</span>
        </div>
    </div>
    <div class="portlet-body">
        <form class="form-horizontal j_categoria" name="formCat" role="form" method="POST" action="" enctype="multipart/form-data">
            <div class="form-body">
                <div class="form-group">
                    <label class="col-md-2 control-label">Categoria Nome</label>
                    <div class="col-md-10">
                        <input type="hidden" name="categoria_id" value="<?php if (isset($data['categoria_id'])) echo $data['categoria_id']; ?>"> 
                        <input type="text" class="form-control" name="categoria_titulo" value="<?php if (isset($data['categoria_titulo'])) echo $data['categoria_titulo']; ?>" placeholder="Categoria"> 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Descrição</label>
                    <div class="col-md-10">
                        <textarea rows="6" class="form-control" name="categoria_descricao"><?php if (isset($data['categoria_descricao'])) echo $data['categoria_descricao']; ?></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Pai</label>
                    <div class="col-md-10">
                        <select class="form-control" name="categoria_parent">
                            <option selected="selected" value="">Categoria Pai...</option>
                            <?php
                            $ReadCat = new Read;
                            $ReadCat->ExeRead("tp_categorias", "ORDER BY categoria_nome ASC");

                            if ($ReadCat->getResult()):
                                foreach ($ReadCat->getResult() as $cat):
                                    extract($cat);
                                    $selected = ($data['categoria_parent'] == $categoria_id ? "selected=\"selected\" " : " ");
                                    ?>
                                    <option <?= $selected ?> value="<?= $categoria_id ?>"><?= $categoria_titulo ?></option>
                                    <?php
                                endforeach;
                            endif;
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Imagem</label>
                    <div class="col-md-10">
                        <div class="fileinput fileinput-new" data-provides="fileinput">
                            <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
                                <img src="uploads/categorias/<?= $data['categoria_image'] ?>" alt="" /> </div>
                            <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"> </div>
                            <div>
                                <span class="btn default btn-file">
                                    <span class="fileinput-new"> Selecionar imagem </span>
                                    <span class="fileinput-exists"> Alterar </span>
                                    <input type="file" name="categoria_image"> </span>
                                <a href="javascript:;" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Topo</label>
                    <div class="col-md-10">
                        <select class="form-control" name="categoria_top">
                            <option value="1">Sim</option>
                            <option value="0">Não</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Ordem</label>
                    <div class="col-md-10">
                        <input type="number" class="form-control" name="categoria_ordem" value="<?php if (isset($data['categoria_ordem'])) echo $data['categoria_ordem']; ?>"> 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Status</label>
                    <div class="col-md-10">
                        <select class="form-control" name="categoria_status">
                            <option value="1">Ativado</option>
                            <option value="0">Desativado</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="form-actions">
                <div class="row">
                    <div class="col-md-offset-2 col-md-10">  
                        <button type="submit" name="sendCatUp" value="enviar" class="btn btn-success"><i class="fa fa-save"></i> <span class="hidden-xs"> Salvar</span></button>
                    </div>
                </div>
        </form>
        <hr>
    </div>   
</div>
<script type="text/javascript" src="<?= INCLUDE_PATH; ?>/js/jquery.form.min.js"></script>
<script type="text/javascript">
    $('.j_categoria').submit(function () {
        var form = $(this);
        console.log(form);
        $(".trigger").remove();
        form.ajaxSubmit({
            url: '__jsc/ajax/categoria_create.php',
            dataType: 'json',
            data: {action: 'update'},
            breforeSubmit: function () {
                console.log('antes');
            },
            uploadProgress: function () {
                console.log('durante');

            },
            success: function (data) {
                console.log(data)
                if (data.error[1] == 'accept') {
                    // document.location.href = 'painel.php?exe=mensagem/sucesso';
                    $("<div class=\"trigger " + data.error[1] + "\">" + data.error[0] + "</div>").prependTo(".j_categoria");
                    $('html, body').animate({scrollTop: 0}, 'slow');
                } else {
                    $("<div class=\"trigger " + data.error[1] + "\">" + data.error[0] + "</div>").prependTo(".j_categoria");
                    $('html, body').animate({scrollTop: 0}, 'slow');
                }
            }
        })

        return false;
    });
//    function comboCity(id_uf, id) {
//        // campos
//        var city = $('.j_loadcity');
//
//        $.ajax({
//            url: '__jsc/ajax/city.php',
//            type: 'POST',
//            data: {usuario_estado: id_uf},
//            success: function (data) {
//                console.log(data);
//                city.html(data);
//
//                city.val(id);
//            }
//        })
//    }
//    window.onload = comboCity(<?= $data['estado_id'] ?>, <?= $data['cidade_id'] ?>);
</script>