<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">

	<!-- Section -->
<div class="cmt-page-title-row bg-base-dark cmt-bg cmt-bgimage-yes clearfix">
   <div class="cmt-titlebar-wrapper-bg-layer cmt-bg-layer"></div>
      <div class="container">
          <div class="row align-items-center">
              <div class="col-lg-12">
                  <div class="cmt-page-title-row-inner">
                      <div class="page-title-heading">
                          <h2 class="title"><?php echo $cat['facultyName'] ; ?></h2>
                      </div>
                      <div class="breadcrumb-wrapper">
                          <span>
                              <a title="Homepage" href="<?php echo base_url();?>">Home</a>
                          </span>
                          <span><?php echo $cat['facultyName'] ; ?></span>
                      </div>
                  </div>
              </div>
          </div>
      </div>                    
  </div>
  <div class="container">
<div class="row">
	<?php
  $session = \Config\Services::session();

        if($session->getFlashdata('success'))
        {
            echo '
            <div class="alert alert-success">'.$session->getFlashdata("success").'</div>
            ';
        }

        ?>
<div class="col-md-12">
	<div class="panel panel-yellow margin-bottom-40">
			<table class="table table-condensed nomargin" id="courses">
				<thead>
					<tr>
						<b>
						<th>#</th>
						<th>Title</th>
						<th>Duration</th>
            <th>Location</th>
						<th>Price</th>
						<?php
						$session = \Config\Services::session();
						$userId = $session->get('userId');
						if($userId){
	echo '<th colspan="2">Action</th>';
						}else{
						echo '<th>View &amp; Register</th>';
					}

						  ?>
					</b>	
				</tr>
				</thead>
				<tbody>
					<?php
					  $i=1;
					foreach($courses as $course ):?>
					<tr>
						<td><?php echo $i; ?></td>
						<td><a href="course/<?php echo $course->courseID;   ?>"><?php echo $course->courseName ;?></a></td>
						<td><span class="label label-success rounded-2x"><?php echo $course->duration.' '.$course->typeName ;?></span></td>
            <td><?php echo $schedule->centerName;   ?></td>
						<td><span class="label label-info rounded-2x"><?php  echo 'KSh'. $course->feeKshs.', US$ '.$course->feeUsd ;?></span></td>

						<?php
						if($userId){
							?>
							<!--td><a class="btn-u btn-u-green" href="course/<?php echo $course->courseID;   ?>" >View Course </a></td>-->
							<td><a class="btn-xs btn btn-primary" href="updatecourse/<?php echo $course->courseID;?>" ><i class="fa fa-pencil-square-o"></i> View/Edit</a></td>
							<td><a class="btn-xs btn btn-danger" href="deletecourse/<?php echo $course->courseID;?>"><i class="fa fa-trash"></i> Delete</a></td>

							<?php

						}else{  ?>
						<td><a class="btn-u btn-xs btn-brd btn-brd-hover rounded-2x btn-u-green"  href="course/<?php echo $course->courseID;   ?>" ><i class="fa fa-tasks"></i> Online Course </a>
							<a class="btn-u btn-xs btn-brd btn-brd-hover rounded-2x btn-u-green"  href="course/<?php echo $course->courseID;   ?>" ><i class="fa fa-tasks"></i> Physical Course</a></td>

						<?php 
						}
						?>

							</tr>
				<?php
				$i++;
			   endforeach;

				     ?>


				</tbody>
				<tfoot>

				</tfoot>
			</table>
		</div>


	</div>
</div>
</div>
</div>

<script type="text/javascript" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

<script>
    $(document).ready( function () {
      $('#courses').DataTable();
  } );
</script>

<script>
      $('.search').select2({
        placeholder: '--- Search User ---',
        ajax: {
          url: '<?php echo base_url('Home/ajaxSearch');?>',
          dataType: 'json',
          delay: 250,
          processResults: function(data){
            return {
              results: data
            };
          },
          cache: true
        }
      });
</script>
	
<script>
function delete_data(id)
{
    if(confirm("Are you sure you want to remove it?"))
    {
        window.location.href="<?php echo base_url(); ?>/Home/deletecourse/"+courseID;
    }
    return false;
}
</script>


<script>

$(document).ready(function(){
  $('#sample_table').DataTable({
    "order":[],
    "serverSide":true,
    "ajax":{
      url:"<?php echo base_url("/ajax_crud/fetch_all"); ?>",
      type:"POST",
    }
  });

  $('#add_record').click(function(){

    $('#user_form')[0].reset();

    $('.modal-title').text('Add Data');

    $('#name_error').text('');

    $('#email_error').text('');

    $('#gender_error').text('');

    $('#action').val('Add');

    $('#submit_button').val('Add');

    $('#userModal').modal('show');

  });

  $('#user_form').on('submit', function(event){

    event.preventDefault();

    $.ajax({

      url:"<?php echo base_url('/ajax_crud/action'); ?>",

      method:"POST",

      data:$(this).serialize(),

      dataType:"JSON",

      beforeSend:function(){

        $('#submit_button').val('wait...');

        $('#submit_button').attr('disabled', 'disabled');

      }, 

      success:function(data)
      {

        $('#submit_button').val('Add');

        $('#submit_button').attr('disabled', false);

        if(data.error == 'yes')
        {

          $('#name_error').text(data.name_error);

          $('#email_error').text(data.email_error);

          $('#gender_error').text(data.gender_error);

        }
        else
        {

          $('#userModal').modal('hide');

          $('#message').html(data.message);

          $('#sample_table').DataTable().ajax.reload();

          setTimeout(function(){

            $('#message').html('');

          }, 5000);

        }

      }

    })

  });

  $(document).on('click', '.edit', function(){

    var id = $(this).data('id');

    $.ajax({

      url:"<?php echo base_url('/ajax_crud/fetch_single_data'); ?>",

      method:"POST",

      data:{id:id},

      dataType:'JSON',

      success:function(data)
      {

        $('#name').val(data.name);

        $('#email').val(data.email);

        $('#gender').val(data.gender);

        $('.modal-header').text('Edit Data');

        $('#name_error').text('');

        $('#email_error').text('');

        $('#gender_error').text('');

        $('#action').val('Edit');

        $('#submit_button').val('Edit');

        $('#userModal').modal('show');

        $('#hidden_id').val(id);

      }

    })

  });

  $(document).on('click', '.delete', function(){

    var id = $(this).data('courseID');

    if(confirm("Are you sure you want to remove it?"))
    {

      $.ajax({

        url:"<?php echo base_url('/Home/delete'); ?>",

        method:"POST",

        data:{id:id},

        success:function(data)
        {

          $('#message').html(data);

          $('#sample_table').DataTable().ajax.reload();

          setTimeout(function(){

            $('#message').html('');

          }, 5000);

        }

      })

    }

  });

});

</script>