<?php

namespace App\Http\Controllers\Master;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cabang;
use App\Helpers\AuditTrailHelper;
use App\Models\Admin;
use App\Models\CostCentre;
use App\Models\Region;
use App\Models\Rayon;

class CabangController extends Controller
{
  public function index()
  {
		$this->checkAuthorization(auth()->user(), ['cabang.view']);
		session()->put('redirect_url', route('admin.cabang.index'));

		return view('backend.pages.master.cabang.index', [
			'cabang' => Cabang::with(['head', 'region', 'cost'])->get()
		]);
  }

	public function getCabangByRegion($regionCode)
	{
		$region_id = Region::where('code', $regionCode)->first()->id;
		$cabang = Cabang::where('region_id', $region_id)->get(['code', 'name']);
		return response()->json($cabang);
	}

	public function getRayonByCabang($cabangCode)
	{
		$cabang_id = Cabang::where('code', $cabangCode)->first()->id;
		$rayon = Rayon::where('cabang_id', $cabang_id)->get(['code', 'name']);
		return response()->json($rayon);
	}

	public function getCabangByRegionId($regionId)
	{
		$cabang = Cabang::where('region_id', $regionId)->get();
		return response()->json($cabang);
	}

	public function getRayonByCabangId($cabangId)
	{
		$rayon = Rayon::where('cabang_id', $cabangId)->get();
		return response()->json($rayon);
	}

	public function create()
	{
		$this->checkAuthorization(auth()->user(), ['cabang.create']);
		return view('backend.pages.master.cabang.create', [
			'users' => Admin::getUserWhereRoleIsNot('Medical Representatif'),
			'cost_centres' => CostCentre::all(),
			'regions' => Region::all()
		]);
	}

	public function store(Request $request)
	{
		$this->checkAuthorization(auth()->user(), ['cabang.create']);
		$request->validate([
			'head_id' => 'required|exists:admins,id',
			'region_id' => 'required|exists:region,id',
			'cost_id' => 'required|exists:cost_centre,id',
			'code' => 'required|unique:cabang',
			'name' => 'required|unique:cabang'
		]);

		Cabang::create([
			'head_id' => $request->head_id,
			'region_id' => $request->region_id,
			'cost_id' => $request->cost_id,
			'code' => $request->code,
			'name' => $request->name
		]);

		AuditTrailHelper::AddAuditTrail('Insert', 'Insert New Record at Cabang Table (' . $request->name . ')');
		session()->flash('success', 'Cabang has been added successfully.');
		return redirect()->back();
	}

	public function edit($id)
	{
		$this->checkAuthorization(auth()->user(), ['cabang.edit']);
		return view('backend.pages.master.cabang.edit', [
			'cabang' => Cabang::findOrfail($id),
			'users' => Admin::getUserWhereRoleIsNot('Medical Representatif'),
			'cost_centres' => CostCentre::all(),
			'regions' => Region::all()
		]);
	}

	public function update(Request $request, $id)
	{
		$this->checkAuthorization(auth()->user(), ['cabang.edit']);
		$request->validate([
			'head_id' => 'required|exists:admins,id',
			'region_id' => 'required|exists:region,id',
			'cost_id' => 'required|exists:cost_centre,id',
			'code' => 'required|unique:cabang,code,' . $id,
			'name' => 'required|unique:cabang,name,' . $id
		]);

		Cabang::findOrfail($id)->update([
			'head_id' => $request->head_id,
			'region_id' => $request->region_id,
			'cost_id' => $request->cost_id,
			'code' => $request->code,
			'name' => $request->name
		]);

		AuditTrailHelper::AddAuditTrail('Update', 'Update New Record at Cabang Table (' . $request->name . ')');
		session()->flash('success', 'Cabang has been updated successfully.');
		return redirect()->back();
	}

	public function destroy($id)
	{
		$this->checkAuthorization(auth()->user(), ['cabang.delete']);
		$cabang = Cabang::findOrfail($id);
		AuditTrailHelper::AddAuditTrail('Delete', 'Delete Record at Cabang Table (' . $cabang->name . ')');

		$cabang->delete();
		session()->flash('success', 'Cabang has been deleted successfully.');
		return redirect()->back();
	}
}