<?php   
// Include main file
include_once('../libs/main.php');
// Check dependant Login
include_once('checkLogin.php');

// Logical section of page
$subscriptions_subscribed_logObj =  new  subscriptions_subscribed_log();


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 = " 1=1 ";

    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != '')
    {
        $where .= " AND c.clinic_id = '".$_REQUEST['clinic_id']."'";
    
    }


    /*if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where .= " AND m.messagecentereventlog_id LIKE '%".$_REQUEST['search']['value']."%' OR  m.appointment_id LIKE '%".$_REQUEST['search']['value']."%' OR  m.email LIKE '%".$_REQUEST['search']['value']."%' OR m.phone LIKE '%".$_REQUEST['search']['value']."%' OR fcmtoken LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_id LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_name LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_phone LIKE '%".$_REQUEST['search']['value']."%' OR c.clinic_email LIKE '%".$_REQUEST['search']['value']."%' OR m.eventname LIKE '%".$_REQUEST['search']['value']."%' OR m.notificationtype LIKE '%".$_REQUEST['search']['value']."%' ";
    }*/


    $orderFields = array('log_subscribed_subscription_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(" log_subscribed_subscription_id desc ");
    }

    
    $tables = array('tbl_clinic c');
    $jfields = array('c.clinic_id=s.log_subscribed_clinic_id');
    $jtype   = array("INNER");
    $data   = array('where' => $where,  'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'orderBy'=>$orderby,'limit' => $limit);

    $subscriptionlogDetails = $subscriptions_subscribed_logObj->getAllRecords($data);
    //print_r($subscriptionlogDetails);exit;
    $recordsTotal   =   $subscriptions_subscribed_logObj->getAllCount($data);
     
    if ($subscriptionlogDetails && count($subscriptionlogDetails)>0){
        
        foreach ($subscriptionlogDetails as $key => $subscriptionlogdata)
        {
            $invoice_pdf = '';
            if(!is_null($subscriptionlogdata->log_subscribed_invoicepdf) != '')
            {
                $invoice_pdf = "<a href='".$subscriptionlogdata->log_subscribed_invoicepdf."' target='_blank'>Download</a>";
            }

            
            
            $response[] = array( 
                    
                    "<b>Xcare Id</b>: ".$subscriptionlogdata->clinic_id."<br/><b>Name: </b>".$subscriptionlogdata->clinic_name."<br/><b>Phone</b>: ".$subscriptionlogdata->clinic_phone."<br/><b>Email</b>: ".$subscriptionlogdata->clinic_email,
                    $subscriptionlogdata->log_subscribed_subscription_id,
                    $subscriptionlogdata->log_subscribed_start_date,
                    $subscriptionlogdata->log_subscribed_end_date,
                    $subscriptionlogdata->log_subscribed_payment_status,
                    $invoice_pdf,
                    $subscriptionlogdata->log_subscribed_invoice_created_at,
                    
            );
        }
        $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');
?>