<?php
include_once('../libs/main.php');
include_once('checkLogin.php');

$supportObj   = new support();
$userObj = new user();
$clinicObj    = new clinic();

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

    $where = " 1=1 ";

    $tables  = array('tbl_user u','tbl_clinic c');
    $jfields = array(
        's.userId = u.user_id',
        'c.clinic_id = s.clinicId'
    );
    $jtype   = array('LEFT','LEFT');

    $orderby = array(" s.created_at DESC ");
    
    $data = array(
        'where'=>$where,'joinTables'=>$tables,'joinFields'=>$jfields,'joinTypes'=>$jtype,
        'orderBy'=>$orderby,'limit'=>$limit
    );
    //$data['debug']=true;
    $ContactDetails  = $supportObj->getAllRecords($data);
    
    $recordsTotal = $supportObj->getAllCount($data);

    if ($ContactDetails && count($ContactDetails)>0){
        foreach ($ContactDetails as $contact){
            
            $clinicInfo = "<b>Clinic:</b> ".$contact->clinic_name."<br/>
                           <b>Email:</b> ".$contact->clinic_email."<br/>
                           <b>Phone:</b> ".$contact->clinic_phone."<br/>
                           <b>Website:</b> ".$contact->clinic_website;

            $userInfo =    "<b>ID:</b> ".$contact->user_id."<br/>
                            <b>Name:</b> ".($contact->user_fname)." ".($contact->user_lname)."<br/>
                            <b>Email:</b> ".($contact->user_email_address);


            $supportInfo = "<b>Type:</b> ".$contact->type."<br/>
                           <b>Subject:</b> ".($contact->subject)."<br/>
                           <b>Date:</b> ".$contact->date."<br/>
                           <b>Time:</b> ".$contact->time."<br/>"
                           ;


            $created = "<b>Created At:</b> ".$contact->created_at;

            $response[] = array($clinicInfo,$userInfo,$supportInfo,$created);
        }
        $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');
?>
