<?php namespace App\Http\Controllers;

use App\Models\Rekaplapormorfologi;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use Validator, Input, Redirect ; 


class RekaplapormorfologiController extends Controller {

	protected $layout = "layouts.main";
	protected $data = array();	
	public $module = 'rekaplapormorfologi';
	static $per_page	= '50';

	public function __construct()
	{
		
		parent::__construct();
		$this->model = new Rekaplapormorfologi();
		$this->info = $this->model->makeInfo( $this->module);
		$this->access = array();
	
		$this->data = array_merge(array(
			'pageTitle'	=> 	$this->info['title'],
			'pageNote'	=>  $this->info['note'],
			'pageModule'=> 'rekaplapormorfologi',
			'return'	=> self::returnUrl()
			
		),$this->data);


		
	}

	public function index( Request $request )
	{
		// Make Sure users Logged 
		if(!\Auth::check()) 
			return redirect('user/login')->with('status', 'error')->with('message','You are not login');

		$kebun = $request->input('filter_kebun', NULL);
		$rayon = $request->input('filter_rayon', NULL);
		$tahun = $request->input('filter_tahun', '2025');
		$periode = $request->input('filter_periode', []);
		if (!is_array($periode)) {
			$periode = [$periode];
		}
		$periode = implode(',', $periode);
		if(!empty($periode)) {
			$result = \DB::select("CALL sp_rekap_morfologi_lapor_multi(?, ?, ?, ?)", [
				$kebun,
				$rayon,
				$tahun,
				$periode
			]);
		}else {
			$result = [];
		}
		

		$this->data['results'] = $result;
		// Render into template
		return view( $this->module.'.index',$this->data);
	}	

	public function detail( Request $request ) 
	{
		$kebun = $request->input('filter_kebun', NULL);
		$rayon = $request->input('filter_rayon', NULL);
		$afdeling = $request->input('filter_afdeling', NULL);
		$periode = $request->input('filter_periode', NULL);
		$kategori = $request->input('kategori', NULL);

		if(!empty($periode)) {
			if($kategori ==	1){
				$result = \DB::select("CALL sp_detail_lahan_lapor_multi(?, ?, ?, ?, ?)", [
					$kebun,
					$rayon,
					$afdeling,
					'2025',
					$periode
				]);
			}else if($kategori == 2){
				$result = \DB::select("CALL sp_detail_lahan_tkp_multi(?, ?, ?, ?, ?)", [
					$kebun,
					$rayon,
					$afdeling,
					'2025',
					$periode
				]);
			}
		}else {
			$result = [];
		}
		$html = '<table border="1" cellpadding="5" cellspacing="0"><thead><tr>';
		if (!empty($result)) {
			foreach (array_keys((array)$result[0]) as $col) {
				$html .= '<th>' . htmlspecialchars($col) . '</th>';
			}
			$html .= '</tr></thead><tbody>';
			foreach ($result as $row) {
				$html .= '<tr>';
				foreach ((array)$row as $value) {
					$html .= '<td>' . htmlspecialchars($value) . '</td>';
				}
				$html .= '</tr>';
			}
			$html .= '</tbody></table>';
		} else {
			$html .= '<th>No Data</th></tr></thead><tbody><tr><td colspan="100%">No results found.</td></tr></tbody></table>';
		}
		return response($html);
	}

	function show( Request $request , $id ) 
	{
		/* Handle import , export and view */
		$task =$id ;
		switch( $task)
		{

			case 'export':
				return $this->getExport( $request );
				break;
			case 'detail':
				return $this->detail( $request );
				break;

		}
	}
}