<?php

namespace App\Http\Controllers\Parametres\Base;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ParametreBaseController extends Controller
{
    protected $_structure_id;
    protected $_route;
    protected $_type_parent;

    public function __construct(string $route, string $type_parent = 'Structure')
    {
        $this->middleware('permission:see_parametre')->only(['index']);
        $this->middleware('permission:add_parametre')->only(['create', 'store']);
        $this->middleware('permission:edit_parametre')->only(['edit', 'update']);
        $this->middleware('permission:delete_parametre')->only(['destroy']);

        $this->middleware(function ($request, $next) {
            $this->_structure_id = Auth::user()->structure_id;
            return $next($request);
        });
        $this->_route = $route;
        $this->_type_parent = $type_parent;
    }

    /**
     * Permet de vérifier si l'utilisateur modifie un paramètre de sa structure
     * @param int $parametre_structure_id
     * @return bool
     */
    protected function checkAccesStructure(int $parametre_structure_id)
    {
        return $parametre_structure_id == $this->_structure_id;
    }
}
