<?php include_once('header.php'); ?>
<?php
  $action = '';
  $cat_id = '';
  if(isset($_GET['action']) && isset($_GET['cat_id'])){
    $action = $_GET['action'];
    $cat_id = $_GET['cat_id'];
  }
?>
<style type="text/css">
    .error{
        border-color: #ecbeb6;
        color: #b55351;
        text-transform: capitalize;
        text-align: left !important;
    }
    #error{
        width: 100%;
    }
    .alert{
        width: 100%;
        text-align: left;
    }
</style>
<div class="container">
  <div class="card-deck mb-3 text-center">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 mt-3 border-bottom" style="width: 100%;">
        <h1 class="h2">Newsletters</h1>
      </div>

      <div id="error"></div>
      <!-- new user section -->

      <div class="row text-left">
        <div id="live_data" class="col-md-8"></div>
        <div class="col-md-4">
          <h4 style="text-align: left;">Add Newsletter</h4><br>
          
          <form class="needs-validation" style="text-align: left;" id="add-resource" method="post" enctype="multipart/form-data">
            <div id="err"></div>
            <div class="row">
              <div class="col-md-12 mb-3">
                <label for="firstName" style="text-align:left; float:left">Title</label>
                <input type="text" class="form-control" id="title" name="title" required>
              </div>
              <div class="col-md-12 mb-3">
                <label for="firstName" style="text-align:left; float:left">Select File</label>
                <input type="file" class="form-control" id="file" name="file" required>
              </div>
              <div class="col-md-8 mb-3">
                <button class="btn btn-primary btn-md form-control" type="submit" name="add_newsletter" id="add_resource" style="float: left; margin-right: 5px;">
                   Add Newsletter
                </button>
              </div>
              </div>
            </div>
          </form>
       
        </div>
      </div>
  </div>

</div>


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script>window.jQuery || document.write('<script src="../../js/vendor/jquery-slim.min.js"><\/script>')</script>
    <script src="../../js/vendor/popper.min.js"></script>
    <script src="../../js/bootstrap.min.js"></script>
    <script src="../../js/vendor/holder.min.js"></script>
    <script type="text/javascript" src="../../js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="../../js/validation.min.js"></script>
    <script src="../../js/tables/jquery.dataTables.js"></script>
    <script src="../../js/tables/dataTables.bootstrap4.js"></script>

     <!--  summernote JS
        ============================================ -->
    <script src="../../js/summernote/summernote-updated.min.js"></script>
    <script src="../../js/summernote/summernote-active.js"></script>


        <script>
          (function($) {
          'use strict';
          $(function() {
            $('#order-listing').DataTable({
              "aLengthMenu": [
              [10, 20, 35, -1],
              [10, 20, 35, "All"]
              ],
              "iDisplayLength": 10
            });
          });
        })(jQuery);


        function fetchData(){
          $.ajax({
              url:"select-newsletters.php",
              method:"POST",
              success:function(data){
                   $('#live_data').html(data);
              }
             });
        }
          $(document).ready(function(){

            //loads system users
            $.ajax({
              url:"select-newsletters.php",
              method:"POST",
              success:function(data){
                   $('#live_data').html(data);
              }
             });

            $("#add-resource").validate({
              rules:
            {
              title: {required: true},
              file: { required:true}
             },
            messages:
             {
                    title: "Please enter title..",
                    file: "Please select the file"

               },
             submitHandler: saveFile
               });
             /* validation */

             /* login submit */
             function saveFile()
             {
              //var data = $("#add-resource").serialize();
              var formData = $("#add-resource").submit(function () {
                  return;
              });
              var formData = new FormData(formData[0]);

              $.ajax({

              type : 'POST',
              url  : 'sql_statements.php',
              data : formData,
              beforeSend: function()
              {
                $("#err").fadeOut();
                $("#add_resource").html('<i class="fa fa-spin fa-spinner"></i> Saving Newsletter ...');
              },
              success :  function(response)
                 {  //alert(response);

                  //if the response returns true
                  if(response == 1){
                    $("#err").fadeIn(1000, function(){
                      $("#err").html('<div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-ok-circle"></span> A newsletter has been saved.</div>');
                      });
                    $("#add-resource")[0].reset();
                    $("#err").delay(6000).fadeOut(function () { });
                    $("#add_resource").html('Save Newsletter');

                  }
                  else if(response == 2){
                    $("#err").fadeIn(1000, function(){
                      $("#err").html('<div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button>There was an error adding a file!</div>');
                    $("#add_resource").html('Save Newsletter');
                          });//Your username/password entered doesn't match
                    $("#err").delay(6000).fadeOut(function(){});
                  }
                  else if(response == 3){
                    $("#err").fadeIn(1000, function(){
                      $("#err").html('<div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button>The uploaded file is invalid, it must be of type pdf, docx, doc</div>');
                    $("#add_resource").html('Save Newsletter');
                          });//Your username/password entered doesn't match
                    $("#err").delay(6000).fadeOut(function(){});
                  }
                  fetchData()

                },
                contentType: false,
                processData: false,
                cache: false
              });
                return false;
            }
             /* login submit */
          

          });
          //delete user details
          $(document).on('click', '.btn_delete', function(){
                 var id=$(this).data("id3");
                 var action="delete_newsletter";
                 //alert(" Resource ID:"+id);
                 if(confirm("Are you sure you want to remove this newsletter?"))
                 {
                      $.ajax({
                           url:"sql_statements.php",
                           method:"POST",
                           data:{resource_id:id, action:action},
                           dataType:"text",
                           success:function(data){
                              //alert(data);
                              if(data == 1){
                                  $("#error").fadeIn(2000, function(){
                                  $("#error").html('<div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-ok-circle"></span>&nbsp; A newsletter has been removed!</div>');
                          });//Your username/password entered doesn't match
                          $("#error").fadeOut(5000,function(){});
                                }
                                fetchData();
                           }
                      });
                 }
            });
          Holder.addTheme('thumb', {
            bg: '#55595c',
            fg: '#eceeef',
            text: 'Thumbnail'
          });

        </script>
  </body>
</html>
