<?php

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


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