<?php
/**
 * app/plugins/notifications/controllers/admin/notifications.php
 *
 * This is the notifications admin controller.
 *
 * @package TP Notifications
 * @version 5.0.1
 * @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\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\Models\Notification as NotificationsModel;
use TheTempusProject\Models\Group;
use TheTempusProject\Models\User;
use TheTempusProject\Houdini\Classes\Forms;
use TheTempusProject\Classes\Forms as FormChecker;

class Notifications extends AdminController {
    protected static $notifications;
    public static $user;
    public static $group;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Notifications';
        self::$notifications = new NotificationsModel;
        self::$user = new User;
        self::$group = new Group;
        $view = Navigation::activePageSelect( 'nav.admin', '/admin/Notifications' );
        Components::set( 'ADMINNAV', $view );
    }

    public function index( $data = null ) {
        // this is just a simple form to allow admins to send notifications
        return $this->create();
    }

    public function create() {
        $select = Forms::getSelectHtml(
            'groupSelect',
            self::$group->listGroupsSimple( true )
        );
        Components::set( 'groupSelect', $select );
        if ( ! Input::exists( 'submit' ) ) {
            return Views::view( 'notifications.admin.send' );
        }
        if ( ! FormChecker::check( 'createNotification' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return Views::view( 'notifications.admin.send' );
        }
        if ( Input::exists( 'expires' ) ) {
            $expiresAt = time() + intval( Input::post( 'expires' ) );
        } else {
            $expiresAt = 0;
        }

        if ( Input::exists( 'groupSelect' ) && Input::post( 'groupSelect' ) != 0 && Input::post( 'groupSelect' ) != 'all' ) {
            $list = self::$group->listMembers( Input::post( 'groupSelect' ) );
        } else {
            $list = self::$user->userList();
        }

        $results = [];
        foreach ( $list as $recipient ) {
            $results[] = self::$notifications->create( Input::post( 'notification' ), 'Admin Issued', $recipient->ID, $expiresAt );
        }

        // $result = self::$notifications->create( Input::post( 'notification' ), 'Admin Issued', XXXXXXX_user_idsXXXXXX, $expiresAt );
        if ( ! empty( $results ) ) {
            Issues::add( 'success', 'Your notification has been sent.' );
        } else {
            Issues::add( 'error', [ 'There was an unknown error submitting your data.' => Check::userErrors() ] );
        }
        Views::view( 'notifications.admin.send' );
    }
}
