<?php

  $title="Irigasi";

//   $judul=$title;

  $url='irigasi';

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

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

  // }



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

	

	// cek validasi

	$validation=null;



	//tidak boleh kosong

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

		$validation[]='Pilih Kecamatan';

	}

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

		$validation[]='Pilih Desa';

	}

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

		$validation[]='Kondisi Irigasi 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['kondisi_irigasi']=$_POST['kondisi_irigasi'];		

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

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

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

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

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

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

		$exec=$db->update("irigasi",$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;

	// end hapus file di dalam folder

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

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

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

  $kondisi_irigasi="";

  $kecamatan="";

  $nama_desa="";



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

  	$id=$_GET['id'];

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



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



		if($db->count>0){

			// $id=$row->id;

			$kondisi_irigasi=$row->kondisi_irigasi;			

			$kecamatan=$row->kecamatan;

			$nama_desa=$row->nama_desa;

		}

  }

  // value ketika validasi

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

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

  }

?>

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

    <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>Kondisi Irigasi</label>

    		<div class="row">

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

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

		    	</div>

	    	</div>

    	</div>

    	<div class="form-group">

    		<label>Nama Kecamatan</label>

    		<div class="row">

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

	    			<?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>Nama Desa</label>

    		<div class="row">

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

	    			<?php

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

		    				$opt['']='Pilih Desa';

		    				$db->orderBy("kecamatan,nama_desa","asc");

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

		    				foreach ($getdata as $row) {

		    					$opt[$row->nama_desa]=$row->nama_desa;

		    				}

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

		    				$opt['']='Pilih Desa';

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

		    				$db->orderBy("nama_desa","asc");

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

		    				foreach ($getdata as $row) {

		    					$opt[$row->nama_desa]=$row->nama_desa;

		    				}

		    			}

	    			?>

	    			<?=select('nama_desa',$opt,$nama_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 Irigasi')?>



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

			<th>Nama Desa</th>

			<th>Kondisi Irigasi</th>

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

			<th>Aksi</th>

			<?php }?>

		</tr>

	</thead>

	<tbody>

		<?php

			$no=1;

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

				$db->orderBy("kecamatan,nama_desa","asc");

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

			}else{

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

				$db->orderBy("kecamatan,nama_desa","asc");

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

			}

			foreach ($getdata as $row) {

				?>

					<tr>

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

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

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

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

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

						<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 } ?>