<?php

  $title="Koordinat";

//   $judul=$title;

  $url='koordinat';

  // if ($session->get('level')!='Admin'){

  // 	redirect(url('beranda'));

  // }



if(isset($_POST['simpan'])){

	$file=upload('geojson_koordinat','geojson');

	if($file!=false){

		$data['geojson_koordinat']=$file;

		if($_POST['id_koordinat']!=''){

			// hapus file di dalam folder

			$db->where('id_koordinat',$_GET['id']);

			$get=$db->ObjectBuilder()->getOne('m_koordinat');

			$geojson_koordinat=$get->geojson_koordinat;

			unlink('assets/unggah/geojson/'.$geojson_koordinat);

			// end hapus file di dalam folder

		}

	}





	// cek validasi

	$validation=null;

	// cek kode apakah sudah ada

	if($_POST['id_koordinat']!=""){

		$db->where('id_koordinat !='.$_POST['id_koordinat']);

	}

	$db->where('kd_koordinat',$_POST['kd_koordinat']);

	$db->get('m_koordinat');

	if($db->count>0){

		$validation[]='Kode Koordinat Sudah Ada';

	}

	//tidak boleh kosong

	if($_POST['nm_koordinat']==''){

		$validation[]='Nama Koordinat Tidak Boleh Kosong';

	}



	if($validation!=null){

		$setTemplate=false;

		$session->set('error_validation',$validation);

		$session->set('error_value',$_POST);

		redirect($_SERVER['HTTP_REFERER']);

		return false;

	}

	// cek validasi







	if($_POST['id_koordinat']==""){

		$data['kd_koordinat']=$_POST['kd_koordinat'];

		$data['nm_koordinat']=$_POST['nm_koordinat'];

		$data['warna_koordinat']=$_POST['warna_koordinat'];

		$exec=$db->insert("m_koordinat",$data);

		$info='<div class="alert alert-success alert-dismissible">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                <h4><i class="icon fa fa-ban"></i> Sukses!</h4> Data Sukses Ditambah </div>';

		

	}

	else{

		$data['kd_koordinat']=$_POST['kd_koordinat'];

		$data['nm_koordinat']=$_POST['nm_koordinat'];

		$data['warna_koordinat']=$_POST['warna_koordinat'];

		$db->where('id_koordinat',$_POST['id_koordinat']);

		$exec=$db->update("m_koordinat",$data);

		$info='<div class="alert alert-success alert-dismissible">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                <h4><i class="icon fa fa-ban"></i> Sukses!</h4> Data Sukses diubah </div>';

	}



	if($exec){

		$session->set('info',$info);

	}

	else{

      $session->set("info",'<div class="alert alert-danger alert-dismissible">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                <h4><i class="icon fa fa-ban"></i> Error!</h4> Proses gagal dilakukan <br>'.$db->getLastError().'

              </div>');

	}

	redirect(url($url));

}



if(isset($_GET['hapus'])){

	$setTemplate=false;

	// hapus file di dalam folder

	$db->where('id_koordinat',$_GET['id']);

	$get=$db->ObjectBuilder()->getOne('m_koordinat');

	$geojson_koordinat=$get->geojson_koordinat;

	unlink('assets/unggah/geojson/'.$geojson_koordinat);

	// end hapus file di dalam folder

	$db->where("id_koordinat",$_GET['id']);

	$exec=$db->delete("m_koordinat");

	$info='<div class="alert alert-success alert-dismissible">

            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

            <h4><i class="icon fa fa-ban"></i> Sukses!</h4> Data Sukses dihapus </div>';

	if($exec){

		$session->set('info',$info);

	}

	else{

      $session->set("info",'<div class="alert alert-danger alert-dismissible">

                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

                <h4><i class="icon fa fa-ban"></i> Error!</h4> Proses gagal dilakukan

              </div>');

	}

	redirect(url($url));

}



elseif(isset($_GET['tambah']) OR isset($_GET['ubah'])){

  $id_koordinat="";

  $kd_koordinat="";

  $nm_koordinat="";

  $geojson_koordinat="";

  $warna_koordinat="";

  if(isset($_GET['ubah']) AND isset($_GET['id'])){

  	$id=$_GET['id'];

  	$db->where('id_koordinat',$id);

	$row=$db->ObjectBuilder()->getOne('m_koordinat');

	if($db->count>0){

		$id_koordinat=$row->id_koordinat;

		$kd_koordinat=$row->kd_koordinat;

		$nm_koordinat=$row->nm_koordinat;

		$geojson_koordinat=$row->geojson_koordinat;

		$warna_koordinat=$row->warna_koordinat;

	}

  }

  // value ketika validasi

  if($session->get('error_value')){

  	extract($session->pull('error_value'));

  }

?>

<?=content_open('Form Koordinat')?>

    <form method="post" enctype="multipart/form-data">

    	<?php

    		// menampilkan error validasi

  			if($session->get('error_validation')){

  				foreach ($session->pull('error_validation') as $key => $value) {

  					echo '<div class="alert alert-danger">'.$value.'</div>';

  				}

  			}

    	?>

    	<?=input_hidden('id_koordinat',$id_koordinat)?>

    	<div class="form-group">

    		<label>Kode</label>

    		<div class="row">

	    		<div class="col-md-4">

	    			<?=input_text('kd_koordinat',$kd_koordinat)?>

		    	</div>

	    	</div>

    	</div>

    	<div class="form-group">

    		<label>Nama Koordinat</label>

    		<div class="row">

	    		<div class="col-md-6">

	    			<?=input_text('nm_koordinat',$nm_koordinat)?>

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>GeoJSON</label>

    		<div class="row">

	    		<div class="col-md-4">

    				<?=input_file('geojson_koordinat',$geojson_koordinat)?>

    			</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>Warna</label> 

    		<div class="row">

	    		<div class="col-md-3">

	    			<?=input_color('warna_koordinat',$warna_koordinat)?>

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<button type="submit" name="simpan" class="btn btn-info"><i class="fa fa-save"></i> Simpan</button>

			<a href="<?=url($url)?>" class="btn btn-danger" ><i class="fa fa-reply"></i> Kembali</a>

    	</div>

    </form>

<?=content_close()?>



<?php  } else { ?>

<?=content_open('Data Koordinat')?>



<?php if ($session->get('level')=='Admin'){ ?>

<a href="<?=url($url.'&tambah')?>" class="btn btn-success" ><i class="fa fa-plus"></i> Tambah</a>

<?php }?>

<hr>

<?=$session->pull("info")?>



<table class="table table-bordered">

	<thead>

		<tr>

			<th>No</th>

			<th>Kode</th>

			<th>Nama Koordinat</th>

			<th>GeoJSON</th>

			<th>Warna</th>

			<th>Aksi</th>

		</tr>

	</thead>

	<tbody>

		<?php

			$no=1;

			$getdata=$db->ObjectBuilder()->get('m_koordinat');

			foreach ($getdata as $row) {

				?>

					<tr>

						<td><?=$no?></td>

						<td><?=$row->kd_koordinat?></td>

						<td><?=$row->nm_koordinat?></td>

						<td><a href="<?=assets('unggah/geojson/'.$row->geojson_koordinat)?>" target="_BLANK"><?=$row->geojson_koordinat?></a></td>

						<td style="background: <?=$row->warna_koordinat?>"></td>

						<td>

							<a href="<?=url($url.'&ubah&id='.$row->id_koordinat)?>" class="btn btn-info"><i class="fa fa-edit"></i> Ubah</a>

							<a href="<?=url($url.'&hapus&id='.$row->id_koordinat)?>" class="btn btn-danger" onclick="return confirm('Hapus data?')"><i class="fa fa-trash"></i> Hapus</a>

						</td>

					</tr>

				<?php

				$no++;

			}



		?>

	</tbody>

</table>

<?=content_close()?>

<?php } ?>