<?php
/**
 * app/controllers/admin/routes.php
 *
 * This is the admin routes/redirects controller.
 *
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers\Admin;

use TheTempusProject\Classes\Forms as TTPForms;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Forms;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Models\Routes as RoutesClass;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;

class Routes extends AdminController {
    public static $routes;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Redirects';
        self::$routes = new RoutesClass;
    }

    public function create() {
        if ( ! Input::exists( 'redirect_type' ) ) {
            return Views::view( 'admin.routes.create' );
        }
        
        if ( !TTPForms::check( 'createRoute' ) ) {
            Issues::add( 'error', [ 'There was an error with your route.' => Check::userErrors() ] );
            return Views::view( 'admin.routes.create' );
        }

        if ( self::$routes->create(
            Input::post( 'original_url' ),
            Input::post( 'forwarded_url' ),
            Input::post( 'nickname' ),
            Input::post( 'redirect_type' )
        ) ) {
            Session::flash( 'success', 'Route Created' );
            Redirect::to( 'admin/routes' );
        }

        Issues::add( 'error', 'There was an unknown error saving your redirect.' );
        Views::view( 'admin.routes.create' );
    }

    public function delete( $id = null ) {
        if ( Input::exists( 'submit' ) ) {
            $id = Input::post( 'R_' );
        }
        if ( self::$routes->delete( [ $id ] ) ) {
            Session::flash( 'success', 'Route(s) deleted.' );
        } else {
            Session::flash( 'error', 'There was an error with your request.' );
        }
        Redirect::to( 'admin/routes' );
    }

    public function edit( $id = null ) {
        $route = self::$routes->findById( $id );
        if ( Input::exists( 'redirect_type' ) ) {
            if ( !TTPForms::check( 'editRoute' ) ) {
                Issues::add( 'error', [ 'There was an error with your route.' => Check::userErrors() ] );
            } else {
                if ( self::$routes->update(
                    $id,
                    Input::post( 'original_url' ),
                    Input::post( 'forwarded_url' ),
                    Input::post( 'nickname' ),
                    Input::post( 'redirect_type' )
                ) ) {
                    Session::flash( 'success', 'Route Updated' );
                    Redirect::to( 'admin/routes' );
                }
            }
        }
        Forms::selectOption( $route->redirect_type );
        return Views::view( 'admin.routes.edit', $route );
    }

    public function index() {
        return Views::view( 'admin.routes.list', self::$routes->listPaginated() );
    }

    public function view( $id = null ) {
        return Views::view( 'admin.routes.view', self::$routes->findById( $id ) );
    }
}
