<?php 

require __DIR__ . '/index.php';


if( $_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['nonce']) ) die( '' );


//! Confirm Security;

if( $_POST['nonce'] !== uss::nonce( $_SESSION['uss_session_id'] ) ) {
	
	uss::stop( false, "<i class='mdi mdi-shield-off me-1'></i> Security Check Failed" );
	
} else if( empty(uss::$global['user']) ) {
	
	uss::stop( false, "<i class='mdi mdi-account-lock me-1'></i> Account is signed out" );
	
};


//! 

events::addListener('admin.support-ticket-chat::delete', function() {
	
	$permitted = roles::user( uss::$global['user']['id'] )::hasPermission('manage-ticket');
	
	if( !$permitted ) uss::stop( false, "You do not have permission to remove the message" );
	
	$_POST['id'] = uss::$global['mysqli']->real_escape_string( $_POST['id'] );
	
	$prefix = DB_TABLE_PREFIX;
	
	$SQL = "
		DELETE FROM {$prefix}_ticket_replies
		WHERE id = '{$_POST['id']}'
	";
	
	$status = uss::$global['mysqli']->query( $SQL );
	
	uss::stop( $status, $status ? "Message deleted" : "Message not deleted" );
	
});


//! 

events::exec( 'admin.support-ticket-chat::delete', uss::$global['user'] );