<?php
namespace App\Controller\Admin01;

/**
 * Login Controller
 *
 *
 * @method \App\Model\Entity\Login[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class LoginController extends AppController
{

    public function beforeRender(\Cake\Event\Event $event) {
        parent::beforeRender($event);
        $this->viewBuilder()->layout('Admin01/admin01_login');
        if($this->Auth->user() != null){
            return $this->redirect(['controller' => 'dashboard', 'action' => 'index']); 
        }
    }
    
    /**
     * Index method
     *
     * @return \Cake\Http\Response|void
     */
    public function index() {
        if ($this->request->is('post')) {

            $administrator = $this->Auth->identify();

            if ($administrator) {                
                $this->Auth->setUser($administrator);
                
                $this->Flash->admin01_success(__('Welcome! {0}', $administrator['first_name'].' '.$administrator['last_name']));
                return $this->redirect(['controller' => 'dashboard', 'action' => 'index']);
            }

            $this->Flash->admin01_error("Invalid Login");
        }
        $this->set('_serialize', ['login']);
    }

    public function logout() {
        $this->Flash->admin01_success("You have successfully logged out!");
        return $this->redirect($this->Auth->logout());
    }
}
