<?php
/**
 * app/plugins/wip/controllers/admin/wip.php
 *
 * This is the projects admin controller.
 *
 * @package TP Wip
 * @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\Projects;

class Wip extends AdminController {
    public static $projects;

    public function __construct() {
        parent::__construct();
        self::$projects = new Projects;
        self::$title = 'Admin - WIP';
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/wip' );
        Components::set( 'ADMINNAV', $view );
    }

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

    public function create( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'wip.admin.create' );
        }
        if ( !Forms::check( 'newProject' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return $this->index();
        }
        $result = self::$projects->create( Input::post( 'title' ), Input::post( 'description' ), Input::post( 'progress' ), Input::post( 'startDate' ) );
        if ( $result ) {
            Issues::add( 'success', 'Your projects 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( 'wip.admin.edit', self::$projects->findById( $data ) );
        }
        if ( !Forms::check( 'editProject' ) ) {
            Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
            return $this->index();
        }
        $fields = [
            'title' => Input::post( 'title' ),
            'description' => Input::post( 'description' ),
            'progress' => Input::post( 'progress' ),
            'startDate' => Input::post( 'startDate' ),
        ];
        if ( self::$projects->update( $data, $fields ) ) {
            Issues::add( 'success', 'Projects Updated.' );
            return $this->index();
        }
        Issues::add( 'error', 'There was an error with your request.' );
        $this->index();
    }

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

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