<% layout('layout') %>

<%- include('../../partials/navbar.ejs'); %>

	<!-- Page Preloder -->
	<div id="preloder">
		<div class="loader"></div>
	</div>


	<!-- Blog page section  -->
	<div class="container">
		<div class="row">
			<div class="col-lg-8 post-list">
				<div class="post-item">
				</div>
			</div>
			<!-- sidebar -->
			<div class="col-sm-8 col-md-5 col-lg-4 col-xl-3 offset-xl-1 offset-0 pl-xl-0 ">

				<!-- widget -->
				<div class="widget">
					<h5 class="widget-title">Berita Terbaru</h5>
					<hr style="color: #1f827c; border-style:solid">
					<div class="recent-post-widget">
						<!-- recent post -->
						<div class="card">
							<div class="card-body">
								<div class="rp-item"></div>
							</div>
						</div>
					</div>
				</div>
				<!-- widget -->
				
				</div>
				</div>
			</div>
		</div>
	</div>
	

    <%- include('../../partials/footer_petugas.ejs'); %>

<script>
    function getName(id){
      let usr_id = (typeof id !== 'undefined') ?  id : 1;
	  var nama;
      $.ajax({
          type: "GET",
          url: '/api-v1/intern/detail-user',
          //headers: {
          //Authorization: 'Bearer ' + localStorage.getItem('token') },
          dataType: 'JSON',
          data: { 
              id: usr_id
          },
          async: false,
		  success: function(data) {
              console.log(data, "ini data user id")
              let dataCoba = data.data.data;

              $.each(dataCoba, (i, value) => {
                console.log(value.firstname, "ini user id")

				nama = value.firstname + ' ' + value.lastname;
              })
          },
        })
		return nama;
      }
    $(document).ready(function(){
        let searchParams = new URLSearchParams(window.location.search);
        let param = searchParams.get('id');

        $.ajax({
			type: "GET",
			url: '/api-v1/intern/get-kegiatan',
			//headers: {
			//Authorization: 'Bearer ' + localStorage.getItem('token') },
			dataType: 'JSON',
            data: { 
				id: param
			},
			success: function(data) {
				console.log(data, "ini data lengkapppp")
				let dataCoba = data.data.data;
	
				// ITERATING THROUGH OBJECTS
				$('.post-item').empty();

			   // add content
               $.each(dataCoba, (i, value) => {
					console.log(value, "ini value")
					if(value.gambar == undefined){
						$('.post-item').append('<div class="post-content">'+
						'<div class="card">'+
						'<img src=https://cima-afrique.org/cima/images/not-available.png alt= ' + value.nama_kegiatan + ' class="card-img-top"></img>'+
						'<div class="card-body">'+
						'<h4 class="card-title">' + value.nama_kegiatan + '</h4>'+
						'<div class="card-subtitle">'+
						'<span><i class="fas fa-calendar-alt"></i>'+ "  " + value.tanggal_kegiatan +'</span>'+
						'<br>'+
						'<span><i class="fas fa-user"></i>'+ "  " + getName(value.created_by) +'</span>'+
						'</div>'+
						'<br>'+
						'<h6> Nama Tatanan: ' + value.nama_tatanan + '</h6>'+
						'<h6> Jenis Indikator: ' + value.jenis_indikator + '</h6>'+
						'<h6> Kategori: ' + value.kategori + '</h6>'+
						'<h6> Nama Indikator: ' + value.nama_indikator + '</h6>'+
						'<h6> SubIndikator: ' + value.subindikator + '</h6>'+
						'<h6> Pelaksana: ' + value.pelaksana + '</h6>'+
						'<hr>'+
						'<p class="card-text">' + value.deskripsi +'</p>'+
						'</div>'+
						'</div>'+
						'</div>'+
						'<br>');
					}else{
						$('.post-item').append('<div class="post-content">'+
						'<div class="card">'+
						'<img src=' + value.gambar + ' alt= ' + value.nama_kegiatan + ' class="card-img-top"></img>'+
						'<div class="card-body">'+
						'<h4 class="card-title">' + value.nama_kegiatan + '</h4>'+
						'<div class="card-subtitle">'+
						'<span><i class="fas fa-calendar-alt"></i>'+ "  " + value.tanggal_kegiatan +'</span>'+
						'<br>'+
						'<span><i class="fas fa-user"></i>'+ "  " + getName(value.created_by) +'</span>'+
						'</div>'+
						'<p class="card-text">' + 
						'<h6> Nama Tatanan: ' + value.nama_tatanan + '</h6>'+
						'<h6> Jenis Indikator: ' + value.jenis_indikator + '</h6>'+
						'<h6> Kategori: ' + value.kategori + '</h6>'+
						'<h6> Nama Indikator: ' + value.nama_indikator + '</h6>'+
						'<h6> SubIndikator: ' + value.subindikator + '</h6>'+
						'<h6> Pelaksana: ' + value.pelaksana + '</h6>'+
						'<hr>'+
						'<p class="card-text">' + value.deskripsi +'</p>'+
						'</div>'+
						'</div>'+
						'</div>'+
						'<br>');
					}
			  }); 
			},
				error : function(e) {
				  alert("ERROR: ", e);
				  console.log("ERROR: ", e);
				}
			})
    })
</script>

<script type="text/javascript">
	$(document).ready(function(){
		$.ajax({
			type: "GET",
			url: '/api-v1/intern/get-all-kegiatan?page=1',
			//headers: {
			//Authorization: 'Bearer ' + localStorage.getItem('token') },
			dataType: 'JSON',
			success: function(data) {
				console.log(data, "ini data lengkapppp")
				let dataCoba = data.data.results
	
				// ITERATING THROUGH OBJECTS
				$('.rp-item').empty();

			   // add content
				$.each(dataCoba, (i, value) => {
					console.log(value, "ini value yaaaak")
					if(value.gambar == undefined){
						$('.rp-item').append('<center><img src=https://cima-afrique.org/cima/images/not-available.png width="200" height="95"></img></center>');
					}else{
						$('.rp-item').append('<img src='+ value.gambar +' width="200" height="95"></img>');
					}
					$('.rp-item').append('<div class="rp-content">');
					$('.rp-item').append('<h6 id="moreBerita" data-id=' + value.id + '><a href="#">' + value.nama_kegiatan + '</a></h6>');
					$('.rp-item').append('<span><i class="fas fa-clock-o"></i>'+ "  " + value.tanggal_kegiatan +'</span>');
					$('.rp-item').append('<br>');
					$('.rp-item').append('<center><hr style="width: 90%;"></center>');;
			  }); 
			},
				error : function(e) {
				  alert("ERROR: ", e);
				  console.log("ERROR: ", e);
				}
			})
	})
</script>

<script>
	$(document).ready(function(){
		$("body").on("click", "#moreBerita", function(e){  
			e.preventDefault();
			window.location.replace("/detail-berita?id=" + $(this).attr('data-id')); 
		})
	})
  
</script>