<?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'];
$filter=$json['filter'];
$sql="SELECT * FROM amenities_service WHERE amenities_service.service_id='$serviceid' and amenities_service.subservice_id='$subid' and amenities_service.status='1' order by amenities_service.id asc";
$result = $conn->query($sql);
$arr=array();
while($row = mysqli_fetch_assoc($result)) {
$show=0;   
$pro_filter=array();
foreach($filter as $sname){
    $explode=explode("*",$row['service_name']);
    if(in_array($sname['id'],$explode)){
    $show=1;
    array_push($pro_filter,$show);
    }
}
$implode=implode(",",$pro_filter);
$explode=explode(",",$implode);
if(in_array(1,$explode)){
$vendor_id=$row['vendor_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){
$sql="SELECT user_details.* FROM users INNER JOIN user_details ON users.user_id=user_details.user_id WHERE users.user_id='$vendor_id'";
$result_rslt=$conn->query($sql);    
$fetchrow=mysqli_fetch_array($result_rslt);
$phone=isset($fetchrow['phone']) ? $fetchrow['phone'] :'';
$image=$base_url."image/no_image_available.jpg";
if(@is_array(getimagesize($base_url."/image/services/".$row['image']))){
 $image=$base_url."image/services/".$row['image']; 
}
$arr[]=array("id" => $row['id'],"title"=>$row['title'],"month_price" => $row['month_price'],"deposit" => $row['deposit'],"description" => $row['description'],"image" => $image,"phone" => $phone);
}
}
}
}
$data = array("data" => $arr);
echo json_encode($data);     
$conn->close();
?>