<?php
header("Content-type: application/json; charset=utf-8");
date_default_timezone_set('Asia/Jerusalem');
require_once('class.db.php');
require_once 'MyAPI.CLASS.php';
//require 'fcm.class.php'; 

$result = array();
//var_dump(explode(",",get_client_ip()));


$lang = 'en';

if(isset($_REQUEST['lang'])){
    
    $lang = $_REQUEST['lang'];
}


if(isset($_REQUEST['api_data'])){

$password = $_REQUEST['api_data'];
$checkLogin = new MyAPICore();


if($password!='82e4b4fd3a16ad99229af9911ce8e6d2'){


$result['status']= false;
$result['http_code']= (int) 401;
$result['msg']='API Password not correct!, try again';
$result['data']=array();

  
  print json_encode($result,JSON_UNESCAPED_UNICODE);
  exit;



}// error password


}
else {
$result['status']= false;
$result['http_code']= (int) 401;
$result['msg']='API password not set, please set api_data value and try again!';
$result['data']=array();

  
  print json_encode($result,JSON_UNESCAPED_UNICODE);
  exit;

}


if(!isset($_REQUEST['action'])){
  
  
$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='Action controller not set, please set action to execute your request ';
$result['data']=array();

  
  
  print json_encode($result,JSON_UNESCAPED_UNICODE);
  exit;
}
$action = $_REQUEST['action'];


