<?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 mt-3">
  <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">Sacco Statistics</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
              <div class="btn-group mr-2">     
                <a class="btn btn-sm btn-outline-secondary" href="create-sacco.php">Add New Sacco</a> 
                <a class="btn btn-sm btn-outline-secondary" href="sacco_list.php">Sacco List</a>   
              </div>
            </div>
      </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 Statistics</h4><br>
          <div id="err"></div>
          <form class="needs-validation" style="text-align: left;" id="add-resource" method="post" enctype="multipart/form-data">
            <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"> Type</label>
                <select class="form-control" name="type">
                  <option value="">Select Statistics Type..</option>
                  <option value="1">Malawi Statistics</option>
                  <option value="2">World Statistics</option>
                </select>
              </div>
              <div class="col-md-12 mb-3">
                <label for="firstName" style="text-align:left; float:left">Select File(doc, docx, pdf, jpeg, jpg, png)</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_stat" id="add_resource" style="float: left; margin-right: 5px;">
                   Save
                </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-stat.php",
              method:"POST",
              success:function(data){
                   $('#live_data').html(data);
              }
             });
        }
          $(document).ready(function(){

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

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

               },
             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 File...');
              },
              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 sacco statistics file has been saved.</div>');
                      });
                    $("#add-resource")[0].reset();
                    $("#err").delay(6000).fadeOut(function () { });
                    $("#add_resource").html('Save');

                  }
                  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 saving the sacco statistics file, Please try again!</div>');
                    $("#add_resource").html('Save');
                          });
                    $("#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 not a valid file, make sure that the file should be of type doc, docx, pdf, jpeg, jpg, png</div>');
                    $("#add_resource").html('Save');
                          });
                    $("#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_stat";
                 //alert(" Resource ID:"+id);
                 if(confirm("Are you sure you want to remove this file?"))
                 {
                      $.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(1000, 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 file has been deleted!</div>');
                                    });
                                  $("#error").delay(6000).fadeOut(function(){});
                                }

                                if(data == 2){
                                  $("#error").fadeIn(1000, function () {
                                      $("#error").html('<div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert">×</button><i class="icon wb-warning" aria-hidden="true"></i> There was an error deleting the file.</div>');
                                      $("#new_make").html('&nbsp; Add Make');
                                  });//Your username/password entered doesn't match
                                  $("#error").delay(6000).fadeOut(function () { });
                                }
                                fetchData();
                           }
                      });
                 }
            });
          Holder.addTheme('thumb', {
            bg: '#55595c',
            fg: '#eceeef',
            text: 'Thumbnail'
          });



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