<?php include_once('header.php'); ?>
<?php
$sacco_id = '';
$sacco = '';
if (isset($_GET['id'])) {
  $sacco_id = $_GET['id'];
  
}
$sacco = $con->getRows('saccos', array('where' => 'sacco_id="' . $sacco_id . '"', 'return_type' => 'single'));
?>

<div class="container">
  <div class="card-deck mb-3 text-center">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap mt-3 align-items-center pb-2 mb-3 border-bottom" style="width: 100%;">
      <h1 class="h2">Upddate Sacco</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>
          <a class="btn btn-sm btn-outline-secondary" href="sacco_stats.php">Sacco Statistics</a>
        </div>
      </div>
    </div>


    <!-- new vacancy section -->
    <div id="new-vacancy" style="width: 100%">

      <div class="d-flex justify-content-between flex-wrap flex-md-nowrap mt-3 pb-2 mb-3 border-bottom" style="width: 100%; text-align: left;">
        <div class="col-md-12 order-md-1">
          <div id="error"></div>
          <form id="add-sacco" method="post" action="">
            <div class="row mb-4">
              <div class="col-md-4 mb-3">
                <label for="Password">Sacco Name</label>
                <input type="text" class="form-control" name="sacco" value="<?= $sacco['sacco_name'] ?>">

              </div>
              <div class="col-md-4 mb-3">
                <label for="firstName">Region</label>
                <select class="form-control" name="region">
                  <option value="">Choose Region</option>
                  <?php
                  $regions = $con->getRows('regions', array('order_by' => 'region_id'));
                  if (!empty($regions)) {
                    foreach ($regions as $region) {
                      echo '<option value="' . $region['region_id'] . '"';
                      if ($region['region_id'] == $sacco['region_id']) {
                        echo "selected";
                      }
                      echo '>' . $region['region_name'] . '</option>';
                    }
                  }
                  ?>
                </select>
              </div>
              <div class="col-md-4 mb-3">
                <label for="Password">Location</label>
                <input type="text" class="form-control" name="location" value="<?= $sacco['location'] ?>">

              </div>

            </div>
            <div class="row">
              <div class="col-md-4 mb-3">
                <label for="Password">Contact Person</label>
                <input type="text" class="form-control" name="contact" value="<?= $sacco['contact_person'] ?>">
              </div>
              <div class="col-md-4 mb-3">
                <label for="Password">Email Address</label>
                <input type="email" class="form-control" name="email" value="<?= $sacco['email'] ?>">
              </div>
              <div class="col-md-4 mb-3">
                <label for="Password">Phone Number</label>
                <input type="text" class="form-control" name="phone" value="<?= $sacco['phone'] ?>">
              </div>
            </div>

            <div class="row">
              <div class="col-md-6 mb-3">
                <label for="Password">Postal Address</label><br>
                <input type="text" class="form-control" name="address" value="<?= $sacco['address'] ?>">
              </div>
              <div class="col-md-6 mb-3">
                <label for="Password">Website URL</label><br>
                <input type="url" class="form-control" name="url" value="<?= $sacco['url'] ?>">
              </div>
            </div>
            <input type="hidden" name="id" value="<?= $sacco['sacco_id'] ?>">
            <button class="btn btn-primary btn-md" type="submit" name="update_sacco" id="save_sacco" style="float: right; margin-right: 5px;">
              Update Sacco
            </button>
          </form>
        </div>
      </div>

    </div>
    <!-- end new user form -->
  </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 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>
  $(document).ready(function() {

    //adds a new vacancy
    /* validation */
    $("#add-sacco").validate({
      rules: {
        sacco: {
          required: true
        },
        location: {
          required: true
        },
        region: {
          required: true
        },
        contact: {
          required: true
        },
      },
      messages: {
        sacco: {
          required: "Please enter sacco name"
        },
        location: "Please enter sacco location",
        region: "Please enter sacco region",
        contact: "Please enter sacco's contact person",
      },
      submitHandler: submitForm
    });
    /* validation */

    /* login submit */
    function submitForm() {
      var data = $("#add-sacco").serialize();

      $.ajax({

        type: 'POST',
        url: 'sql_statements.php',
        data: data,
        beforeSend: function() {
          $("#error").fadeOut();
          $("#save_sacco").html('<i class="fa fa-spin fa-spinner"></i> Updating Sacco...');
        },
        success: function(response) { //alert(response);
          if (response == 1) {
            $("#error").fadeIn(1000, function() {
              $("#error").html('<div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert">×</button><i class="icon wb-check" aria-hidden="true"></i> Sacco details have been updated successfuly.</div>');
              $("#save_sacco").html('Update Sacco');
              $("#add-sacco")[0].reset();
            });
            $("#error").delay(6000).fadeOut(function() {});
            //setTimeout('window.location.href = "admin/dashboard.php"; ', 1000);
          }
          //login error
          else if (response == 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 is an error updating sacco details, there is no new information to update.</div>');
              $("#save_sacco").html('Update Sacco');
            });
            $("#error").delay(6000).fadeOut(function() {});
          }

        }
      });
      return false;
    }
    /*end add vacancy script */


  });


  Holder.addTheme('thumb', {
    bg: '#55595c',
    fg: '#eceeef',
    text: 'Thumbnail'
  });
</script>
</body>

</html>