<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Http\Request;
use DB;
use Validator;
use App\Http\Model\GradeFundModel;

class GradeFundController extends Controller
{
	public function __construct()
	{
		$this->GradeFund= new GradeFundModel;
	}
	public function LoadGradeFund()
    {
		$data['url']='loadgradefund';
		$Mid=$this->GradeFund->UserTypePermission($data);
		$data['delete']= $Mid->Delete;
		$data['edit']= $Mid->Edit;
		$data['create']= $Mid->create;
		$status='Deleted';
		//$data['fiscalyear'][0]->fyr_id
		$data['fiscalyear'] = DB::table('fyr_fiscalyear')->whereRaw('? between fyr_startdate and fyr_enddate', [date('Y-m-d')])->where([["fyr_status","=",'Active']])->get();
		$fyid = $data['fiscalyear'][0]->fyr_id;
		$data['gradefund']=DB::select( DB::raw("SELECT * FROM grfn_gradefund LEFT JOIN grd_grade on grfn_gradefund.grd_id=grd_grade.grd_id LEFT join ftyp_fundtype on ftyp_fundtype.ftyp_id=grfn_gradefund.ftyp_id  where grfn_gradefund.grfn_status != '$status' AND grfn_gradefund.fyr_id='$fyid' ORDER BY grd_grade.grd_prefix") );
		return view('gradefund',compact('data'));
    }
    public function InsertGradeFund(Request $req)
    {
		$grfn_id = $req->input('grfn_id');
		$ftyp_id = $req->input('ftyp_id');
		$grd_id = $req->input('grd_id');
		$fyr_id = $req->input('fyr_id');
		$grfn_amt = $req->input('grfn_amt');
		$grfn_paycycle = $req->input('grfn_paycycle');
		$grfn_description = $req->input('grfn_description');
		$grfn_status = $req->input('grfn_status');
		
		if($grfn_id == 0)
		{
			$qinsert = DB::table('grfn_gradefund')->insertGetId(['ftyp_id' => $ftyp_id,'grd_id' => $grd_id,'fyr_id' => $fyr_id,'grfn_amt' => $grfn_amt,'grfn_paycycle' => $grfn_paycycle,'grfn_description' => $grfn_description,'grfn_status' => $grfn_status]);	
			$modules=DB::table('modules')->get();
			foreach($modules as $modu)
			{
				$ModuleId=$modu->Mid;
				DB::table('permission')->insert(['UsrId' => $qinsert,'Mid' => $ModuleId]);
			}			
			return response()->json(['status' => '1', 'message' => trans('Gradewise Fund record inserted successfully...')]);
		}
		else
		{
			$data = array('ftyp_id' => $ftyp_id,'grd_id' => $grd_id,'fyr_id' => $fyr_id,'grfn_amt' => $grfn_amt,'grfn_paycycle' => $grfn_paycycle,'grfn_description' => $grfn_description,'grfn_status' => $grfn_status);
			$updatedetails = DB::table('grfn_gradefund')->where('grfn_id',$grfn_id)->update($data);
			if (!is_null($updatedetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Updated Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Updation.. Please Try again..')]);
			}
		}
    }
	//Edit User type records
	public function EditGradeFund(Request $req)
	{
		$id = $req->id;
		$sqlstatement = DB::table('grfn_gradefund')->where(['grfn_id'=>$id])->get();
		return response()->json($sqlstatement);
	}
	//Delete User type records
	public function DeleteGradeFund(Request $req)
	{
		$id = $req->id;
		$status= 'Deleted';
		$data = array('grfn_status'=>$status);		
		$UpdateDetails = DB::table('grfn_gradefund')->where('grfn_id',$id)->update($data);
		if (!is_null($UpdateDetails))
		{
			return response()->json(['status' => '1', 'message' => trans('Record Deleted Successfully.')]);
		}
		else
		{
			return response()->json(['status' => '0', 'message' => trans('Error in Deletion.. Please Try again..')]);
		}
	}
	//Edit User type records
}