<?php namespace App\Http\Controllers;

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


class RekaptimbsdkebunvspgController extends Controller {

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

	public function __construct()
	{
		
		parent::__construct();
		$this->model = new Rekaptimbsdkebunvspg();
		$this->info = $this->model->makeInfo( $this->module);
		$this->access = array();
	
		$this->data = array_merge(array(
			'pageTitle'	=> 	$this->info['title'],
			'pageNote'	=>  $this->info['note'],
			'pageModule'=> 'rekaptimbsdkebunvspg',
			'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');
		$this->grab( $request) ;

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

	public function getTable(Request $request)
	{
		$tanggal = $request->input('tanggal');
		$kebun = $request->input('kebun');

		// Table 1: sp_rekap_timbangan_sd_kebun_pg
		$params1 = [
			$tanggal,
			$kebun
		];
		$results1 = \DB::select("CALL sp_rekap_timbangan_sd_kebun_pg(?, ?)", $params1);

		$html = '<h4>Rekap Timbangan SD Kebun PG</h4>';
		$html .= '<table class="table table-bordered"><thead><tr>';
		if (!empty($results1)) {
			foreach (array_keys((array)$results1[0]) as $col) {
				$html .= '<th>' . htmlspecialchars($col) . '</th>';
			}
			$html .= '</tr></thead><tbody>';
			foreach ($results1 as $row) {
				$html .= '<tr>';
				foreach ((array)$row as $cell) {
					$html .= '<td>' . htmlspecialchars($cell) . '</td>';
				}
				$html .= '</tr>';
			}
			$html .= '</tbody>';
		} else {
			$html .= '<th>No data found</th></tr></thead><tbody></tbody>';
		}
		$html .= '</table>';

	
		return response($html);
	}

	public function getTable2(Request $request){
			// Table 2: sp_rekap_timbangan_sd_pg_kebun
		$tanggal = $request->input('tanggal2');
		$pg = $request->input('pg');
		$params2 = [
			$tanggal,
			$pg ? $pg : null
		];
		$results2 = \DB::select("CALL sp_rekap_timbangan_sd_pg_kebun(?, ?)", $params2);

		$html = '';
		$html .= '<br><br><h4>Rekap Timbangan SD PG Kebun</h4>';
		$html .= '<table class="table table-bordered"><thead><tr>';
		if (!empty($results2)) {
			foreach (array_keys((array)$results2[0]) as $col) {
				$html .= '<th>' . htmlspecialchars($col) . '</th>';
			}
			$html .= '</tr></thead><tbody>';
			foreach ($results2 as $row) {
				$html .= '<tr>';
				foreach ((array)$row as $cell) {
					$html .= '<td>' . htmlspecialchars($cell) . '</td>';
				}
				$html .= '</tr>';
			}
			$html .= '</tbody>';
		} else {
			$html .= '<th>No data found</th></tr></thead><tbody></tbody>';
		}
		$html .= '</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 'getTable':
				return $this->getTable( $request );
				break;
			case 'getTable2':
				return $this->getTable2( $request );	
				break;
		}
	}
}