<?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 Input;
	use App\Http\Model\ShedModel;
	use Validator;
	
	class ShedController extends Controller
	{
		public function __construct()
		{
			$this->Shed= new ShedModel;
		}
		public function LoadShed()
		{
		    $data['url']='shed';
		    $Mid=$this->Shed->ShedPermission($data);
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			$status= 'Deleted';
			$data['Shed'] = DB::select( DB::raw("SELECT * FROM `shd_shed` inner join br_branch on shd_shed.br_id=br_branch.br_id inner join usr_user on shd_shed.usr_id=usr_user.usr_id where shd_status != '$status'") );
			return view('shed',compact('data'));
		}
		public function firm()
		{
		    $data['HOC']=$this->Shed->HeadOfcompany();
		    return view('HeadOfcompany',compact('data'));
		}
		
		public function imageUpoload(Request $request)
		{
			$image=$request->input('image');
			$counts='1';
			$path = 'Upload/'.$counts;
			
			if(!File::exists($path)) 
			{
				$result = File::makeDirectory($path,0777);
				$destinationPath=$path;
				$fn=Input::file('image');
				if($fn)
		        {
			        
					$f="Photo.jpg";
					$fname=Input::file('image')->getClientOriginalName();
					$data['image']=Input::file('image')->move($destinationPath,$f);
				}
				else
				{
					$data['image']=$request->input('image');
				}
			}
			else
			{
				$destinationPath=$path;
				
				$fn=Input::file('image');
				if($fn)
		        {
					
					$f="Photo.jpg";
					$fname=Input::file('image')->getClientOriginalName();
					$data['image']=Input::file('image')->move($destinationPath,$f);
				}
				else
				{
					$data['image']=$request->input('image');
				}
				
			}
			
			return $id=$this->Shed->uploadImage($data);
			
		}
		public function EditCompanyProfile()
		{
			$data['company']=$this->Shed->HeadOfcompany();
			return view('companyProfile',compact('data'));
		}
		public function InsertShed(Request $req)
		{
			$shd_id = $req->input('shd_id');
			$branch= $req->input('branch');
			$user= $req->input('user');
			$shedname= $req->input('shedname');
			if($req->input('description') == '')
			{
				$description= ''; 
			}
			else
			{
				$description= $req->input('description'); 
			}
			$status = $req->input('status');
			$data = array('br_id'=>$branch,'usr_id'=>$user,'shd_name'=>$shedname,'shd_description'=>$description,'shd_status'=>$status);
			
			if($shd_id == 0)
			{
				$insertdetails = DB::table('shd_shed')->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('shd_shed')->where('shd_id',$shd_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 LoadBranch()
		{
			$status= 'Deleted';
			$branch = DB::select( DB::raw("SELECT * FROM `br_branch` where br_status != '$status'") );
			return response()->json($branch );
		}
		
		public function LoadUser(Request $req)
		{
			$status= 'Deleted';
			$brid= $req->input('branch');
			$user = DB::select( DB::raw("SELECT * FROM `usr_user` where usr_status != '$status' and br_id=$brid") );
			return response()->json($user );
		}
		
		public function EditShed(Request $req)
		{
			$id = $req->id;
			$shed = DB::table('shd_shed')->where(['shd_id'=>$id])->get();
			return response()->json($shed);
		}
		
		public function DeleteShed(Request $req)
		{
			$id = $req->id;
			$status= 'Deleted';
			$data = array('shd_status'=>$status);
			
			$UpdateDetails = DB::table('shd_shed')->where('shd_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..')]);
			}
		}
		public function updateProfileCompany(Request $request)
		{
			$data['Cname']=$request->input('Cname');
			$data['Ccountry']=$request->input('Ccountry');
			$data['CEmail']=$request->input('CEmail');
			$data['CMob']=$request->input('CMob');
			$data['CPhone']=$request->input('CPhone');
			$data['Cadd']=$request->input('Cadd');
			$data['Ccity']=$request->input('Ccity');
			$data['Cstate']=$request->input('Cstate');
			return $company=$this->Shed->UpdateOfcompany($data);
		}
	}
?>
