<?php namespace App\Http\Controllers;

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


class MonitoringsyncronisasiController extends Controller {

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

	public function __construct()
	{
		
		parent::__construct();
		$this->model = new Monitoringsyncronisasi();
		$this->info = $this->model->makeInfo( $this->module);
		$this->access = array();
	
		$this->data = array_merge(array(
			'pageTitle'	=> 	$this->info['title'],
			'pageNote'	=>  $this->info['note'],
			'pageModule'=> 'monitoringsyncronisasi',
			'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');
		$filter = " where MONTH(tgl_timbang) = '05' and YEAR(tgl_timbang) = '2025' ";
		
		$params = array(
			"params" => $filter 
		);
		$this->grab( $request, $params) ;
		$month = $request->input('month', date('m')); // Default to May if not provided
		$year = $request->input('year', date('Y')); // Default to 2025 if not provided

		$query = \DB::table('master_pg')
			->select(
				'master_pg.kode_pg',
				'master_pg.nama_pg',
				'data_timbangan.tgl_timbang',
				\DB::raw('COUNT(data_timbangan.no_spta) as jml_spta'),
				\DB::raw('COUNT(data_prodis_console.no_spta) as jml_spta_console')
			)
			->leftJoin('data_timbangan', function($join) use ($month, $year) {
				$join->on('master_pg.kode_pg', '=', \DB::raw('LEFT(data_timbangan.no_spta,4) COLLATE utf8mb4_general_ci'))
					 ->whereRaw('MONTH(data_timbangan.tgl_timbang) = ?', [$month])
					 ->whereRaw('YEAR(data_timbangan.tgl_timbang) = ?', [$year]);
			})

			->leftJoin('data_prodis_console', function($join) use ($month, $year) {
				$join->on('data_timbangan.no_spta', '=', 'data_prodis_console.no_spta');
			})


			->groupBy('master_pg.kode_pg', 'data_timbangan.tgl_timbang')
			->get();

		$this->data['results'] = $query;
		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);
	}	

	public function mejatebu( Request $request )
	{
		// Make Sure users Logged 
		if(!\Auth::check()) 
			return redirect('user/login')->with('status', 'error')->with('message','You are not login');
		$filter = " where MONTH(tgl_timbang) = '05' and YEAR(tgl_timbang) = '2025' ";
		
		$params = array(
			"params" => $filter 
		);
		$this->grab( $request, $params) ;
		$month = $request->input('month', date('m')); // Default to May if not provided
		$year = $request->input('year', date('Y')); // Default to 2025 if not provided

		$query = \DB::table('master_pg')
			->select(
				'master_pg.kode_pg as kode_pg',
				'master_pg.nama_pg as nama_pg',
				'data_meja_tebu.tgl_giling as tgl_giling',
				\DB::raw('COUNT(data_meja_tebu.no_spta) as jml_spta'),
				\DB::raw('COUNT(data_prodis_console.no_spta) as jml_spta_console')
			)
			->leftJoin('data_meja_tebu', function($join) use ($month, $year) {
				$join->on('master_pg.kode_pg', '=', \DB::raw('LEFT(data_meja_tebu.no_spta, 4) COLLATE utf8mb4_general_ci'))
					 ->whereRaw('MONTH(data_meja_tebu.tgl_giling) = ?', [$month])
					 ->whereRaw('YEAR(data_meja_tebu.tgl_giling) = ?', [$year]);
			})
			->leftJoin('data_prodis_console', function($join) use ($month, $year) {
				$join->on('data_meja_tebu.no_spta', '=', 'data_prodis_console.no_spta');
			})

			->groupBy('master_pg.kode_pg', 'data_meja_tebu.tgl_giling')
			->get();

		$this->data['results'] = $query;
		if($this->access['is_view'] ==0) 
			return redirect('dashboard')->with('message', __('core.note_restric'))->with('status','error');				
		// Render into template
		return view( $this->module.'.mejatebu',$this->data);
	}	

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

		$date = $request->input('tgl',date('Y-m-d')); // Default to 2025-05-09 if not provided
		$pg = $request->input('kode_pg', ''); // Default to SG26 if not provided

		$query = \DB::table('data_timbangan')
			->select(
				'master_kebun.nama_kebun',
				'master_rayon.nama_rayon',
				'data_timbangan.tgl_timbang',
				\DB::raw('COUNT(data_timbangan.no_spta) as jml')
			)
			->join('master_petak', 'data_timbangan.nomer_petak', '=', 'master_petak.lahan_id')
			->join('master_afd', 'master_petak.afd_new', '=', 'master_afd.kode_afd')
			->join('master_rayon', 'master_afd.kode_rayon', '=', 'master_rayon.kode_rayon')
			->join('master_kebun', 'master_rayon.kode_kebun', '=', 'master_kebun.kode_kebun')
			->where('data_timbangan.tgl_timbang', '=', $date)
			->whereRaw('LEFT(data_timbangan.no_spta, 4) = ?', [$pg])
			->groupBy('master_rayon.nama_rayon', 'data_timbangan.tgl_timbang')
			->get();

		$this->data['results'] = $query;

		// Return data as JSON
		return response()->json(['status' => 'success', 'data' => $this->data['results']]);
	}

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

		$date = $request->input('tgl',date('Y-m-d')); // Default to 2025-05-09 if not provided
		$pg = $request->input('kode_pg', ''); // Default to SG26 if not provided

		$query = \DB::table('data_meja_tebu')
			->select(
				'master_kebun.nama_kebun',
				'master_rayon.nama_rayon',
				'data_meja_tebu.tgl_giling',
				\DB::raw('COUNT(data_meja_tebu.no_spta) as jml')
			)
			->join('master_petak', 'data_meja_tebu.no_petak', '=', 'master_petak.lahan_id')
			->join('master_afd', 'master_petak.afd_new', '=', 'master_afd.kode_afd')
			->join('master_rayon', 'master_afd.kode_rayon', '=', 'master_rayon.kode_rayon')
			->join('master_kebun', 'master_rayon.kode_kebun', '=', 'master_kebun.kode_kebun')
			->where('data_meja_tebu.tgl_giling', '=', $date)
			->whereRaw('LEFT(data_meja_tebu.no_spta, 4) = ?', [$pg])
			->groupBy('master_rayon.nama_rayon', 'data_meja_tebu.tgl_giling')
			->get();

		$this->data['results'] = $query;

		// Return data as JSON
		return response()->json(['status' => 'success', 'data' => $this->data['results']]);
	}
	
	function show( Request $request , $id ) 
	{
		/* Handle import , export and view */
		$task =$id ;
		switch( $task)
		{

			case 'export':
				return $this->getExport( $request );
				break;
			case 'mejatebu':
				return $this->mejatebu( $request );
				break;
			case 'getdatadetail':
				return $this->getdatadetail( $request );
				break;
			case 'getdatadetail2':
				return $this->getdatadetail2( $request );
				break;	
	
		}
	}
}