<?php
   require $_SERVER["DOCUMENT_ROOT"].'/inc/topo.php';
   require $_SERVER["DOCUMENT_ROOT"].'/inc/menu.php';
?>
<link rel="stylesheet" href="/assets/vendor_plugins/iCheck/all.css">
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
   <!-- Content Header (Page header) -->
   <section class="content-header">
      <h1>
         Mudar Senha
         <small>Conta</small>
      </h1>
   </section>
   <!-- Main content -->
   <section class="content">
      <div class="row">
        <div class="col-6 offset-3">
           <div class="box box-solid bg-black">
              <div class="box-header">
                 <h4 class="box-title"><strong class="event-strong">Alterar sua senha</strong></h4>
                 <a href="javascript:history.back('-1');" class="btn btn-danger btn-md pull-right"><i class="fa fa-reply"></i> &nbsp; Voltar</a>
              </div>
              <div class="box-content">
                 <div class="box-body">
                  <form action="/api/changePassword" method="POST" id="changePassForm">
                      <input type="password" name="pass" class="form-control" placeholder="Senha atual" />
                      <input type="password" name="npass" class="form-control" placeholder="Nova senha" style="margin: 5px 0;" />
                      <button id="changePassBtn" type="submit" class="btn btn-block btn-lg btn-success"><i class="fa fa-check"></i> &nbsp; Alterar</button>
                  </form>
                 </div>
              </div>
           </div>
        </div>
      </div>
      <!-- /.row -->
   </section>
   <!-- /.content -->
</div>
<!-- /.content-wrapper -->
<!-- ./wrapper -->
<?php require $_SERVER["DOCUMENT_ROOT"].'/inc/rodape.php'; ?>
<script>
   $(function(){
      $(document).ready(function(){

         $('#changePassForm').submit(function(e){
           var dados = $( this ).serialize();
           var f = $(this);
           var btn = $('#changePassBtn').html();
           $('#changePassBtn').toggleClass('btn-success btn-info').html('<i class="fa fa-spinner fa-spin"></i> &nbsp; Processando...');

           $.ajax({
             type: "POST",
             url: f.attr('action'),
             data: dados,
             success: function( d ){
               try {
                   JSON.parse(d);
                   var dd = $.parseJSON(d);
                   console.log(dd);
                   if(dd.success != undefined){

                     $.toast({
                        heading: 'Pronto!',
                        text: 'A senha foi alterada com sucesso.',
                        position: 'top-right',
                        loaderBg: '#FFF',
                        icon: 'success',
                        hideAfter: 3500,
                        stack: 6
                     });
                   } else {

                     $.toast({
                        heading: 'OPS!',
                        text: dd.error,
                        position: 'top-right',
                        loaderBg: '#FFF',
                        icon: 'error',
                        hideAfter: 3500,
                        stack: 6
                     });
                   }
               } catch (e) {

                     $.toast({
                        heading: 'OPS!',
                        text: dd.error,
                        position: 'top-right',
                        loaderBg: '#FFF',
                        icon: 'error',
                        hideAfter: 3500,
                        stack: 6
                     });
               }


                $('#changePassBtn').toggleClass('btn-success btn-info').html(btn);
             }
           });

           return false;
           e.preventDefault();
         });
      });
   });
</script>