<?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\Http\Request; // need to be added
use DB; // need to be added

class BlockController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
	function insert (Request $request)
	{
				// passing values from text box to the variable
			$pk=$request->input('pk');	
		$block=$request->input('block'); 	
		$description=$request->input('description');
		$status=$request->input('status');
	
		
		// passing values to the db now
		
		if(empty($pk))
		{
		
		 DB::table('blok_block')->insertGetId(['blok_name'=>$block,'blok_note'=>$description,'blok_status'=>$status]);
		}
		else
		{
				 DB::table('blok_block')-> where('blok_id',$pk)-> update(['blok_name'=>$block,'blok_note'=>$description,'blok_status'=>$status]);
			
		}
		 // insertgetid shows u under which id the inserted value stores
		 
		 echo'sucess';
		 $this->getData();

	}
	
	// function to display the data
	function getData()
	{
		$data['data']=DB::table('blok_block') -> where ('blok_status','=','Active')-> get();
		if(count($data)>0)
		{
			return view('block',$data);
		}
		else
		{
			return view('block');
		} 
		return view('block');
	}
	
	function delrecs(request $request)
	
	{
		$id=$request->input('id');
 DB::table('blok_block')-> where('blok_id',$id)-> update(['blok_status'=>'Deleted']);
	
	}
	
	function editblock(Request $request)
	{
			$id=$request->input('id');
		$data=DB::table('blok_block')-> where ('blok_id','=',$id)-> where ('blok_status','=','Active')->get();	
		return response()-> json($data);
	}
	
}


