<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\GradeMaster;
use Auth;

class GradeMasterController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
	
	public function create()
	{	
		$grade = GradeMaster::all();
		return view('grade/create',compact('grade'));
	}

	public function store(Request $request)
	{	
		
		
		if($request->get('grade_id') != ''){
			$update = GradeMaster::where('id','=',$request->get('grade_id'))
								->update([
											'grade' => strtoupper(trim($request->get('grade'))),
											'is_active' => ($request->get('active')) ? 'Y' : 'N'
										]);
		return redirect()->route('grade_master.create')->with(['update' => true]);
		}
		else{
			$exist = GradeMaster::where('grade','=',strtoupper(trim($request->get('grade'))))->count();
			
			if($exist != 0){
				return redirect()->route('grade_master.create')->with(['exist' => true]);
			}

			$insert = GradeMaster::create([
											'grade' => strtoupper(trim($request->get('grade'))),
											'is_active' => ($request->get('active')) ? 'Y' : 'N'
										]);

			return redirect()->route('grade_master.create')->with(['message' => true]);
		}
		
	}

	public function edit(Request $request)
	{
		$id = $request->get('option');
		$grade = GradeMaster::find($id);

		return $grade;
	}
}