<?php

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	
	$_POST = array_map('trim', $_POST);
	
	if( !empty($_POST['id']) ) {
		
		$SQL = sQuery::update( "{$prefix}_subjects", array( 
			'name' => $_POST['subject'] 
		), "id = '{$_POST['id']}'", uss::$global['mysqli'] );
		
		$type = 'updated';
		
	} else {
		
		$SQL = sQuery::insert( "{$prefix}_subjects", array( "name" => $_POST['subject'] ), uss::$global['mysqli'] );
		
		$type = 'added';
		
	};
	
	
	// Message
	
	$status = uss::$global['mysqli']->query( $SQL );
	
	if( $status ) $message = "The subject was successfully {$type}";
	else $message = "Error: The subject could not be {$type}";
	
	
	// Output;
	
	uss::console( ':alert', $message );
	
	
} else {
	
	if( isset($_GET['req']) && isset($_GET['id']) ) {
		
		$subject = dashboard::fetch_assoc( "{$prefix}_subjects", $_GET['id'] );
		
		if( $_GET['req'] == 'delete' ) {
			
			$SQL = "DELETE FROM {$prefix}_subjects WHERE id = '{$_GET['id']}'";
			
			$delete = uss::$global['mysqli']->query( $SQL );
			
			if( $delete ) {
				
				uss::console( ':alert', "<i class='fas fa-trash-alt me-1'></i> The subject was delete" );
				
				$subject = [];
				
			} else uss::console( ':alert', "<i class='fas fa-exclamation-triangle me-1'></i> Error: The subject was not delete" );
			
		};
		
	};
	
};


if( empty($subject) ) $subject = [];