<?php
/**
 * app/plugins/suggestions/controllers/admin/suggestions.php
 *
 * This is the suggestions admin controller.
 *
 * @package TP Suggest
 * @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\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Models\Suggestions as SuggestionsModel;
use TheTempusProject\Models\Comments;
use TheTempusProject\Houdini\Classes\Forms as TemplateForm;

class Suggestions extends AdminController {
    protected static $suggestions;
    protected static $comments;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Suggestions';
        self::$suggestions = new SuggestionsModel;
    }

    public function index( $data = null ) {
        Views::view( 'suggestions.admin.list', self::$suggestions->list() );
    }

    public function view( $id = null ) {
        $data = self::$suggestions->findById( $id );
        if ( $data == false ) {
            Issues::add( 'error', 'Suggestion not found.' );
            return $this->index();
        }
        if ( empty( self::$comments ) ) {
            self::$comments = new Comments;
        }
        Components::set( 'COMMENT_TYPE', 'suggestions' );
        Components::set( 'count', self::$comments->count( 'suggestion', $id ) );
        Components::set( 'COMMENTS', Views::simpleView( 'comments.list', self::$comments->display( 10, 'suggestion', $id ) ) );
        Views::view( 'suggestions.admin.view', $data );
    }

    public function approve( $id = null ) {
        $data = self::$suggestions->findById( $id );
        if ( $data == false ) {
            Issues::add( 'error', 'Suggestion not found.' );
            return $this->index();
        }
        if ( !self::$suggestions->approve( $id ) ) {
            Issues::add( 'error', 'Suggestion not approved.' );
            return $this->index();
        }
        Issues::add( 'success', 'Suggestion Approved' );
        return $this->index();
    }

    public function reject( $id = null ) {
        $data = self::$suggestions->findById( $id );
        if ( $data == false ) {
            Issues::add( 'error', 'Suggestion not found.' );
            return $this->index();
        }
        if ( !self::$suggestions->reject( $id ) ) {
            Issues::add( 'error', 'Suggestion not rejected.' );
            return $this->index();
        }
        Issues::add( 'success', 'Suggestion Rejected' );
        return $this->index();
    }

    public function edit( $id = null ) {
        $data = self::$suggestions->findById( $id );
        if ( false == $data ) {
            return $this->index();
        }
        TemplateForm::selectRadio( 'approved', $data->approved );
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'suggestions.admin.edit', $data );
        }
        if ( !Forms::check( 'editSuggestion' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return Views::view( 'suggestions.admin.edit', $data );
        }
        if ( self::$suggestions->update( $id, Input::post( 'title' ), Input::post( 'suggestion' ), Input::post( 'approved' ) ) ) {
            Issues::add( 'success', 'Suggestion updated' );
        } else {
            return Views::view( 'suggestions.edit', $data );
        }
    }

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

    public function clear( $data = null ) {
        self::$suggestions->empty();
        $this->index();
    }
}
