<?php 
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Jenis_Layanan_Informasi_OfflineAddRequest;
use App\Http\Requests\Jenis_Layanan_Informasi_OfflineEditRequest;
use App\Models\Jenis_Layanan_Informasi_Offline;
use Illuminate\Http\Request;
use Exception;
class Jenis_Layanan_Informasi_OfflineController extends Controller
{
	

	/**
     * List table records
	 * @param  \Illuminate\Http\Request
     * @param string $fieldname //filter records by a table field
     * @param string $fieldvalue //filter value
     * @return \Illuminate\View\View
     */
	function index(Request $request, $fieldname = null , $fieldvalue = null){
		$view = "pages.jenis_layanan_informasi_offline.list";
		$query = Jenis_Layanan_Informasi_Offline::query();
		$limit = $request->limit ?? 10;
		if($request->search){
			$search = trim($request->search);
			Jenis_Layanan_Informasi_Offline::search($query, $search); // search table records
		}
		if($request->orderby){
			$orderby = $request->orderby;
			$ordertype = ($request->ordertype ? $request->ordertype : "desc");
			$query->orderBy($orderby, $ordertype);
		}
		else{
			$query->orderBy("jenis_layanan_informasi_offline.id", "ASC");
		}
		if($fieldname){
			$query->where($fieldname , $fieldvalue); //filter by a table field
		}
		$records = $query->paginate($limit, Jenis_Layanan_Informasi_Offline::listFields());
		return $this->renderView($view, compact("records"));
	}
	

	/**
     * Select table record by ID
	 * @param string $rec_id
     * @return \Illuminate\View\View
     */
	function view($rec_id = null){
		$query = Jenis_Layanan_Informasi_Offline::query();
		$record = $query->findOrFail($rec_id, Jenis_Layanan_Informasi_Offline::viewFields());
		return $this->renderView("pages.jenis_layanan_informasi_offline.view", ["data" => $record]);
	}
	

	/**
     * Display form page
     * @return \Illuminate\View\View
     */
	function add(){
		return $this->renderView("pages.jenis_layanan_informasi_offline.add");
	}
	

	/**
     * Save form record to the table
     * @return \Illuminate\Http\Response
     */
	function store(Jenis_Layanan_Informasi_OfflineAddRequest $request){
		$modeldata = $this->normalizeFormData($request->validated());
		
		//save Jenis_Layanan_Informasi_Offline record
		$record = Jenis_Layanan_Informasi_Offline::create($modeldata);
		$rec_id = $record->id;
		return $this->redirect("jenis_layanan_informasi_offline", "Record added successfully");
	}
	

	/**
     * Update table record with form data
	 * @param string $rec_id //select record by table primary key
     * @return \Illuminate\View\View;
     */
	function edit(Jenis_Layanan_Informasi_OfflineEditRequest $request, $rec_id = null){
		$query = Jenis_Layanan_Informasi_Offline::query();
		$record = $query->findOrFail($rec_id, Jenis_Layanan_Informasi_Offline::editFields());
		if ($request->isMethod('post')) {
			$modeldata = $this->normalizeFormData($request->validated());
			$record->update($modeldata);
			return $this->redirect("jenis_layanan_informasi_offline", "Record updated successfully");
		}
		return $this->renderView("pages.jenis_layanan_informasi_offline.edit", ["data" => $record, "rec_id" => $rec_id]);
	}
	

	/**
     * Delete record from the database
	 * Support multi delete by separating record id by comma.
	 * @param  \Illuminate\Http\Request
	 * @param string $rec_id //can be separated by comma 
     * @return \Illuminate\Http\Response
     */
	function delete(Request $request, $rec_id = null){
		$arr_id = explode(",", $rec_id);
		$query = Jenis_Layanan_Informasi_Offline::query();
		$query->whereIn("id", $arr_id);
		$query->delete();
		$redirectUrl = $request->redirect ?? url()->previous();
		return $this->redirect($redirectUrl, "Record deleted successfully");
	}
}
