<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/Notification.php';
require_once '../Model/SystemPermission.php';
require_once '../Model/RolePermission.php';
require_once '../Model/UserPermission.php';
$notification = new Notification();
$system_permission = new SystemPermission();
$role_permission = new RolePermission();
$user_permission = new UserPermission();

$notifications = $notification->read_all_notifications();

if(isset($_GET['all_notifications'])){
    echo json_encode(
        array("data"=>$notifications)
    ); 
}

if(isset($_POST['add_notification'])){
    $notification_name = $_POST['notification_name'];
    $data = array(
        "notification_name"=>$notification_name
    );
    $success = $notification->add_notification($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_notification'])){
    $notification_id = $_POST['notification_id'];
    $notification_name = $_POST['notification_name'];
    $data = array(
        "notification_name"=>$notification_name
    );
    $key = array(
        "notification_id"=>$notification_id
    );
    $success = $notification->update_notification($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_notification'])){
    $notification_id = $_POST['notification_id'];
    $data = $notification->remove_notification($notification_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

// Util Functions
function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}


if(isset($_GET['fetchNotification'])){
    $system_request_notification = array();
    $folder_page = $_SERVER['PHP_SELF'];
    $get_page = $get_class = array_map('trim', explode('/', $folder_page));
    $page = 'item_request_approval.php';
    $page_Details = $system_permission->get_system_permission_by_page($page);
    $system_permission_id = $page_Details[0]['system_permission_id'];
    $has_access_to_notification = $role_permission->check_page_role_permission($_SESSION['role_id'],$system_permission_id);
    if($has_access_to_notification){
        $system_request_notification = $notification->read_all_request_notifications();
    ?>
<?php if($system_request_notification):?>
    <?php foreach($system_request_notification as $srn):?>
    <li class="unread">
        <div class="d-flex align-items-center show-child-on-hover">
            <span class="d-flex flex-column flex-1">
                <span class="name d-flex align-items-center"><?php echo $srn['notification_title'] ?> <span class="badge badge-success fw-n ml-1">New <?php echo $srn['type'] ?></span></span>
                <span class="msg-a fs-sm">
                    <?php echo $srn['notification_message'] ?>
                </span>
                <span class="fs-nano text-muted mt-1"><?php echo time_elapsed_string($srn['notification_created_at'],false) ?></span>
            </span>
            <div class="show-on-hover-parent position-absolute pos-right requests_btn_section pos-bottom p-3">
            <?php if($srn['type']=="Request"):?>
            <a href="item_request_approval.php" class="btn btn-xs btn-info" title="View"  id="view_orders-<?php echo $srn['notification_id'] ?>" ><i class="fal fa-eye"></i> View</a>
            <?php endif?>
            </div>
            <div class="show-on-hover-parent position-absolute pos-right requests_info_section d-none pos-bottom p-3"></div>
        </div>
    </li>
        <?php endforeach ?>
    <?php endif ?>
    <?php
    }
}

if(isset($_GET['fetchNotificationCounter'])){
    $system_request_notification = array();
    $folder_page = $_SERVER['PHP_SELF'];
    $get_page = $get_class = array_map('trim', explode('/', $folder_page));
    $page = 'item_request_approval.php';
    $page_Details = $system_permission->get_system_permission_by_page($page);
    $system_permission_id = $page_Details[0]['system_permission_id'];
    $has_access_to_notification = $role_permission->check_page_role_permission($_SESSION['role_id'],$system_permission_id);
    if($has_access_to_notification){
        $system_request_notification = $notification->read_all_request_notifications();
    ?>
<?php if($system_request_notification):?>
                    <?php if($system_request_notification):?>
                        <span styl="color:#FF4341 !important;">!</span>
                    <?php endif ?>
    <?php endif ?>
    <?php
    }
}