<?php
// clinicPointsTrans.php

// Include main file
include_once('../libs/main.php');
include_once('checkLogin.php');

$agencyClinicPointObj = new agencyclinic_point(); 

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load') {
    
    $records = $_REQUEST['length'];
    $start   = ($_GET['start'] ?? 0);
    $limit   = array('start'=>$start,'record'=>$records);

    $where = " 1=1 ";

    // if(!empty($_REQUEST['clinic_id'])){
    //     $where .= " AND a.clinic_id = '".($_REQUEST['clinic_id'])."' ";
    // }
    // if(!empty($_REQUEST['pointmaster_id'])){
    //     $where .= " AND a.pointmaster_id = '".($_REQUEST['pointmaster_id'])."' ";
    // }


    $tables  = array('tbl_clinic c','tbl_agencyuser au','tbl_user u','tbl_pointmaster pm');
    $fields = array('a.*, c.clinic_name, au.agencyuser_fname, au.agencyuser_lname, u.user_fname, u.user_lname, pm.pointmaster_title');
    $jfields = array(
        'c.clinic_id = a.clinic_id', 
        'au.agencyuser_id = a.agencyuser_id',
        'u.user_id = a.user_id',
        'pm.pointmaster_id = a.pointmaster_id'
    );
    
    $jtype   = array('LEFT','LEFT','LEFT','LEFT'); 

    
    $orderby = array(" a.created_at DESC ");

    $data = array(
        'where'=>$where,
        'joinTables'=>$tables,
        'fields'=>$fields,
        'joinFields'=>$jfields,
        'joinTypes'=>$jtype,
        'orderBy'=>$orderby,
        'limit'=>$limit
    );
    //$data['debug'] =true;
    $transDetails  = $agencyClinicPointObj->getAllRecords($data);
    $recordsTotal = $agencyClinicPointObj->getAllCount($data); 

    if ($transDetails && count($transDetails)>0){
        foreach ($transDetails as $trans){
            
            
            $clinic_name = $trans->clinic_name ?? '-';

            
            $agency_user_name = '-';
            if(!empty($trans->agencyuser_id) && !empty($trans->agencyuser_fname)){
                 $agency_user_name = ($trans->agencyuser_fname) . ' ' . ($trans->agencyuser_lname);
            }

            
            $user_name = '-';
            if(!empty($trans->user_id) && !empty($trans->user_fname)){
                 $user_name = ($trans->user_fname) . ' ' . ($trans->user_lname);
            }

            
            $point_title = $trans->pointmaster_title ?? '-';
            
            $receipt_url_html = '-';
            if(!empty($trans->receipt_url)){
                $receipt_url_html = '<a href="'. ($trans->receipt_url) .'" target="_blank">View Receipt</a>';
            }

            $response[] = array(
                $clinic_name,
                $point_title, 
                "<b>Points</b>: ".$trans->points."<br/>
                <b>Amount</b>: $ ".$trans->amount."<br/>
                <b>Type</b>: ".$trans->type."<br/>",
                $trans->description,
                "<b>Id</b>: ".$trans->transaction."<br/>
                <b>Receipt</b>:".$receipt_url_html."<br/>",
                $agency_user_name,
                $user_name,
                $trans->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');
?>