<?php
/**
 * app/plugins/resume/controllers/admin/resume.php
 *
 * This is the Resume admin controller.
 *
 * @package TP Resume
 * @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\Positions;

class Resume extends AdminController {
    public static $positions;

    public function __construct() {
        parent::__construct();
        self::$positions = new Positions;
        self::$title = 'Admin - Resume';
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/resume' );
        Components::set( 'ADMINNAV', $view );
    }

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

    public function create( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'resume.admin.create' );
        }
        if ( !Forms::check( 'newPosition' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return $this->index();
        }
        $result = self::$positions->create( Input::post( 'name' ), Input::post( 'position' ), Input::post( 'start' ), Input::post( 'end' ), Input::post( 'details' ), Input::post( 'detailsNobs' ) );
        if ( $result ) {
            Issues::add( 'success', 'Your position 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( 'resume.admin.edit', self::$positions->findById( $data ) );
        }
        if ( !Forms::check( 'editPosition' ) ) {
            Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
            return $this->index();
        }
        $fields = [
            'name' => Input::post( 'name' ),
            'position' => Input::post( 'position' ),
            'start' => Input::post( 'start' ),
            'end' => Input::post( 'end' ),
            'details' => Input::post( 'details' ),
            'details_nobs' => Input::post( 'detailsNobs' ),
        ];
        if ( self::$positions->update( $data, $fields ) ) {
            Issues::add( 'success', 'Position Updated.' );
            return $this->index();
        }
        Issues::add( 'error', 'There was an error with your request.' );
        $this->index();
    }

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

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