<?php
/**
 * app/plugins/reviews/controllers/admin/reviews.php
 *
 * This is the reviews admin controller.
 *
 * @package TP Reviews
 * @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\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Models\Review;
use TheTempusProject\Models\ReviewCategory;
use TheTempusProject\Classes\Forms;

class Reviews extends AdminController {
    protected static $reviews;
    protected static $categories;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Reviews';
        self::$reviews = new Review;
        self::$categories = new ReviewCategory;
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/reviews' );
        Components::set( 'ADMINNAV', $view );
    }

    public function index( $data = null ) {
        $reviews = Views::simpleView( 'reviews.admin.reviewList', self::$reviews->list() );
        $categories = Views::simpleView( 'reviews.admin.categoryList', self::$categories->list() );
        Components::set( 'reviews', $reviews );
        Components::set( 'reviewCategories', $categories );
        Views::view( 'reviews.admin.dashboard' );
    }

    public function categoryView( $id = null ) {
        $category = self::$categories->findById( $id );
        if ( ! $category ) {
            Issues::add( 'error', 'Unknown Category.' );
            return $this->index();
        }
        Views::view( 'reviews.admin.category', $category );
    }

    public function categoryCreate( ) {
        if ( ! Input::exists( 'submit' ) ) {
            return Views::view( 'reviews.admin.categoryCreate' );
        }
        if ( ! Forms::check( 'categoryCreate' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return Views::view( 'reviews.admin.categoryCreate' );
        }
        $result = self::$categories->create(
            Input::post( 'name' ),
            Input::post( 'slug' ),
        );
        if ( $result ) {
            Issues::add( 'success', 'Your review category has been created.' );
            return $this->index();
        }
        Issues::add( 'error', [ 'There was an unknown error submitting your data.' => Check::userErrors() ] );
        Views::view( 'reviews.admin.categoryCreate' );
    }

    public function categoryEdit( $id = null ) {
        $category = self::$categories->findById( $id );
        if ( ! $category ) {
            Issues::add( 'error', 'Unknown Category.' );
            return $this->index();
        }
        if ( ! Input::exists( 'submit' ) ) {
            return Views::view( 'reviews.admin.categoryEdit', $category );
        }
        if ( ! Forms::check( 'categoryEdit' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return Views::view( 'reviews.admin.categoryEdit', $category );
        }
        $result = self::$categories->update(
            $id,
            Input::post( 'name' ),
            Input::post( 'slug' ),
        );
        if ( $result ) {
            Issues::add( 'success', 'Your review category has been updated.' );
            return $this->index();
        }
        Issues::add( 'error', [ 'There was an unknown error submitting your data.' => Check::userErrors() ] );
        Views::view( 'reviews.admin.categoryEdit', $category );
    }

    public function categoryDelete( $id = null ) {
        if ( self::$categories->delete( $id ) ) {
            Issues::add( 'success', 'review category deleted' );
        } else {
            Issues::add( 'error', 'There was an error with your request.' );
        }
        $this->index();
    }

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

    public function reviewApprove( $id = null ) {
        if ( self::$reviews->approve( $id ) ) {
            Issues::add( 'success', 'review approved' );
        } else {
            Issues::add( 'error', 'There was an error with your request.' );
        }
        $this->index();
    }

    public function reviewHide( $id = null ) {
        if ( self::$reviews->hide( $id ) ) {
            Issues::add( 'success', 'review hidden' );
        } else {
            Issues::add( 'error', 'There was an error with your request.' );
        }
        $this->index();
    }

    public function reviewDelete( $id = null ) {
        if ( self::$reviews->delete( $id ) ) {
            Issues::add( 'success', 'review deleted' );
        } else {
            Issues::add( 'error', 'There was an error with your request.' );
        }
        $this->index();
    }
}
