<?php
/**
 * app/controllers/admin/errors.php
 *
 * This is the error logs controller.
 *
 * @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\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Models\Log;

class Errors extends AdminController {
    public static $log;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Error Logs';
        self::$log = new Log;
    }

    public function delete( $id = null ) {
        if ( Input::exists( 'submit' ) ) {
            $id = Input::post( 'E_' );
        }
        if ( self::$log->delete( $id ) ) {
            Issues::add( 'success', 'Error-log deleted' );
        } else {
            Issues::add( 'error', 'There was an error deleting log(s)' );
        }
        $this->index();
    }

    public function index() {
        return Views::view( 'admin.logs.error_list', self::$log->list( 'error' ) );
    }

    public function view( $id = null ) {
        return Views::view( 'admin.logs.error', self::$log->findById( $id ) );
    }

    public function clear() {
        self::$log->clear( 'error' );
        Issues::add( 'success', 'Error Logs Cleared' );
        $this->index();
    }
}
