<?php
/**
 * Created by PhpStorm.
 * User: eater
 * Date: 4/2/16
 * Time: 12:56 PM
 */

namespace Eater\Glim\Handler\Login;


use Aura\Session\Segment;
use Eater\Glim\Handler\Main;
use Eater\Glim\Model\UserQuery;

class Show extends Main
{
    public function beforeHandle()
    {
        $session = $this->get('session');
        $segment = $session->getSegment('main');
        $user = $segment->get('user');

        if ($user) {
            $this->redirect('/panel');
        }

        if (!UserQuery::create()->findOne()) {
            return $this->redirect('/install');
        }
    }

    public function handle()
    {
        /** @var Segment $segment */
        $segment = $this->get('session')->getSegment('main');

        $this->get('twig-vars')->def('error', $segment->getFlash('error'));

        return $this->render(
            "login.html.twig"
        );
    }
}