<?php
declare(strict_types=1);

namespace App\Controller;

/**
 * CitasServicios Controller
 *
 * @property \App\Model\Table\CitasServiciosTable $CitasServicios
 */
class CitasServiciosController extends AppController
{
    /**
     * Verificación centralizada de permisos
     * Se ejecuta antes de cualquier acción
     */
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index()
    {
        $query = $this->CitasServicios->find()
            ->contain(['Servicios', 'Citas']);
        $citasServicios = $this->paginate($query);

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

    /**
     * View method
     *
     * @param string|null $id Citas Servicio id.
     * @return \Cake\Http\Response|null|void Renders view
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $citasServicio = $this->CitasServicios->get($id, contain: ['Servicios', 'Citas']);
        $this->set(compact('citasServicio'));
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $citasServicio = $this->CitasServicios->newEmptyEntity();
        if ($this->request->is('post')) {
            $citasServicio = $this->CitasServicios->patchEntity($citasServicio, $this->request->getData());
            if ($this->CitasServicios->save($citasServicio)) {
                $this->Flash->success(__('The citas servicio has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The citas servicio could not be saved. Please, try again.'));
        }
        $servicios = $this->CitasServicios->Servicios->find('list', limit: 200)->all();
        $citas = $this->CitasServicios->Citas->find('list', limit: 200)->all();
        $this->set(compact('citasServicio', 'servicios', 'citas'));
    }

    /**
     * Edit method
     *
     * @param string|null $id Citas Servicio 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)
    {
        $citasServicio = $this->CitasServicios->get($id, contain: []);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $citasServicio = $this->CitasServicios->patchEntity($citasServicio, $this->request->getData());
            if ($this->CitasServicios->save($citasServicio)) {
                $this->Flash->success(__('The citas servicio has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The citas servicio could not be saved. Please, try again.'));
        }
        $servicios = $this->CitasServicios->Servicios->find('list', limit: 200)->all();
        $citas = $this->CitasServicios->Citas->find('list', limit: 200)->all();
        $this->set(compact('citasServicio', 'servicios', 'citas'));
    }

    /**
     * Delete method
     *
     * @param string|null $id Citas Servicio 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']);
        $citasServicio = $this->CitasServicios->get($id);
        if ($this->CitasServicios->delete($citasServicio)) {
            $this->Flash->success(__('The citas servicio has been deleted.'));
        } else {
            $this->Flash->error(__('The citas servicio could not be deleted. Please, try again.'));
        }

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