<?php
include_once('header.php');
$vacancy_id = '';

if (isset($_GET['id'])) {
  $vacancy_id = $_GET['id'];
}
?>

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

    <div id="error"></div>

    <!-- new vacancy section -->
    <div id="" style="width: 100%">
      <div class="d-flex justify-content-between flex-wrap flex-md-nowrap  pb-2 mb-3 border-bottom" style="width: 100%; text-align: left;">
        <div class="col-md-12 order-md-1">

          <?php

          $vac = $con->getRows('vacancies', array('where' => 'vacancy_id="' . $vacancy_id . '"', 'return_type' => 'single'));
          ?>
          <?php include_once('sql_statements.php'); ?>
          <form class="needs-validation" method="post" action="">
            <input type="hidden" name="update_vacancy">
            <div class="row">
              <div class="col-md-6 mb-3">
                <label for="firstName">Job Type</label>
                <select class="form-control" name="category" id="category">
                  <option value="">Choose</option>
                  <?php
                  $categories = $con->getRows('job_categories', array('order_by' => 'category'));
                  foreach ($categories as $cat) {
                    echo '<option value="' . $cat['category_id'] . '"';
                    if ($cat['category_id'] == $vac['job_category']) {
                      echo "selected";
                    }
                    echo '>' . $cat['category'] . '</option>';
                  }
                  ?>

                </select>
              </div>
              <div class="col-md-6 mb-3">
                <label for="Password">Closing Date</label>
                <input type="date" class="form-control" id="closing_date" name="closing_date" value="<?php echo $vac['closing_date']; ?>">

              </div>

            </div>
            <div class="row">

              <div class="col-md-12 mb-3">
                <label for="Password">Job Title</label>
                <input type="text" class="form-control" id="job_title" name="job_title" value="<?php echo $vac['job_title']; ?>">

              </div>
            </div>

            <div class="row">
              <div class="col-md-12 mb-3">
                <label for="Password">Job Details</label><br>
                <textarea class="form-control" name="job_details" id="textbox" rows="9"><?php echo $vac['job_description']; ?></textarea>

              </div>
            </div>

            <input type="hidden" name="vacancy_id" value="<?php echo $vacancy_id; ?>">
            <input class="btn btn-primary btn-md" type="submit" name="update_vacancy" id="update_vacancy" value="Update Vacancy" style="float: right; margin-right: 5px;">
          </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($) {
      $('#edit-vacancy').hide(); //hide new user form on page load
      'use strict';
      $(function() {
        $('#order-listing').DataTable({
          "aLengthMenu": [
            [10, 20, 35, -1],
            [10, 20, 35, "All"]
          ],
          "iDisplayLength": 10
        });
      });
    })(jQuery);


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

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



      $('#new-vacancy').hide(); //hide new user form on page load
      //when a new user button has been clicked
      $('#new_vacancy').click(function() {
        $("#new-vacancy").show();
        $("#hide").click(function() {
          $("#new-vacancy").hide();
        })
      });

      //});

      //adds a new vacancy
      /* validation */
      $("#new_jo").validate({
        rules: {
          job_title: {
            required: true
          },
          location: {
            required: true
          },
        },
        messages: {
          job_title: {
            required: "Please enter job title"
          },
          location: "Please enter your location",
        },
        submitHandler: submitForm
      });
      /* validation */

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

        $.ajax({

          type: 'POST',
          url: 'sql_statements.php',
          data: data,
          beforeSend: function() {
            $("#error").fadeOut();
            $("#create_vacancy").html('<i class="fa fa-spin fa-spinner"></i> Checking ...');
          },
          success: function(response) {
            alert(response);
            if (response == 3) {
              $("#create_vacancy").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Signing In ...');
              setTimeout(' window.location.href = "user_accounts/editors/dashboard.php"; ', 2000);
            } else if (response == 4) {
              $("#error").fadeIn(2000, function() {
                $("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-user"></span> Your username/password combination is invalid!</div>');
                $("#login").html('<span class="fa fa-sign-in"></span> &nbsp; Sign In');
              }); //Your username/password entered doesn't match
              $("#error").fadeOut(5000, function() {});
            } else if (response == 5) {
              $("#error").fadeIn(2000, function() {
                $("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-lock"></span> Your account has been locked, contact the Systems Administrator to help you unlock the account!</div>');
                $("#login").html('<span class="fa fa-lock"></span> &nbsp; Account Locked');
              }); //Your username/password entered doesn't match
              $("#error").fadeOut(180000, function() {});
            }

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


    });

    //delete vacancy details details
    $(document).on('click', '.btn_delete', function() {
      var id = $(this).data("id3");
      var delete_vacancy = "delete_vacancy";
      //alert(user+" "+id); 
      if (confirm("Are you sure you want to remove this user?" + id)) {
        $.ajax({
          url: "sql_statements.php",
          method: "POST",
          data: {
            vacancy_id: id,
            delete_vacancy: delete_vacancy
          },
          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 vacancy has been removed!</div>');
              }); //Your username/password entered doesn't match
              $("#error").fadeOut(5000, function() {});
            }
            fetchData();
          }
        });
      }
    });

    //show vacancy details 
    $(document).on('click', '.show_btn', function() {
      var id = $(this).data("id3");
      var show_vacancy = "show_vacancy";

      //alert(id);

      $.ajax({
        url: "sql_statements.php",
        method: "POST",
        data: {
          vacancy_id: id,
          show_vacancy: show_vacancy
        },
        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 vacancy can now be accessed by the public!</div>');
            }); //Your username/password entered doesn't match
            $("#error").fadeOut(5000, function() {});
          }
          fetchData();
        }
      });

    });

    //show vacancy details 
    $(document).on('click', '.hide_btn', function() {
      var id = $(this).data("id3");
      var hide_vacancy = "hide_vacancy";

      alert(id);

      $.ajax({
        url: "sql_statements.php",
        method: "POST",
        data: {
          vacancy_id: id,
          hide_vacancy: hide_vacancy
        },
        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 vacancy has been hidden from the public/expired!</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>