<?php

namespace App\Traits\Basics;

trait HasCrudPermissions
{
    protected function registerCrudPermissions(string $entity, ?array $permissions = null, bool $basic = true): void
    {
        if ($basic) {
            $this->middleware("can:$entity.view")->only(['index', 'yajra']);
            $this->middleware("can:$entity.create")->only(['create', 'store']);
            $this->middleware("can:$entity.edit")->only(['edit', 'update', 'updateStatus', 'updateStatusSelector']);
            $this->middleware("can:$entity.delete")->only(['destroy', 'destroySelector']);
        }

        if (!empty($permissions)) {
            foreach ($permissions as $item) {
                $this->middleware("can:{$entity}.{$item['permission']}")->only($item['functions']);
            }
        }
    }
}

