<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include 'dbconfig.php';

if(isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
}

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$json = json_decode(file_get_contents('php://input'), true);
$serviceid=$json['serviceid'];
$subid=$json['subid'];
$sql=$conn->query("select U.user_id, U.user_name, ur.role_id, U.last_logged_on, r.role_name FROM users U JOIN user_roles ur ON U.user_id = ur.user_id JOIN roles r ON ur.role_id = r.role_id WHERE U.active = 1 AND ur.role_id = '2'");
$arr=array();
while($row = mysqli_fetch_assoc($sql)) {
$vendor_id=$row['user_id'];
$wallet=$conn->query("SELECT user_id, (SUM(debit)*-1) + SUM(credit) AS CustomerBalance FROM wallet WHERE user_id='".$vendor_id."' GROUP BY user_id");
$walletrow=mysqli_fetch_array($wallet);
$CustomerBalance=isset($walletrow['CustomerBalance']) ? round($walletrow['CustomerBalance']) :0;
$wallet_settings=$conn->query("SELECT minimum_balance,monthly_balance FROM wallet_settings");
$walletsetrow=mysqli_fetch_array($wallet_settings);
$minimum_balance=isset($walletsetrow['minimum_balance']) ? $walletsetrow['minimum_balance'] :0;
$monthly_balance=isset($walletsetrow['monthly_balance']) ? $walletsetrow['monthly_balance'] :0;
$date=date("Y-m-d");
$validsub=$conn->query("SELECT id,date,vendor_id,expiry_date FROM subscription WHERE vendor_id='".$vendor_id."' and date(expiry_date)>='$date' order by id desc limit 0,1");  
$validrow=mysqli_fetch_array($validsub);
if(mysqli_num_rows($validsub)>0){
if($CustomerBalance>=$minimum_balance){
$sqlres="SELECT sp.*,ss.service_id from serviceprice sp INNER JOIN subservices ss ON ss.id=sp.service_id  INNER JOIN services  ON services.id=sp.service_id WHERE sp.service_id='".$serviceid."' AND sp.subservice_id='".$subid."' AND sp.vendor_id='".$vendor_id."'";
$result = $conn->query($sqlres);
if(mysqli_num_rows($result)>0) {
    $sqlrating=$conn->query("SELECT * FROM rating WHERE vendor_id='".$vendor_id."' and status='1'");
    $rating=0;
    $s=mysqli_num_rows($sqlrating);
    while($sqlrow=mysqli_fetch_array($sqlrating)){
      $rating+=$sqlrow['rating'];  
    }
    $star=0;
    if($s>0){
     $star=($rating/$s);
    }
     $arr[]=array("id" => $row['user_id'],"user_name" => $row['user_name'],"rating" =>number_format(round((float)$star,2),1),"review" => $s);
}
}
}
}
$data = array("data" => $arr);
echo json_encode($data);     
$conn->close();
?>