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

$clinicObj = new clinic();

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

    $where = " d.dependant_member_id != 0 AND d.dependant_member_id != '' ";

    // Tables & Joins
    $tables  = array('tbl_clinic_patient cp','tbl_dependant d');
    $jfields = array(
        'c.clinic_id = cp.clinic_patient_cid',
        'd.dependant_id = cp.clinic_patient_depid'
    );
    $jtype   = array('INNER','INNER');

    // Fields
    $fields = array('c.clinic_id','c.clinic_name', 'COUNT(d.dependant_id) AS total_members');

    $groupBy = array("cp.clinic_patient_cid");
    $orderby = array(" total_members DESC ");

    $data = array(
        'fields'=>$fields,
        'where'=>$where,
        'joinTables'=>$tables,
        'joinFields'=>$jfields,
        'joinTypes'=>$jtype,
        'groupBy'=>$groupBy,
        'orderBy'=>$orderby,
        'limit'=>$limit
    );
    //$data['debug'] = true;

    $clinicMembers  = $clinicObj->getAllRecords($data);
    $recordsTotal   = $clinicObj->getAllCount($data);

    if ($clinicMembers && count($clinicMembers) > 0) {
        foreach ($clinicMembers as $row) {
            //$clinicInfo = "<b>Clinic:</b> ".$row->clinic_name;
            $clinicInfo = '<a href="javascript:void(0)" 
                            class="viewMembers" 
                            data-cid="'.($row->clinic_id).'" 
                            data-name="'.$row->clinic_name.'">
                            '.$row->clinic_name.'
                        </a>';
            $countInfo  = "<b>Total Members:</b> ".$row->total_members;

            $response[] = array($clinicInfo,$countInfo);
        }
        $result = array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    } else {
        $result = array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }

    echo json_encode($result);
    exit;
}

if (isset($_REQUEST['act']) && $_REQUEST['act'] == 'members') {


    $clinicId = ($_GET['cid'] ?? '');

    $records = $_REQUEST['length'] ?? 10;
    $start   = $_REQUEST['start'] ?? 0;

    $limit = array(
        'start'  => $start,
        'record' => $records
    );

    
    $where = " cp.clinic_patient_cid = '".$clinicId."'
               AND d.dependant_member_id != 0
               AND d.dependant_member_id != '' ";

    
    $tables  = array(
        'tbl_clinic_patient cp',
        'tbl_dependant d'
    );

    $jfields = array(
        'c.clinic_id = cp.clinic_patient_cid',
        'cp.clinic_patient_depid = d.dependant_id'
    );

    $jtype = array('INNER');

    
    $fields = array(
        'd.dependant_member_id',
        'd.dependant_fname',
        'd.dependant_lname'
    );

    $data = array(
        'fields'      => $fields,
        'where'       => $where,
        'joinTables'  => $tables,
        'joinFields'  => $jfields,
        'joinTypes'   => $jtype,
        'limit'       => $limit
    );
    //$data['debug'] = true;
    $rows  = $clinicObj->getAllRecords($data);
    $count = $clinicObj->getAllCount($data);

    $response = array();

    if ($rows) {
        foreach ($rows as $row) {
            $response[] = array(
                $row->dependant_member_id,
                w::d($row->dependant_fname),
                w::d($row->dependant_lname)
            );
        }
    }

    echo json_encode(array(
        "recordsTotal"    => (int)$count,
        "recordsFiltered" => (int)$count,
        "data"            => $response
    ));
    exit;
}


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