<?php namespace App\Http\Controllers;

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


class RekaptimbhariankebunpgController extends Controller {

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

	public function __construct()
	{
		
		parent::__construct();
		$this->model = new Rekaptimbhariankebunpg();
		$this->info = $this->model->makeInfo( $this->module);
		$this->access = array();
	
		$this->data = array_merge(array(
			'pageTitle'	=> 	$this->info['title'],
			'pageNote'	=>  $this->info['note'],
			'pageModule'=> 'rekaptimbhariankebunpg',
			'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');
		
		// Render into template
		return view( $this->module.'.index',$this->data);
	}	

	public function getTable(Request $request)
	{
		$tanggal_awal = $request->input('tanggal_awal');
		$tanggal_akhir = $request->input('tanggal_akhir');
		$cluster = $request->input('cluster');
		$kebun = $request->input('kebun');
		$jenis_report = $request->input('jenis_report', 'TON');

		$params = [
			$tanggal_awal,
			$tanggal_akhir,
			$cluster,
			$kebun,
			$jenis_report
		];


		$results = \DB::select("CALL sp_rekap_timbangan_harian_kebun_pg(?, ?, ?, ?, ?)", $params);

		$html = '<table class="table table-bordered"><thead><tr>';
		if (!empty($results)) {
			foreach (array_keys((array)$results[0]) as $col) {
				$html .= '<th>' . strtoupper(str_replace('_', ' ', $col)) . '</th>';
			}
			$html .= '</tr></thead><tbody>';
			foreach ($results 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;

		}
	}
}