<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\StandardMaster;

class StandardMasterController extends Controller
{
    public function index()
	{	
		$standard = StandardMaster::all();
		return view('standard_master/index',compact('standard'));
	}

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

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

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

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

		return $standard;
	}
}
