<?php
require_once 'config.php';
//$error=false;
$errors=array();
$cities=array();
$cities1=array();
$cities2=array();
$cities3=array();
$cities4=array();
$cities5=array();
$cities6=array();
$cities7=array();
$cities8=array();
$cities9=array();
$cities10=array();
$cities11=array();

	$query='SELECT * FROM categories ';
	$row = mysqli_query($con,$query);     
    if (mysqli_num_rows($row) >= 1) {			
		 while($r=mysqli_fetch_array($row)){
			$cities[]=['id'=>$r['id'],'name'=>$r['name'],'image'=>$r['image']];	
			
		}   
		$errors['categories']=$cities;
		
    } else {
        
		$errors['success']='There is no categories found';
    }
	
	$query1="SELECT * FROM smsgroups ORDER BY id DESC";
	$row1 = mysqli_query($con,$query1);   
    if (mysqli_num_rows($row1) >= 1) {
			
		 while($r1=mysqli_fetch_array($row1)){
			 
			$cities1[]=['id'=>$r1['id'],'group_name'=>$r1['group_name'],'status'=>$r1['status'],'created_at'=>$r1['created_at']];
			
		}   
		$errors['popupgroups']=$cities1;		
		
    } else {
        
		$errors['success']=false;
		
    }
	
	
	$query2='SELECT * FROM banners ';
	$row2 = mysqli_query($con,$query2);     
    if (mysqli_num_rows($row2) >= 1) {
		while($r2=mysqli_fetch_array($row2)){			 
			$cities2[]=['id'=>$r2['id'],'image'=>$r2['image'],'url'=>$r2['url']];		
		}   
		$errors['banners']=$cities2;		
    } else {
        
		$errors['success']='There is no banners found';
    }
	
	
	$query3='SELECT * FROM promotions ORDER BY id DESC ';
	$row3 = mysqli_query($con,$query3);     
    if (mysqli_num_rows($row3) >= 1) {
		while($r3=mysqli_fetch_array($row3)){			 
		$cities3[]=['id'=>$r3['id'],'title'=>$r3['title'],'image'=>$r3['image'],'description'=>str_replace("\r\n","",strip_tags($r3['description']))];			
		}   
		$errors['promotions']=$cities3;		
    } else {
        
		$errors['success']='There is no banners found';
    }
	
	$query4='SELECT * FROM products WHERE cate_id!=30 ORDER BY id DESC LIMIT 10';
	$row4 = mysqli_query($con,$query4);     
    if (mysqli_num_rows($row4) >= 1) {
		while($r4=mysqli_fetch_array($row4)){
			 $row5 = mysqli_query($con,"SELECT * FROM product_img WHERE product_id= ".$r4['id']." LIMIT 1");			 
			$r5=mysqli_fetch_array($row5);			
			$cities4[]=['id'=>$r4['id'],'name'=>$r4['name'],'price'=>$r4['price'],'description'=>$r4['description'],'images'=>$r5['image'],'created_at'=>$r4['created_at']];
			
		}   
		$errors['products']=$cities4;
		
    } else {
        
		$errors['success']='There is no products found';
    }
	
	$query5='SELECT * FROM products WHERE cate_id=30';
	$row5 = mysqli_query($con,$query5);     
    if (mysqli_num_rows($row5) >= 1) {
		while($r5=mysqli_fetch_array($row5)){
			 $row6 = mysqli_query($con,"SELECT * FROM product_img WHERE product_id= ".$r5['id']." LIMIT 1");			 
			$r6=mysqli_fetch_array($row6);			
			$cities5[]=['id'=>$r5['id'],'name'=>$r5['name'],'price'=>$r5['price'],'description'=>$r5['description'],'images'=>$r6['image'],'created_at'=>$r5['created_at']];
			
		}   
		$errors['voucherproducts']=$cities5;
		
    } else {
        
		$errors['success']='There is no products found';
    }
	
	$query6='SELECT * FROM users WHERE role=2 AND status=1';
	$row7 = mysqli_query($con,$query6);     
    if (mysqli_num_rows($row7) >= 1) {			
		 while($r7=mysqli_fetch_array($row7)){
			$cities6[]=['id'=>$r7['id'],'merchant_name'=>$r7['first_name'],'merchant_email'=>$r7['email'],'merchant_lo'=>$r7['profile_pic']];
		
		}   
		$errors['merchants']=$cities6;
		
    } else {
        
		$errors['success']='There is no merchants found';
    }
	
	
	$query7 = "SELECT * FROM users WHERE id=".$_GET['user_id'];	
    $row8 = mysqli_query($con,$query7);     
    if (mysqli_num_rows($row8) >= 1) { 
		while($r8=mysqli_fetch_array($row8)){			
			$errors['user_id'] = $r8['id'];			
			$errors['credits']=$r8['credits'];
			$errors['vouchers']=$r8['vouchers'];
			if(isset($r8['point_wallet']) && $r8['point_wallet']!=''){
			$errors['points']=$r8['point_wallet'];
			}else{
				$errors['points']=0;
			}
			
		}
		$errors['success']='User Wallet';
		
    } else {
        //$error=true;
		$errors['points']=0;
		$errors['success']='No data found !!';
    }
	
	
	$status=0;
	
	$query8='SELECT * FROM users where id='.$_GET['user_id'];
	$row9 = mysqli_query($con,$query8);     
    if (mysqli_num_rows($row9) == 1) {		
		$r9=mysqli_fetch_array($row9);			
			 $query9='SELECT id FROM orders where user_id='.$_GET['user_id'];
			 $row10 = mysqli_query($con,$query9);
			 if(mysqli_num_rows($row10) == 0){
				 $status=1;
			 }else{
				 $status=2;
			 }
			 
			 if($r9['is_insta']==1){
				 $status=3;
				 $errors['insta_no']=$r9['insta_no'];
				 $errors['pin']=$r9['pin'];
			 }
			
		$errors['userstatus']=$status;		
		$errors['success']='user status';
    } else {
        
		$errors['success']='There is no buser';
    }
	
	
	$fromuser=mysqli_query($con,"SELECT id,first_name,last_name,point_wallet FROM users WHERE id=".$_GET['user_id']);
   $setting=mysqli_query($con,"SELECT id,card_image FROM settings WHERE id=1");
   $r11=mysqli_fetch_array($fromuser);
	$cities7[]=['id'=>$r11['id'],'first_name'=>$r11['first_name'],'last_name'=>$r11['last_name'],'balance'=>$r11['point_wallet']];
	$r12=mysqli_fetch_array($setting);
	$cities8[]=['id'=>$r12['id'],'card_image'=>$r12['card_image']];
		
		 if($fromuser){		
	     	$errors['success']=true;
	     	$errors['user']=$cities7;
	     	$errors['setting']=$cities8;
	     	
	 }else{
	     $errors['success']=false;
	     $errors['message']='Somthing went wrong !!';
	 }
	 
	 
	 $query13='SELECT * FROM cart_items WHERE user_id='.$_GET['user_id'];
	$row13 = mysqli_query($con,$query13);     
    if (mysqli_num_rows($row13) >= 1) {		
		$totalcart=0;
		 while($r13=mysqli_fetch_array($row13)){			
			$cat=mysqli_fetch_array(mysqli_query($con,"SELECT products.*,product_img.* FROM products LEFT JOIN product_img ON products.id = product_img.product_id WHERE products.id=".$r13['product_id']));
			
			$row14 = mysqli_query($con,'SELECT * FROM product_img WHERE product_id= '.$r13['product_id']);
			 while($r14=mysqli_fetch_array($row14)){
				 $images[]=['id'=>$r14['id'],'image'=>$r14['image']];
			 }
			$cities9[]=['itemid'=>$r13['id'],'user_id'=>$r13['user_id'],'product_id'=>$r13['product_id'],'name'=>$cat['name'],'quantity'=>$r13['quantity'],'itemprice'=>$cat['price'],
			'itemimages'=>$cat['image'],'status'=>$r13['status']];
			$totalcart=$totalcart+($cat['price']*$r13['quantity']);
			
		}   
		$errors['cartitems']=$cities9;
		$errors['carttotal']=$totalcart;
		$errors['cartitemscount']=mysqli_num_rows($row13);
		$errors['success']=true;
    } else {
        $errors['cartitems']=array();
		$errors['carttotal']=0;
		$errors['cartitemscount']=0;
		$errors['success']='There is no item found';
    }
	 
		$query15='SELECT * FROM notifications where id NOT IN (SELECT notification_id FROM notificationby_user WHERE user_id='.$_GET['user_id'].')';
		$row15 = mysqli_query($con,$query15);
		 if (mysqli_num_rows($row15) >= 1) {	
		 $errors['notificationcount']=mysqli_num_rows($row15);
		 } else{
			 $errors['notificationcount']=0;
		 }
	echo json_encode($errors);
?>