<?php

declare(strict_types=1);

namespace App\Controller;

/**
 * Personal Controller
 *
 * @property \App\Model\Table\PersonalTable $Personal
 */
class PersonalController extends AppController
{
    /**
     * Verificación centralizada de permisos
     * Se ejecuta antes de cualquier acción
     */
    public function beforeFilter(\Cake\Event\EventInterface $event)
    {
        parent::beforeFilter($event);

        $currentAction = $this->request->getParam('action');

        $this->verificarPermisoORedireccionar('Personal', $currentAction);
    }

    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index()
    {
        $query = $this->Personal->find();
        $personal = $this->paginate($query);

        $this->set(compact('personal'));
    }

    /**
     * View method
     *
     * @param string|null $id Personal id.
     * @return \Cake\Http\Response|null|void Renders view
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $persona = $this->Personal->get($id, contain: []);
        $this->set(compact('persona'));
        // Usar un layout diferenciado para solicitudes normales o AJAX
        if ($this->request->is('ajax')) {
            $this->viewBuilder()->setLayout('ajax');
        } else {
            $this->viewBuilder()->setLayout('default');
        }
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $persona = $this->Personal->newEmptyEntity();
        if ($this->request->is('post')) {
            $persona = $this->Personal->patchEntity($persona, $this->request->getData());
            if ($this->Personal->save($persona)) {
                $this->Flash->success(__('El personal ha sido guardado.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('El personal no se pudo guardar. Por favor, intente nuevamente.'));
        }
        $this->set(compact('persona'));
        // Usar un layout diferenciado para solicitudes normales o AJAX
        if ($this->request->is('ajax')) {
            $this->viewBuilder()->setLayout('ajax');
        } else {
            $this->viewBuilder()->setLayout('default');
        }
    }

    /**
     * Edit method
     *
     * @param string|null $id Personal id.
     * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $persona = $this->Personal->get($id, contain: []);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $persona = $this->Personal->patchEntity($persona, $this->request->getData());
            if ($this->Personal->save($persona)) {
                $this->Flash->success(__('El personal ha sido guardado.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('El personal no se pudo guardar. Por favor, intente nuevamente.'));
        }
        $this->set(compact('persona'));
        // Usar un layout diferenciado para solicitudes normales o AJAX
        if ($this->request->is('ajax')) {
            $this->viewBuilder()->setLayout('ajax');
        } else {
            $this->viewBuilder()->setLayout('default');
        }
    }

    /**
     * Delete method
     *
     * @param string|null $id Personal id.
     * @return \Cake\Http\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $persona = $this->Personal->get($id);
        if ($this->Personal->delete($persona)) {
            $this->Flash->success(__('El personal ha sido eliminado.'));
        } else {
            $this->Flash->error(__('El personal no se pudo eliminar. Por favor, intente nuevamente.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
