<?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 WardController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
	function insert (Request $request)
	{
				// passing values from text box to the variable
				$pk=$request->input('pk');	
		$ward=$request->input('ward'); 	
		$description=$request->input('note');
		$status=$request->input('status');
$blkid=$request->input('0');
		if (empty($pk))
		{
		// passing values to the db now
		
	DB::table('ward_ward')->insertGetId(['ward_name'=>$ward,'ward_note'=>$description,'ward_status'=>$status,'blok_id'=>$blkid]);
		}
		else
		{
		DB::table('ward_ward')-> where ('ward_id',$pk)->update(['ward_name'=>$ward,'ward_note'=>$description,'ward_status'=>$status,'blok_id'=>$blkid]);	
		}	
		 // 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('ward_ward')-> where('ward_status','=','Active')->get();
		if(count($data)>0)
		{
			return view('Ward',$data);
		}
		else
		{
			return view('Ward');
		}
		return view('ward');
	} 
	
	
	function delrecs(request $request)
	{
		$id=$request->input('id');
		DB::table('ward_ward')-> where('ward_id',$id)-> update(['ward_status'=>'Deleted']);	
	}
	
	function editward(Request $request)
	{
		$id=$request->input('id');
		$data=DB::table('ward_ward')-> where ('ward_id','=',$id)-> where ('ward_status','=','Active')->get();	
		return response()-> json($data);
	}
	
}


