<?php 

$page           = (!empty($_GET['page-id']) && is_numeric($_GET['page-id'])) ? $_GET['page-id'] : 1;
$filter_keyword = (!empty($_GET['query'])) ? Wo_Secure($_GET['query']) : '';
$filter_type    = '';
$db->pageLimit  = 50;


if (!empty($filter_keyword)) {
  $sql   = "(`title` LIKE '%$filter_keyword%' OR `description` LIKE '%$filter_keyword%')";
  $posts = $db->where($sql)->orderBy('id', 'DESC')->objectbuilder()->paginate(T_JOB, $page);
} 

else {
  $posts = $db->objectbuilder()->orderBy('id', 'DESC')->paginate(T_JOB, $page);
}

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
  header("Location: " . Wo_LoadAdminLinkSettings('manage-jobs'));
  exit();
}

?>

<div class="container-fluid">
    <div class="block-header">
        <h2>Manage Features > Jobs</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage Jobs</h2>
                </div>
                <div class="body">
                    <div class="row">
                      <div class="col-md-4" style="margin-bottom:0;">
                        <form method="get" action="<?php echo Wo_LoadAdminLinkSettings('manage-jobs'); ?>">
                          <div class="row">
                            <div class="col-md-11">
                              <div class="form-group form-float">
                                  <div class="form-line">
                                      <input type="text" name="query" id="query" class="form-control" value="<?php echo($filter_keyword); ?>">
                                      <label class="form-label search-form">
                                        Search for job title, description.
                                      </label>
                                  </div>
                              </div>
                            </div>
                            <div class="col-md-1">
                               <button class="btn btn-info">Search</button>
                            </div>
                          </div>
                          <div class="clearfix"></div>
                        </form>
                      </div>
                    </div>
                    <input type="hidden" id="hash_id" name="hash_id" value="<?php echo Wo_CreateSession();?>">
                    <div class="clearfix"></div>
                    <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                            <tr>
                             <th>ID</th>
                             <th>Publisher</th>
                             <th>Title</th>
                             <th>Job Link</th>
                             <th>Posted</th>
                             <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>

                            <?php 
                              foreach ($posts as $post) {
                                $wo['story'] = $post;
                                if (!empty($post->user_id)) {
                                  $wo['story']->publisher = Wo_UserData($post->user_id);
                                  $wo['story']->job = Wo_GetJobById($post->id);
                                  if (!empty($wo['story']->publisher)) {
                                    echo Wo_LoadAdminPage('manage-jobs/list');
                                  }
                                }
                                
                                
                              }
                            ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="wo-admincp-feturepager">
                        <div class="pull-left">
                            <span>
                              <?php echo "Showing $page out of " . $db->totalPages; ?>
                            </span>
                        </div>
                        <div class="pull-right">
                            <nav>
                                <ul class="pagination">
                                    <li>
                                      <a href="<?php echo Wo_LoadAdminLinkSettings('manage-jobs?page-id=1'); ?>" class="waves-effect" title='First Page'>
                                          <i class="material-icons">first_page</i>
                                      </a>
                                    </li>
                                    <?php if ($page > 1) {  ?>
                                      <li>
                                          <a href="<?php echo Wo_LoadAdminLinkSettings('manage-jobs?page-id=' . ($page - 1)); ?>" class="waves-effect" title='Previous Page'>
                                              <i class="material-icons">chevron_left</i>
                                          </a>
                                      </li>
                                    <?php  } ?>

                                    <?php 
                                      $nums       = 0;
                                      $nums_pages = ($page > 4) ? ($page - 4) : $page;

                                      for ($i=$nums_pages; $i <= $db->totalPages; $i++) { 
                                        if ($nums < 20) {
                                    ?>
                                      <li class="<?php echo ($page == $i) ? 'active' : ''; ?>">
                                        <a href="<?php echo Wo_LoadAdminLinkSettings('manage-jobs?page-id=' . ($i)); ?>" class="waves-effect">
                                          <?php echo $i ?>   
                                        </a>
                                      </li>

                                    <?php } $nums++; }?>

                                    <?php if ($db->totalPages > $page) { ?>
                                      <li>
                                          <a href="<?php echo Wo_LoadAdminLinkSettings('manage-jobs?page-id=' . ($page + 1)); ?>" class="waves-effect" title="Next Page">
                                              <i class="material-icons">chevron_right</i>
                                          </a>
                                      </li>
                                    <?php } ?>
                                    <li>
                                      <a href="<?php echo Wo_LoadAdminLinkSettings('manage-jobs?page-id=' . ($db->totalPages)); ?>" class="waves-effect" title='Last Page'>
                                          <i class="material-icons">last_page</i>
                                      </a>
                                    </li>
                                </ul>
                            </nav>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </div>

            </div>
        </div>
        <div class="clearfix"></div>
    </div>

<script>

function Wo_AdminDeletePost(job_id) {
    if (confirm("Are you sure you want to delete this job?") == false) {
      return false;
    }
    hash_id = $('#hash_id').val();
    $('#PostID_' + job_id).fadeOut(300, function() {
       $(this).remove();
    });
    $.post(Wo_Ajax_Requests_File() + '?f=admin_setting&s=delete_job', {job_id:job_id, hash_id:hash_id});
}

</script>