<?php
/**
 * app/plugins/blog/controllers/admin/blog.php
 *
 * This is the Blog admin controller.
 *
 * @package TP Blog
 * @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\Check;
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\Classes\Forms;
use TheTempusProject\Models\Posts;

class Blog extends AdminController {
    public static $posts;

    public function __construct() {
        parent::__construct();
        self::$posts = new Posts;
        self::$title = 'Admin - Blog';
    }

    public function index( $data = null ) {
        Views::view( 'blog.admin.list', self::$posts->listPosts( ['includeDrafts' => true] ) );
    }

    public function create( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'blog.admin.create' );
        }
        if ( !Forms::check( 'newBlogPost' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return $this->index();
        }
        
        $result = self::$posts->newPost( Input::post( 'title' ), Input::post( 'blogPost' ), Input::post( 'slug' ), Input::post( 'submit' ) );
        if ( $result ) {
            Issues::add( 'success', 'Your post has been created.' );
            return $this->index();
        } else {
            Issues::add( 'error', [ 'There was an unknown error submitting your data.' => Check::userErrors() ] );
            return $this->index();
        }
    }

    public function edit( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'blog.admin.edit', self::$posts->findById( $data ) );
        }
        if ( Input::post( 'submit' ) == 'preview' ) {
            return Views::view( 'blog.admin.preview', self::$posts->preview( Input::post( 'title' ), Input::post( 'blogPost' ) ) );
        }
        if ( !Forms::check( 'editBlogPost' ) ) {
            Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
            return $this->index();
        }
        if ( self::$posts->updatePost( $data, Input::post( 'title' ), Input::post( 'blogPost' ), Input::post( 'slug' ), Input::post( 'submit' ) ) === true ) {
            Issues::add( 'success', 'Post Updated.' );
            return $this->index();
        }
        Issues::add( 'error', 'There was an error with your request.' );
        $this->index();
    }

    public function view( $data = null ) {
        $blogData = self::$posts->findById( $data );
        if ( $blogData !== false ) {
            return Views::view( 'blog.admin.view', $blogData );
        }
        Issues::add( 'error', 'Post not found.' );
        $this->index();
    }

    public function delete( $data = null ) {
        if ( $data == null ) {
            if ( Input::exists( 'B_' ) ) {
                $data = Input::post( 'B_' );
            }
        }
        if ( !self::$posts->delete( (array) $data ) ) {
            Issues::add( 'error', 'There was an error with your request.' );
        } else {
            Issues::add( 'success', 'Post has been deleted' );
        }
        $this->index();
    }

    public function preview( $data = null ) {
        Views::view( 'blog.admin.preview', self::$posts->preview( Input::post( 'title' ), Input::post( 'blogPost' ) ) );
    }
}
