<?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 AreaController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
	function insert (Request $request)
	{
	// passing values from text box to the variable
	$pk=$request->input('pk'); 	
	$an=$request->input('aname'); 	
	//$blk=$request->input('block');
	$wrd=$request->input('ward');
	$note=$request->input('note');
	$status=$request->input('status');
	$userid=$request->input('agent');
	// passing values to the db now
		if(empty($pk))
	{
		 DB::table('area_area')->insertGetId(['ward_id'=>$wrd,'area_name'=>$an,'area_note'=>$note,'area_status'=>$status,'usr_id'=>$userid]);
 		 // insertgetid shows u under which id the inserted value stores
	}
		else
	{
		DB::table('area_area')->where('area_id',$pk)->update(['ward_id'=>$wrd,'area_name'=>$an,'area_note'=>$note,'area_status'=>$status]);
	}
		 echo'Sucess';
		 $this->getData();

	   }
	
	// function to display the data
	function getData()
	{
	$data['data']=DB::table('area_area')
	-> leftjoin ('ward_ward','ward_ward.ward_id','=','area_area.ward_id')
	-> leftjoin ('blok_block','blok_block.blok_id','=','ward_ward.blok_id')
	-> leftjoin ('usr_user','usr_user.usr_id','=','area_area.usr_id')
	-> where ('area_status','Active')->get();
	
	$data['block']=DB::table('blok_block')-> where ('blok_status','Active')->get();
	$data['ward']=DB::table('ward_ward')-> where ('ward_status','Active')->get();
	$data['agent']=DB::table('usr_user')-> where ('usr_status','Active')->get();
	return view('Area',compact ('data'));
	}
	
	function delrecs(Request $request)
	{
	$id=$request->input('id');
	DB::table('area_area')-> where('area_id',$id)-> update(['area_status'=>'Deleted']);
	}
	
	function editarea(Request $request)
	{
		$id=$request->input('id');
		$data=DB::table('area_area')-> where ('area_id','=',$id)
		-> leftjoin ('ward_ward','ward_ward.ward_id','=','area_area.ward_id')
		-> leftjoin ('blok_block','blok_block.blok_id','=','ward_ward.blok_id')
        -> leftjoin ('usr_user','usr_user.usr_id','=','area_area.usr_id')
		-> where ('area_status','=','Active')->get();	
		return response()-> json($data);
	}
	
}


