
<script src="assets/global/plugins/jquery.validate.min.js" type="text/javascript"></script>
<script>
    $(function () {
        // Ação de validação e atualização do formulario de newsletter
        $("#formEdi").validate({
            //definir as regras
            //definir as regras
            rules: {
                name: {
                    required: true, minlength: 2
                }
            },
            //Definir as mensagens de erro para cada regra
            messages: {
                name: {
                    required: "O nome do newsletter é obrigatório", minlength: "O nome precisar ter no mínimo 3 letras"
                }
            },
            submitHandler: function (form) {
                var dados = $(form).serialize();

                $.ajax({
                    type: "POST",
                    url: "__jsc/ajax/newsletter.php",
                    data: dados,
                    dataType: 'json',
                    success: function (data)
                    {
                        console.log(data);
                        if (data.error[1] == 'accept') {
                            $("<div class=\"trigger trigger-" + data.error[1] + "\">" + data.error[0] + "</div>").prependTo(".form-horizontal");
                        } else {
                            $("<div class=\"trigger trigger-" + data.error[1] + "\">" + data.error[0] + "</div>").prependTo(".form-horizontal");
                        }
                    }
                });

                return false;
            }
        });

    });
</script>
<ul class="page-breadcrumb breadcrumb">
    <li>
        <span>Newsletter</span>
        <i class="fa fa-circle"></i>
    </li>
    <li>
        <span>Alterar Newsletter</span>
    </li>
</ul>
<div class="portlet light">
    <div class="portlet-title">
        <div class="caption">
            <span class="caption-subject font-dark bold uppercase">Alterar Newsletter</span>
        </div>
        <div class="actions">
            <a href="painel.php?exe=newsletter/lista" class="btn btn-xs default" alt="lista" title="lista"><i class="fa fa-chevron-left"></i> <span> Voltar</span></a>
        </div>
    </div>
    <div class="portlet-body">
        <?php
        $data = filter_input_array(INPUT_POST, FILTER_DEFAULT);
        $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
        if (!empty($data['SendPostForm'])):
            unset($data['SendPostForm']);

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


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

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

            if (!$read->getResult()):
                header('Location: painel.php?exe=newsletter/lista&empty=true');
            else:
                $data = $read->getResult()[0];
            endif;
        endif;
        ?>
        <form class="form-horizontal" id="formEdi" name="UseForm" method="POST" action="" enctype="multipart/form-data">
            <div class="form-body">
                <div class="form-group ">
                    <label class="col-md-2 control-label" for="form_control_1">Nome
                        <span class="required">*</span>
                    </label>
                    <div class="col-md-10">
                        <input type="text" class="form-control" placeholder="" name="name" value="<?php if (isset($data['name'])) echo htmlspecialchars($data['name']); ?>">
                        <div class="form-control-focus"> </div>
                    </div>
                </div>
                <div class="form-group ">
                    <label class="col-md-2 control-label" for="form_control_1">E-Mail
                        <span class="required">*</span>
                    </label>
                    <div class="col-md-10">
                        <input type="text" class="form-control" placeholder="" name="email" value="<?php if (isset($data['email'])) echo htmlspecialchars($data['email']); ?>">
                        <div class="form-control-focus"> </div>
                    </div>
                </div>
            </div>
            <div class="form-actions">
                <div class="row">
                    <div class="col-md-offset-2 col-md-10">  
                        <input type="hidden" name="id" value="<?= $id; ?>">
                        <input type="hidden" name="action" value="update">
                        <button type="submit" name="SendPostForm" value="enviar" class="btn btn-warning"><i class="fa fa-save"></i> <span class="hidden-xs"> Atualizar</span></button>
                    </div>
                </div>
            </div>
        </form>
        <hr>
    </div>   
</div>