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


    if(!empty($_REQUEST['clinic_rating'])){
        $where .= " AND clinic_displayRating LIKE '%".($_REQUEST['clinic_rating'])."%' ";
    }
    
    if(isset($_REQUEST['clinic_id']) && $_REQUEST['clinic_id'] != ''){
        $where .= " AND ( clinic_id  = '".$_REQUEST['clinic_id']."') ";
    }

    $orderby = array(" clinic_displayRating Asc ");

    //echo $where;exit;
    $data = array(
        'where'=>$where,
        'orderBy'=>$orderby,'limit'=>$limit
    );
    //$data['debug']=true;
    $clinicDetails  = $clinicObj->getAllRecords($data);
    
    $recordsTotal = $clinicObj->getAllCount($data);

    if ($clinicDetails && count($clinicDetails)>0){
        foreach ($clinicDetails as $clinic){

            $response[] = array($clinic->clinic_id,
            $clinic->clinic_name,
            $clinic->clinic_displayRating,
                    
             );
        }
        $result = array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    } else {
        $result = array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

if (isset($_POST['act']) && $_POST['act'] == 'saveRating') {

    $clinic_id = intval($_POST['clinic_id']);
    $rating    = trim($_POST['rating']);


    $data['clinic_displayRating'] = $rating;
    $rs=$clinicObj->updateRecords($data,$clinic_id);

    if ($rs) {
        echo json_encode(["status" => "success"]);
        exit;
    } else {
        echo json_encode(["status" => "error", "msg" => $db->error]);
    }

    exit;
}


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