<?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 App\Http\Model\BranchModel;
	use Validator;
	
	class BranchController extends Controller
	{
		public function __construct()
		{
			$this->User= new BranchModel;
		}
		public function LoadBranch()
		{
			$data['url']='branch';
		    $Mid=$this->User->BranchPermission($data);
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			$status= 'Deleted';
			$data['Branch']=DB::table('br_branch')->where([["br_status","<>",$status]])->get();
			return view('branch',compact('data'));
		}		
		public function InsertBranch(Request $req)
		{
			$br_id = $req->input('br_id');
			$branchname= $req->input('branchname');
			$branchtype= $req->input('branchtype');
			$description= $req->input('description');
			$status = $req->input('status');
			
			if($br_id == 0)
			{
				$data = array('br_name'=>$branchname,'br_type'=>$branchtype,'br_description'=>$description,'br_status'=>$status);
				$insertdetails = DB::table('br_branch')->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
			{
				$data = array('br_name'=>$branchname,'br_type'=>$branchtype,'br_description'=>$description,'br_status'=>$status);
				$updatedetails = DB::table('br_branch')->where('br_id',$br_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 EditBranch(Request $req)
		{
			$id = $req->id;
			$branch = DB::table('br_branch')->where(['br_id'=>$id])->get();
			return response()->json($branch);
		}
		public function DeleteBranch(Request $req)
		{
			$id = $req->id;
			$status= 'Deleted';
			$data = array('br_status'=>$status);
			
			$UpdateDetails = DB::table('br_branch')->where('br_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 LoadBranchList()
		{
			$status= 'Deleted';
			$branch = DB::select( DB::raw("SELECT * FROM `br_branch` where br_status != '$status'") );
			return response()->json($branch );
		}
	}