switch ($action) {
       case "OrderDetailsWeb":
       $api = new MyAPICore(); 
       
        if(!isset($_REQUEST['order_num'])){
       $msg = 'order_num is required';
        if($lang=='ar'){
        $msg = 'order_num  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         $id=$_REQUEST['token'];
         $order_num=$_REQUEST['order_num'];
         $date=date("Y-m-d H:i:s");
         $db = new Db(); 
         $order=$db -> select("SELECT * FROM `orders` where  order_num='$order_num' limit 1");
         $cart=json_decode($order[0]['cart']);
         $data=array();
         $option2=array();
         for($i=0 ; $i < count($cart); $i++){
            $p_td= $cart[$i]->product_id;
            $pData=$db -> select("SELECT * FROM `products` where products.id=$p_td limit 1");
            $option=$cart[$i]->option;
            //var_dump($option);exit();
            if($option != null){
            for($j=0 ; $j < count($option); $j++){
                  $type_id=$option[$j]->type_id;
                  $type=$db -> select("SELECT * FROM `option_type` where option_type.id=$type_id limit 1");
                  $type_list_id=$option[$j]->type_list_id;
                  $option_type_list=$db -> select("SELECT * FROM `option_type_list` where option_type_list.id=$type_list_id limit 1");
                  $option1=array(
                                'type_id'=>$type_id,
                                'type_name'=>$type[0]['type'],
                                'type_list'=>array(
                                                  'id'=>$type_list_id,
                                                  'name'=>$option_type_list[0]['name']
                                                  )
                                );
                                array_push($option2, $option1);
            }
            }
            $data1=array(
                         'product_id'=>$cart[$i]->product_id,
                         'product_name'=>$pData[0]['name'],
                         'price'=>$cart[$i]->price,
                         'quantity'=>$cart[$i]->quantity,
                         'total'=>$cart[$i]->total,
                         'option'=>$option2
                        );
        array_push($data, $data1);
         }
         
         $product_id=$cart[0]->product_id;
         $user_id=$order[0]['user_id'];
         $statuso=$order[0]['status'];
         $vendordata=$db -> select("SELECT * FROM `users` where users.id=$user_id limit 1");
         $order_status=$db -> select("SELECT * FROM `order_status` where order_status.id=$statuso limit 1");
         $order[0]['order_status']=$order_status[0]['name_ar'];
         $order[0]['userName']=$vendordata[0]['name'];
         $order[0]['userPhoto']=$vendordata[0]['photo'];
         

$order_payment=$db -> select("SELECT * FROM `order_payment` where order_num='$order_num' ORDER BY `order_payment`.`order_num` ASC limit 1");
if(!empty($order_payment)){
$is_paid=$order_payment[0]['is_paid'];
}else{
$is_paid="0";    
}
$order_rate=$db -> select("SELECT * FROM `order_rate` where order_num='$order_num' ORDER BY `order_num` ASC limit 1");
if(!empty($order_rate)){
$rate_data=$order_rate;
$is_rated="1";
}else{
$rate_data=array(); 
$is_rated="0";
} 
 $order[0]['is_rated']=$is_rated; 
 $order[0]['rate_data']=$rate_data;
 $order[0]['is_paid']=$is_paid;
 
          $order[0]['cart']=$data;
         $result['status']= true;
          $result['http_code']= (int) 200;
          $result['msg']='Order Details';
          $result['data']=$order;
          print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 

     }
     
     break;
       case "viewOrdersVendor":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
         $date=date("Y-m-d H:i:s");
         $db = new Db();
         
          $order=$db -> select("SELECT *,(SELECT name_ar from order_status WHERE order_status.id=orders.status) as status_name FROM `orders` ");
          
          $data=array();
          for($i=0 ; $i < count($order); $i++){
        
/////////////////diff date 
$date=$order[$i]['date'];
$date1 = strtotime($date);              
$date2 = strtotime(date("Y-m-d"));  
$diff = abs($date2 - $date1);  
$years = floor($diff / (365*60*60*24));  
$months = floor(($diff - $years * 365*60*60*24) 
                               / (30*60*60*24));  
$days = floor(($diff - $years * 365*60*60*24 -  
             $months*30*60*60*24)/ (60*60*24));
             
//////////////////////////////////////
$cart=json_decode($order[$i]['cart']);
$product_id=$cart[0]->product_id;
//$product=$db -> select("Select *,(SELECT name from category WHERE category.id=products.category_id) as cat_name from products ");
$vendordata=$db -> select("SELECT * FROM `users` where users.id=(SELECT user_id FROM `products` where id=$product_id) limit 1");
$vendid=(int) $vendordata[0]['id'];
if($vendid==$id){
$order_num=$order[$i]['order_num'];
$order_payment=$db -> select("SELECT * FROM `order_payment` where order_num='$order_num' ORDER BY `order_payment`.`order_num` ASC limit 1");
if(!empty($order_payment)){
$is_paid=$order_payment[0]['is_paid'];
}else{
$is_paid="0";    
}
$order_rate=$db -> select("SELECT * FROM `order_rate` where order_num='$order_num' ORDER BY `order_num` ASC limit 1");
if(!empty($order_rate)){
$rate_data=$order_rate;
$is_rated="1";
}else{
$rate_data=array(); 
$is_rated="0";
} 

                 $data1=array(
                               "id"=>$order[$i]['id'],
                               "order_num"=>$order[$i]['order_num'],
                               "total"=>$order[$i]['final_total'],
                               "day"=>$days,
                               "date"=>$date,
                               "status"=>$order[$i]['status'],
                               "status_name"=>$order[$i]['status_name'],
                               "vendor"=>$vendordata[0]['name'],
                               "photo"=>$vendordata[0]['photo'],
                               "is_paid"=>$is_paid,
                               "is_rated"=>$is_rated,
                               "rate_data"=>$rate_data
                                );
                                array_push($data, $data1);
          }}
          
          $result['status']= true;
          $result['http_code']= (int) 200;
          $result['msg']='Orders User';
          $result['data']=$data;
          print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 

     }
     break;
    case "testFilter":
      $db = new Db(); 
         $order=$db -> select("SELECT * FROM `orders` ");
          $data=array();
         for($x=0 ; $x < count($order); $x++){
         $cart=json_decode($order[$x]['cart']);
       
         $option2=array();
         for($i=0 ; $i < count($cart); $i++){
            $p_td= $cart[$i]->product_id;
           
        array_push($data, $p_td);
         }   }
         $vals = array_count_values($data);
         arsort($vals);
         $seller=array();
         foreach ($vals as $key => $val) {
          
          $p_id = $key;
           $resultff = $db -> select("Select *,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and category_id=".$subCategoryId." and id=$p_id order by id DESC  ");
      if(!empty($resultff)) {  
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultff[0]['isFavorite']=1;  
     }else{
        $resultff[0]['isFavorite']=0;  
     }
            
        }else{
           $resultff[0]['isFavorite']=0;  
        }
    $resultff[0]['typeProduct']="Offer";
    $resultff[0]['priceOffer']=$user_arr[0]['price'];
    }else{
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultff[0]['isFavorite']=1;  
     }else{
        $resultff[0]['isFavorite']=0;  
     }
            
        }else{
           $resultff[0]['isFavorite']=0;  
        }
     $resultff[0]['typeProduct']="Product";
    $resultff[0]['priceOffer']='';   
    }
      
      
      array_push($seller, $resultff);   
      }
          }
          
         $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Update Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$seller;
            print json_encode($result,JSON_UNESCAPED_UNICODE);
    break;    
    case "payment":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        if(!isset($_REQUEST['order_num'])){
       $msg = 'order_num is required';
        if($lang=='ar'){
        $msg = 'order_num  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['is_paid'])){
       $msg = 'is_paid is required';
        if($lang=='ar'){
        $msg = 'is_paid  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
         $order_num=$_REQUEST['order_num'];
         $is_paid=$_REQUEST['is_paid'];
         $date=date("Y-m-d");
         $db = new Db();  
         if($is_paid==1){
         $rows = $db ->query("UPDATE `order_payment` SET is_paid=$is_paid  WHERE order_num='$order_num' and date='$date' order by id DESC limit 1 "); 
        $api ->changeOrderStatus($order_num,1,0) ;
 
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=' Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);
         }else{
                  $msg = ' Payment failed';
        if($lang=='ar'){
        $msg = 'فشلت عملية الدفع  ';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();   
         }
     }
     }
     }
     break;
     
         case "refreshFirebaseToken":
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['token']='';
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);  exit();
     }else{

       $db = new Db();
       if(!isset($_REQUEST['firebaseToken'])){
       $msg = 'firebase Token is required';
        if($lang=='ar'){
        $msg = 'firebaseToken مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['token']=$token;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);  exit();
     }else{
       $user_id=$_REQUEST['token'];
       $firebaseToken=$_REQUEST['firebaseToken'];
      // $resultu = $db -> query("UPDATE `users_profile` SET `firebaseToken`='$firebaseToken' WHERE `user_id` = $user_id");
      $rows = $db -> select(" SELECT * FROM `user_firebaseToken` where user_id=$user_id");
      $vart=0;
  foreach ($rows as $val) {
  $to= $val['firebaseToken'];
  if($to==$firebaseToken){
      $result['status']= true;
            $result['http_code']= (int) 200;
             $result['msg']=' ';
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);exit();
  }else{
      $vart=0; 
  }
  
  }

  if($vart==0){
      $result1 = $db -> query("INSERT INTO `user_firebaseToken` ( `user_id`, `firebaseToken`)
     VALUES ($user_id,'$firebaseToken')");
     }
       $result['status']= true;
            $result['http_code']= (int) 200;
             $result['msg']=' ';
             $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 

       
     }
     }
    
    break;
         case "activateNotifications":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        if(!isset($_REQUEST['status'])){
       $msg = 'status is required';
        if($lang=='ar'){
        $msg = 'status  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $status=$_REQUEST['status'];
         $date=date("Y-m-d");
         $db = new Db();  
        $resultm = $db -> query("UPDATE `users` SET `activateNotify`= $status WHERE `id` = $id");
       
       if($status == 0){
           $msg = ' تم تعطيل الاشعارات .';
         if($lang=='ar'){
         
           $msg = ' updated  .';
        } 
       }else{
          $msg = ' تم تفعيل الاشعارات .';
        if($lang=='ar'){
          $msg = ' updated  .';
        } 
       }
         $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']= $db -> select("SELECT * FROM `users` WHERE `id` = $id ");
            print json_encode($result,JSON_UNESCAPED_UNICODE);
     }
     
     }
     break;
      case "updateStatusOrder":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        if(!isset($_REQUEST['order_num'])){
       $msg = 'order_num is required';
        if($lang=='ar'){
        $msg = 'order_num  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['status'])){
       $msg = 'status is required';
        if($lang=='ar'){
        $msg = 'status  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
         $order_num=$_REQUEST['order_num'];
         $status=$_REQUEST['status'];
         $date=date("Y-m-d");
         $db = new Db();  
         //$rows = $db ->query("UPDATE `order_payment` SET is_paid=$is_paid  WHERE order_num='$order_num'  order by id DESC limit 1 "); 
         $rows = $db ->query("UPDATE `orders` SET status=$status  WHERE order_num='$order_num'  order by id DESC limit 1 "); 
          $order_st=$db -> select("SELECT * FROM `order_status` where id=$status limit 1 ");
          $name_ar=$order_st[0]['name_ar'];
          if($status==3){
            $rows = $db ->query("UPDATE `order_delevery` SET 	is_delevery=1  WHERE order_num='$order_num'  order by id DESC limit 1 "); 
            
          }
         $api ->changeOrderStatus($order_num,$status,$id) ;
         $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Update Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);
     }
     }
     }
     break;
    case "viewOrderDetails":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        if(!isset($_REQUEST['order_num'])){
       $msg = 'order_num is required';
        if($lang=='ar'){
        $msg = 'order_num  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         $id=$_REQUEST['token'];
         $order_num=$_REQUEST['order_num'];
         $date=date("Y-m-d H:i:s");
         $db = new Db(); 
         $order=$db -> select("SELECT * FROM `orders` where orders.user_id=$id and  order_num='$order_num' limit 1");
         $cart=json_decode($order[0]['cart']);
         $data=array();
         $option2=array();
         for($i=0 ; $i < count($cart); $i++){
            $p_td= $cart[$i]->product_id;
            $pData=$db -> select("SELECT * FROM `products` where products.id=$p_td limit 1");
            $option=$cart[$i]->option;
            //var_dump($option);exit();
            if($option != null){
            for($j=0 ; $j < count($option); $j++){
                  $type_id=$option[$j]->type_id;
                  $type=$db -> select("SELECT * FROM `option_type` where option_type.id=$type_id limit 1");
                  $type_list_id=$option[$j]->type_list_id;
                  $option_type_list=$db -> select("SELECT * FROM `option_type_list` where option_type_list.id=$type_list_id limit 1");
                  $option1=array(
                                'type_id'=>$type_id,
                                'type_name'=>$type[0]['type'],
                                'type_list'=>array(
                                                  'id'=>$type_list_id,
                                                  'name'=>$option_type_list[0]['name']
                                                  )
                                );
                                array_push($option2, $option1);
            }
            }
            $data1=array(
                         'product_id'=>$cart[$i]->product_id,
                         'product_name'=>$pData[0]['name'],
                         'price'=>$cart[$i]->price,
                         'quantity'=>$cart[$i]->quantity,
                         'total'=>$cart[$i]->total,
                         'option'=>$option2
                        );
        array_push($data, $data1);
         }
         
         $product_id=$cart[0]->product_id;
         $vendordata=$db -> select("SELECT * FROM `users` where users.id=(SELECT user_id FROM `products` where id=$product_id) limit 1");
         $order[0]['vendorName']=$vendordata[0]['name'];
         $order[0]['vendorPhoto']=$vendordata[0]['photo'];
         

$order_payment=$db -> select("SELECT * FROM `order_payment` where order_num='$order_num' ORDER BY `order_payment`.`order_num` ASC limit 1");
if(!empty($order_payment)){
$is_paid=$order_payment[0]['is_paid'];
}else{
$is_paid="0";    
}
$order_rate=$db -> select("SELECT * FROM `order_rate` where order_num='$order_num' ORDER BY `order_num` ASC limit 1");
if(!empty($order_rate)){
$rate_data=$order_rate;
$is_rated="1";
}else{
$rate_data=array(); 
$is_rated="0";
} 
 $order[0]['is_rated']=$is_rated; 
 $order[0]['rate_data']=$rate_data;
 $order[0]['is_paid']=$is_paid;
 
          $order[0]['cart']=$data;
         $result['status']= true;
          $result['http_code']= (int) 200;
          $result['msg']='Order Details';
          $result['data']=$order;
          print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 

     }
     }
     break;
     /////////////////////////
    case "viewOrdersUser":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
         $date=date("Y-m-d H:i:s");
         $db = new Db();
         
          $order=$db -> select("SELECT * FROM `orders` where orders.user_id=$id ");
          
          $data=array();
          for($i=0 ; $i < count($order); $i++){
        
/////////////////diff date 
$date=$order[$i]['date'];
$date1 = strtotime($date);              
$date2 = strtotime(date("Y-m-d"));  
$diff = abs($date2 - $date1);  
$years = floor($diff / (365*60*60*24));  
$months = floor(($diff - $years * 365*60*60*24) 
                               / (30*60*60*24));  
$days = floor(($diff - $years * 365*60*60*24 -  
             $months*30*60*60*24)/ (60*60*24));
             
//////////////////////////////////////
$cart=json_decode($order[$i]['cart']);
$product_id=$cart[0]->product_id;
$vendordata=$db -> select("SELECT * FROM `users` where users.id=(SELECT user_id FROM `products` where id=$product_id) limit 1");
$order_num=$order[$i]['order_num'];
$order_payment=$db -> select("SELECT * FROM `order_payment` where order_num='$order_num' ORDER BY `order_payment`.`order_num` ASC limit 1");
if(!empty($order_payment)){
$is_paid=$order_payment[0]['is_paid'];
}else{
$is_paid="0";    
}
$order_rate=$db -> select("SELECT * FROM `order_rate` where order_num='$order_num' ORDER BY `order_num` ASC limit 1");
if(!empty($order_rate)){
$rate_data=$order_rate;
$is_rated="1";
}else{
$rate_data=array(); 
$is_rated="0";
} 

                 $data1=array(
                               "id"=>$order[$i]['id'],
                               "order_num"=>$order[$i]['order_num'],
                               "total"=>$order[$i]['final_total'],
                               "day"=>$days,
                               "date"=>$date,
                               "status"=>$order[$i]['status'],
                               "vendor"=>$vendordata[0]['name'],
                               "photo"=>$vendordata[0]['photo'],
                               "is_paid"=>$is_paid,
                               "is_rated"=>$is_rated,
                               "rate_data"=>$rate_data
                                );
                                array_push($data, $data1);
          }
          
          $result['status']= true;
          $result['http_code']= (int) 200;
          $result['msg']='Orders User';
          $result['data']=$data;
          print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 

     }
     break;
     
    case "addOrder":
       $api = new MyAPICore(); 
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['address_id'])){
       $msg = 'address_id is required';
        if($lang=='ar'){
        $msg = 'address_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['cart'])){
       $msg = 'cart is required';
        if($lang=='ar'){
        $msg = 'cart  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
       
         if(!isset($_REQUEST['final_total'])){
       $msg = 'final_total is required';
        if($lang=='ar'){
        $msg = 'final_total  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $order_num=$api ->genOrderNum();
        $id=$_REQUEST['token'];
        $address_id=$_REQUEST['address_id'];
        $cart=$_REQUEST['cart'];
        $lat=$_REQUEST['lat'];
        $lon=$_REQUEST['lon'];
        $tax_value=$_REQUEST['tax_value'];
        $delivery=$_REQUEST['delivery'];
        $capone=$_REQUEST['capone'];
        $discount=$_REQUEST['discount'];
        $total=$_REQUEST['total'];
        $final_total=$_REQUEST['final_total'];
        $payment_type=$_REQUEST['payment_type'];
        	
 $date= date("Y-m-d");

  $status=0;  

  $db = new Db();
  $result1 = $db ->query("INSERT INTO `orders`(`user_id`,`order_num`,`cart`,`address_id`,`lat`,`lon`,`tax_value`,`delivery`,`capone`,`discount`,`final_total`,`payment_type`,`date`,`status`) VALUES ($id,'$order_num','$cart',$address_id,'$lat','$lon','$tax_value','$delivery','$capone','$discount','$final_total','$payment_type','$date',$status)");
 //$result1 = $db ->query("INSERT INTO `orders`(`user_id`,`order_num`,`cart`,`address_id`,`lat`,`lon`,`tax_value`,`delivery`,`capone`,`discount`,`final_total`,`date`) VALUES ($id,'$order_num','$cart',$address_id,'$lat','$lon','$tax_value','$delivery','$capone','$discount','$final_total',$date')");
 
if($result1){
 $resultf = $db ->query("INSERT INTO `order_payment`(`order_num`,`is_paid`,`payment_type`,`date`) VALUES ('$order_num',0,'$payment_type','$date')");
  $resultf = $db ->query("INSERT INTO `order_delevery`(`order_num`,`is_delevery`,`date`) VALUES ('$order_num',0,'$date')");
$api ->changeOrderStatus($order_num,0,0) ;
 
 if($payment_type=='cash'){
$api ->changeOrderStatus($order_num,1,0) ;
 
     
 }
 
 $order=$db -> select("SELECT * FROM `orders` where  order_num='$order_num' limit 1");
      /*   $cart=json_decode($order[0]['cart']);
         $data=array();
         $option2=array();
         for($i=0 ; $i < count($cart); $i++){
            $p_td= $cart[$i]->product_id;
            $pData=$db -> select("SELECT * FROM `products` where products.id=$p_td limit 1");
            $option=$cart[$i]->option;
            for($j=0 ; $j < count($option); $j++){
                  $type_id=$option[$j]->type_id;
                  $type=$db -> select("SELECT * FROM `option_type` where option_type.id=$type_id limit 1");
                  $type_list_id=$option[$j]->type_list_id;
                  $option_type_list=$db -> select("SELECT * FROM `option_type_list` where option_type_list.id=$type_list_id limit 1");
                  $option1=array(
                                'type_id'=>$type_id,
                                'type_name'=>$type[0]['type'],
                                'type_list'=>array(
                                                  'id'=>$type_list_id,
                                                  'name'=>$option_type_list[0]['name']
                                                  )
                                );
                                array_push($option2, $option1);
            }
            $data1=array(
                         'product_id'=>$cart[$i]->product_id,
                         'product_name'=>$pData[0]['name'],
                         'price'=>$cart[$i]->price,
                         'quantity'=>$cart[$i]->quantity,
                         'total'=>$cart[$i]->total,
                         'option'=>$option2
                        );
        array_push($data, $data1);
         }
         
         $product_id=$cart[0]->product_id;
         */
         $vendordata=$db -> select("SELECT * FROM `users` where users.id=(SELECT user_id FROM `products` where id=$product_id) limit 1");
         $order[0]['vendorName']=$vendordata[0]['name'];
         $order[0]['vendorPhoto']=$vendordata[0]['photo'];
         

$order_payment=$db -> select("SELECT * FROM `order_payment` where order_num='$order_num' ORDER BY `order_payment`.`order_num` ASC limit 1");
if(!empty($order_payment)){
$is_paid=$order_payment[0]['is_paid'];
}else{
$is_paid="0";    
}
$order_rate=$db -> select("SELECT * FROM `order_rate` where order_num='$order_num' ORDER BY `order_num` ASC limit 1");
if(!empty($order_rate)){
$rate_data=$order_rate;
$is_rated="1";
}else{
$rate_data=array(); 
$is_rated="0";
} 
 $order[0]['is_paid']=$is_paid; 
 $order[0]['is_rated']=$is_rated; 
 $order[0]['rate_data']=$rate_data; 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']="add success";
$result['data']=$order;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);

     }
     
     }
     }
     }
    break;    
      case "homePage":
       $api = new MyAPICore();    
      
         
        $id=$_REQUEST['token'];
        $address_id=$_REQUEST['address_id'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
        ////////////////////////////home page//////////////
 $title_1_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_1_key' ");
 $title_2_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_2_key' ");
 $title_3_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_3_key' ");
 $title_4_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_4_key' ");
 $title_5_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_5_key' ");


          $slider1=$db -> select("SELECT * FROM `slider`");
         
        $slider=array();
        for($i=0 ; $i < count($slider1); $i++){
            $date1 = date("Y-m-d");
            $date2 = $slider1[$i]['end_date'];
            if ($date1 < $date2){
              $slider[$i]['id']=$slider1[$i]['id']; 
              $slider[$i]['photo']=$slider1[$i]['photo']; 
              $slider[$i]['title']=$slider1[$i]['title']; 
              $slider[$i]['price']=$slider1[$i]['price'];
              $slider[$i]['description']=$slider1[$i]['description']; 
              $slider[$i]['type']=$slider1[$i]['type'];
              $slider[$i]['type_val']=$slider1[$i]['type_val'];
            }
            
        }
        ////////////////////////////////////////
        $banner0=$db -> select("SELECT * FROM `banner`");
        $banner=array();
        for($i=0 ; $i < count($banner0); $i++){
            $date1 = date("Y-m-d");
            $date2 = $banner0[$i]['end_date'];
            if ($date1 < $date2){
              $banner[$i]['id']=$banner0[$i]['id']; 
              $banner[$i]['photo']=$banner0[$i]['photo']; 
              $banner[$i]['type']=$banner0[$i]['type'];
              $banner[$i]['type_val']=$banner0[$i]['type_val'];
            }
            
        }
      //  var_dump($banner);exit();
       $banner1=array();$banner2=array();
        if(!empty($banner[0])){
            $banner1=array($banner[0]);
        }
        if(!empty($banner[1])){
            $banner2=array($banner[1]);
        }
        ////////////////////////////////////////
         $resultc = $db -> select("SELECT * from category where status=1 ");
     $datac=array();
      for($i=0 ; $i < count($resultc); $i++){
          $category_id=$resultc[$i]['id'];
          $category_name=$resultc[$i]['name'];
          $category_photo=$resultc[$i]['photo'];
          
       $result1 = $db -> select("SELECT * FROM `sub_category` where category_id=$category_id  and status=1 ORDER BY id ASC"); 
     $data2=array(
                    'category_id'=>$category_id,
                    'category_name'=>$category_name,
                    'category_photo'=>$category_photo,
                    'sub_category'=>$result1
                      );
       $data1=array('main_category'=> $data2);
       array_push($datac, $data1);
      }
      ////////////////////////////////////////////////////
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status=1  order by id DESC  ");
       if(!empty($resultf)){
       for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    ///////////////////////
    $resultff = $db -> select("select DISTINCT id_option_type from option_type_list where product_id=$p_id");
     $datao=array();
     for($ii=0 ; $ii < count($resultff); $ii++){
       $id_option_type=$resultff[$ii]['id_option_type'];
       $result11 = $db -> select("select * from option_type where id=$id_option_type"); 
       $name=$result11[0]['type'];
       $option_type_id=$result11[0]['id'];
       $datao1=array('option_type_name'=> $name,'option_type_id'=> $option_type_id,
                    'option_type_list'=>$db -> select("select * from option_type_list where id_option_type=$id_option_type and  product_id=$p_id"));
       array_push($datao, $datao1);
      }
    ////////////////////////
    if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    //////////////////////
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    $resultf[$i]['option']=$datao;
    }else{
      
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';
    $resultf[$i]['option']=$datao;
    }
      }
       }
      ///////////////////////////////////////////////////
     $offerh = $db -> select("Select *,(SELECT name from products WHERE products.id=offers.product_id) as pro_name,(SELECT photo from products WHERE products.id=offers.product_id) as photo from offers  order by id DESC ");
     
     ////////////////////////section //////////////////////////
     $section1 = $db -> select("SELECT * FROM `home_setting` where home_setting.keyt='section1'  order by id ASC ");
     $cato1=$section1[0]['val'];
     $main_section1 = $db -> select("SELECT id,name,photo FROM `sub_category` where sub_category.category_id=$cato1  order by id ASC limit 2 ");
     $section2 = $db -> select("SELECT * FROM `home_setting` where home_setting.keyt='section2'  order by id ASC ");
     $cato2=$section2[0]['val'];
     $main_section2 = $db -> select("SELECT id,name,photo FROM `sub_category` where sub_category.category_id=$cato2  order by id ASC limit 6 ");
     $section3 = $db -> select("SELECT * FROM `home_setting` where home_setting.keyt='section3'  order by id ASC "); 
     $cato3=$section3[0]['val'];
     /////////offer section/////////////////
     $resultos = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status=1 and category_id=$cato3 order by id DESC  ");
       if(!empty($resultos)){
       for($i=0 ; $i < count($resultos); $i++){

    $p_id = $resultos[$i]['id'];
    ///////////////////////
    $resultff = $db -> select("select DISTINCT id_option_type from option_type_list where product_id=$p_id");
     $datao=array();
     for($ii=0 ; $ii < count($resultff); $ii++){
       $id_option_type=$resultff[$ii]['id_option_type'];
       $result11 = $db -> select("select * from option_type where id=$id_option_type"); 
       $name=$result11[0]['type'];
       $option_type_id=$result11[0]['id'];
       $isRequired=$result11[0]['required'];
       $isMultChoice=$result11[0]['isMultChoice'];
       $datao1=array('option_type_name'=> $name,'option_type_id'=> $option_type_id,'isRequired'=>$isRequired,'isMultChoice'=>$isMultChoice,
                    'option_type_list'=>$db -> select("select * from option_type_list where id_option_type=$id_option_type and  product_id=$p_id"));
       array_push($datao, $datao1);
      }
    ////////////////////////
    if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultos[$i]['isFavorite']=1;  
     }else{
        $resultos[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultos[$i]['isFavorite']=0;  
        }
    //////////////////////
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
    $resultos[$i]['typeProduct']="Offer";
    $resultos[$i]['priceOffer']=$user_arr[0]['price'];
    $resultos[$i]['option']=$datao;
    }else{
      
     $resultos[$i]['typeProduct']="Product";
    $resultos[$i]['priceOffer']='';
    $resultos[$i]['option']=$datao;
    }
      }
       }
       /////////////////////////////////////////////////////////////////////////
     
     
     
        $datah=array(
                   'slider'=> $slider,
                    'category'=>$datac,
                    'banner1'=>$banner1,
                    'simelar_products'=>array(array(
                                    'title'=>$title_1_key[0]['value_ar'],
                                    'list'=>$resultf)),
                    'offers'=>array(array(
                                    'title'=>$title_2_key[0]['value_ar'],
                                    'list'=>array(
                                                array('photo'=>'http://mayar.mtc.org.ps/vendor/api/uploads/s.png'),
                                                 array('photo'=>'http://mayar.mtc.org.ps/vendor/api/uploads/m1.PNG'),
                                                 array('photo'=>'http://mayar.mtc.org.ps/vendor/api/uploads/s.png'),
                                        
                                                 )
                                    )),
                    'banner2'=>$banner2,
                    //'products'=>$resultf
                    'main_section1'=>array(array(   'title'=>$title_3_key[0]['value_ar'],
                                               'list'=>$main_section1)),
                    'main_section2'=>array(array(    'title'=>$title_4_key[0]['value_ar'],
                                               'list'=>$main_section2)),
                    'offers_section'=>array(array(
                        'title'=>$title_5_key[0]['value_ar'],
                                               'list'=>$resultos
                                               ))
                  ); 
        
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='home page';
            $result['token']=$_REQUEST['token'];
            $result['data']=$datah;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     
     
     
     break;
     case "deleteAddressUser":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['address_id'])){
       $msg = 'address_id is required';
        if($lang=='ar'){
        $msg = 'address_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $address_id=$_REQUEST['address_id'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
        $rows = $db ->query("UPDATE `address` SET status=2  WHERE user_id=$id and id=$address_id"); 
        
         if($rows){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Edit Product Rating Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Error';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
     }
     }
     break;
    case "lookUpData":
       $api = new MyAPICore();    
        
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
      $resultf20 = $db -> select("SELECT * FROM `look_up_data` order by id asc");
      $resultf20[0]['name']='Categories';
      $resultc = $db -> select("SELECT * from category where status=1");
     $data=array();
      for($i=0 ; $i < count($resultc); $i++){
          $category_id=$resultc[$i]['id'];
          $category_name=$resultc[$i]['name'];
          $category_photo=$resultc[$i]['photo'];
          
       $result1 = $db -> select("SELECT * FROM `sub_category` where category_id=$category_id  and status=1 ORDER BY id ASC"); 
     $data2=array(
                    'category_id'=>$category_id,
                    'category_name'=>$category_name,
                    'category_photo'=>$category_photo,
                    'sub_category'=>$result1
                      );
       $data1=array('main_category'=> $data2);
       array_push($data, $data1);
       //$data[$i][$name]=$db -> select("select * from option_type_list where id_option_type=$id_option_type");
      }
           $resultf20[0]['value']=$data; 
           
////////////////////////////home page//////////////
 $title_1_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_1_key' ");
 $title_2_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_2_key' ");
 $title_3_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_3_key' ");
 $title_4_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_4_key' ");
 $title_5_key=$db -> select("SELECT * FROM `string_msg` where string_msg.key='title_5_key' ");


          $slider1=$db -> select("SELECT * FROM `slider`");
        $slider=array();
        for($i=0 ; $i < count($slider1); $i++){
            $date1 = date("Y-m-d");
            $date2 = $slider1[$i]['end_date'];
            if ($date1 < $date2){
              $slider[$i]['id']=$slider1[$i]['id']; 
              $slider[$i]['photo']=$slider1[$i]['photo']; 
              $slider[$i]['title']=$slider1[$i]['title']; 
              $slider[$i]['price']=$slider1[$i]['price'];
              $slider[$i]['description']=$slider1[$i]['description']; 
              $slider[$i]['type']=$slider1[$i]['type'];
              $slider[$i]['type_val']=$slider1[$i]['type_val'];
            }
            
        }
        ////////////////////////////////////////
        $banner1=$db -> select("SELECT * FROM `banner`");
        $banner=array();
        for($i=0 ; $i < count($banner1); $i++){
            $date1 = date("Y-m-d");
            $date2 = $banner1[$i]['end_date'];
            if ($date1 < $date2){
              $banner[$i]['id']=$banner1[$i]['id']; 
              $banner[$i]['photo']=$banner1[$i]['photo']; 
              $banner[$i]['type']=$banner1[$i]['type'];
              $banner[$i]['type_val']=$banner1[$i]['type_val'];
            }
            
        }
        $banner1=array();$banner2=array();
         if(!empty($banner[0])){
            $banner1=array($banner[0]);
        }
        if(!empty($banner[1])){
            $banner2=array($banner[1]);
        }
        ////////////////////////////////////////
         $resultc = $db -> select("SELECT * from category where status=1");
     $datac=array();
      for($i=0 ; $i < count($resultc); $i++){
          $category_id=$resultc[$i]['id'];
          $category_name=$resultc[$i]['name'];
          $category_photo=$resultc[$i]['photo'];
          
       $result1 = $db -> select("SELECT * FROM `sub_category` where category_id=$category_id  and status=1 ORDER BY id ASC"); 
     $data2=array(
                    'category_id'=>$category_id,
                    'category_name'=>$category_name,
                    'category_photo'=>$category_photo,
                    'sub_category'=>$result1
                      );
       $data1=array('main_category'=> $data2);
       array_push($datac, $data1);
      }
      ////////////////////////////////////////////////////
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status=1  order by id DESC  ");
       if(!empty($resultf)){
       for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    ///////////////////////
    $resultff = $db -> select("select DISTINCT id_option_type from option_type_list where product_id=$p_id");
     $datao=array();
     for($ii=0 ; $ii < count($resultff); $ii++){
       $id_option_type=$resultff[$ii]['id_option_type'];
       $result11 = $db -> select("select * from option_type where id=$id_option_type"); 
       $name=$result11[0]['type'];
       $option_type_id=$result11[0]['id'];
       $isRequired=$result11[0]['required'];
       $isMultChoice=$result11[0]['isMultChoice'];
       $datao1=array('option_type_name'=> $name,'option_type_id'=> $option_type_id,'isRequired'=>$isRequired,'isMultChoice'=>$isMultChoice,
                    'option_type_list'=>$db -> select("select * from option_type_list where id_option_type=$id_option_type and  product_id=$p_id"));
       array_push($datao, $datao1);
      }
    ////////////////////////
    if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    //////////////////////
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    $resultf[$i]['option']=$datao;
    }else{
      
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';
    $resultf[$i]['option']=$datao;
    }
      }
       }
      ///////////////////////////////////////////////////
   
     ////////////////////////section //////////////////////////
     $section1 = $db -> select("SELECT * FROM `home_setting` where home_setting.keyt='section1'  order by id ASC ");
     $cato1=$section1[0]['val'];
     $main_section1 = $db -> select("SELECT id,name,photo FROM `sub_category` where sub_category.category_id=$cato1  order by id ASC limit 2 ");
     $section2 = $db -> select("SELECT * FROM `home_setting` where home_setting.keyt='section2'  order by id ASC ");
     $cato2=$section2[0]['val'];
     $main_section2 = $db -> select("SELECT id,name,photo FROM `sub_category` where sub_category.category_id=$cato2  order by id ASC limit 6 ");
     $section3 = $db -> select("SELECT * FROM `home_setting` where home_setting.keyt='section3'  order by id ASC "); 
     $cato3=$section3[0]['val'];
     /////////offer section/////////////////
     $resultos = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status=1 and category_id=$cato3 order by id DESC  ");
       if(!empty($resultos)){
       for($i=0 ; $i < count($resultos); $i++){

    $p_id = $resultos[$i]['id'];
    ///////////////////////
    $resultff = $db -> select("select DISTINCT id_option_type from option_type_list where product_id=$p_id");
     $datao=array();
     for($ii=0 ; $ii < count($resultff); $ii++){
       $id_option_type=$resultff[$ii]['id_option_type'];
       $result11 = $db -> select("select * from option_type where id=$id_option_type"); 
       $name=$result11[0]['type'];
       $option_type_id=$result11[0]['id'];
       $isRequired=$result11[0]['required'];
       $isMultChoice=$result11[0]['isMultChoice'];
       $datao1=array('option_type_name'=> $name,'option_type_id'=> $option_type_id,'isRequired'=>$isRequired,'isMultChoice'=>$isMultChoice,
                    'option_type_list'=>$db -> select("select * from option_type_list where id_option_type=$id_option_type and  product_id=$p_id"));
       array_push($datao, $datao1);
      }
    ////////////////////////
    if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultos[$i]['isFavorite']=1;  
     }else{
        $resultos[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultos[$i]['isFavorite']=0;  
        }
    //////////////////////
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
    $resultos[$i]['typeProduct']="Offer";
    $resultos[$i]['priceOffer']=$user_arr[0]['price'];
    $resultos[$i]['option']=$datao;
    }else{
      
     $resultos[$i]['typeProduct']="Product";
    $resultos[$i]['priceOffer']='';
    $resultos[$i]['option']=$datao;
    }
      }
       }
       /////////////////////////////////////////////////////////////////////////
     
     
     
        $datah=array(
                   'slider'=> $slider,
                    'category'=>$datac,
                    'banner1'=>$banner1,
                    'simelar_products'=>array(array(
                                    'title'=>$title_1_key[0]['value_ar'],
                                    'list'=>$resultf)),
                    'offers'=>array(array(
                                    'title'=>$title_2_key[0]['value_ar'],
                                    'list'=>array(
                                                 array('photo'=>'http://mayar.mtc.org.ps/vendor/api/uploads/s.png'),
                                                 array('photo'=>'http://mayar.mtc.org.ps/vendor/api/uploads/s.png'),
                                        
                                                 )
                                    )),
                    'banner2'=>$banner2,
                    //'products'=>$resultf
                    'main_section1'=>array(array(   'title'=>$title_3_key[0]['value_ar'],
                                               'list'=>$main_section1)),
                    'main_section2'=>array(array(    'title'=>$title_4_key[0]['value_ar'],
                                               'list'=>$main_section2)),
                    'offers_section'=>array(array(
                        'title'=>$title_5_key[0]['value_ar'],
                                               'list'=>$resultos
                                               ))
                  );    
    $order_status=$db -> select("SELECT * FROM `order_status` order by id ASC ");      
                
        $resultf20[6]['id']="6";
        $resultf20[6]['name']='Home Page';
        $resultf20[6]['value']=$datah; 
         $resultf20[10]['id']="10";
        $resultf20[10]['name']='order status';
        $resultf20[10]['value']=$order_status; 
          $resultf20[11]['id']="11";
        $resultf20[11]['name']='payment type';
        $resultf20[11]['value']=$db -> select("SELECT * FROM `payment_type` order by id ASC "); 
        $common_questions=$db -> select("SELECT * FROM `common_questions` order by id ASC ");      
    
       // $resultf20[11]['id']="11";
       // $resultf20[11]['name']='common questions';
       // $resultf20[11]['value']=$common_questions; 
