<?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\GradeModel;

class GradeController extends Controller
{
	public function __construct()
	{
		$this->Grade= new GradeModel;
	}
	public function LoadGrade()
    {
		$data['url']='loadgrade';
		$Mid=$this->Grade->UserTypePermission($data);
		$data['delete']= $Mid->Delete;
		$data['edit']= $Mid->Edit;
		$data['create']= $Mid->create;
		$status= 'Deleted';
		$data['grade']=DB::table('grd_grade')->where([["grd_status","<>",$status]])->get();
		return view('grade',compact('data'));
    }
    public function InsertGrade(Request $req)
    {
		$grd_id = $req->input('grd_id');
		$grd_grade = $req->input('grd_grade');
		$grd_prefix = $req->input('grd_prefix');
		$grd_description = $req->input('grd_description');
		$grd_status = $req->input('grd_status');		
		if($grd_id == 0)
		{			
			$qinsert = DB::table('grd_grade')->insertGetId(['grd_grade' => $grd_grade,'grd_prefix' => $grd_prefix,'grd_description' => $grd_description,'grd_status' => $grd_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('Grade record inserted...')]);
		}
		else
		{
			$data = array('grd_grade'=>$grd_grade,'grd_prefix'=>$grd_prefix,'grd_description'=>$grd_description,'grd_status'=>$grd_status);
			$updatedetails = DB::table('grd_grade')->where('grd_id',$grd_id)->update($data);
			if (!is_null($updatedetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Grade Updated Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Updation.. Please Try again..')]);
			}
		}
    }	
	//Edit User type records
	public function EditGrade(Request $req)
	{
		$id = $req->id;
		$sqlstatement = DB::table('grd_grade')->where(['grd_id'=>$id])->get();
		return response()->json($sqlstatement);
	}	
	//Delete User type records
	public function DeleteGrade(Request $req)
	{
		$id = $req->id;
		$status= 'Deleted';
		$data = array('grd_status'=>$status);
		
		$UpdateDetails = DB::table('grd_grade')->where('grd_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..')]);
		}
	}
	//This will load branch details to the combo box
	public function LoadGradeList()
	{
		$status= 'Deleted';
		$reclist = DB::select( DB::raw("SELECT * FROM grd_grade where grd_status != '$status'") );
		return response()->json($reclist);
	}
}