<?php

namespace App\Http\Controllers;

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

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

	public function store(Request $request)
	{	
		if($request->get('unit_id') != ''){
			$update = UnitMeasurement::where('id','=',$request->get('unit_id'))
								->update([
											'unit' => strtoupper(trim($request->get('unit'))),
											'unit_code' => strtoupper($request->get('unitCode'))
										]);
			return redirect()->route('unit_master.create')->with(['update' => true]);
		}
		else{

			$exist = UnitMeasurement::where('unit','=',strtoupper(trim($request->get('unit'))))->count();
			
			if($exist != 0){
				return redirect()->route('unit_master.create')->with(['exist' => true]);
			}
			$insert = UnitMeasurement::create([
											'unit' => strtoupper(trim($request->get('unit'))),
											'unit_code' => strtoupper($request->get('unitCode'))
										]);

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

		
	}

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

		return $unit;
	}
}