<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$notificationObj =  new  notification();


if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['fcm_id']))
{
    $notificationData = $notificationObj->fromId($_REQUEST['fcm_id']);
    
    if($notificationData)
    {
        $dependantObj = new dependant();
        $fields = array('dependant_id');
        $where = array("dependant_login_id="=>$notificationData->notification_patient_id);
        $dependantArg = array("fields"=>$fields,"where"=>$where);
        $dependantDetail = $dependantObj->fromFields($dependantArg);

        $patient_detail_changeObj = new patient_detail_change();
        $patient_detail_changeData =array(
            "patient_detail_change_pid"=>$dependantDetail->dependant_id,
            "patient_detail_change_cid"=>'',
            "patient_detail_change_type"=>'Fcm token remove',
            "patient_detail_change_oldvalue"=>$notificationData->notification_fcmtoken,
            "patient_detail_change_value"=>'FCM token remove session out',
            "patient_detail_change_loadeddate"=>null,
            "patient_detail_change_created_at"=>date("Y-m-d H:i:s")
        );
        $patient_detail_changeObj->addRecords($patient_detail_changeData);
    }

    $rs_del = $notificationObj->deleteFromId($_REQUEST['fcm_id']);
    $successMsg = base64_encode(RECORD_DELETE);
    if($rs_del)
    {
        header('Location:fcmtokendetail.php?fcm_id='.$notificationData->notification_patient_id);
        exit;
    }
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);

        $rs = $notificationObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " notification_patient_id = '".$_REQUEST['fcm_id']."' ";

    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" notification_id desc ");
    }
    //print_r($orderby);exit;

    
    $data    = array('where' => $where, 'limit' => $limit, 'orderBy'=>$orderby);

    $notificationDetails = $notificationObj->getAllRecords($data);
    
    $recordsTotal   =   $notificationObj->getAllCount($data);

        
    if ($notificationDetails && count($notificationDetails)>0){
        foreach ($notificationDetails as $key => $notification){

            $action = '
                 <a id="delete-confirm" onclick="delete_entry('.$notification->notification_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>';

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$notification->notification_id.'" class="check_class" />',

                    "<b>Id: </b>".$notification->notification_id, 
                    $notification->notification_fcmtoken,
                    $action
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>