<?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\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Plugins\Members as MemberModel;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Hermes\Functions\Route as Routes;
use TheTempusProject\Hermes\Functions\Redirect;

class Members extends AdminController {
    public function __construct() {
        parent::__construct();
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/member' );
        Components::set( 'ADMINNAV', $view );
    }

    public function index( $data = null ) {
        self::$title = 'Admin - Membership Scripts';
        Views::view( 'members.admin.scripts' );
    }

    public function orphans( $data = null, $id = null ) {
        self::$title = 'Admin - Orphaned PRoducts';
        if ( $data = 'abandon' && ! empty( $id ) ) {
            MemberModel::orphanAbandon( $id );
            Redirect::to('admin/members/orphans');
        }
        $orphans = MemberModel::findOrphans();
        Views::view( 'members.admin.orphans', $orphans );
    }

    public function webhooks( $data = null, $id = null ) {
        self::$title = 'Admin - Membership Webhooks';

        if ( $data = 'delete' && ! empty( $id ) ) {
            MemberModel::webhookRemove( $id );
            Redirect::to('admin/members/webhooks');
        }
        
        $data = [];
        $webhooks = MemberModel::webhookList();
        foreach ($webhooks->data as $key => $webhook) {
            $hook = new \stdClass;
            $hook->id = $webhook->id;
            $hook->enabled_events = implode( ', <br>', $webhook->enabled_events );
            $hook->status = $webhook->status;
            $hook->url = $webhook->url;
            $data[] = $hook;
        }
        Components::set( 'urltouse', Routes::getAddress() );

        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'members.admin.webhooks', $data );
        }
        MemberModel::webhookSetup();
        Issues::add( 'success', 'Webhooks Generated' );
        Issues::add( 'error', 'Now, LEAVE!' );
        Redirect::to('admin/members/webhooks');
    }
}
