<?php namespace App\Http\Controllers;

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


class PrognosaController extends Controller {

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

	public function __construct()
	{
		
		parent::__construct();
		$this->model = new Prognosa();
		$this->info = $this->model->makeInfo( $this->module);
		$this->access = array();
	
		$this->data = array_merge(array(
			'pageTitle'	=> 	$this->info['title'],
			'pageNote'	=>  $this->info['note'],
			'pageModule'=> 'prognosa',
			'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');
		$fid = substr(session('fid'), 0, 6);
		$filter = ' and LEFT(kode_rayon,6) = "'.$fid.'" ';
		$params = array(
			'params' => $filter
		);

		$today = \Carbon\Carbon::now();
		$year = $today->year;
		$month = $today->month;
		$startA = \Carbon\Carbon::create($year, $month, 1);
		$endA = \Carbon\Carbon::create($year, $month, 15);
		$startB = \Carbon\Carbon::create($year, $month, 16);
		$endB = \Carbon\Carbon::create($year, $month, $startB->daysInMonth);
		$isA = $today->between($startA, $endA);
		$isB = $today->between($startB, $endB);

		$setperiode = '';
		if ($isA) {
			$setperiode = date('n').'A';
		} elseif ($isB) {
			$setperiode = date('n').'B';
		}

		$this->data['periode'] = $setperiode;
		$this->grab( $request, $params) ;
		
		if($this->access['is_view'] ==0) 
			return redirect('dashboard')->with('message', __('core.note_restric'))->with('status','error');				
		// Render into template
		return view( $this->module.'.index',$this->data);
	}	
	function show( Request $request , $id ) 
	{
		/* Handle import , export and view */
		$task =$id ;
		switch( $task)
		{

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

	
		}
	}

	public function store( Request $request){

		$task = $request->input('action_task');
		switch ($task)
		{
			default:
				return $this->postData( $request);
				break;
			case 'save':
					$data = $request->input('data', []);
					$filter_pg = $request->input('filter_pg');
					$filter_periode = $request->input('filter_periode');

					foreach ($data as $kode_rayon => $kategoriArr) {
						foreach ($kategoriArr as $kategori => $bulanArr) {
							foreach ($bulanArr as $bulan_tanam => $prognosa) {
								\DB::table('tx_prognosa')->updateOrInsert(
									[
										'pg_penggiling' => $filter_pg,
										'kode_rayon'    => $kode_rayon,
										'kategori'      => $kategori,
										'bulan_tanam'   => $bulan_tanam,
										'periode'       => $filter_periode,
									],
									[
										'prognosa'      => $prognosa,
										'entry_by'      => session('uid'),
										'createdOn'     => now(),
										'updatedOn'     => now(),
									]
								);
							}
						}
					}
					return response()->json(array('status'=>'success','message'=> __('core.note_success')));	

				break;
			
		}
	}

	public function checkPrognosa(Request $request)
	{
		$pg_penggiling = $request->input('pg_penggiling');
		$periode = $request->input('periode');
		if (!$pg_penggiling) {
			return response()->json(['status' => 'error', 'message' => 'Parameter pg_penggiling diperlukan'], 400);
		}

		$data = \DB::table('tx_prognosa')
			->where('pg_penggiling', $pg_penggiling)
			->where('periode', $periode)
			->get();

		return response()->json([
			'status' => 'success',
			'data' => $data
		]);
	}
}