////////////////////////////////////////
           
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view look Up Data';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf20;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
      
    break;
    case "statusOrder":
         $api = new MyAPICore();    
      
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
        $order_status=$db -> select("SELECT * FROM `order_status` order by id ASC "); 
        $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view status Order';
            $result['token']=$_REQUEST['token'];
            $result['data']=$order_status;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
    break; 
     case "viewUserAddresses":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
      $resultf = $db -> select("SELECT * FROM `address` WHERE `user_id` = $id and status=1");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view User Addres';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
    case "addUserAddress":
$api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['lat'])){
       $msg = 'lat is required';
        if($lang=='ar'){
        $msg = 'lat  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['lon'])){
       $msg = 'lon is required';
        if($lang=='ar'){
        $msg = 'lon  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
           if(!isset($_REQUEST['name'])){
       $msg = 'name is required';
        if($lang=='ar'){
        $msg = 'name  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
 $user_id = $_REQUEST['token'];
 $name = $_REQUEST['name'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 $google_description = $_REQUEST['google_description'];
 $details = $_REQUEST['details'];
 	
 $date=date("Y-m-d H:i:s");

  $db = new Db();
  $result1 = $db ->query("INSERT INTO `address`( `user_id`, `lat`, `lon`, `name`, `google_description`, `details`, `date`) 
  VALUES ($user_id,'$lat','$lon','$name','$google_description','$details','$date' )");
  $address_id=mysqli_insert_id($db->getCon());
 
if($result1){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='Add Addres Success';
$result['data']=array('address_id'=>$address_id);

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 
}
}}}
break;
        /////////////////////////////
      case "productChat":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $comment=$_REQUEST['msg'];
        $db = new Db();
       $result1 = $db -> select("Select * from products  where id=$product_id order by id DESC  ");
       $to_id=$result1[0]['user_id'];
       $date=date("Y-m-d H:i:s");
         $resultf = $db ->query("INSERT INTO `chating`(`product_id`,`from_id`,`to_id`,`msg`,`date`) VALUES ($product_id,$id,$to_id,'$comment','$date')");

             if($resultf){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Add Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);     
             }else{
               $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE);   
             }
              }
     } 
    break;
    case "viewChatProduct":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        //$comment=$_REQUEST['msg'];
        $db = new Db();
       $result1 = $db -> select("Select * from products  where id=$product_id order by id DESC  ");
       $to_id=$result1[0]['user_id'];
       $date=date("Y-m-d H:i:s");
        $resultf = $db -> select("SELECT *,(SELECT name from users where users.id= chating.from_id ) as full_name
         ,(SELECT photo from users where users.id= chating.from_id ) as photo
         FROM `chating` WHERE product_id=$product_id and from_id=$id and to_id=$to_id or to_id=$id and from_id=$to_id ORDER BY `chating`.`id` ASC");
             if($resultf){
                   $temp_arr = array();

   

  for($i=0 ; $i < count($resultf); $i++){

    $from_id = $resultf[$i]['from_id'];
    if($from_id==$id){
        $resultf[$i]['type']='sent';
    }else{
       $resultf[$i]['type']='receiving'; 
    }
    array_push($temp_arr,$resultf[$i]);

  }
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$temp_arr;
            print json_encode($result,JSON_UNESCAPED_UNICODE);     
             }else{
               $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='No data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE);   
             }
              }
     } 
    break;
       case "viewChatWithVendor":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['to_id'])){
       $msg = 'to_id is required';
        if($lang=='ar'){
        $msg = 'to_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $to_id=$_REQUEST['to_id'];
        //$comment=$_REQUEST['msg'];
        $db = new Db();
       $date=date("Y-m-d H:i:s");
         $rows = $db ->query("UPDATE `chating` SET `chating`.`read`=1 WHERE from_id=$id and to_id=$to_id or to_id=$id and from_id=$to_id ORDER BY `chating`.`id` ASC"); 
        
        $resultf = $db -> select("SELECT *,(SELECT name from users where users.id= chating.from_id ) as full_name ,
        (SELECT photo from users where users.id= chating.from_id ) as photo ,
        (SELECT name from products where products.id= chating.product_id ) as product_name 
        FROM `chating` WHERE from_id=$id and to_id=$to_id or to_id=$id and from_id=$to_id ORDER BY `chating`.`id` ASC");
             if($resultf){
                           $temp_arr = array();

   

  for($i=0 ; $i < count($resultf); $i++){

    $from_id = $resultf[$i]['from_id'];
    if($from_id==$id){
        $resultf[$i]['type']='sent';
    }else{
       $resultf[$i]['type']='receiving'; 
    }
    array_push($temp_arr,$resultf[$i]);

  }  
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$temp_arr;
            print json_encode($result,JSON_UNESCAPED_UNICODE);     
             }else{
               $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='No data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE);   
             }
              }
     } 
    break;
            case "deleteChat":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['to_id'])){
       $msg = 'to_id is required';
        if($lang=='ar'){
        $msg = 'to_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $to_id=$_REQUEST['to_id'];
        //$comment=$_REQUEST['msg'];
        $db = new Db();
       $date=date("Y-m-d H:i:s");
          $result1 = $db ->query("DELETE FROM `chating`   WHERE from_id=$id and to_id=$to_id or to_id=$id and from_id=$to_id ORDER BY `chating`.`id` ASC ");
        
             if($result1){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Delete Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
              }
     } 
    break;
           case "readMessage":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['to_id'])){
       $msg = 'to_id is required';
        if($lang=='ar'){
        $msg = 'to_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $to_id=$_REQUEST['to_id'];
        //$comment=$_REQUEST['msg'];
        $db = new Db();
       $date=date("Y-m-d H:i:s");
         $rows = $db ->query("UPDATE `chating` SET `chating`.`read`=1 WHERE from_id=$id and to_id=$to_id or to_id=$id and from_id=$to_id ORDER BY `chating`.`id` ASC"); 
         if($rows){
               
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Update Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE);     
             }else{
               $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Error';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE);   
             }
              }
     } 
    break;
    case "chatList":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         
        $id=$_REQUEST['token'];
        //$comment=$_REQUEST['msg'];
        $db = new Db();
       $date=date("Y-m-d H:i:s");
        $resultf = $db -> select("select *,(SELECT name from users where users.id= chhh.to_id ) as full_name
        ,(SELECT photo from users where users.id= chhh.to_id ) as photo
 from (select DISTINCT to_id from (SELECT * FROM `chating` WHERE  from_id=$id ORDER BY `chating`.`id` DESC) as chat) as chhh");
             if($resultf){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE);     
             }else{
               $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='No data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE);   
             }
              
     } 
    break;
                    case "productsForSameVendor":
       $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName from products   where status<>3 and user_id=(SELECT user_id from products WHERE products.id=$product_id) and id<>$product_id order by id DESC  ");
       if(!empty($resultf)){
       for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
     if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
     }    
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Product Data';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
     } 
    break;
    case "similarProducts":
      $api = new MyAPICore();    
      if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName from products   where status<>3 and category_id=(SELECT category_id from products WHERE products.id=$product_id) and id<>$product_id order by id DESC  ");
       if(!empty($resultf)){
       for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
      if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
     if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
     }    
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Product Data';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
     }   
    break;
             case "viewAllFavorite":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         
        $id=$_REQUEST['token'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
          $arr = $db -> select("SELECT *  FROM `favorite` WHERE  user_id=$id ");
           $findArray=array();
    $resultf1=array();
    for($i=0;$i<count($arr);$i++){
        
        $type_in = $arr[$i]['type'];
        $product_id = $arr[$i]['product_id'];
        if($type_in==1){
            $resultf1 = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and id=".$product_id." order by id DESC  ");
           if(!empty($resultf1)){
            $resultf1[0]['typeProduct']="Product";
               $resultf1[0]['priceOffer']=""; 
               array_push($findArray,$resultf1[0]);
           }
        }else{
          $resultf0 = $db -> select("Select * from offers   where id=".$product_id." order by id DESC  ");
          $p_id=$resultf0[0]['product_id'];
           $resultf1 = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and id=".$p_id." order by id DESC  ");
          if(!empty($resultf1)){
           $resultf1[0]['priceOffer']=$resultf0[0]['price']; 
        $resultf1[0]['typeProduct']="Offer";
        array_push($findArray,$resultf1[0]);
          }
        }
        
    }
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View All Product Favorite';
            $result['token']=$_REQUEST['token'];
            $result['data']=$findArray;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
          
     
     
     }
    break;
        case "addToFavorites":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $type=1;
        $db = new Db();
        $date=date("Y-m-d H:i:s");
      
         $result1 = $db ->query("INSERT INTO `favorite`(`product_id`,`user_id`,`type`,`date`) VALUES ($product_id,$id,$type,'$date')");
             if($result1){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Add Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
          
     
     }
     }
    break;
            case "removeFromFavorites":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        //$type=$_REQUEST['type'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
       $result1 = $db ->query("DELETE FROM `favorite`   WHERE product_id=$product_id and user_id=$id  ");
        
             if($result1){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Remove Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
          
     
     }
     }
    break;
         case "orderRating":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['order_num'])){
       $msg = 'order_num is required';
        if($lang=='ar'){
        $msg = 'order_num  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
          if(!isset($_REQUEST['rating_val'])){
       $msg = 'rating_val is required';
        if($lang=='ar'){
        $msg = 'rating_val  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $order_num=$_REQUEST['order_num'];
        $rating_val=$_REQUEST['rating_val'];
        $comment=$_REQUEST['comment'];
        $db = new Db();
        $date=date("Y-m-d");
         $rows = $db -> select("SELECT * FROM `order_rate` WHERE  order_num='$order_num' ");
         if(empty($rows)){
         $result1 = $db ->query("INSERT INTO `order_rate`(`order_num`,`rate`,`comment`,`date`) VALUES ('$order_num',$rating_val,'$comment','$date')");
         /////////////////////////////////////////////////////////////
         $note_ar="تم تقييم الطلب # ".$order_num;
         $api ->addOrderActivity($order_num,$id,$note_ar,'',$date);  
          $order =$db -> select("SELECT * FROM `orders` where `order_num`  = '$order_num' limit 1  ");
         $cart=json_decode($order[0]['cart']);
         $product_id=$cart[0]->product_id;
         $productdata=$db -> select("SELECT * FROM `products` where id=$product_id limit 1");
         $vendor_id=$productdata[0]['user_id'];
         $sub="  تقييم الطلب # ";
         $msg='تم تقييم الطلب # '.$order_num;
         $api -> sendPushNotification($id,array("title"=> urldecode($sub),"body"=>urldecode($msg)));  
         $api -> sendPushNotification($vendor_id,array("title"=> urldecode($sub),"body"=>urldecode($msg)));  
    
             if($result1){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Add Rating Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=array($db -> select("SELECT * FROM `order_rate` WHERE  order_num='$order_num' "));
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
          
     }else{
       $msg = 'You have already evaluated';
        if($lang=='ar'){
        $msg = 'قمت بالتقييم مسبقا.  ';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows[0];
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();   
     }
     } 
     }
     }
    break;
          case "productRating":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
          if(!isset($_REQUEST['rating_val'])){
       $msg = 'rating_val is required';
        if($lang=='ar'){
        $msg = 'rating_val  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $rating_val=$_REQUEST['rating_val'];
        $comment=$_REQUEST['comment'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
         $rows = $db -> select("SELECT * FROM `rating_product` WHERE user_id=$id and product_id=$product_id");
         if(empty($rows)){
         $result1 = $db ->query("INSERT INTO `rating_product`(`product_id`,`user_id`,`rate`,`comment`,`date`) VALUES ($product_id,$id,$rating_val,'$comment','$date')");
             if($result1){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Add Rating Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
          
     }else{
       $msg = 'You have already evaluated';
        if($lang=='ar'){
        $msg = 'قمت بالتقييم مسبقا.  ';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows[0];
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();   
     }
     } 
     }
     }
    break;
    case "reportRatingProduct":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
         $star1 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=1"); 
         $star2 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=2"); 
         $star3 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=3"); 
         $star4 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=4"); 
         $star5 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=5"); 
         $s=$star1[0]['cnt']*1+$star2[0]['cnt']*2+$star3[0]['cnt']*3+$star4[0]['cnt']*4+$star5[0]['cnt']*5;
         $s2=$star1[0]['cnt'] + $star2[0]['cnt'] + $star3[0]['cnt'] +$star4[0]['cnt'] +$star5[0]['cnt'];
         if($s2!=0){
         $a=number_format((float)($s/$s2), 2, '.', '');
         }else{
           $a=0;  
         }
         $row=array(
                   'oneStar'=>$star1[0]['cnt'],
                   'twoStar'=>$star2[0]['cnt'],
                   'threeStar'=>$star3[0]['cnt'],
                   'fourStar'=>$star4[0]['cnt'],
                   'fiveStar'=>$star5[0]['cnt'],
                   'Avg'=>$a,
                   'totalNum'=>$star1[0]['cnt'] + $star2[0]['cnt'] + $star3[0]['cnt'] +$star4[0]['cnt'] +$star5[0]['cnt']
                    );
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Product Rating ';
            $result['token']=$_REQUEST['token'];
            $result['data']=$row;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          
     }
     }
     break;
    case "viewAllProductRating":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $rating_val=$_REQUEST['rating_val'];
        $comment=$_REQUEST['comment'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
       if(!empty($id)){
           $rows1 = $db -> select("SELECT *,(SELECT name from users where users.id= rating_product.user_id ) as full_name
         ,(SELECT photo from users where users.id= rating_product.user_id ) as photo
         FROM `rating_product` WHERE product_id=$product_id and user_id<>$id order by id DESC "); 
       }else{
         $rows1 = $db -> select("SELECT *,(SELECT name from users where users.id= rating_product.user_id ) as full_name
         ,(SELECT photo from users where users.id= rating_product.user_id ) as photo
         FROM `rating_product` WHERE product_id=$product_id order by id DESC");   
       }
         if(!empty($rows1)){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Product Rating ';
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows1;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='No Product Rating';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
     }
     }
     break;
      case "viewProductRatingUser":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $rating_val=$_REQUEST['rating_val'];
        $comment=$_REQUEST['comment'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
         $rows = $db -> select("SELECT *,(SELECT fname from users where users.id= rating_product.user_id ) as fname
         ,(SELECT lname from users where users.id= rating_product.user_id ) as lname
         ,(SELECT photo from users where users.id= rating_product.user_id ) as photo
         FROM `rating_product` WHERE product_id=$product_id and user_id=$id"); 
         if(!empty($rows)){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Product Rating';
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='No Product Rating';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
     }
     }
     break;
     case "editProductRatingUser":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $rating_val=$_REQUEST['rating_val'];
        $comment=$_REQUEST['comment'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
        $rows = $db ->query("UPDATE `rating_product` SET comment='$comment',rate=$rating_val WHERE product_id=$product_id and user_id=$id"); 
        
         if($rows){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Edit Product Rating Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Error';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
     }
     }
     break;
     case "deleteProductRatingUser":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
        $db = new Db();
        $date=date("Y-m-d H:i:s");
         $result1 = $db ->query("DELETE FROM `rating_product`   WHERE product_id=$product_id and user_id=$id ");
        
         if($result1){
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='Delete Product Rating Success';
            $result['token']=$_REQUEST['token'];
            $result['data']=$rows;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }else{
             $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Error';
$result['data']=array(); 
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
          }
     }
     }
     break;
      case "activeAccount":

 $api = new MyAPICore();    
        if(!isset($_REQUEST['username'])){
       $msg = 'username is required';
        if($lang=='ar'){
        $msg = '  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['password'])){
       $msg = 'password is required';
        if($lang=='ar'){
        $msg = '  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            $result['token']='';
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['active_code'])){
       $msg = 'active_code is required';
        if($lang=='ar'){
        $msg = 'active_code  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            $result['token']='';
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
    $email = $_REQUEST['username'];
    $password = $_REQUEST['password'];
    $active_code=$_REQUEST['active_code'];

    $db = new Db();



    $rows = $db -> select("SELECT * FROM `users` WHERE `email` = '$email'   AND userType=3 ");

    if(count($rows)>0){
        if($active_code=='12345'){
        $result = array();
        
        $user_idd = $rows[0]['id'];
           $result1 = $db ->query("UPDATE `users` SET  `status` =1 WHERE `id` = $user_idd");

       
        //$rows2 = $db -> select("SELECT * FROM `users` WHERE ( `email` = '$email' OR `username` = '$email' ) AND `password` = '$password'");
        
         $msg = 'The account was successfully activated ';
        if($lang=='ar'){
        $msg = '  تم تفعيل الحساب بنجاح';
         } 
        $result['status']= true;
        $result['http_code']= (int) 200;
        $result['msg']=$msg;
        $result['data']=$rows[0];
        $result['token']=$rows[0]['id'];
        }else{
          $msg = 'Activation Code error';
        if($lang=='ar'){
        $msg = '  كود التفعيل خاطئ الرجاء التأكد منه   ';
         }
                    $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']=$msg;
$result['data']=array();
$result['token']='';
        } 
        
    }else{
        $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array();
$result['token']='';
    }
}
}
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 break;
    case "login":

 $api = new MyAPICore();    
        if(!isset($_REQUEST['username'])){
       $msg = 'Email is required';
        if($lang=='ar'){
        $msg = 'Email  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['password'])){
       $msg = 'password is required';
        if($lang=='ar'){
        $msg = '  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            $result['token']='';
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
    $email = $_REQUEST['username'];
    $password = $_REQUEST['password'];


    $db = new Db();


// AND `password` = '$password'
    $rows = $db -> select("SELECT * FROM `users` WHERE  `email` = '$email' AND userType=3 limit 1 ");

    if(count($rows)>0){
       $dbpassword=$rows[0]['password'];
       if (password_verify($password, $dbpassword)) {
      if($rows[0]['status']==1){
        $result = array();
        
        $user_idd = $rows[0]['id'];
        
       
        //$rows2 = $db -> select("SELECT * FROM `users` WHERE ( `email` = '$email' OR `username` = '$email' ) AND `password` = '$password'");
        
         $msg = 'Login Success';
        if($lang=='ar'){
        $msg = '  تم تسجيل الدخول بنجاح';
         } 
         $rows[0]['full_name']=$rows[0]['name'];
        $result['status']= true;
        $result['http_code']= (int) 200;
        $result['msg']=$msg;
        $result['data']=array($rows[0]);
        $result['token']=$rows[0]['id'];
        }else{
           $msg = 'The account is inactive ';
        if($lang=='ar'){
        $msg = '  الحساب غير فعال   ';
         }   
                  $result['status']= false;
$result['http_code']= (int) 550;
$result['msg']=$msg;
$result['data']=array();
$result['token']='';  
        }
} else {
           $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='password Error,Please check the data';
$result['data']=array();
$result['token']='';
}
    
        
    }else{
        $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array();
$result['token']='';
    }
}
}

print json_encode($result,JSON_UNESCAPED_UNICODE);
 break;
   case "filterProducts":
       $api = new MyAPICore();    
      
        if(!isset($_REQUEST['subCategoryId'])){
       $msg = 'subCategoryId is required';
        if($lang=='ar'){
        $msg = 'subCategoryId  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
          $db = new Db();
        $id=$_REQUEST['token'];
        $subCategoryId=$_REQUEST['subCategoryId'];
        $filter=$_REQUEST['filter'];
         $subCat_arr = $db -> select(" SELECT * from sub_category WHERE sub_category.id=$subCategoryId  order by id DESC limit 1");
        $mainCat=$subCat_arr[0]['category_id'];
        $mainCat_arr = $db -> select(" SELECT * from category WHERE category.id=$mainCat  order by id DESC limit 1");
        $resultf = $db -> select("Select *,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and category_id=".$subCategoryId." order by id DESC  ");
      
        if($filter==1){
         // $product_id=$_REQUEST['product_id'];
       
     for($i=0 ; $i < count($resultf); $i++){
         $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
         $star1 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$p_id  and rate=1"); 
         $star2 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$p_id  and rate=2"); 
         $star3 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$p_id  and rate=3"); 
         $star4 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$p_id  and rate=4"); 
         $star5 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$p_id  and rate=5"); 
         $s=$star1[0]['cnt']*1+$star2[0]['cnt']*2+$star3[0]['cnt']*3+$star4[0]['cnt']*4+$star5[0]['cnt']*5;
         $s2=$star1[0]['cnt'] + $star2[0]['cnt'] + $star3[0]['cnt'] +$star4[0]['cnt'] +$star5[0]['cnt'];
         if($s2!=0){
         $a=number_format((float)($s/$s2), 2, '.', '');
         }else{
           $a=0;  
         }
         $resultf[$i]['Avg']=$a;
        
        }
        $columns = array_column($resultf, 'Avg');
       $resultf1=array_multisort($columns, SORT_DESC, $resultf);
       
       
        $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='filter Products';
            $result['token']=$_REQUEST['token'];
            $result['data']=array(
                     'mainCategory'=>$mainCat_arr[0]['name'],
                     'subCategory'=>$subCat_arr[0]['name'],
                     'Products'=>$resultf
                
                          );
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
        }
        else if($filter==2){
            $order=$db -> select("SELECT * FROM `orders` ");
          $data=array();
         for($x=0 ; $x < count($order); $x++){
         $cart=json_decode($order[$x]['cart']);
       
         $option2=array();
         for($i=0 ; $i < count($cart); $i++){
            $p_td= $cart[$i]->product_id;
           
        array_push($data, $p_td);
         }   }
         $vals = array_count_values($data);
         arsort($vals);
         $seller=array();
         foreach ($vals as $key => $val) {
          
          $p_id = $key;
           $resultff = $db -> select("Select *,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and category_id=".$subCategoryId." and id=$p_id order by id DESC  ");
      if(!empty($resultff)) {  
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultff[0]['isFavorite']=1;  
     }else{
        $resultff[0]['isFavorite']=0;  
     }
            
        }else{
           $resultff[0]['isFavorite']=0;  
        }
    $resultff[0]['typeProduct']="Offer";
    $resultff[0]['priceOffer']=$user_arr[0]['price'];
    }else{
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultff[0]['isFavorite']=1;  
     }else{
        $resultff[0]['isFavorite']=0;  
     }
            
        }else{
           $resultff[0]['isFavorite']=0;  
        }
     $resultff[0]['typeProduct']="Product";
    $resultff[0]['priceOffer']='';   
    }
      
      
      array_push($seller, $resultff);   
      }
          }
          
    if(!empty($seller)){
       $resultf= $seller;
    }else{
        for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
        if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
            
            
    }      
         $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='filter Products';
            $result['token']=$_REQUEST['token'];
            $result['data']=array(
                     'mainCategory'=>$mainCat_arr[0]['name'],
                     'subCategory'=>$subCat_arr[0]['name'],
                     'Products'=>$resultf
                
                          );
            print json_encode($result,JSON_UNESCAPED_UNICODE);  
        }
        else{
         
  for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
        if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Products';
            $result['token']=$_REQUEST['token'];
            $result['data']=array(
                     'mainCategory'=>$mainCat_arr[0]['name'],
                     'subCategory'=>$subCat_arr[0]['name'],
                     'Products'=>$resultf
                
                          );
            print json_encode($result,JSON_UNESCAPED_UNICODE);   
        }
     }
     break;
  case "viewProducts":
       $api = new MyAPICore();    
      
        if(!isset($_REQUEST['subCategoryId'])){
       $msg = 'subCategoryId is required';
        if($lang=='ar'){
        $msg = 'subCategoryId  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $subCategoryId=$_REQUEST['subCategoryId'];
        $db = new Db();
        $subCat_arr = $db -> select(" SELECT * from sub_category WHERE sub_category.id=$subCategoryId  order by id DESC limit 1");
        $mainCat=$subCat_arr[0]['category_id'];
        $mainCat_arr = $db -> select(" SELECT * from category WHERE category.id=$mainCat  order by id DESC limit 1");
       $resultf = $db -> select("Select *,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and category_id=".$subCategoryId." order by id DESC  ");
      
  for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
        if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id  order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
         if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id  order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Products';
            $result['token']=$_REQUEST['token'];
            $result['data']=array(
                     'mainCategory'=>$mainCat_arr[0]['name'],
                     'subCategory'=>$subCat_arr[0]['name'],
                     'Products'=>$resultf
                
                          );
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
      
    break;
    case "productDetails":
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and id=".$product_id." order by id DESC  ");
       for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
        if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
      
     ///////////////////////////////////
     $rows = $db -> select("SELECT *,(SELECT name from users where users.id= rating_product.user_id ) as full_name
         ,(SELECT photo from users where users.id= rating_product.user_id ) as photo
         FROM `rating_product` WHERE product_id=$product_id and user_id=$id"); 
       if($rows){
         $rows=$rows;  
       }else{
        $rows=array();   
       }  
       if(!empty($id)){
           $rows1 = $db -> select("SELECT *,(SELECT name from users where users.id= rating_product.user_id ) as full_name
         ,(SELECT photo from users where users.id= rating_product.user_id ) as photo
         FROM `rating_product` WHERE product_id=$product_id and user_id<>$id order by id DESC"); 
       }else{
         $rows1 = $db -> select("SELECT *,(SELECT name from users where users.id= rating_product.user_id ) as full_name
         ,(SELECT photo from users where users.id= rating_product.user_id ) as photo
         FROM `rating_product` WHERE product_id=$product_id order by id DESC ");   
       }
         
         
           $star1 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=1"); 
         $star2 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=2"); 
         $star3 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=3"); 
         $star4 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=4"); 
         $star5 = $db -> select("select count(*)as cnt FROM `rating_product` WHERE product_id=$product_id  and rate=5"); 
         $s=$star1[0]['cnt']*1+$star2[0]['cnt']*2+$star3[0]['cnt']*3+$star4[0]['cnt']*4+$star5[0]['cnt']*5;
         $s2=$star1[0]['cnt'] + $star2[0]['cnt'] + $star3[0]['cnt'] +$star4[0]['cnt'] +$star5[0]['cnt'];
         if($s2!=0){
         $a=number_format((float)($s/$s2), 2, '.', '');
         }else{
           $a="0";  
         }
         $row2=array(
                   'oneStar'=>$star1[0]['cnt'],
                   'twoStar'=>$star2[0]['cnt'],
                   'threeStar'=>$star3[0]['cnt'],
                   'fourStar'=>$star4[0]['cnt'],
                   'fiveStar'=>$star5[0]['cnt'],
                   'Avg'=>$a,
                   'totalNum'=>$star1[0]['cnt'] + $star2[0]['cnt'] + $star3[0]['cnt'] +$star4[0]['cnt'] +$star5[0]['cnt']
                    );
    ////////////////////////////
    $resultf1 = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName from products   where status<>3 and category_id=(SELECT category_id from products WHERE products.id=$product_id) and id<>$product_id order by id DESC  ");
       if(!empty($resultf1)){
       for($i=0 ; $i < count($resultf1); $i++){

    $p_id = $resultf1[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
     if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf1[$i]['isFavorite']=1;  
     }else{
        $resultf1[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf1[$i]['isFavorite']=0;  
        }
    $resultf1[$i]['typeProduct']="Offer";
    $resultf1[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
         if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf1[$i]['isFavorite']=1;  
     }else{
        $resultf1[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf1[$i]['isFavorite']=0;  
        }
     $resultf1[$i]['typeProduct']="Product";
    $resultf1[$i]['priceOffer']='';   
    }
      }
     } 
     //////////////////////////////
     $resultf2 = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName from products   where status<>3 and user_id=(SELECT user_id from products WHERE products.id=$product_id) and id<>$product_id order by id DESC  ");
       if(!empty($resultf2)){
       for($i=0 ; $i < count($resultf2); $i++){

    $p_id = $resultf2[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf2[$i]['isFavorite']=1;  
     }else{
        $resultf2[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf2[$i]['isFavorite']=0;  
        }
    $resultf2[$i]['typeProduct']="Offer";
    $resultf2[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
         if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf2[$i]['isFavorite']=1;  
     }else{
        $resultf2[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf2[$i]['isFavorite']=0;  
        }
     $resultf2[$i]['typeProduct']="Product";
    $resultf2[$i]['priceOffer']='';   
    }
      }
     } 
        //////////////////////////////
     $resultf3 = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName from products   where status<>3 and user_id<>(SELECT user_id from products WHERE products.id=$product_id) and category_id<>(SELECT category_id from products WHERE products.id=$product_id) and id<>$product_id order by id DESC  ");
       if(!empty($resultf3)){
       for($i=0 ; $i < count($resultf3); $i++){

    $p_id = $resultf3[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
       if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf3[$i]['isFavorite']=1;  
     }else{
        $resultf3[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf3[$i]['isFavorite']=0;  
        }
    $resultf3[$i]['typeProduct']="Offer";
    $resultf3[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
         if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf3[$i]['isFavorite']=1;  
     }else{
        $resultf3[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf3[$i]['isFavorite']=0;  
        }
     $resultf3[$i]['typeProduct']="Product";
    $resultf3[$i]['priceOffer']='';   
    }
      }
     } 
    ///////////////////////////
    $resulto = $db -> select("select DISTINCT id_option_type from option_type_list where product_id=$product_id");
     $datao=array();
      for($i=0 ; $i < count($resulto); $i++){
          $id_option_type=$resulto[$i]['id_option_type'];
       $result1o = $db -> select("select * from option_type where id=$id_option_type"); 
       
       $name=$result1o[0]['type'];
       $option_type_id=$result1o[0]['id'];
       $isRequired=$result1o[0]['required'];
       $isMultChoice=$result1o[0]['isMultChoice'];
       $data1=array('option_type_name'=> $name,'option_type_id'=> $option_type_id,'isRequired'=>$isRequired,'isMultChoice'=>$isMultChoice,
                    'option_type_list'=>$db -> select("select * from option_type_list where id_option_type=$id_option_type and  product_id=$product_id"));
      array_push($datao, $data1);
       //$data[$i][$name]=$db -> select("select * from option_type_list where id_option_type=$id_option_type");
      }
     $more_images_list1=$db -> select("select photo from product_images where product_id=$product_id"); 
     if(!empty($more_images_list1)){
        $more_images_list= $more_images_list1;
     }else{
       $more_images_list=array();  
     }
    $datas=array(
        'viewProductData'=>$resultf,
        'optionsProduct'=>$datao,
        'viewProductRatingUser'=>$rows,
        'viewAllProductRating'=>$rows1,
        'reportRatingProduct'=>$row2,
        'similarProducts'=>$resultf1,
        'more_images_list'=>$more_images_list,
        'productsForSameVendor'=>$resultf2,
        'productsMayLike'=>$resultf3
                 );
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Product ';
            $result['token']=$_REQUEST['token'];
            $result['data']=$datas;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
    break;
    case "optionsProduct":
       $api = new MyAPICore();    
         
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
    
        $db = new Db();
     $resultf = $db -> select("select DISTINCT id_option_type from option_type_list where product_id=$product_id");
     $data=array();
      for($i=0 ; $i < count($resultf); $i++){
          $id_option_type=$resultf[$i]['id_option_type'];
       $result1 = $db -> select("select * from option_type where id=$id_option_type"); 
       $name=$result1[0]['type'];
       $option_type_id=$result1[0]['id'];
        $isRequired=$result1[0]['required'];
       $isMultChoice=$result1[0]['isMultChoice'];
       $data1=array('option_type_name'=> $name,'option_type_id'=> $option_type_id,'isRequired'=>$isRequired,'isMultChoice'=>$isMultChoice,
                    'option_type_list'=>$db -> select("select * from option_type_list where id_option_type=$id_option_type and  product_id=$product_id"));
  
        array_push($data, $data1);
       //$data[$i][$name]=$db -> select("select * from option_type_list where id_option_type=$id_option_type");
      }
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Product Data';
            $result['token']=$_REQUEST['token'];
            $result['data']=$data;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
       
    break;
    case "viewProductData":
       $api = new MyAPICore();    
         
         if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $product_id=$_REQUEST['product_id'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id) as subCategoryName from products   where status<>3 and id=".$product_id." order by id DESC  ");
       for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $id_offer and type=2 order by id DESC limit 1");
     if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $p_id and type=1 order by id DESC limit 1");
         if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Product Data';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
       
    break; 
   case "viewUserData":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
      $resultf = $db -> select("SELECT * FROM `users` WHERE `id` = $id ");
      
            $resultf[0]['full_name']=$resultf[0]['name'];  
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view User Data';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
 
///////////////////////////////

 
 
    case "register":
             if(!isset($_REQUEST['email'])){
       $msg = 'email is required';
        if($lang=='ar'){
        $msg = 'email  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['password'])){
       $msg = 'password is required';
        if($lang=='ar'){
        $msg = ' password مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
     if(!isset($_REQUEST['name'])){
       $msg = 'name is required';
        if($lang=='ar'){
        $msg = ' name مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['phone'])){
       $msg = 'phone is required';
        if($lang=='ar'){
        $msg = ' phone مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
    $password1 = $_REQUEST['password'];
    $name = $_REQUEST['name'];
    $phone = $_REQUEST['phone'];
    $email = $_REQUEST['email'];
   // $photo = $_REQUEST['photo'];
 
    $options = [
  'cost' => 10
];
$password= password_hash($password1, PASSWORD_DEFAULT);
    
    
     $date=date("Y-m-d H:i:s");

     $db = new Db();
    $result1 = $db ->query("INSERT INTO `users`(`name`,`email`,`password`,`status`,`phone`,`date`) VALUES ('$name','$email','$password',0,'$phone','$date')");
    $last_id2 = mysqli_insert_id($db->getCon());
    $code = $last_id2*12345;
    $msgSend = "Your Code is: ".$code;
    $sub = urlencode('تفعيل الحساب  ');
    $msg = urlencode("شكرا على اشتراكك ، لاتمام العملية عليك تفعيل حساب ");
    $msg .= urlencode("  <br> ");
     $msg .= urlencode("  كود التفعيل الخاص بحسابك = ".$code);
   // file_get_contents("https://mayar.mtc.org.ps/vendor/api/emailSend.php?to={$email}&sub={$sub}&msg={$msg}&btype=reg&code={$code}");
if($result1){
    
 $result['status']= true;
$result['http_code']= (int) 200;
//$result['token']=$last_id2;
$result['msg']='User created successfully';
$result['data']=array();

    
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
    
}}}}

 break;
 case "editUserData":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
        $category_id=$_POST['category_id']; 
        $token=$_REQUEST['token'];
        $full_name=$_REQUEST['full_name'];
         $phone = $_REQUEST['phone'];
        //$email = $_REQUEST['email'];
        $up = uploadImage($_FILES);
$rows2 = $db -> select("SELECT * FROM `users` WHERE `id` = $token ");
$emaildb=$rows2[0]['email'];

      if($up == false)
      $photo=$rows2[0]['photo'];
      else {
        $photo = $up;
      }
           $result1 = $db ->query("UPDATE `users` SET  `name` = '$full_name',`phone`='$phone',`photo`='$photo' WHERE `id` = $token");

     if($result1){  
                      $msg = 'Edit Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
     }
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Data Error,Please check the data';
$result['data']=array();
 print json_encode($result,JSON_UNESCAPED_UNICODE); 
}
      
     }   
         
      
    break;
     case "editPassword":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         if(!isset($_REQUEST['old_password'])){
       $msg = 'old_password is required';
        if($lang=='ar'){
        $msg = 'old_password  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['new_password'])){
       $msg = 'new_password is required';
        if($lang=='ar'){
        $msg = 'new_password  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
        $token=$_REQUEST['token'];
        $old_password=$_REQUEST['old_password'];
        $new_password1=$_REQUEST['new_password'];
     $rows = $db -> select("SELECT * FROM `users` WHERE id=$token ");  
     $password=$rows[0]['password'];
     if (password_verify($old_password, $password)) {
         $options = [
  'cost' => 10
];
$new_password=password_hash($new_password1, PASSWORD_DEFAULT);
          $result1 = $db ->query("UPDATE `users` SET  `password` = '$new_password' WHERE `id` = $token");
     
    if($result1){  
                      $msg = 'Edit Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
     }
     }else{
        $result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Old Password Error,Please check it.';
$result['data']=array();
print json_encode($result,JSON_UNESCAPED_UNICODE);exit();  
     }
  
         
     }   
     }   
     }
    break;
    case "notification":
         if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       $api = new MyAPICore();    
        $token=$_REQUEST['token'];
        $db = new Db();
       $resultc = $db -> select("SELECT * from notifications where user_id=$token  ");

    
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='View Notification';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultc;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }
    break;
     case "stores":
       $api = new MyAPICore();    
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
       $resultc = $db -> select("SELECT * from users where userType=2 and status=1 ");
        for($i=0 ; $i < count($resultc); $i++){

          $resultc[$i]['full_name'] = $resultc[$i]['name'];
        }
     $data=array();
    
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view stores';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultc;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
       
    break;
    case "searchStores":
       $api = new MyAPICore();    
        
        $id=$_REQUEST['token'];
        $text=$_REQUEST['text'];
        $db = new Db();
       $resultc = $db -> select("SELECT * from users where userType=2 and status=1 and name LIKE '%$text%'");
     $data=array();
   for($i=0 ; $i < count($resultc); $i++){

          $resultc[$i]['full_name'] = $resultc[$i]['name'];
        }        
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view stores';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultc;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
       
    break;
    case "getVendor":
       $api = new MyAPICore();    
       if(!isset($_REQUEST['vendor_id'])){
       $msg = 'vendor_id is required';
        if($lang=='ar'){
        $msg = 'vendor_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{  
        $id=$_REQUEST['token'];
        $vendor_id=$_REQUEST['vendor_id'];
        $db = new Db();
       $data1 = $db -> select("SELECT * from users where userType=2 and status=1 and id=$vendor_id ");
       $data1[0]['full_name'] = $data1[0]['name'];
       //////////////////////////
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from sub_category WHERE sub_category.id=products.category_id  order by id DESC limit 1) as sub_category_name,(SELECT name from users WHERE users.id=products.user_id) as vendorName from products   where status<>3 and user_id=".$vendor_id." order by id DESC  ");
      
  for($i=0 ; $i < count($resultf); $i++){

    $p_id = $resultf[$i]['id'];
    $user_arr = $db -> select("SELECT * FROM `offers` WHERE  `product_id` = $p_id order by id DESC limit 1");
    $id_offer=$user_arr[0]['id'];
    if(!empty($user_arr)){
        if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $id_offer and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     
    $resultf[$i]['typeProduct']="Offer";
    $resultf[$i]['priceOffer']=$user_arr[0]['price'];
    }else{
         if(isset($_REQUEST['token'])){
         $favorite_arr = $db -> select("SELECT * FROM `favorite` WHERE  `product_id` = $id_offer and user_id=$id and type=2 order by id DESC limit 1");
        if(!empty($favorite_arr)){
       $resultf[$i]['isFavorite']=1;  
     }else{
        $resultf[$i]['isFavorite']=0;  
     }
            
        }else{
           $resultf[$i]['isFavorite']=0;  
        }
     $resultf[$i]['typeProduct']="Product";
    $resultf[$i]['priceOffer']='';   
    }
      }
            
      
       
       /////////////////////////////
       $data2;
       $resultc=array(
              'vendor_info'=>$data1,
              'vendor_product'=>$resultf
                     );
       
     $data=array();
    
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view stores';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultc;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
       
    break;
     case "viewCategories":
       $api = new MyAPICore();    
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
       $resultc = $db -> select("SELECT * from category where status=1");
     $data=array();
      for($i=0 ; $i < count($resultc); $i++){
          $category_id=$resultc[$i]['id'];
          $category_name=$resultc[$i]['name'];
          $category_photo=$resultc[$i]['photo'];
          
       $result1 = $db -> select("SELECT * FROM `sub_category` where category_id=$category_id  and status=1 ORDER BY id ASC"); 
     $data2=array(
                    'category_id'=>$category_id,
                    'category_name'=>$category_name,
                    'category_photo'=>$category_photo,
                    'sub_category'=>$result1
                      );
       $data1=array('main_category'=> $data2);
       array_push($data, $data1);
       //$data[$i][$name]=$db -> select("select * from option_type_list where id_option_type=$id_option_type");
      }
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Categories';
            $result['token']=$_REQUEST['token'];
            $result['data']=$data;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
       
    break;
      case "viewSubCategories":
       $api = new MyAPICore();    
      
        if(!isset($_REQUEST['category_id'])){
       $msg = 'category_id is required';
        if($lang=='ar'){
        $msg = 'category_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $category_id=$_REQUEST['category_id'];
        $db = new Db();
       $resultf = $db -> select("Select * from sub_category   where status<>3 and 	category_id =$category_id  order by id DESC ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='view Sub Categories ';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     
     }  
    break;
    
    
///////////////////////////////////////////////////
    case "addCategory":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       if(!isset($_REQUEST['name'])){
       $msg = 'name is required';
        if($lang=='ar'){
        $msg = 'name  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['status'])){
       $msg = 'status is required';
        if($lang=='ar'){
        $msg = 'status  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(isset($_REQUEST['photo'])){
        $b64=$_POST['photo']; 
        $ext=$_REQUEST['ext'];     
        $token=$_REQUEST['token'];
        $name=$_REQUEST['name'];
        $status=$_REQUEST['status'];
      //  $b64 = urlencode($b64);
        
        
       // print $b64;
    //    exit;
       
       
        $folderPath = "../uploads/";
        $bin = base64_decode($b64);
       $im = imageCreateFromString($bin);
      if (!$im) {
           
           $msg="there is error";
           if($lang='ar'){
            $msg='هناك خطأ ما.'   ;
           }
              $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();
      }
       $r=uniqid();
       $img_file = $folderPath . $r.$ext;
      $date=date("Y-m-d");
       imagepng($im, $img_file, 0);
        $pathImg='https://myfamous.app/uploads/'.$r.$ext;
        $db = new Db();
       $resultf = $db -> query("INSERT INTO `category` (`user_id`,`name`, `photo`, `status`,`date`)
      VALUES ($token,'$name', '$pathImg',$status,'$date')");
      
            
                      $msg = 'Add Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
           $msg = 'photo is required';
        if($lang=='ar'){
        $msg = 'photo  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();   
         } 
         
     }  
         
     }   
         
     }  
    break;
      case "editCategory":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         
        if(isset($_REQUEST['category_id'])){
        $category_id=$_POST['category_id']; 
        $token=$_REQUEST['token'];
        $name=$_REQUEST['name'];
        $status=$_REQUEST['status'];
     $vv = $db ->query("UPDATE `category` SET  `name` = '$name',status=$status WHERE `id` = $category_id");
 
      
                      $msg = 'Edit Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
       $msg = 'category_id is required';
        if($lang=='ar'){
        $msg = 'category_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     } 
         
       
         
     }   
         
      
    break;
          case "deleteCategory":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         
        if(isset($_REQUEST['category_id'])){
        $category_id=$_POST['category_id']; 
        $token=$_REQUEST['token'];
         $db = new Db();
      $result1 = $db ->query("DELETE FROM `category` WHERE  `id` =  $category_id ");
                      $msg = 'Delete Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
       $msg = 'category_id is required';
        if($lang=='ar'){
        $msg = 'category_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     } 
         
       
         
     }   
         
      
    break;
    case "categories":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
       $resultf = $db -> select("Select * from category  where  status=1 and user_id=".$id." order by id DESC ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
     case "category":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['category_id'])){
       $msg = 'category_id is required';
        if($lang=='ar'){
        $msg = 'category_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $category_id=$_REQUEST['category_id'];
    
        $db = new Db();
       $resultf = $db -> select("Select * from category  where status=1 and user_id=".$id." and id=".$category_id." order by id DESC ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
     }  
    break; 
    /////////////////////////////
     case "currencies":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
       $resultf = $db -> select("Select * from currencies  order by id ASC ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
    case "addProduct":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       if(!isset($_REQUEST['category_id'])){
       $msg = 'category_id is required';
        if($lang=='ar'){
        $msg = 'category_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['price'])){
       $msg = 'price is required';
        if($lang=='ar'){
        $msg = 'price  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['name'])){
       $msg = 'name is required';
        if($lang=='ar'){
        $msg = 'name  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['currency_name'])){
       $msg = 'currency_name is required';
        if($lang=='ar'){
        $msg = 'currency_name  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['status'])){
       $msg = 'status is required';
        if($lang=='ar'){
        $msg = 'status  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(isset($_REQUEST['photo'])){
        $b64=$_POST['photo']; 
        $ext=$_REQUEST['ext'];     
        $token=$_REQUEST['token'];
        $name=$_REQUEST['name'];
        $status=$_REQUEST['status'];
        $category_id=$_REQUEST['category_id'];
        $note=$_REQUEST['note'];
        $price=$_REQUEST['price'];
        $currency_name=$_REQUEST['currency_name'];
      //  $b64 = urlencode($b64);
        
        
       // print $b64;
    //    exit;
       
       
        $folderPath = "../uploads/";
        $bin = base64_decode($b64);
       $im = imageCreateFromString($bin);
      if (!$im) {
           
           $msg="there is error";
           if($lang='ar'){
            $msg='هناك خطأ ما.'   ;
           }
              $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();
      }
       $r=uniqid();
       $img_file = $folderPath . $r.$ext;
      $date=date("Y-m-d");
       imagepng($im, $img_file, 0);
        $pathImg='https://myfamous.app/uploads/'.$r.$ext;
        $db = new Db();
       $resultf = $db -> query("INSERT INTO `products` (`user_id`,`name`, `photo`, `status`,`date`,`category_id`,`note`,`currency`,`price`)
      VALUES ($token,'$name', '$pathImg',$status,'$date',$category_id,'$note','$currency_name','$price')");
      
            
                      $msg = 'Add Product Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
           $msg = 'photo is required';
        if($lang=='ar'){
        $msg = 'photo  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit();   
         } 
         
     }  
     }  
     }   
     }   
     }  
     }
    break;
    case "products":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from category WHERE category.id=products.category_id) as cat_name from products   where status<>3 and user_id=".$id." order by id DESC  ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
     
     
     
     
           case "deleteProduct":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         
        if(isset($_REQUEST['product_id'])){
        $product_id=$_POST['product_id']; 
        $token=$_REQUEST['token'];
         $db = new Db();
      $result1 = $db ->query("DELETE FROM `products` WHERE  `id` =  $product_id ");
                      $msg = 'Delete Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     } 
         
       
         
     }   
         
      
    break;
     case "editProduct":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         
        if(isset($_REQUEST['product_id'])){
        $product_id=$_POST['product_id']; 
        $token=$_REQUEST['token'];
        $name=$_REQUEST['name'];
        $category_id=$_REQUEST['category_id'];
        $note=$_REQUEST['note'];
        $currency=$_REQUEST['currency_name'];
        $price=$_REQUEST['price'];     
        $status=$_REQUEST['status'];
     $vv = $db ->query("UPDATE `products` SET  `name` = '$name',`category_id`=$category_id,`note`='$note',currency='$currency',price='$price',status=$status WHERE `id` = $product_id");
 
      
                      $msg = 'Edit Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     } 
         
       
         
     }   
         
      
    break;
    ////////////////////////////////
    case "addProductOffer":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       if(!isset($_REQUEST['product_id'])){
       $msg = 'product_id is required';
        if($lang=='ar'){
        $msg = 'product_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
          if(!isset($_REQUEST['price'])){
       $msg = 'price is required';
        if($lang=='ar'){
        $msg = 'price  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['title'])){
       $msg = 'title is required';
        if($lang=='ar'){
        $msg = 'title  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['currency_name'])){
       $msg = 'currency_name is required';
        if($lang=='ar'){
        $msg = 'currency_name  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        if(!isset($_REQUEST['description'])){
       $msg = 'Description is required';
        if($lang=='ar'){
        $msg = 'Description  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       if(!isset($_REQUEST['target_group'])){
       $msg = 'target_group is required';
        if($lang=='ar'){
        $msg = 'target_group  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['start_date'])){
       $msg = 'start_date is required';
        if($lang=='ar'){
        $msg = 'start_date  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         if(!isset($_REQUEST['end_date'])){
       $msg = 'end_date is required';
        if($lang=='ar'){
        $msg = 'end_date  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{
         
        $token=$_REQUEST['token'];
        $title=$_REQUEST['title'];
        $price=$_REQUEST['price'];
        $product_id=$_REQUEST['product_id'];
        
        $target_group=$_REQUEST['target_group'];
        $start_date=$_REQUEST['start_date'];
        $end_date=$_REQUEST['end_date'];
        $description=$_REQUEST['description'];
        $currency_name=$_REQUEST['currency_name'];
   
        $db = new Db();
        $date=date("Y-m-d");
       $resultf = $db -> query("INSERT INTO `offers` (`user_id`,`title`, `start_date`, `end_date`,`date`,`product_id`,`description`,`currency`,`price`,`target_group`)
      VALUES ($token,'$title', '$start_date','$end_date','$date',$product_id,'$description','$currency_name','$price',$target_group)");
      
            
                      $msg = 'Add Product Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
     }}     
     }  
         
     }  
     }  
     }   
     }   
     }  
     }
    break;
       case "editOffer":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         
        if(isset($_REQUEST['offer_id'])){
        $offer_id=$_POST['offer_id']; 
        $token=$_REQUEST['token'];
        $title=$_REQUEST['title'];
        $price=$_REQUEST['price'];
        $product_id=$_REQUEST['product_id'];
        
        $target_group=$_REQUEST['target_group'];
        $start_date=$_REQUEST['start_date'];
        $end_date=$_REQUEST['end_date'];
        $description=$_REQUEST['description'];
        $currency_name=$_REQUEST['currency_name'];
   
    $vv = $db ->query("UPDATE `offers` SET  `title` = '$title',`price`='$price',`target_group`='$target_group',`start_date`='$start_date',`end_date`='$end_date',product_id=$product_id,description='$description',currency='$currency_name' WHERE `id` = $offer_id");
 
      
                      $msg = 'Edit Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
       $msg = 'offer_id is required';
        if($lang=='ar'){
        $msg = 'offer_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     } 
         
       
         
     }   
         
      
    break;
     case "targetGroup":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
        $resultf=array();
       $resultf[0]['id'] =1; 
       $resultf[1]['id'] =2; 
       $resultf[2]['id'] =3;
       
       $resultf[0]['name'] ="Android"; 
       $resultf[1]['name'] ="IOS"; 
       $resultf[2]['name'] ="Both"; 
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
      case "offers":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        
        $id=$_REQUEST['token'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from products WHERE products.id=offers.product_id) as pro_name,(SELECT photo from products WHERE products.id=offers.product_id) as photo from offers  where user_id=".$id." order by id DESC ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
         
     }  
    break;
     case "offerData":
       $api = new MyAPICore();    
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
         if(!isset($_REQUEST['offer_id'])){
       $msg = 'offer_id is required';
        if($lang=='ar'){
        $msg = 'offer_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
        $id=$_REQUEST['token'];
        $offer_id=$_REQUEST['offer_id'];
    
        $db = new Db();
       $resultf = $db -> select("Select *,(SELECT name from products WHERE products.id=offers.product_id) as pro_name,(SELECT photo from products WHERE products.id=offers.product_id) as photo from offers  where id=".$offer_id." order by id DESC ");
      
            
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']='';
            $result['token']=$_REQUEST['token'];
            $result['data']=$resultf;
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
     }
     }  
    break; 
           case "deleteOffers":
       $api = new MyAPICore(); 
       $db = new Db();
        if(!isset($_REQUEST['token'])){
       $msg = 'token is required';
        if($lang=='ar'){
        $msg = 'token  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     }else{ 
       
         
        if(isset($_REQUEST['offer_id'])){
        $offer_id=$_POST['offer_id']; 
        $token=$_REQUEST['token'];
         $db = new Db();
      $result1 = $db ->query("DELETE FROM `offers` WHERE  `id` =  $offer_id ");
                      $msg = 'Delete Success .';
       
            $result['status']= true;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$token;
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); 
            
             
         }else{
       $msg = 'offer_id is required';
        if($lang=='ar'){
        $msg = 'offer_id  مطلوب';
         }
            
            $result['status']= false;
            $result['http_code']= (int) 200;
            $result['msg']=$msg;
            $result['token']=$_REQUEST['token'];
            $result['data']=array();
            print json_encode($result,JSON_UNESCAPED_UNICODE); exit(); 
     } 
         
       
         
     }   
         
      
    break;
    
    
    
    

 

 case "addComments":

 $user_id = $_REQUEST['token'];
 $post_id = $_REQUEST['post_id'];
 $body = $_REQUEST['body'];
  $date=date("Y-m-d H:i:s");

  $db = new Db();
   $result1 = $db ->query("INSERT INTO `comments`( `post_id`, `user_id`, `body`, `status`, `date`) VALUES ( $post_id, $user_id, '$body',1, '$date')");

 


// send note sendNote($user_id,$type,$device_id,$title,$body,$data);




$comm_arr = $db -> select("SELECT *  FROM `comments` WHERE  `post_id` = $post_id AND `user_id` !=  $user_id ORDER BY `id` DESC" );


for($i=0; $i<count($comm_arr); $i++){

  $c_c_user_id = $comm_arr[$i]['user_id'];

  $dev_arr = $db -> select("SELECT *  FROM `device_fcm` WHERE  `user_id` = $c_c_user_id AND `os` ='android' ORDER BY `id` DESC");
  if(count($dev_arr)>0)
  sendNote($c_c_user_id,'comments',$dev_arr[0]['code'],"New comment","You have a new comment for post you active on it before",array('post_id'=>$post_id,'type'=>'post'));
  

  $dev_arr2 = $db -> select("SELECT *  FROM `device_fcm` WHERE  `user_id` = $c_c_user_id AND `os` ='ios' ORDER BY `id` DESC");
  if(count($dev_arr2)>0)
  sendNote($c_c_user_id,'comments',$dev_arr2[0]['code'],"New comment","You have a new comment for post you active on it before",array('post_id'=>$post_id,'type'=>'post'));

}


////////////



 
if($result1){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array();

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;



case "getWeather":

 $user_id = $_REQUEST['token'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 
 
 $json = file_get_contents("https://api.openweathermap.org/data/2.5/weather?units=metric&lat=$lat&lon=$lon&appid=cbc8e0bc9de09525cb3ad888e5ec6b01");
 $arr_json = json_decode($json);
  $date=date("Y-m-d H:i:s");

  $db = new Db();
   $result1 = $db ->query("DELETE FROM `posts` WHERE  `id` =  $post_id  AND `user_id` =  $user_id");
 
if($lat and $lon){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']=' ';
$result['data']=$arr_json;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;

case "deletePost":

 $user_id = $_REQUEST['token'];
 $post_id = $_REQUEST['post_id'];

 
  $date=date("Y-m-d H:i:s");

  $db = new Db();
   $result1 = $db ->query("DELETE FROM `posts` WHERE  `id` =  $post_id  AND `user_id` =  $user_id");
 
if($result1){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='Your Post has been deleted successfully ';
$result['data']=array();

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;


case "checkInHome":

 $user_id = $_REQUEST['token'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 
 //$google = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon&key=AIzaSyDEf2RXBZKGFyDFoTxWLdK-mjfC1tYKKsA&language=$lang");
 

 $google = file_get_contents("https://maps.googleapis.com/maps/api/place/search/json?location=$lat,$lon&radius=5000&sensor=false&key=AIzaSyDEf2RXBZKGFyDFoTxWLdK-mjfC1tYKKsA&language=$lang");
 $json = json_decode($google);
if($json){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']=' ';
$result['data']=$json;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;





case "checkInAction":

 $user_id = $_REQUEST['token'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 
 $google = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon&key=AIzaSyDEf2RXBZKGFyDFoTxWLdK-mjfC1tYKKsA&language=$lang");
 

 $json = json_decode($google);
if($json){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']=' ';
$result['data']=$json;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;


case "searchCheckIn":

 $user_id = $_REQUEST['token'];

 $q = urlencode($_REQUEST['q']);
 
 //$google = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon&key=AIzaSyDEf2RXBZKGFyDFoTxWLdK-mjfC1tYKKsA&language=$lang");
 


 $google = file_get_contents("https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=$q&inputtype=textquery&fields=photos,formatted_address,name,rating,opening_hours,geometry&key=AIzaSyDEf2RXBZKGFyDFoTxWLdK-mjfC1tYKKsA");
 $json = json_decode($google);
if($json){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']=' ';
$result['data']=$json;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;




case "calculate_points":

 $user_id = $_REQUEST['token'];
 $lat1 = $_REQUEST['lat1'];
 $lat2 = $_REQUEST['lat2'];
 $lon1 = $_REQUEST['lon1'];
 $lon2 = $_REQUEST['lon2'];

 $q = urlencode($_REQUEST['q']);
 
 //$google = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lon&key=AIzaSyDEf2RXBZKGFyDFoTxWLdK-mjfC1tYKKsA&language=$lang");
 



 $json = array('d'=>rand(3,6));
if($json){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']=' ';
$result['data']=$json;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;



case "deleteStory":

 $user_id = $_REQUEST['token'];
 $story_id = $_REQUEST['story_id'];

 
  $date=date("Y-m-d H:i:s");

  $db = new Db();
   $result1 = $db ->query("DELETE FROM `stories` WHERE  `id` =  $story_id  AND `user_id` =  $user_id");
 
if($result1){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array();

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;


case "whosAreByType":

 $user_id = $_REQUEST['token'];
 $type = $_REQUEST['type'];
 $ref_id = $_REQUEST['ref_id'];

 
  $date=date("Y-m-d H:i:s");

  $db = new Db();
  $test = false;

  $result1= array();

  if($type=='stars_post'){
  $result1 = $db -> select("SELECT *  FROM `posts_star` WHERE  `post_id` = $ref_id");
  $test = true;
  }

  if($type=='profile_view'){
    $result1 = $db -> select("SELECT *  FROM `posts_star` WHERE  `post_id` = $ref_id");
    $test = true;
  }

  if($type=='stories_view'){
    $result1 = $db -> select("SELECT *  FROM `posts_star` WHERE  `post_id` = $ref_id");
    $test = true;
  }

    $temp_arr = array();

   

  for($i=0 ; $i < count($result1); $i++){

    $u_id = $result1[$i]['user_id'];
    $user_arr = $db -> select("SELECT * FROM `users` WHERE  `id` = $u_id");

    if(count($user_arr)>0)
    array_push($temp_arr,$user_arr[0]);

  }


 
if($test){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=$temp_arr;

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Missing  Type';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;




case "addDeviceToken":

 $user_id = $_REQUEST['token'];
 $fcm__device_token = $_REQUEST['fcm__device_token'];
 $os = $_REQUEST['os'];
 $date=date("Y-m-d H:i:s");

  $db = new Db();
  $result1 = $db ->query("INSERT INTO `device_fcm`( `user_id`, `os`, `code`, `status`, `date`) VALUES ( $user_id, '$os', '$fcm__device_token', 1, '$date' )");
 
if($result1){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array();

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;




case "addUserLocation":

 $user_id = $_REQUEST['token'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 $date=date("Y-m-d H:i:s");

  $db = new Db();
  $result1 = $db ->query("INSERT INTO `users_gps`( `user_id`, `lat`, `lon`, `status`, `date`) 
  VALUES ($user_id,'$lat','$lon',1,'$date' )");
 
if($result1){
 
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array();

 
}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);
 


break;

 


case "starPost":

$user_id = $_REQUEST['token'];
$post_id = $_REQUEST['post_id'];
$test = false;
$isStar = 2;
 $date=date("Y-m-d H:i:s");

 $db = new Db();

 $check_star_arr = $db -> select("SELECT *  FROM `posts_star` WHERE  `post_id` = $post_id AND  `user_id` = $user_id ");

  if(count($check_star_arr)>0){

    $result1 = $db ->query("DELETE FROM `posts_star` WHERE  `post_id` = $post_id  AND  `user_id` = $user_id ");
    $test = true;
    $isStar = 0;
  }
  else{
  $result1 = $db ->query("INSERT INTO `posts_star`( `post_id`, `user_id`, `date`) VALUES ( $post_id, $user_id, '$date')");
  $test = true;
  $isStar = 1;
  }

if($test){

$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array('isStar'=> $isStar);


}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array('isStar'=> $isStar);
}
print json_encode($result,JSON_UNESCAPED_UNICODE);



break;





case "followUser":

$user_id = $_REQUEST['token'];
$following = $_REQUEST['following_id'];
$test = false;
$isStar = 2;
 $date=date("Y-m-d H:i:s");

 $db = new Db();

 $check_star_arr = $db -> select("SELECT *  FROM `following` WHERE  `following_user_id` = $following AND  `user_id` = $user_id ");

  if(count($check_star_arr)>0){

    $result1 = $db ->query("DELETE FROM `following` WHERE  `following_user_id` = $following AND  `user_id` = $user_id ");
    $test = true;
    $isStar = 0;
  }
  else{
  $result1 = $db ->query("INSERT INTO `following`( `user_id`, `following_user_id`,`status`, `date`) VALUES ( $user_id, $following,1, '$date')");
  $test = true;
  $isStar = 1;
  }

if($test){

$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array('isfollowing'=> $isStar);


}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array('isfollowing'=> $isStar);

}
print json_encode($result,JSON_UNESCAPED_UNICODE);



break;





case "addFindMe":

$user_id = $_REQUEST['token'];
$adding_user_id = $_REQUEST['adding_user_id'];
$type = $_REQUEST['type'];
$test = false;
$isStar = 2;
 $date=date("Y-m-d H:i:s");

 $db = new Db();

 $check_star_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $adding_user_id AND  `user_id` = $user_id AND `type` = $type ");

  if(count($check_star_arr)>0){

    $result1 = $db ->query("DELETE FROM `groups` WHERE  `adding_user_id` = $adding_user_id AND  `user_id` = $user_id AND `type` = $type ");
    $test = true;
    $isStar = 0;
  }
  else{
        $result1 = $db ->query("INSERT INTO `groups`( `user_id`, `adding_user_id`, `type`, `status`, `date`) VALUES ($user_id,$adding_user_id,$type,1,'$date')");

//  $result1 = $db ->query("INSERT INTO `groups`( `user_id`, `following_user_id`,`status`, `date`) VALUES ( $user_id, $following,1, '$date')");
  $test = true;
  $isStar = 1;
  }

if($test){

$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array('inGroup'=> $isStar);


}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array('inGroup'=> $isStar);

}
print json_encode($result,JSON_UNESCAPED_UNICODE);



break;



case "addUser":

$user_id = $_REQUEST['token'];
$following = $_REQUEST['friend_id'];
$test = false;
$isStar = 2;
 $date=date("Y-m-d H:i:s");

 $db = new Db();

 $check_star_arr = $db -> select("SELECT *  FROM `following` WHERE  `following_user_id` = $following AND  `user_id` = $user_id ");

  if(count($check_star_arr)>0){

    $result1 = $db ->query("DELETE FROM `following` WHERE  `following_user_id` = $following AND  `user_id` = $user_id ");
    $test = true;
    $isStar = 0;
  }
  else{
  $result1 = $db ->query("INSERT INTO `following`( `user_id`, `following_user_id`,`status`, `date`) VALUES ( $user_id, $following,1, '$date')");
  $test = true;
  $isStar = 1;
  }

if($test){

$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=array('isFriends'=> $isStar);


}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array('isFriends'=> $isStar);

}
print json_encode($result,JSON_UNESCAPED_UNICODE);



break;


case "acceptRequest":
    
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Internal Server Error 500';
$result['data']=array();

print json_encode($result,JSON_UNESCAPED_UNICODE);
break;
 
  case "myStories":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();

$user_id = $_REQUEST['token'];

 $db = new Db();

    $rows = $db -> select("SELECT * FROM `stories` WHERE  `user_id` = $user_id");

    if(count($rows)>0){
        
        $result = array();
        $result['status']= true;
        $result['http_code']= (int) 200;
        $result['msg']='';
        $result['data']=$rows;
        $result['token']=$user_id;
        
    }




print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;


 
 
 case "UserListByCountry":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $country_id = $_REQUEST['country_id'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id  limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;

    $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    

    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;



case "UserList":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $country_id = $_REQUEST['country_id'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id  limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
     $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    


    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;





case "getUsersListInMap":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $group_id = $_REQUEST['group_id'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 $r = $_REQUEST['r'];
 $in_your_city = $_REQUEST['in_your_city'];
 $country = $_REQUEST['in_your_country'];
 
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id AND `userType` = 6  limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
     $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    



    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
  
  
  
  
  
  

case "getUsersListInGroupID":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $group_id = $_REQUEST['group_id'];
 $lat = $_REQUEST['lat'];
 $lon = $_REQUEST['lon'];
 $r = $_REQUEST['r'];
 $in_your_city = $_REQUEST['in_your_city'];
 $country = $_REQUEST['in_your_country'];
 
 
  $db = new Db();
  
  $userList = array();
  
  $groups_array = $db -> select("SELECT *   FROM `groups` where `user_id` =  $user_id AND `type` = $group_id  limit 100");
   array_push($userList, 0);
  for($i=0;$i<count($groups_array);$i++){
      
      $c_user_id = $groups_array[$i]['adding_user_id'];
      array_push($userList, $c_user_id);
  }

  $x= rand(1,6);
 
     $rows = $db -> select("SELECT *  FROM `users` WHERE `id` IN (" . implode(',', array_map('intval', $userList)) . ")");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
     $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    



    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;




case "UserListVIP":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $country_id = $_REQUEST['country_id'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id AND `userType` = 6  limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
     $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    



    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;




case "BrandList":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $country_id = $_REQUEST['country_id'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id AND `userType` = 4  limit 100");

     $temp_arr = array();
     $re = array();
     
     
     

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
     $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    

    

    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
     $temp_arr[$i]['brand_category_id'] =  1;
     $temp_arr[$i]['brand_category_name'] =  'Store';
     $temp_arr[$i]['brand_have_mneu'] =  'no';
     $temp_arr[$i]['brand_website'] =  'http://';
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;




case "UserListGuest":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $country_id = $_REQUEST['country_id'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id AND `userType` = 5  limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
    
      $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    




    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
  
  
  
  case "UserListFamous":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $country_id = $_REQUEST['country_id'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users` where `id` !=  $user_id AND `userType` = 2  limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *   FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
    
      $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    



    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;


  case "usersSearch":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $q = $_REQUEST['q'];
 
  $db = new Db();

  $x= rand(222,2222);
 
     $rows = $db -> select("SELECT *   FROM `users`     limit 100");

     $temp_arr = array();
     $re = array();

 for($i=0;$i<count($rows);$i++){
     
     
    $f_u_id = $rows[$i]['id'];
    $following = $db -> select("SELECT *  FROM `following` where `user_id` =  $user_id  and `following_user_id` = $f_u_id");
    
    $you_follow = 0;

    if(count($following)>0) $you_follow = 1;
    
        $check_groups_arr = $db -> select("SELECT *  FROM `groups` WHERE  `adding_user_id` = $f_u_id AND  `user_id` = $user_id  ");
    $findMeGroupType=array();
    
    for($ii=0;$ii<count($check_groups_arr);$ii++){
        
        $type_in = $check_groups_arr[$ii]['type'];
        array_push($findMeGroupType,$type_in);
    }
    //$temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    



    $temp_arr[$i]['followers'] = 0;
    $temp_arr[$i]['following_by_you'] = $you_follow;
    $temp_arr[$i]['findMeGroupType'] = implode(',', $findMeGroupType);
    $temp_arr[$i]['user_id'] = $rows[$i]['id'];
    $temp_arr[$i]['username'] = $rows[$i]['username'];
    $temp_arr[$i]['email'] =  $rows[$i]['email'];
    $temp_arr[$i]['photo'] =  $rows[$i]['photo'];
    $temp_arr[$i]['userType'] =  $rows[$i]['userType'];
    $temp_arr[$i]['status'] =  $rows[$i]['status'];
    $temp_arr[$i]['date'] =  $rows[$i]['date'];
    array_push($re,$temp_arr[$i]);
 }

 
     if(count($re)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$re;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
  
 


 case "countryList":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();

$user_id = $_REQUEST['token'];

 $db = new Db();

    $rows = $db -> select("SELECT * FROM `apps_countries` WHERE  `status` = 1");

    if(count($rows)>0){
        
        $result = array();
        $result['status']= true;
        $result['http_code']= (int) 200;
        $result['msg']='';
        $result['data']=$rows;
        $result['token']=$user_id;
        
    }




print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;



 case "myNotifications":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 
  $db = new Db();
 
     $rows = $db -> select("SELECT `user_id`,`title`,`body`,`type`,`date` FROM `notifications` WHERE  `user_id` = $user_id");
 
     if(count($rows)>0){
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$rows;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
 

 case "sendFCM":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();

$user_id = $_REQUEST['token'];
$device_fcm = $_REQUEST['device_fcm'];

//require 'fcm.class.php'; 


//$FCMPushNotification = new \BD\FCMPushNotification('AAAAjX1lE1M:APA91bEi35IgXTOAqS_688vbU4qD68v1-omWsJVngdHsyAMaR82v8mK9TVqPjVtq5L2MLXRLiKn1-_9SMOnbbRORfKL1zziG6X6snuyhmwg4oMGkoerWiOAyh1-HplCDsBukYWob2HLb');

$sDeviceToken = $device_fcm;

$aPayload = array(
  'data' => array("test"=>123),
  'notification' => array(
    'title' => 'Example app',
    'body'=> 'This is an example message',
    'sound'=> 'default'
  )
);

$aOptions = array(
  'time_to_live' => 0 //means messages that can't be delivered immediately are discarded. 
);

$aResult = $FCMPushNotification->sendToDevice(
  $sDeviceToken,		
  $aPayload,
  $aOptions // optional
);


$result = array();
$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=$aResult;
$result['token']=$user_id;




print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;
 
 
 
 
 
  case "addPost":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();


$user_id = $_REQUEST['token'];
//$photo = $_REQUEST['photo'];
$type = $_REQUEST['type'];
$body = $_REQUEST['body'];


  $up = uploadImage($_FILES);


      if($up == false)
      $photo="https://myfamous.app/api/uploads/noc.png";
      else {
        $photo = $up;
      }
    
    

     $date=date("Y-m-d H:i:s");
$db = new Db();
      $result1 = $db ->query("INSERT INTO `posts`(`user_id`, `photo`, `type`, `body`, `status`, `date`)
      VALUES ($user_id, '$photo','$type','$body',1,'$date')");

$last_id2 = mysqli_insert_id($db->getCon());

if($type=='v'){
  $last_id = mysqli_insert_id($db->getCon());
  $vv = $db ->query("INSERT INTO `videos`( `post_id`, `type`, `v_url`, `status`, `date`) VALUES ($last_id,'post','$photo',1,'$date')");
  
  $vv = $db ->query("UPDATE `posts` SET  `photo` = 'https://image.freepik.com/free-vector/blank-purple-patterned-background-vector_53876-77835.jpg' WHERE `id` = $last_id");
 }
    
    
    if($result1 ){
        
        $result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='Post has beed added successfully!';
$result['data']=array();
        
    }







    // send note sendNote($user_id,$type,$device_id,$title,$body,$data);




$comm_arr = $db -> select("SELECT *  FROM `following` WHERE  `user_id` = $user_id ");


for($i=0; $i<count($comm_arr); $i++){

  $c_c_user_id = $comm_arr[$i]['following_user_id'];

  $dev_arr = $db -> select("SELECT *  FROM `device_fcm` WHERE  `user_id` = $c_c_user_id AND `os` ='android' ORDER BY `id` DESC");
  if(count($dev_arr)>0)
  sendNote($c_c_user_id,'comments',$dev_arr[0]['code'],"New Post","Your famous add new post",array('post_id'=>$post_id));
  

  $dev_arr2 = $db -> select("SELECT *  FROM `device_fcm` WHERE  `user_id` = $c_c_user_id AND `os` ='ios' ORDER BY `id` DESC");
  if(count($dev_arr2)>0)
  sendNote($c_c_user_id,'comments',$dev_arr2[0]['code'],"New comment","You have a new comment for post you active on it before",array('post_id'=>$last_id2));

}


////////////
    

print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;

 
 case "showPost":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 
 $user_id = $_REQUEST['token'];
 $post_id = $_REQUEST['post_id'];
 
 $db = new Db();

 $post_arr = $db -> select("SELECT * FROM `posts` WHERE  `id` = $post_id");



 if($post_arr[0]['type']=='v'){
  $pp_idd =  $post_arr[0]['id'];
  $vv = $db -> select("SELECT * FROM `videos` WHERE  `post_id` = $pp_idd AND `type`='post'");
  if(count($vv)>0)
  $post_arr[0]['photo']=$vv[0]['v_url'];

 }

 $pp_id =  $post_arr[0]['id'];
 $isStar = $db -> select("SELECT *  FROM `posts_star` WHERE `user_id`  = $user_id AND `post_id` = $pp_id");

 if(count($isStar)>0)
 $post_arr[0]['isStar'] = true ;
 else
 $post_arr[0]['isStar'] = false ;

 $postComments2 = $db -> select("SELECT *  FROM `comments` WHERE   `post_id` = $pp_id ORDER BY `id` DESC");

 $postStars2 = $db -> select("SELECT *  FROM `posts_star` WHERE   `post_id` = $pp_id");
$usrid = $post_arr[0]['user_id'];
 $user_arrr = $db -> select("SELECT *  FROM `users` WHERE   `id` = $usrid");

 $post_arr[0]['comments_count'] = (int) count($postComments2) ;
 $post_arr[0]['stars_count'] = (int) count($postStars2) ;

 $post_arr[0]['user_obj'] =$user_arrr[0] ;

 $postComments = $db -> select("SELECT *  FROM `comments` WHERE   `post_id` = $pp_id ORDER BY `id` DESC");

$temp_arr = array();

 for($i=0;$i<count($postComments);$i++){

    $user_id = $postComments[$i]['user_id'];
    $user_arr = $db -> select("SELECT *  FROM `users` WHERE   `id` = $user_id");
    $postComments[$i]['user_obj']=$user_arr[0];

    array_push($temp_arr,$postComments[$i]);
 }

 $post_arr[0]['comments_count'] = (int) count($postComments) ;
 $post_arr[0]['comments'] = $temp_arr ;
     
     if(count($post_arr)>0 and $post_id>0){
         
         $result['status']= true;
 $result['http_code']= (int) 200;
 $result['msg']='';
 $result['data']= $post_arr[0];
         
     }

     
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
 

 case "addStory":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 
 $user_id = $_REQUEST['token'];
 //$photo = $_REQUEST['photo'];
 $type = $_REQUEST['type'];
 $body = $_REQUEST['body'];
      $date=date("Y-m-d H:i:s");
      
      
       $up = uploadImage($_FILES);


      if($up == false)
      $photo="https://myfamous.app/api/uploads/noc.png";
      else {
        $photo = $up;
      }
    
 $db = new Db();
       $result1 = $db ->query("INSERT INTO `stories`(`user_id`, `type`, `url`, `status`,  `date`)
       VALUES ($user_id, '$type','$photo',1,'$date')");

       if($type=='v'){
        $last_id = mysqli_insert_id($db->getCon());
        $vv = $db ->query("INSERT INTO `videos`( `post_id`, `type`, `v_url`, `status`, `date`) VALUES ($last_id,'story','$photo',1,'$date')");
        
        $vv = $db ->query("UPDATE `stories` SET  `url` = 'https://image.freepik.com/free-vector/blank-purple-patterned-background-vector_53876-77835.jpg' WHERE `id` = $last_id");
       }
     
     
     if($result1){
         
         $result['status']= true;
 $result['http_code']= (int) 200;
 $result['msg']='';
 $result['data']=array();
         
     }
     
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
  
 
  case "forgetPassword":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();


$user_id = $_REQUEST['token'];
$email = $_REQUEST['email'];

    
    if($email){
        
        $result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='Email Sent!';
$result['data']=array();
        
    }
    

print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;


 case "updatePhotoProfile":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();


$user_id = $_REQUEST['token'];


    
$up = uploadImage($_FILES);


if($up == false)
$photo="https://myfamous.app/api/uploads/no.png";
else {
  $photo = $up;
}



$date=date("Y-m-d H:i:s");

$db = new Db();
$result1 = $db ->query("UPDATE `users` SET `photo`='$photo' WHERE `id` = $user_id");
$profile_arr = $db -> select("SELECT * FROM `profile` WHERE  `user_id` = $user_id");
$user_arr = $db -> select("SELECT * FROM `users` WHERE  `id` = $user_id");


$user_arr[0]['name']=$profile_arr[0]['name'];
$user_arr[0]['bio']=$profile_arr[0]['bio'];;

if($result1){

$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=$user_arr[0];


}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);


 break;




 case "updateProfile":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();


$user_id = $_REQUEST['token'];
$name =  $_REQUEST['name'];
$bio =  $_REQUEST['bio'];
    


$date=date("Y-m-d H:i:s");

$db = new Db();
$result1 = $db ->query("UPDATE `profile` SET `name`='$name',  `bio`='$bio'  WHERE `user_id` = $user_id");

$user_arr = $db -> select("SELECT * FROM `users` WHERE  `id` = $user_id");


$user_arr[0]['name']=$name;
$user_arr[0]['bio']=$bio;

if($result1){

$result['status']= true;
$result['http_code']= (int) 200;
$result['msg']='';
$result['data']=$user_arr[0];


}
else {
$result['status']= false;
$result['http_code']= (int) 500;
$result['msg']='Please post all required data';
$result['data']=array();
}
print json_encode($result,JSON_UNESCAPED_UNICODE);


 break;
 
 



 case "lookUpData1":


 
 
 $user_id = $_REQUEST['token'];

 $data = array();
 
 
  $data['appSetting'] = array(

  'radius' => '50'
 );
 

 $data['about'] = array(

  'title' => 'About my famous',
  'body' => 'famous famousfamous famousfamous famousfamous famousfamous famousfamous famousfamous famous'
 );
 
 
 $data['RETURN_POLICY'] = array(

  'title' => 'RETURN POLICY',
  'body' => "
  
Thank you for your purchase. We hope you are happy with your purchase. However, if you are not completely satisfied with your purchase for any reason, you may return it to us for a full refund, store credit, or an exchange. Please see below for more information on our return policy.

RETURNS

All returns must be postmarked within seven (7) days of the purchase date. All returned items must be in new and unused condition, with all original tags and labels attached.

RETURN PROCESS

To return an item, please email customer service at myfamoususa@gmail.com to obtain a Return Merchandise Authorization (RMA) number. After receiving a RMA number, place the item securely in its original packaging and The item and Proof of purchase, and mail your return to the following address:

To the same place
Attn: Returns
RMA #
3303 Durbin Pl  
Falls Church, VA 22041  
United States  


REFUNDS

After receiving your return and inspecting the condition of your item, we will process your return or exchange. Please allow at least seven (7) days from the receipt of your item to process your return or exchange. Refunds may take 1-2 billing cycles to appear on your credit card statement, depending on your credit card company. We will notify you by email when your return has been processed.

EXCEPTIONS    

The following items cannot be returned or exchanged: 
 
     ●     Used Items   
     ●     Damaged   
     ●     Opened     

For defective or damaged products, please contact us at the customer service number below to arrange a refund or exchange. 
 
Please Note
           
     ●     Refund Code   

QUESTIONS

If you have any questions concerning our return policy, please contact us at:
202-999-5959 
myfamoususa@gmail.com 
  
  "
 );
 
 $data['PRIVACY_POLICY'] = array(

  'title' => 'PRIVACY POLICY',
  'body' => "
  
  
Thank you for choosing to be part of our community at My Famous Inc (“Company”, “we”, “us”, or “our”). We are committed to protecting your personal information and your right to privacy. If you have any questions or concerns about our policy, or our practices with regards to your personal information, please contact us at Myfamoususa@gmail.com.

When you visit our mobile application, and use our services, you trust us with your personal information. We take your privacy very seriously. In this privacy policy, we seek to explain to you in the clearest way possible what information we collect, how we use it and what rights you have in relation to it. We hope you take some time to read through it carefully, as it is important. If there are any terms in this privacy policy that you do not agree with, please discontinue use of our Apps and our services.

This privacy policy applies to all information collected through our mobile application, , and/or any related services, sales, marketing or events (we refer to them collectively in this privacy policy as the ).

Please read this privacy policy carefully as it will help you make informed decisions about sharing your personal information with us.



TABLE OF CONTENTS

1. WHAT INFORMATION DO WE COLLECT?

2. HOW DO WE USE YOUR INFORMATION?

3. WILL YOUR INFORMATION BE SHARED WITH ANYONE?

4. DO WE USE COOKIES AND OTHER TRACKING TECHNOLOGIES?

5. DO WE USE GOOGLE MAPS?

6. HOW DO WE HANDLE YOUR SOCIAL LOGINS?

7. WHAT IS OUR STANCE ON THIRD-PARTY WEBSITES?

8. HOW LONG DO WE KEEP YOUR INFORMATION?

9. HOW DO WE KEEP YOUR INFORMATION SAFE?

10. DO WE COLLECT INFORMATION FROM MINORS?

11. WHAT ARE YOUR PRIVACY RIGHTS?

12. DATA BREACH

13. CONTROLS FOR DO-NOT-TRACK FEATURES

14. DO CALIFORNIA RESIDENTS HAVE SPECIFIC PRIVACY RIGHTS?

15. DO WE MAKE UPDATES TO THIS POLICY?

16. HOW CAN YOU CONTACT US ABOUT THIS POLICY?



1. WHAT INFORMATION DO WE COLLECT?


Personal information you disclose to us
In Short:  We collect personal information that you provide to us.

We collect personal information that you voluntarily provide to us when registering at the Apps, expressing an interest in obtaining information about us or our products and services, when participating in activities on the Apps (such as posting messages in our online forums or entering competitions, contests or giveaways) or otherwise contacting us.

The personal information that we collect depends on the context of your interactions with us and the Apps, the choices you make and the products and features you use. The personal information we collect can include the following:

Publicly Available Personal Information. We collect first name, maiden name, last name, and nickname; ID; passport number; current and former address; phone numbers; email addresses; business email; business phone number; criminal records and mugshots; social media; Instagram, facebook and Twitter (URL); voter registration; and other similar data. 

Personal Information Provided by You. We collect data held by a hospital or doctor; data about health, DNA, medical records, FitBit, and similar apps; app usage; CV and other job application data such as background checks; passwords; and other similar data.

Payment Data. We collect data necessary to process your payment if you make purchases, such as your payment instrument number (such as a credit card number), and the security code associated with your payment instrument. All payment data is stored by __________. You may find their privacy policy link(s) here: __________.

Social Media Login Data. We may provide you with the option to register using social media account details, like your Facebook, Twitter or other social media account. If you choose to register in this way, we will collect the Information described in the section called below.

All personal information that you provide to us must be true, complete and accurate, and you must notify us of any changes to such personal information.


Information automatically collected

In Short:   Some information — such as IP address and/or browser and device characteristics — is collected automatically when you visit our Apps.

We automatically collect certain information when you visit, use or navigate the Apps. This information does not reveal your specific identity (like your name or contact information) but may include device and usage information, such as your IP address, browser and device characteristics, operating system, language preferences, referring URLs, device name, country, location, information about how and when you use our Apps and other technical information. This information is primarily needed to maintain the security and operation of our Apps, and for our internal analytics and reporting purposes.

Like many businesses, we also collect information through cookies and similar technologies.

Online Identifiers. We collect devices; applications; tools and protocols, such as IP (Internet Protocol) addresses; cookie identifiers, or others such as the ones used for analytics and marketing; Radio Frequency Identification (RFID) tags; device's geolocation; Vaccine Covid 19 QR ; and other similar data.


Information collected through our Apps

In Short:   We may collect information regarding your geo-location, push notifications, when you use our apps.

If you use our Apps, we may also collect the following information:
Geo-Location Information. We may request access or permission to and track location-based information from your mobile device, either continuously or while you are using our mobile application, to provide location-based services. If you wish to change our access or permissions, you may do so in your device's settings.

Push Notifications. We may request to send you push notifications regarding your account or the mobile application. If you wish to opt-out from receiving these types of communications, you may turn them off in your device's settings.



2. HOW DO WE USE YOUR INFORMATION?

In Short:  We process your information for purposes based on legitimate business interests, the fulfillment of our contract with you, compliance with our legal obligations, and/or your consent.

We use personal information collected via our Apps for a variety of business purposes described below. We process your personal information for these purposes in reliance on our legitimate business interests, in order to enter into or perform a contract with you, with your consent, and/or for compliance with our legal obligations. We indicate the specific processing grounds we rely on next to each purpose listed below.

We use the information we collect or receive:

To facilitate account creation and logon process. If you choose to link your account with us to a third party account (such as your Google or Facebook account), we use the information you allowed us to collect from those third parties to facilitate account creation and logon process for the performance of the contract. See the section below headed for further information.

To send you marketing and promotional communications. We and/or our third party marketing partners may use the personal information you send to us for our marketing purposes, if this is in accordance with your marketing preferences. You can opt-out of our marketing emails at any time (see the below).

Deliver targeted advertising to you. We may use your information to develop and display content and advertising (and work with third parties who do so) tailored to your interests and/or location and to measure its effectiveness.

Request Feedback. We may use your information to request feedback and to contact you about your use of our Apps.

To protect our Services. We may use your information as part of our efforts to keep our Apps safe and secure (for example, for fraud monitoring and prevention).

To enable user-to-user communications. We may use your information in order to enable user-to-user communications with each user's consent.

To enforce our terms, conditions and policies for Business Purposes, Legal Reasons and Contractual.

To respond to legal requests and prevent harm. If we receive a subpoena or other legal request, we may need to inspect the data we hold to determine how to respond.

To manage user accounts. We may use your information for the purposes of managing our account and keeping it in working order.
                         
To deliver services to the user. We may use your information to provide you with the requested service.

To respond to user inquiries/offer support to users. We may use your information to respond to your inquiries and solve any potential issues you might have with the use of our Services.

For other Business Purposes. We may use your information for other Business Purposes, such as data analysis, identifying usage trends, determining the effectiveness of our promotional campaigns and to evaluate and improve our Apps, products, marketing and your experience. We may use and store this information in aggregated and anonymized form so that it is not associated with individual end users and does not include personal information. We will not use identifiable personal information without your consent.


3. WILL YOUR INFORMATION BE SHARED WITH ANYONE?

In Short:  We only share information with your consent, to comply with laws, to provide you with services, to protect your rights, or to fulfill business obligations.

We may process or share data based on the following legal basis:
Consent: We may process your data if you have given us specific consent to use your personal information in a specific purpose.

Legitimate Interests: We may process your data when it is reasonably necessary to achieve our legitimate business interests.

Performance of a Contract: Where we have entered into a contract with you, we may process your personal information to fulfill the terms of our contract.

Legal Obligations: We may disclose your information where we are legally required to do so in order to comply with applicable law, governmental requests, a judicial proceeding, court order, or legal process, such as in response to a court order or a subpoena (including in response to public authorities to meet national security or law enforcement requirements).

Vital Interests: We may disclose your information where we believe it is necessary to investigate, prevent, or take action regarding potential violations of our policies, suspected fraud, situations involving potential threats to the safety of any person and illegal activities, or as evidence in litigation in which we are involved.
More specifically, we may need to process your data or share your personal information in the following situations:


Business Transfers. We may share or transfer your information in connection with, or during negotiations of, any merger, sale of company assets, financing, or acquisition of all or a portion of our business to another company.

Third-Party Advertisers. We may use third-party advertising companies to serve ads when you visit the Apps. These companies may use information about your visits to our Website(s) and other websites that are contained in web cookies and other tracking technologies in order to provide advertisements about goods and services of interest to you.

Business Partners. We may share your information with our business partners to offer you certain products, services or promotions.

Other Users. When you share personal information (for example, by posting comments, contributions or other content to the Apps) or otherwise interact with public areas of the Apps, such personal information may be viewed by all users and may be publicly distributed outside the Apps in perpetuity. If you interact with other users of our Apps and register through a social network (such as Facebook), your contacts on the social network will see your name, profile photo, and descriptions of your activity. Similarly, other users will be able to view descriptions of your activity, communicate with you within our Apps, and view your profile.

Offer Wall. Our Apps may display a third-party hosted “offer wall.” Such an offer wall allows third-party advertisers to offer virtual currency, gifts, or other items to users in return for acceptance and completion of an advertisement offer. Such an offer wall may appear in our mobile application and be displayed to you based on certain data, such as your geographic area or demographic information. When you click on an offer wall, you will leave our mobile application. A unique identifier, such as your user ID, will be shared with the offer wall provider in order to prevent fraud and properly credit your account.


4. DO WE USE COOKIES AND OTHER TRACKING TECHNOLOGIES?
In Short:  We may use cookies and other tracking technologies to collect and store your information.

We may use cookies and similar tracking technologies (like web beacons and pixels) to access or store information. Specific information about how we use such technologies and how you can refuse certain cookies is set out in our Cookie Policy.



5. DO WE USE GOOGLE MAPS?

In Short:  Yes, we use Google Maps for the purpose of providing better service.

This website, mobile application, or Facebook application uses Google Maps APIs. You may find the Google Maps APIs Terms of Service here. To better understand Google’s Privacy Policy, please refer to this link.

By using our Maps API Implementation, you agree to be bound by Google’s Terms of Service. By using our implementation of the Google Maps APIs, you agree to allow us to gain access to information about you including personally identifiable information (such as usernames) and non-personally identifiable information (such as location). We will be collecting the following information:

■  __________

For a full list of what we use information for, please see the previous section titled  and WILL YOUR INFORMATION BE SHARED WITH ANYONE?

The Maps APIs that we use store and access cookies and other information on your devices. If you are a user currently in the European Economic Area (EU countries, Iceland, Liechtenstein and Norway), please take a look at our EU User Consent Policy.



6. HOW DO WE HANDLE YOUR SOCIAL LOGINS?

In Short:  If you choose to register or log in to our services using a social media account, we may have access to certain information about you.

Our Apps offer you the ability to register and login using your third party social media account details (like your Facebook or Twitter logins). Where you choose to do this, we will receive certain profile information about you from your social media provider. The profile Information we receive may vary depending on the social media provider concerned, but will often include your name, e-mail address, friends list, profile picture as well as other information you choose to make public.

We will use the information we receive only for the purposes that are described in this privacy policy or that are otherwise made clear to you on the Apps. Please note that we do not control, and are not responsible for, other uses of your personal information by your third party social media provider. We recommend that you review their privacy policy to understand how they collect, use and share your personal information, and how you can set your privacy preferences on their sites and apps.



7. WHAT IS OUR STANCE ON THIRD-PARTY WEBSITES?

In Short:  We are not responsible for the safety of any information that you share with third-party providers who advertise, but are not affiliated with, our websites.

The Apps may contain advertisements from third parties that are not affiliated with us and which may link to other websites, online services or mobile applications. We cannot guarantee the safety and privacy of data you provide to any third parties. Any data collected by third parties is not covered by this privacy policy. We are not responsible for the content or privacy and security practices and policies of any third parties, including other websites, services or applications that may be linked to or from the Apps. You should review the policies of such third parties and contact them directly to respond to your questions.



8. HOW LONG DO WE KEEP YOUR INFORMATION?

In Short:  We keep your information for as long as necessary to fulfill the purposes outlined in this privacy policy unless otherwise required by law.

We will only keep your personal information for as long as it is necessary for the purposes set out in this privacy policy, unless a longer retention period is required or permitted by law (such as tax, accounting or other legal requirements). No purpose in this policy will require us keeping your personal information for longer than the period of time in which users have an account with us.

When we have no ongoing legitimate business need to process your personal information, we will either delete or anonymize it, or, if this is not possible (for example, because your personal information has been stored in backup archives), then we will securely store your personal information and isolate it from any further processing until deletion is possible.


9. HOW DO WE KEEP YOUR INFORMATION SAFE?

In Short:  We aim to protect your personal information through a system of organizational and technical security measures.

We have implemented appropriate technical and organizational security measures designed to protect the security of any personal information we process. However, please also remember that we cannot guarantee that the internet itself is 100% secure. Although we will do our best to protect your personal information, transmission of personal information to and from our Apps is at your own risk. You should only access the services within a secure environment.



10. DO WE COLLECT INFORMATION FROM MINORS?

In Short:  We do not knowingly collect data from or market to children under 18 years of age.

We do not knowingly solicit data from or market to children under 18 years of age. By using the Apps, you represent that you are at least 18 or that you are the parent or guardian of such a minor and consent to such minor dependent’s use of the Apps. If we learn that personal information from users less than 18 years of age has been collected, we will deactivate the account and take reasonable measures to promptly delete such data from our records. If you become aware of any data we have collected from children under age 18, please contact us at myfamoususa@gmail.com.



11. WHAT ARE YOUR PRIVACY RIGHTS?

In Short:  In some regions, such as the European Economic Area, you have rights that allow you greater access to and control over your personal information. You may review, change, or terminate your account at any time.

In some regions (like the European Economic Area), you have certain rights under applicable data protection laws. These may include the right (i) to request access and obtain a copy of your personal information, (ii) to request rectification or erasure; (iii) to restrict the processing of your personal information; and (iv) if applicable, to data portability. In certain circumstances, you may also have the right to object to the processing of your personal information. To make such a request, please use the contact details provided below. We will consider and act upon any request in accordance with applicable data protection laws.

If we are relying on your consent to process your personal information, you have the right to withdraw your consent at any time. Please note however that this will not affect the lawfulness of the processing before its withdrawal.

If you are resident in the European Economic Area and you believe we are unlawfully processing your personal information, you also have the right to complain to your local data protection supervisory authority. You can find their contact details here: http://ec.europa.eu/justice/data-protection/bodies/authorities/index_en.htm.

If you have questions or comments about your privacy rights, you may email us at myfamoususa@gmail.com.


Account Information
If you would at any time like to review or change the information in your account or terminate your account, you can:

    ■  Log into your account settings and update your user account.

Upon your request to terminate your account, we will deactivate or delete your account and information from our active databases. However, some information may be retained in our files to prevent fraud, troubleshoot problems, assist with any investigations, enforce our Terms of Use and/or comply with legal requirements.

Cookies and similar technologies: Most Web browsers are set to accept cookies by default. If you prefer, you can usually choose to set your browser to remove cookies and to reject cookies. If you choose to remove cookies or reject cookies, this could affect certain features or services of our Apps. To opt-out of interest-based advertising by advertisers on our Apps visit http://www.aboutads.info/choices/.

Opting out of email marketing: You can unsubscribe from our marketing email list at any time by clicking on the unsubscribe link in the emails that we send or by contacting us using the details provided below. You will then be removed from the marketing email list – however, we will still need to send you service-related emails that are necessary for the administration and use of your account. To otherwise opt-out, you may:

    ■  Access your account settings and update preferences.



12. DATA BREACH

A privacy breach occurs when there is unauthorized access to or collection, use, disclosure or disposal of personal information. You will be notified about data breaches when My Famous Inc believes you are likely to be at risk or serious harm. For example, a data breach may be likely to result in serious financial harm or harm to your mental or physical well-being. In the event that My Famous Inc becomes aware of a security breach which has resulted or may result in unauthorized access, use or disclosure of personal information My Famous Inc will promptly investigate the matter and notify the applicable Supervisory Authority not later than 72 hours after having become aware of it, unless the personal data breach is unlikely to result in a risk to the rights and freedoms of natural persons.



13. CONTROLS FOR DO-NOT-TRACK FEATURES

Most web browsers and some mobile operating systems and mobile applications include a Do-Not-Track (“DNT”) feature or setting you can activate to signal your privacy preference not to have data about your online browsing activities monitored and collected. No uniform technology standard for recognizing and implementing DNT signals has been finalized. As such, we do not currently respond to DNT browser signals or any other mechanism that automatically communicates your choice not to be tracked online. If a standard for online tracking is adopted that we must follow in the future, we will inform you about that practice in a revised version of this privacy policy.



14. DO CALIFORNIA RESIDENTS HAVE SPECIFIC PRIVACY RIGHTS?

In Short:  Yes, if you are a resident of California, you are granted specific rights regarding access to your personal information.

California Civil Code Section 1798.83, also known as the “Shine The Light” law, permits our users who are California residents to request and obtain from us, once a year and free of charge, information about categories of personal information (if any) we disclosed to third parties for direct marketing purposes and the names and addresses of all third parties with which we shared personal information in the immediately preceding calendar year. If you are a California resident and would like to make such a request, please submit your request in writing to us using the contact information provided below.

If you are under 18 years of age, reside in California, and have a registered account with the Apps, you have the right to request removal of unwanted data that you publicly post on the Apps. To request removal of such data, please contact us using the contact information provided below, and include the email address associated with your account and a statement that you reside in California. We will make sure the data is not publicly displayed on the Apps, but please be aware that the data may not be completely or comprehensively removed from our systems.



15. DO WE MAKE UPDATES TO THIS POLICY?

In Short:  Yes, we will update this policy as necessary to stay compliant with relevant laws.

We may update this privacy policy from time to time. The updated version will be indicated by an updated “Revised” date and the updated version will be effective as soon as it is accessible. If we make material changes to this privacy policy, we may notify you either by prominently posting a notice of such changes or by directly sending you a notification. We encourage you to review this privacy policy frequently to be informed of how we are protecting your information.



16. HOW CAN YOU CONTACT US ABOUT THIS POLICY?

If you have questions or comments about this policy, you may email us at myfamoususa@gmail.com or by post to:

My Famous Inc 
3303 Durbin Pl
Falls Church 22041
United States


HOW CAN YOU REVIEW, UPDATE, OR DELETE THE DATA WE COLLECT FROM YOU?

Based on the laws of some countries, you may have the right to request access to the personal information we collect from you, change that information, or delete it in some circumstances. To request to review, update, or delete your personal information, please submit a request form by clicking here. We will respond to your request within 30 days.
  "
 );


 $data['terms'] = array(

  'title' => 'PRIVACY POLICY',
  'body' => "
My Famous is licensed to You (End-User) by My Famous Inc, located at 3303 Durbin Pl, Falls Church, Virginia 22041, United States (hereinafter: Licensor), for use only under the terms of this License Agreement.

By downloading the Application from the Apple AppStore, and any update thereto (as permitted by this License Agreement), You indicate that You agree to be bound by all of the terms and conditions of this License Agreement, and that You accept this License Agreement.

The parties of this License Agreement acknowledge that Apple is not a Party to this License Agreement and is not bound by any provisions or obligations with regard to the Application, such as warranty, liability, maintenance and support thereof. My Famous Inc, not Apple, is solely responsible for the licensed Application and the content thereof.

This License Agreement may not provide for usage rules for the Application that are in conflict with the latest App Store Terms of Service. My Famous Inc acknowledges that it had the opportunity to review said terms and this License Agreement is not conflicting with them.

All rights not expressly granted to You are reserved.


1. THE APPLICATION

My Famous (hereinafter: Application) is a piece of software created to Social Media and Advertisement - and customized for Apple mobile devices. It is used to Social Media.

(GDPR)


2. SCOPE OF LICENSE

2.1  You are given a non-transferable, non-exclusive, non-sublicensable license to install and use the Licensed Application on any Apple-branded Products that You (End-User) own or control and as permitted by the Usage Rules set forth in this section and the App Store Terms of Service, with the exception that such licensed Application may be accessed and used by other accounts associated with You (End-User, The Purchaser) via Family Sharing or volume purchasing.

2.2  This license will also govern any updates of the Application provided by Licensor that replace, repair, and/or supplement the first Application, unless a separate license is provided for such update in which case the terms of that new license will govern.

2.3  You may not share or make the Application available to third parties (unless to the degree allowed by the Apple Terms and Conditions, and with My Famous Inc's prior written consent), sell, rent, lend, lease or otherwise redistribute the Application.

2.4  You may not reverse engineer, translate, disassemble, integrate, decompile, integrate, remove, modify, combine, create derivative works or updates of, adapt, or attempt to derive the source code of the Application, or any part thereof (except with My Famous Inc's prior written consent).

2.5  You may not copy (excluding when expressly authorized by this license and the Usage Rules) or alter the Application or portions thereof. You may create and store copies only on devices that You own or control for backup keeping under the terms of this license, the App Store Terms of Service, and any other terms and conditions that apply to the device or software used. You may not remove any intellectual property notices. You acknowledge that no unauthorized third parties may gain access to these copies at any time.

2.6  Violations of the obligations mentioned above, as well as the attempt of such infringement, may be subject to prosecution and damages.

2.7  Licensor reserves the right to modify the terms and conditions of licensing.

2.8  Nothing in this license should be interpreted to restrict third-party terms. When using the Application, You must ensure that You comply with applicable third-party terms and conditions.


3. TECHNICAL REQUIREMENTS

3.1  The Application requires a firmware version 1.0.0 or higher. Licensor recommends using the latest version of the firmware.

3.2  Licensor attempts to keep the Application updated so that it complies with modified/new versions of the firmware and new hardware. You are not granted rights to claim such an update.

3.3  You acknowledge that it is Your responsibility to confirm and determine that the app end-user device on which You intend to use the Application satisfies the technical  specifications mentioned above.

3.4  Licensor reserves the right to modify the technical specifications as it sees appropriate at any time.


4. MAINTENANCE AND SUPPORT

4.1  The Licensor is solely responsible for providing any maintenance and support services for this licensed Application. You can reach the Licensor at the email address listed in the App Store Overview for this licensed Application.

4.2  My Famous Inc and the End-User acknowledge that Apple has no obligation whatsoever to furnish any maintenance and support services with respect to the licensed Application.


5. LIABILITY

5.1  Licensor's responsibility in the case of violation of obligations and tort shall be limited to intent and gross negligence. Only in case of a breach of essential contractual duties (cardinal obligations), Licensor shall also be liable in case of slight negligence. In any case, liability shall be limited to the foreseeable, contractually typical damages. The limitation mentioned above does not apply to injuries to life, limb, or health.

5.2  Licensor takes no accountability or responsibility for any damages caused due to a breach of duties according to Section 2 of this Agreement. To avoid data loss, You are required to make use of backup functions of the Application to the extent allowed by applicable third-party terms and conditions of use. You are aware that in case of alterations or manipulations of the Application, You will not have access to licensed Application.


6. WARRANTY

6.1  Licensor warrants that the Application is free of spyware, trojan horses, viruses, or any other malware at the time of Your download. Licensor warrants that the Application works as described in the user documentation.

6.2  No warranty is provided for the Application that is not executable on the device, that has been unauthorizedly modified, handled inappropriately or culpably, combined or installed with inappropriate hardware or software, used with inappropriate accessories, regardless if by Yourself or by third parties, or if there are any other reasons outside of My Famous Inc's sphere of influence that affect the executability of the Application.

6.3  You are required to inspect the Application immediately after installing it and notify My Famous Inc about issues discovered without delay by e-mail provided in Product Claims. The defect report will be taken into consideration and further investigated if it has been mailed within a period of thirty (30) days after discovery.

6.4  If we confirm that the Application is defective, My Famous Inc reserves a choice to remedy the situation either by means of solving the defect or substitute delivery.

6.5  In the event of any failure of the Application to conform to any applicable warranty, You may notify the App-Store-Operator, and Your Application purchase price will be refunded to You. To the maximum extent permitted by applicable law, the App-Store-Operator will have no other warranty obligation whatsoever with respect to the App, and any other losses, claims, damages, liabilities, expenses and costs attributable to any negligence to adhere to any warranty.

6.6  If the user is an entrepreneur, any claim based on faults expires after a statutory period of limitation amounting to twelve (12) months after the Application was made available to the user. The statutory periods of limitation given by law apply for users who are consumers.

7. PRODUCT CLAIMS

My Famous Inc and the End-User acknowledge that My Famous Inc, and not Apple, is responsible for addressing any claims of the End-User or any third party relating to the licensed Application or the End-User’s possession and/or use of that licensed Application, including, but not limited to:

(i) product liability claims;

(ii) any claim that the licensed Application fails to conform to any applicable legal or regulatory requirement; and

(iii) claims arising under consumer protection, privacy, or similar legislation, including in connection with Your Licensed Application’s use of the HealthKit and HomeKit.


8. LEGAL COMPLIANCE

You represent and warrant that You are not located in a country that is subject to a U.S. Government embargo, or that has been designated by the U.S. Government as a t supportcountry; and that You are not listed on any U.S. Government list of prohibited or restricted parties.


9. CONTACT INFORMATION                  

For general inquiries, complaints, questions or claims concerning the licensed Application, please contact:
     
My FAMOUS Inc
3303 Durbin Pl
Falls Church, VA 22041
United States
http://www.myfamous.com


10. TERMINATION

The license is valid until terminated by My Famous Inc or by You. Your rights under this license will terminate automatically and without notice from My Famous Inc if You fail to adhere to any term(s) of this license. Upon License termination, You shall stop all use of the Application, and destroy all copies, full or partial, of the Application.


11. THIRD-PARTY TERMS OF AGREEMENTS AND BENEFICIARY

My Famous Inc represents and warrants that My Famous Inc will comply with applicable third-party terms of agreement when using licensed Application.

In Accordance with Section 9 of the Instructions for Minimum Terms of Developer's End-User License AgreementApple and Apple's subsidiaries shall be third-party beneficiaries of this End User License Agreement and - upon Your acceptance of the terms and conditions of this license agreement, Apple will have the right (and will be deemed to have accepted the right) to enforce this End User License Agreement against You as a third-party beneficiary thereof.


12. INTELLECTUAL PROPERTY RIGHTS

My Famous Inc and the End-User acknowledge that, in the event of any third-party claim that the licensed Application or the End-User's possession and use of that licensed Application infringes on the third party's intellectual property rights, My Famous Inc, and not Apple, will be solely responsible for the investigation, defense, settlement and discharge or any such intellectual property infringement claims.


13. APPLICABLE LAW

This license agreement is governed by the laws of the Commonwealth of Virginia excluding its conflicts of law rules.


14. MISCELLANEOUS

14.1  If any of the terms of this agreement should be or become invalid, the validity of the remaining provisions shall not be affected. Invalid terms will be replaced by valid ones formulated in a way that will achieve the primary purpose.
             
14.2  Collateral agreements, changes and amendments are only valid if laid down in writing. The preceding clause can only be waived in writing.
   "
 );


 $result['status']= true;
 $result['http_code']= (int) 200;
 $result['msg']='';
 $result['data']=$data;
 





 print json_encode($result,JSON_UNESCAPED_UNICODE);
 
 
  break;
  




 
 
 
 
 
 case "myFollowingStories":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();

$user_id = $_REQUEST['token'];

 $db = new Db();

    $following = $db -> select("SELECT * FROM `following` WHERE  `user_id` = $user_id");
    
    $result_f = array();
    $data = array();
   
    
    
   for($i=0;$i<count($following);$i++){
       
       $c_u_id = $following[0]['following_user_id'];
       $pforile_arr = $db -> select("SELECT *  FROM `profile` WHERE  `user_id` = $c_u_id");
 
       $bio = $pforile_arr[0]['bio'];
       $name = $pforile_arr[0]['name'];
       
       $user = $db -> select("SELECT *, '$bio' as `bio`, '$name' as `name` FROM `users` WHERE `id` = $c_u_id");


       $stories = $db -> select("SELECT * FROM `stories` WHERE `user_id` = $c_u_id");
       $data['user'] = $user;
       $data['stories_count'] = count($stories);
       $data['stories'] = $stories;
       
       
       
       
       array_push($result_f,$data);
       
       
       
   }
   
   
    $result['status']= true;
    $result['http_code']= (int) 200;
    $result['msg']='';
    $result['data']=$result_f;
    $result['token']=$user_id;
   
   



print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;
 
 
 
 
  case "home":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();

$user_id = $_REQUEST['token'];


$data = array();

 $db = new Db();

    $rows = $db -> select("SELECT *, 'Senior System Engineer, Team leader and Father, Senior System Engineer, Team leader and Father, ' as bio FROM `users` WHERE  `id` = $user_id");

    if(count($rows)>0){
        
        
        $data['user'] = $rows[0];
        $data['myFollowingStories'] = getMyFolowingStory($user_id);
        $data['myStories'] = getMyFolowingStory($user_id);
      //  $data['myPosts'] = getUserPosts($user_id);
        $data['MyFolowingPosts'] = getFolowingPosts($user_id);
        
        $result = array();
        $result['status']= true;
        $result['http_code']= (int) 200;
        $result['msg']='';
        $result['data']=$data;
        $result['token']=$user_id;
        
    }




print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;
 
 
  case "myProfile":


$result['status']= false;
$result['http_code']= (int) 405;
$result['msg']='I can not hear you ,, try again !';
$result['data']=array();

$user_id = $_REQUEST['token'];


$data = array();

 $db = new Db();
 $pforile_arr = $db -> select("SELECT *  FROM `profile` WHERE  `user_id` = $user_id");
 
 $bio = $pforile_arr[0]['bio'];
 $name = $pforile_arr[0]['name'];

    $rows = $db -> select("SELECT *, '$bio' as `bio`, '$name' as `name` FROM `users` WHERE  `id` = $user_id");

    if(count($rows)>0){
        
        
        $data['user'] = $rows[0];
        $data['counts '] = array('followers'=>0,'myfamous'=>0,'following'=>0);
        //$data['myFollowingStories'] = getMyFolowingStory($user_id);
        $data['myStories'] = getMyStory($user_id);
        $data['myPosts'] = getUserPosts($user_id);
       // $data['MyFolowingPosts'] = getFolowingPosts($user_id);
        
        $result = array();
        $result['status']= true;
        $result['http_code']= (int) 200;
        $result['msg']='';
        $result['data']=$data;
        $result['token']=$user_id;
        
    }




print json_encode($result,JSON_UNESCAPED_UNICODE);
    


 break;




 case "userProfile":


 $result['status']= false;
 $result['http_code']= (int) 405;
 $result['msg']='I can not hear you ,, try again !';
 $result['data']=array();
 
 $user_id = $_REQUEST['token'];
 $p_id = $_REQUEST['user_id'];
 
 $data = array();
 
  $db = new Db();
  $pforile_arr = $db -> select("SELECT *  FROM `profile` WHERE  `user_id` = $p_id");
  
  $bio = $pforile_arr[0]['bio'];
  $name = $pforile_arr[0]['name'];
 
     $rows = $db -> select("SELECT *, '$bio' as `bio` , '$name' as `name` FROM `users` WHERE  `id` =  $p_id");
 
     if(count($rows)>0){
         
         
         $data['user'] = $rows[0];
         $data['counts '] = array('followers'=>0,'myfamous'=>0,'following'=>0);
         $data['s_clount '] = array('fb'=>0,'tw'=>0,'inst'=>0);
         $data['facebook_url'] = 'https://fb.com/mqedama';
         $data['twitter_url'] = 'https://twitter.com/qedama';
         $data['twitter_url'] = 'https://twitter.com/qedama';
         $data['instgram_url'] = 'https://instagram.com/qedama';
         //$data['myFollowingStories'] = getMyFolowingStory($user_id);
         $data['myStories'] = getMyStory($p_id);
         $data['myPosts'] = getUserPosts($p_id);
        // $data['MyFolowingPosts'] = getFolowingPosts($user_id);
         
         $result = array();
         $result['status']= true;
         $result['http_code']= (int) 200;
         $result['msg']='';
         $result['data']=$data;
         $result['token']=$user_id;
         
     }
 
 
 
 
 print json_encode($result,JSON_UNESCAPED_UNICODE);
     
 
 
  break;
 
 

    default:
        $result = array();
        print json_encode($result,JSON_UNESCAPED_UNICODE);


} //end switch



function getMyStory($user_id){
    
     $db = new Db();

    $rows = $db -> select("SELECT * FROM `stories` WHERE  `user_id` = $user_id");
    return $rows;
}


function getMyFolowingStory($user_id){
    
    $db = new Db();

    $following = $db -> select("SELECT * FROM `following` WHERE  `user_id` = $user_id");
    
    $result_f = array();
    $data = array();
   
    
    
   for($i=0;$i<count($following);$i++){
       
       $c_u_id = $following[$i]['following_user_id'];

       $pforile_arr = $db -> select("SELECT *  FROM `profile` WHERE  `user_id` = $c_u_id");
 
       $bio = $pforile_arr[0]['bio'];
       $name = $pforile_arr[0]['name'];
       
       $user = $db -> select("SELECT * , '$bio' as `bio` , '$name' as `name` FROM `users` WHERE `id` = $c_u_id");
       $stories = $db -> select("SELECT * FROM `stories` WHERE `user_id` = $c_u_id");
       $data['user'] = $user[0];
       $data['stories_count'] = count($stories);
       $data['stories'] = $stories;
       
       
       if(count($stories)<1){
           continue;
       }
       
       array_push($result_f,$data);
       
       
       
   }
   
   return $result_f;
}



function getUserPosts($user_id){
    
    $db = new Db();

    $posts = $db -> select("SELECT * FROM `posts` WHERE  `user_id` = $user_id");
    
    $temp_array= array();


    for($i=0;$i<count($posts);$i++){

        $pp_id = $posts[$i]['id'];
        $isStar = $db -> select("SELECT *  FROM `posts_star` WHERE `user_id`  = $user_id AND `post_id` = $pp_id");
 
        if(count($isStar)>0)
        $posts[$i]['isStar'] = true ;
        else
        $posts[$i]['isStar'] = false ;
 
 
        $postComments = $db -> select("SELECT *  FROM `comments` WHERE   `post_id` = $pp_id ORDER BY `id` DESC");

        $postStars = $db -> select("SELECT *  FROM `posts_star` WHERE   `post_id` = $pp_id ORDER BY `id` DESC");

       
        $posts[$i]['stars_count'] = (int) count($postStars) ;
 
        $posts[$i]['comments_count'] = (int) count($postComments) ;
 
        array_push($temp_array,$posts[$i]);
    }



   return $temp_array;
}


function getFolowingPosts($user_id){
    
    $db = new Db();

    $posts = $db -> select("SELECT * FROM `posts` limit 100");
    
     $result_f = array();
    $data = array();
   
    
    
   for($i=0;$i<count($posts);$i++){


       


       
       $c_u_id = $posts[$i]['user_id'];
       $pforile_arr = $db -> select("SELECT *  FROM `profile` WHERE  `user_id` = $c_u_id");
 
       $bio = $pforile_arr[0]['bio'];
       $name = $pforile_arr[0]['name'];
       $user = $db -> select("SELECT *, '$bio' as `bio` , '$name' as `name` FROM `users` WHERE `id` = $c_u_id");
      
       $data['user'] = $user[0];


       $pp_id = $posts[$i]['id'];
       $isStar = $db -> select("SELECT *  FROM `posts_star` WHERE `user_id`  = $user_id AND `post_id` = $pp_id");

       if(count($isStar)>0)
       $posts[$i]['isStar'] = true ;
       else
       $posts[$i]['isStar'] = false ;


       $postComments = $db -> select("SELECT *  FROM `comments` WHERE   `post_id` = $pp_id ORDER BY `id` DESC");

       $postStars = $db -> select("SELECT *  FROM `posts_star` WHERE   `post_id` = $pp_id ORDER BY `id` DESC");

       $posts[$i]['comments_count'] = (int) count($postComments) ;
       $posts[$i]['stars_count'] = (int) count($postStars) ;


       $data['post'] = $posts[$i];
       
       
       
       
       array_push($result_f,$data);
       
       
       
   }
    
   
   return $result_f;
}




 function uploadImage($files) {

  $target_dir = "uploads/";

  $finalFilename = md5(date("Y-m-d H:i:s")) .basename($files["photo"]["name"]);
  $target_file = $target_dir . $finalFilename;


  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


  if($imageFileType != "MP4" && $imageFileType != "mp4" && $imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "JPG" && $imageFileType != "PNG"
  && $imageFileType != "gif" ) {
    //  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {

  return false;

  } else {


    $name = md5(date("Y-m-d H:i:s").rand(2323,22222222221)).'.' .$imageFileType;
    $target_file2 = $target_dir . $name;



      if (move_uploaded_file($files["photo"]["tmp_name"], $target_file2)) {
        return 'https://mayar.mtc.org.ps/vendor/api/uploads/'.$name;
      } else {
        return false;
      }
  }





}




function sendNote($user_id,$type,$device_id,$title,$body,$data){


  $device_fcm = $device_id;
  
 
  
  
  $FCMPushNotification = new \BD\FCMPushNotification('AAAAjX1lE1M:APA91bEi35IgXTOAqS_688vbU4qD68v1-omWsJVngdHsyAMaR82v8mK9TVqPjVtq5L2MLXRLiKn1-_9SMOnbbRORfKL1zziG6X6snuyhmwg4oMGkoerWiOAyh1-HplCDsBukYWob2HLb');
  
  $sDeviceToken = $device_fcm;
  
  $aPayload = array(
    'data' => $data,
    'notification' => array(
      'title' => $title,
      'body'=> $body,
      'sound'=> 'default'
    )
  );
  
  $aOptions = array(
    'time_to_live' => 0 //means messages that can't be delivered immediately are discarded. 
  );
  
  $aResult = $FCMPushNotification->sendToDevice(
    $sDeviceToken,		
    $aPayload,
    $aOptions // optional
  );



    $rrr = json_encode($aResult,JSON_UNESCAPED_UNICODE);

  $date=date("Y-m-d H:i:s");
  $db = new Db();
  $result1 = $db ->query("INSERT INTO `notifications`( `user_id`, `device_id`, `title`, `body`, `type`, `status`, `date`,`results`)
   VALUES ($user_id,'$device_id','$title','$body','$type',0,'$date','$rrr')");

  
  
 return $aResult;
      

}


?>
