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

class UnitController extends Controller
{
    public function LoadUnit()
    {
    	$status= 'Deleted';
		$data['Unit']=DB::table('unt_unit')->where([["unt_status","<>",$status]])->get();
    	return view('unit',compact('data'));
    }

	public function InsertUnit(Request $req)
    {
		$unt_id = $req->input('unt_id');
    	$unittype= $req->input('unittype');
		$description= $req->input('description');
		$status = $req->input('status');
		$data = array('unt_type'=>$unittype,'unt_description'=>$description,'unt_status'=>$status);
		
		if($unt_id == 0)
		{
			$insertdetails = DB::table('unt_unit')->insert($data);
			if (!is_null($insertdetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Inserted Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Insertion.. Please Try again..')]);
			}
		}
		else
		{
			$UpdateDetails = DB::table('unt_unit')->where('unt_id',$unt_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..')]);
			}
		}
		
    }

   public function EditUnit(Request $req)
	{
		$id = $req->id;
		$unit = DB::table('unt_unit')->where(['unt_id'=>$id])->get();
		return response()->json($unit);
	}
	
	public function DeleteUnit(Request $req)
	{
		$id = $req->id;
		$status= 'Deleted';
		$data = array('unt_status'=>$status);
	
		$UpdateDetails = DB::table('unt_unit')->where('unt_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..')]);
		}
	}
}

?>
