<?php
/**
 * app/plugins/members/controllers/admin/members/products.php
 *
 * This is the Membership Products admin controller.
 *
 * @package TP Members
 * @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\Views;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Models\MembershipProducts;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Bedrock\Functions\Check;

class Products extends AdminController {
    public static $products;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Membership Products';
        self::$products = new MembershipProducts;
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/products' );
        Components::set( 'ADMINNAV', $view );
    }

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

    public function create( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'members.admin.products.create' );
        }
        if ( !Forms::check( 'newMembershipProduct' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return $this->index();
        }
        $result = self::$products->create( Input::post( 'name' ), Input::post( 'description' ), Input::post( 'monthly_price' ), Input::post( 'yearly_price' ) );
        if ( $result ) {
            Issues::add( 'success', 'Your product 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( $id = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'members.admin.products.edit', self::$products->findById( $id ) );
        }
        if ( !Forms::check( 'editMembershipProduct' ) ) {
            Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
            return $this->index();
        }

        if ( self::$products->updatePost( $id, Input::post( 'name' ), Input::post( 'description' ), Input::post( 'monthly_price' ), Input::post( 'yearly_price' ) ) === true ) {
            Issues::add( 'success', 'Your product Updated.' );
            return $this->index();
        }
        Issues::add( 'error', 'There was an error with your request.' );
        $this->index();
    }

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

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