<?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 TariffChartController extends BaseController
	{
		use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
		function insert (Request $request)
		{
			// passing values from text box to the variable
			$pk=$request->input('pk');		
			$tfid=$request->input('0');		
			$ufrm=$request->input('ufrm'); 	
			$uto=$request->input('uto');
			$tch=$request->input('tch');
			$tchrg=$request->input('tchrg');
			$status=$request->input('status');
						
			// passing values to the db now
			if (empty($pk))
			{
				
				
				DB::table('tarc_tarffcharge')->insertGetId(['tarf_id'=>$tfid,'tarc_unitfrom'=>$ufrm,'tarc_unitto'=>$uto,'tarc_tariffrange'=>$tchrg,'tarc_tariffcharge'=>$tch,'tarc_status'=>$status]);
			}
			// insertgetid shows u under which id the inserted value stores
			else
			{
				DB::table('tarc_tarffcharge')->where('tarc_id',$pk)->update(['tarf_id'=>$tfid,'tarc_unitfrom'=>$ufrm,'tarc_unitto'=>$uto,'tarc_tariffrange'=>$tchrg,'tarc_tariffcharge'=>$tch,'tarc_status'=>$status]);
			}
			echo'sucess';
			
			$this->getData();			
		}
		
		// function to display the data
		function getData()
		{
			$data['data']=DB::table('tarc_tarffcharge')-> where ('tarc_status','=','Active')->get();
			/* if(count($data)>0)
			{
				return view('Tariff_Chart',$data);
			}
			else
			{
				return view('Tariff_Chart');
			}  */
			return view('Tariff_Chart',$data);
		}
		
		function delrecs(request $request)		
		{
			$id=$request->input('id');
			DB::table('tarc_tarffcharge')-> where('tarc_id',$id)-> update(['tarc_status'=>'Deleted']);			
		}
		
		function editUserType(Request $request)
		{
			$id=$request->input('id');
			$data=DB::table('tarc_tarffcharge')-> where ('tarc_id','=',$id)-> where ('tarc_status','=','Active')->get();	
			return response()-> json($data);
		}
	}