<?php

  $title="Kelurahan/Desa";

//   $judul=$title;

  $url='desa';

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

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

  // }



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

	$file=uploadDesa('geojson_desa','geojson');

	if($file!=false){

		$data['geojson_desa']=$file;

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

			// hapus file di dalam folder

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

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

			$geojson_desa=$get->geojson_desa;

			unlink('assets/unggah/geojson/BatasDesa/'.$geojson_desa);

			// 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_desa');

	if($db->count>0){

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

	}

	//tidak boleh kosong

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

		$validation[]='Kode Wilayah Tidak Boleh Kosong';

	}

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

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

	}

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

		$validation[]='Pilih Kecamatan';

	}



	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['nama_desa']=$_POST['nama_desa'];

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

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

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

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

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

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

		$exec=$db->update("m_desa",$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_desa');

	$geojson_desa=$get->geojson_desa;

	unlink('assets/unggah/geojson/BatasDesa/'.$geojson_desa);

	// end hapus file di dalam folder

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

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

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

  $nama_desa="";

  $kecamatan="";

  $geojson_desa="";

  $warna_desa="";



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

  	$id=$_GET['id'];

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



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



		if($db->count>0){

			$id=$row->id;

			$kodewilayah=$row->kodewilayah;

			$nama_desa=$row->nama_desa;

			$kecamatan=$row->kecamatan;

			$geojson_desa=$row->geojson_desa;

			$warna_desa=$row->warna_desa;

		}

  }

  // value ketika validasi

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

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

  }

?>

<?=content_open('Form Kelurahan/Desa')?>

    <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-4">

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

		    	</div>

	    	</div>

    	</div>

    	<div class="form-group">

    		<label>Nama Desa</label>

    		<div class="row">

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

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

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>Nama Kecamatan</label>

    		<div class="row">

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

	    			<?php

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

		    				$op['']='Pilih Kecamatan';

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

		    				foreach ($getdata as $row) {

		    					$op[$row->kecamatan]=$row->kecamatan;

		    				}

		    			}elseif ($session->get('level')=='Kecamatan') {

		    				$op['']='Pilih Kecamatan';

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

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

		    				$op[$getdata->kecamatan]=$getdata->kecamatan;

		    			}

	    			?>

	    			<?=select('kecamatan',$op,$kecamatan)?>

	    		</div>

    		</div>

    	</div>

    	<div class="form-group">

    		<label>GeoJSON</label>

    		<div class="row">

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

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

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

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



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

<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 Desa</th>

			<th>Kecamatan</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_desa');

			}else{

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

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

			}

			foreach ($getdata as $row) {

				?>

					<tr>

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

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

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

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

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

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

						<td style="background: <?=$row->warna_desa?>"></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>

						</td>

						<?php }?>

					</tr>

				<?php

				$no++;

			}



		?>

	</tbody>

</table>

<?=content_close()?>

<?php } ?>