<?php
declare(strict_types=1);

namespace App\Controller;

/**
 * IpAuths Controller
 *
 * @property \App\Model\Table\IpAuthsTable $IpAuths
 * @method \App\Model\Entity\IpAuth[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class IpAuthsController extends AppController
{
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index()
    {
        $ipAuths = $this->paginate($this->IpAuths);

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

    /**
     * View method
     *
     * @param string|null $id Ip Auth id.
     * @return \Cake\Http\Response|null|void Renders view
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $ipAuth = $this->IpAuths->get($id, [
            'contain' => [],
        ]);

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

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

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The ip auth could not be saved. Please, try again.'));
        }
        $this->set(compact('ipAuth'));
    }

    /**
     * Edit method
     *
     * @param string|null $id Ip Auth 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)
    {
        $ipAuth = $this->IpAuths->get($id, [
            'contain' => [],
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $ipAuth = $this->IpAuths->patchEntity($ipAuth, $this->request->getData());
            if ($this->IpAuths->save($ipAuth)) {
                $this->Flash->success(__('The ip auth has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The ip auth could not be saved. Please, try again.'));
        }
        $this->set(compact('ipAuth'));
    }

    /**
     * Delete method
     *
     * @param string|null $id Ip Auth id.
     * @return \Cake\Http\Response|null|void Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $ipAuth = $this->IpAuths->get($id);
        if ($this->IpAuths->delete($ipAuth)) {
            $this->Flash->success(__('The ip auth has been deleted.'));
        } else {
            $this->Flash->error(__('The ip auth could not be deleted. Please, try again.'));
        }

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

    public function authChange($id = null, $action = null){
        $this->autoRender = false;
        $rsp = ['result' => false, 'mssg' => 'solicitud invalida'];

        if($this->request->is('post')){
            if($upd){
                $rsp = ['result' => true, 'mssg' => 'Acción aplicada'];
            }else{
                $rsp = ['result' => false, 'mssg' => 'La acción no se pudo aplicar'];
            }
        }

        return $this->response($rsp);
    }

    public function changeToken($id = null){

        if($this->request->is('post')){
            $token = $this->generateToken();

            if($this->IpAuths->updateAll(['token' => $token], ['id' => $id])){
                $this->Flash->success('El token ha sido cambiado');
            }else{
                $this->Flash->error('El token no pudo ser cambiado');
            }
        }

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