<?php

  $title="Kecamatan";

//   $judul=$title;

  $url='kecamatan';

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

  // 	$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> Akses Administrator untuk melihat data

  //     </div>');

  // 	redirect(url('login'));

  // }



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

	$file=uploadKecamatan('geojson_kecamatan','geojson');

	if($file!=false){

		$data['geojson_kecamatan']=$file;

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

			// hapus file di dalam folder

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

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

			$geojson_kecamatan=$get->geojson_kecamatan;

			unlink('assets/unggah/geojson/BatasKab/'.$geojson_kecamatan);

			// end hapus file di dalam folder

		}

	}





	// cek validasi

	$validation=null;

	// cek kode apakah sudah ada

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

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

	}

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

	$db->get('m_kecamatan');

	if($db->count>0){

		$validation[]='Kode Wilayah Kecamatan Sudah Ada';

	}

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

	$db->get('m_kecamatan');

	if($db->count>0){

		$validation[]='Nama Kecamatan Sudah Ada';

	}

	//tidak boleh kosong

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

		$validation[]='Pilih Kecamatan';

	}

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

		$validation[]='Latitude Tidak Boleh Kosong';

	}

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

		$validation[]='Longitude 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']==""){

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

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

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

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

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

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

		$exec=$db->insert("m_kecamatan",$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['kodewilayah']=$_POST['kodewilayah'];

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

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

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

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

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

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

		$exec=$db->update("m_kecamatan",$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',$_GET['id']);

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

	$geojson_kecamatan=$get->geojson_kecamatan;

	unlink('assets/unggah/geojson/BatasKab/'.$geojson_kecamatan);

	// end hapus file di dalam folder

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

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

	$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="";

  $kodewilayah="";

  $kecamatan="";

  $kodepos="";

  $latitude="";

  $longitude="";

  $geojson_kecamatan="";

  $warna_kecamatan="";

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

  	$id=$_GET['id'];

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

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

	if($db->count>0){

		$id=$row->id;

		$kodewilayah=$row->kodewilayah;

		$kecamatan=$row->kecamatan;

		$kodepos=$row->kodepos;

		$latitude=$row->latitude;

		$longitude=$row->longitude;

		$geojson_kecamatan=$row->geojson_kecamatan;

		$warna_kecamatan=$row->warna_kecamatan;

	}

  }

  // value ketika validasi

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

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

  }

?>

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

    <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',$id)?>

    	<div class="form-group">

    		<label>Kode Wilayah</label>

    		<div class="row">

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

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

		    	</div>

	    	</div>

    	</div>

    	<div class="form-group">

    		<label>Nama Kecamatan</label>

    		<div class="row">

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

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

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>Kode Pos</label>

    		<div class="row">

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

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

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>Latitude</label>

    		<div class="row">

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

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

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>Longitude</label>

    		<div class="row">

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

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

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>GeoJSON</label>

    		<div class="row">

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

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

    				<?php if (isset($_GET['ubah'])): ?>

                <small class="text-success">Biarkan kosong jika tidak ingin diubah</small>

            <?php endif ?>

    			</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>Warna</label> 

    		<div class="row">

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

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

	    		</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 Kecamatan')?>



<?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 Wilayah</th>

			<th>Nama Kecamatan</th>

			<th>Kode Pos</th>

			<th>Latitude</th>

			<th>Longitude</th>

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

			<th>GeoJSON</th>

			<th>Warna</th>

			<th>Aksi</th>

			<?php }?>

		</tr>

	</thead>

	<tbody>

		<?php

			$no=1;



			if ($session->get('level')=='Admin' OR $session->get('level')=='User'){

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

			}else{

				$db->where("kecamatan",$session->get('lokasi'));

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

			}

			foreach ($getdata as $row) {

				?>

					<tr>

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

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

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

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

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

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

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

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

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

						<td>

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

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

						<?php }?>

						</td>

					</tr>

				<?php

				$no++;

			}



		?>

	</tbody>

</table>

<?=content_close()?>

<?php } ?>