<?php
// header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
// header("Access-Control-Allow-Headers: *");
$req_body = file_get_contents('php://input');
$_POST = json_decode($req_body,true);

    if($_POST['userStep']=='0')
    {
        loginuser($_POST['username'], $_POST['password']);
    }
    else if($_POST['userStep']=='1')
    {
        getEvents($_POST['id']);
    }
    else if($_POST['userStep']=='2')
    {
        homeData();
    }
    else if($_POST['userStep']=='3')
    {
        updatePassword($_POST['id'], $_POST['password']);
    }
     else if($_POST['userStep']=='4')
    {
        uploadImage($_POST['id'], $_POST['url']);
    }
    else if($_POST['userStep']=='5')
    {
        event_types();
    }
    else if($_POST['userStep']=='6')
    {
       register($_POST['fullName'], $_POST['phone'],$_POST['email'], $_POST['password']);
    }
    else if($_POST['userStep']=='7')
    {
        checkUser($_POST['email'], $_POST['phone']);
    }
    else if($_POST['userStep']=='8')
    {
        addView($_POST['id'] );
    }
    else if($_POST['userStep']=='9')
    {
        updateToken($_POST['token'], $_POST['id']);
    }
    else if($_POST['userStep']=='10')
    {
        updaterecord($_POST['name'], $_POST['address'], $_POST['country'], $_POST['email'], $_POST['city'], $_POST['id'], $_POST['phone'], $_POST['image']);
    }
    else if($_POST['userStep']=='11')
    {
        updaterecord2($_POST['name'], $_POST['address'], $_POST['country'], $_POST['email'], $_POST['city'], $_POST['id'], $_POST['phone']);
    }
    
    else if($_POST['userStep']=='12')
    {
       addEvent($_POST['user_id'], $_POST['title'],$_POST['description'], $_POST['image'], $_POST['event_type'], $_POST['startTime'],$_POST['time'],
                $_POST['address']);
    }
    else if($_POST['userStep']=='13')
    {
       addReception($_POST['user_id'], $_POST['title'],$_POST['description'], $_POST['image'], $_POST['days'], $_POST['city'],$_POST['person'],
                $_POST['phone'], $_POST['latitude'], $_POST['longitude'], $_POST['type'], $_POST['id']);
    }
    else if($_POST['userStep']=='14')
    {
        checkFav($_POST['user_id'], $_POST['post_id']);
    }
    else if($_POST['userStep']=='15')
    {
        getReadyComments();
    }
    else if($_POST['userStep']=='16')
    {
        addPhone($_POST['full_name'], $_POST['phone'], $_POST['id']);
    }
    else if($_POST['userStep']=='17')
    {
        getReceptions();
    }
    else if($_POST['userStep']=='18')
    {
        checkPayments($_POST['user'], $_POST['product']);
    }
    else if($_POST['userStep']=='19')
    {
       addAlbum($_POST['user_id'], $_POST['photo']);
    }
    else if($_POST['userStep']=='20')
    {
        contactUs();
    }
    else if($_POST['userStep']=='21')
    {
        getPosts($_POST['category']);
    }
    else if($_POST['userStep']=='22')
    {
        closeBid($_POST['id']);
    }
    else if($_POST['userStep']=='23')
    {
        addPost($_POST['user_id'], $_POST['category'],$_POST['sub_category'], $_POST['title'], $_POST['photos']);
    }
    else if($_POST['userStep']=='24')
    {
        deleteCycle($_POST['id']);
    }
    else if($_POST['userStep']=='25')
    {
        EditPost($_POST['title'], $_POST['text'],$_POST['images'], $_POST['id'], $_POST['video'], $_POST['post_id']);
    }
    else if($_POST['userStep']=='26')
    {
        deleteFav($_POST['id'], $_POST['user_id']);
    }
    else if($_POST['userStep']=='27')
    {
        addFav($_POST['id'], $_POST['post_id']);
    }
    
    else if($_POST['userStep']=='28')
    {
        banner();
    }
    else if($_POST['userStep']=='29')
    {
        delAccount($_POST['id']);
    }
    else if($_POST['userStep']=='33')
    {
        getReviews($_POST['id']);
    }
    else if($_POST['userStep']=='34')
    {
       addReview($_POST['album_id'], $_POST['user_id'], $_POST['rating'], $_POST['review']);
    }
    else if($_POST['userStep']=='35')
    {
        getSettings();
    }
    else if($_POST['userStep']=='36')
    {
        getFav($_POST['id']);
    }
    else if($_POST['userStep']=='37')
    {
        myPosts($_POST['id']);
    }
    else if($_POST['userStep']=='38')
    {
        deleteAd($_POST['id']);
    }
    else if($_POST['userStep']=='39')
    {
       markSold($_POST['post_id'], $_POST['status']);
    }
    else if($_POST['userStep']=='40')
    {
        subCategories($_POST['id']);
    }
    else if($_POST['userStep']=='41')
    {
       addComment($_POST['user_id'], $_POST['event_id'], $_POST['comment']);
    }
    else if($_POST['userStep']=='42')
    {
        EditProduct($_POST['post_id'], $_POST['category'],$_POST['sub_categories'], $_POST['english_name'], $_POST['arabic_name'], $_POST['price'], $_POST['pictures'], 
                    $_POST['color'], $_POST['father_name'], $_POST['mother_name'], $_POST['winnings'], $_POST['mothers_father_name'], $_POST['numbers_of_x']);
    }
    else if($_POST['userStep']=='43')
    {
        getNews();
    }
    else if($_POST['userStep']=='44')
    {
        getComments($_POST['id']);
    }
    else if($_POST['userStep']=='45')
    {
        getUserPosts($_POST['id']);
    }
    else if($_POST['userStep']=='46')
    {
        addNewsView($_POST['id'] );
    }
    else if($_POST['userStep']=='47')
    {
        addReceptionView($_POST['id'] );
    }
    else if($_POST['userStep']=='48')
    {
        advancedFilter($_POST['city'], $_POST['day'] );
    }
    else if($_POST['userStep']=='49')
    {
        getDateEvents($_POST['date']);
    }
    
//functions details

function getDateEvents($date)
{
      $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
      
      $sql2 = "SELECT * FROM events WHERE eventDate = '$date'";
      
      $result2= mysqli_query($con,$sql2);
      while($fetch2 = mysqli_fetch_assoc($result2))
      {
        $temp2 = $fetch2;
        $user = $temp2['user_id'];
          $sql5 = "SELECT * FROM users WHERE id = '$user'";
      
          $result5= mysqli_query($con,$sql5);
          while($fetch5 = mysqli_fetch_assoc($result5))
          {
            $usertemp = $fetch5;
          }
          
          $temp2['user']  = $usertemp;
          $events[] = $temp2;
      }
      
      header('Content-type: application/json');
      echo json_encode($events);
}

function advancedFilter($city, $day)
{
    $cycles=array();
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
      
        $sql1 = "SELECT * FROM receptions WHERE (city = '$city' OR '$city' = '') AND (days = '$day' OR '$day' = '')";
      
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $cycles[] = $fetch;
     }
       if(sizeof($cycles) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($cycles);
       }
        else
        {
           $cycles = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($cycles));
        }
}

function addNewsView($id)
{
      $views;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT views FROM news WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $views = $fetch['views'];
     }
     
     $views = $views + 1;
     
     $sql2 = "UPDATE news SET views = '$views' WHERE id = '$id'";
      $result2= mysqli_query($con,$sql2);
       if($result2)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        } 
     
}

function getUserPosts($user_id)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
      $sql1 = "SELECT * FROM albums WHERE user_id = '$user_id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         
          $user = $fetch['user_id'];
          $sql4 = "SELECT * FROM users WHERE id = '$user'";
      
          $result4= mysqli_query($con,$sql4);
          while($fetch4 = mysqli_fetch_assoc($result4))
          {
            $usertemp = $fetch4;
          }
          
          $fetch['user']  = $usertemp;
          
         $dealers[] = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode($dealers);
        }
}

function getComments($id)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql1 = "SELECT * FROM event_comments WHERE event_id = '$id' ORDER BY id DESC";
      
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
      {
        
        $temp2 = $fetch;
        $user = $temp2['user_id'];
          $sql5 = "SELECT * FROM users WHERE id = '$user'";
      
          $result5= mysqli_query($con,$sql5);
          while($fetch5 = mysqli_fetch_assoc($result5))
          {
            $usertemp = $fetch5;
          }
          
          $temp2['user']  = $usertemp;
          $events[] = $temp2;
      }
      
      header('Content-type: application/json');
      echo json_encode($events);
}

function getNews()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql1 = "SELECT * FROM news WHERE status = '1'";
      
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
      {
        $temp = $fetch;
        $categories[] = $temp;
      }
      
      header('Content-type: application/json');
      echo json_encode($categories);
}

function addPost($user_id, $category, $sub_category, $title, $photos)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ('charset in DB didn\'t change');
      
    $latLng = $lat.','.$lng;
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");

        $result5 = mysqli_query($con,"INSERT INTO albums (user_id, category, sub_category, title, photos) VALUES 
                                    ('$user_id', '$category', '$sub_category', '$title', '$photos')"); 
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            } 
       
    
    
}

function EditProduct($post_id, $category, $sub_categories, $english_name, $arabic_name, $price, $pictures, $color, $father_name, $mother_name, $winnings, $mothers_father_name, $numbers_of_x)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    
    $statSQL= 'SET CHARACTER SET utf8';
      
      mysqli_query($con,$statSQL) 
        or die ('charset in DB didn\'t change');
    
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        
        $sql1 = "UPDATE classified_products SET category='$category', english_name='$english_name', arabic_name='$arabic_name', price='$price', 
        pictures = '$pictures', color = '$color', father_name = '$father_name', mother_name = '$mother_name', winnings = '$winnings', mothers_father_name = '$mothers_father_name', numbers_of_x = '$numbers_of_x' WHERE id='$post_id'";
        $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
       
    
    
}

function addComment($user_id, $event_id, $comment)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
    $latLng = $lat.','.$lng;
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");

        $result5 = mysqli_query($con,"INSERT INTO event_comments (user_id, event_id, comment ) VALUES ('$user_id', '$event_id', '$comment')"); 
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            } 
       
    
    
}

function subCategories($id)
{
    $cycles;
    $totals;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
        
      $sql1 = "SELECT * FROM classified_sub_categories WHERE category = '$id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $cycles[] = $fetch;
     }
       if(sizeof($cycles) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($cycles);
       }
        else
        {
           $cycles = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($cycles));
        }
}

function markSold($post_id, $status)
    {
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "UPDATE classified_products SET status = '$status' WHERE id = '$post_id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
    }
    
function deleteAd($id)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "DELETE FROM classified_products WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function myPosts($id)
{
      $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
      
      $special_sql = "SELECT * FROM classified_products WHERE user_id = '$id' ORDER BY id ASC";
      $special_result= mysqli_query($con,$special_sql);
      while($cat_fetch = mysqli_fetch_assoc($special_result))
      {
          $user = $cat_fetch['user_id'];
          $sql4 = "SELECT * FROM users WHERE id = '$user'";
      
          $result4= mysqli_query($con,$sql4);
          while($fetch4 = mysqli_fetch_assoc($result4))
          {
            $usertemp = $fetch4;
          }
          
          $cat_fetch['user']  = $usertemp;
      
        $classified_products[] = $cat_fetch;
      }
      
      header('Content-type: application/json');
      echo json_encode($classified_products);
}

function checkFav($user_id, $dish_id)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM saved_products WHERE user_id = '$user_id' AND product_id = '$dish_id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $dealers[] = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($dealers));
        }
}

function addFav($user_id, $dish_id)
{

   $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    $latLng = $lat.','.$lng;
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");

        $result5 = mysqli_query($con,"INSERT INTO saved_products (user_id, product_id) VALUES 
                                    ('$user_id', '$dish_id')"); 
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            }
    
}

function deleteFav($id ,$user_id)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "DELETE FROM saved_products WHERE user_id='$user_id' AND product_id='$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function getFav($id)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
     $sql1 = "SELECT classified_products.* FROM classified_products, saved_products WHERE saved_products.user_id= '$id' AND saved_products.product_id=classified_products.id";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $user = $fetch['user_id'];
          $sql4 = "SELECT * FROM users WHERE id = '$user'";
      
          $result4= mysqli_query($con,$sql4);
          while($fetch4 = mysqli_fetch_assoc($result4))
          {
            $usertemp = $fetch4;
          }
          
          $fetch['user']  = $usertemp;
          
           $dealers[] = $fetch;
     }
    
    
     if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($dealers));
        }
      
}

function getSettings()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM settings";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $dealers = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode($dealers);
        }
}

function addReview($album_id, $user_id, $rating, $comment)
{
    $id;
    $average;
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ('charset in DB didn\'t change');
      
    $result5 = mysqli_query($con,"INSERT INTO album_reviews (album_id, user_id, rate, comment) VALUES ('$album_id','$user_id','$rating', '$comment')");
    
    header('Content-type: application/json');
    $status1 = array("status"=>"1");
    $status0 = array("status"=>"0");
    if($result5)
    {
      
       echo json_encode(array($status1));
       updateRating($album_id);
       
    }
    else 
    {
       echo json_encode(array($status0));
    }
}

function updateRating($album_id)
{
    $counter=0;$rate=0.0;
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ('charset in DB didn\'t change');
      
    $result4 = mysqli_query($con,"SELECT rate FROM album_reviews WHERE album_id = '$album_id' AND rate > 0");
     while($fetch4 = mysqli_fetch_assoc($result4))
     {
         $rate = $rate + (float) $fetch4['rate'];
         $counter++;
     }
     if($counter==0)
     {
        
     }
     else
     {
        $rateAvg1 = round($rate/$counter, 1);
        $result6 = mysqli_query($con,"UPDATE albums SET rate = '$rateAvg1' WHERE id = '$album_id'");
        
         $rateAvg  = array("avgRate"=>$rateAvg1);
         $counter1 = array("totalRating"=>$counter);
     }
   
}

function getReviews($id)
{
      $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
      $sql1 = "SELECT users.*, album_reviews.rate, album_reviews.comment, album_reviews.created_at as datetime FROM album_reviews, users WHERE album_reviews.album_id = '$id' AND 
               album_reviews.user_id = users.id ORDER BY album_reviews.id DESC";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
        //  $fetch['datetime'] = time_ago($fetch['datetime']);
         $dealers[] = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($dealers));
        }
}

function delAccount($id)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "DELETE FROM users WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
          $sql1 = "DELETE FROM events WHERE user_id = '$id'";
          $result1= mysqli_query($con,$sql1);
      
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function homeData()
{   
      $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql3 = "SELECT * FROM classified_banners ORDER BY id DESC";
      
      $result3= mysqli_query($con,$sql3);
      while($fetch = mysqli_fetch_assoc($result3))
      {
        $temp = $fetch;
        $banners[] = $temp;
      }
      
      $sql1 = "SELECT * FROM main_categories ORDER BY id";
      $result1= mysqli_query($con,$sql1);
      while($row = mysqli_fetch_assoc($result1))
     {
         $mainCatName = $row['name'];
         $data = array();
         $sql2 = "SELECT * FROM classified_categories WHERE main_cat_id = '$row[id]' ORDER BY order_by";
          $result2= mysqli_query($con,$sql2);
          while($dir = mysqli_fetch_assoc($result2))
         {
            $data[] = $dir;
         }
         $temp = new stdClass;
         $temp->main_cat_name = $mainCatName;
         $temp->categories = $data;
         $cycles[] = $temp;  
         
     }
      
      header('Content-type: application/json');
      echo json_encode(array('banners' => $banners, 'all_categories' => $cycles));
      
}

function banner()
{
    $cycles;
    $totals;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM banners WHERE status='1'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $cycles[] = $fetch;
     }
       if(sizeof($cycles) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($cycles);
       }
        else
        {
           $cycles = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($cycles));
        }
}

function EditPost($post_title, $post_text, $post_images, $user_id, $video, $id)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        
        $sql1 = "UPDATE my_posts SET post_title='$post_title', user_id='$user_id', post_text='$post_text', post_images='$post_images', video='$video' WHERE id='$id'";
        $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
       
    
    
}

function deleteCycle($id)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "DELETE FROM cycles WHERE id='$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function checkPayments($user, $product)
{
    $cycles  = array();
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
      $sql1 = "SELECT * FROM auction_payments WHERE user_id = '$user' AND product_id = '$product' AND status = '1'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $cycles[] = $fetch;
     }
       if(sizeof($cycles) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($cycles);
       }
        else
        {
           $cycles = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($cycles));
        }
}

function closeBid($id)
{
    $con  = new mysqli("localhost","noornet_murtaza","murtazajutt123","noornet_auction");
    
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        
        $sql1 = "UPDATE auction_products SET status = '0' WHERE id = '$id'";
        $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function getPosts($category_id)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
      $sql2 = "SELECT * FROM sub_categories WHERE category_id = '$category_id' ORDER BY id ASC";
      
      $result2= mysqli_query($con,$sql2);
      while($fetch2 = mysqli_fetch_assoc($result2))
      {
        $temp2 = $fetch2;
        $sub_categories[] = $temp2;
      }
      
      $sql1 = "SELECT * FROM albums WHERE category = '$category_id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         
          $user = $fetch['user_id'];
          $sql4 = "SELECT * FROM users WHERE id = '$user'";
      
          $result4= mysqli_query($con,$sql4);
          while($fetch4 = mysqli_fetch_assoc($result4))
          {
            $usertemp = $fetch4;
          }
          
          $fetch['user']  = $usertemp;
          
         $dealers[] = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode(array('albums' => $dealers, 'sub_categories' => $sub_categories));
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode($dealers);
        }
}

function contactUs()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM contacts WHERE status='1'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $dealers[] = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($dealers));
        }
}

function checkUser($email, $phone)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM users WHERE email = '$email'";
         $result1= mysqli_query($con,$sql1);
         while($fetch = mysqli_fetch_assoc($result1))
         {
             $dealers[] = $fetch;
         }
         if(sizeof($dealers) > 0)
         {
             $dealers = array(
                       "id" => '1'
                    );
            header('Content-type: application/json');
            echo json_encode(array($dealers));
         }
         else
         {
         $sql1 = "SELECT * FROM users WHERE phone = '$phone'";
             $result1= mysqli_query($con,$sql1);
             while($fetch = mysqli_fetch_assoc($result1))
             {
                 $dealers[] = $fetch;
             }
            if(sizeof($dealers) > 0)
             {
                 $dealers = array(
                           "id" => '3'
                        );
                header('Content-type: application/json');
                echo json_encode(array($dealers));
             }
             else {
                $dealers = array(
                       "id" => '-1'
                    );
                    header('Content-type: application/json');       
                    echo json_encode(array($dealers));  
             }
            
            
            
         }
      
}

function getReceptions()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
      $sql1 = "SELECT * FROM receptions WHERE status='1'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $user_id = $fetch['user_id'];
         $bids = array();
         $sql2 = "SELECT * FROM users WHERE id = '$user_id'";
         $result2= mysqli_query($con,$sql2);
         while($fetch2 = mysqli_fetch_assoc($result2))
         {
            $bids = $fetch2;
         }
         $fetch['user'] = $bids;
         $dealers[] = $fetch;
     }
       if(sizeof($dealers) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($dealers);
       }
        else
        {
           $dealers = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($dealers));
        }
}

function getReadyComments()
{
    $cycles  = array();
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
      $sql1 = "SELECT * FROM ready_comments WHERE status = '1'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $cycles[] = $fetch;
     }
       if(sizeof($cycles) > 0)
       {
           header('Content-type: application/json');
           echo json_encode($cycles);
       }
        else
        {
           $cycles = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($cycles));
        }
}

function event_types()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql1 = "SELECT * FROM event_types WHERE status = '1'";
      
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
      {
        $temp = $fetch;
        $categories[] = $temp;
      }
      
      header('Content-type: application/json');
      echo json_encode($categories);
}

function addView($id)
{
      $views;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT views FROM albums WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $views = $fetch['views'];
     }
     
     $views = $views + 1;
     
     $sql2 = "UPDATE albums SET views = '$views' WHERE id = '$id'";
      $result2= mysqli_query($con,$sql2);
       if($result2)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        } 
     
}

function addReceptionView($id)
{
      $views;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT views FROM receptions WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $views = $fetch['views'];
     }
     
     $views = $views + 1;
     
     $sql2 = "UPDATE receptions SET views = '$views' WHERE id = '$id'";
      $result2= mysqli_query($con,$sql2);
       if($result2)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        } 
     
}

function addReception($user_id, $title, $description, $image, $days, $city, $person, $phone, $latitude, $longitude, $type, $id)
{
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
    $latLng = $lat.','.$lng;
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");
        
        if($type == 'Add') {
            $result5 = mysqli_query($con,"INSERT INTO receptions (user_id, title, description, image, days, city, person, phone, latitude, longitude) VALUES 
                                ('$user_id', '$title', '$description', '$image', '$days', '$city', '$person', '$phone', '$latitude', '$longitude')");
        }
        else {
            $sql1 = "UPDATE receptions SET title='$title', description='$description', image='$image', days='$days', city='$city', person='$person', phone='$phone',
                     latitude = '$latitude', longitude = '$longitude' WHERE id = '$id'";
            $result5= mysqli_query($con,$sql1);
        }
        
         
       
        
            if($result5)
            {
                echo json_encode($status1);
            }
            else 
            {
                echo json_encode($status0);
            } 
}

function getEvents($user_id)
{
      $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
      
      $sql2 = "SELECT * FROM events WHERE status = '1'";
      
      $result2= mysqli_query($con,$sql2);
      while($fetch2 = mysqli_fetch_assoc($result2))
      {
        $temp2 = $fetch2;
        $user = $temp2['user_id'];
          $sql5 = "SELECT * FROM users WHERE id = '$user'";
      
          $result5= mysqli_query($con,$sql5);
          while($fetch5 = mysqli_fetch_assoc($result5))
          {
            $usertemp = $fetch5;
          }
          
          $temp2['user']  = $usertemp;
          $events[] = $temp2;
      }
      
      $special_sql = "SELECT * FROM events ORDER BY id DESC LIMIT 5";
      $special_result= mysqli_query($con,$special_sql);
      while($cat_fetch = mysqli_fetch_assoc($special_result))
      {
          $user = $cat_fetch['user_id'];
          $sql4 = "SELECT * FROM users WHERE id = '$user'";
      
          $result4= mysqli_query($con,$sql4);
          while($fetch4 = mysqli_fetch_assoc($result4))
          {
            $usertemp = $fetch4;
          }
          
          $cat_fetch['user']  = $usertemp;
      
        $last_events[] = $cat_fetch;
      }
      
      $sql3 = "SELECT * FROM banners WHERE status = '1'";
      
      $result3= mysqli_query($con,$sql3);
      while($fetch3 = mysqli_fetch_assoc($result3))
      {
        $temp3 = $fetch3;
        $banners[] = $temp3;
      }
      
      header('Content-type: application/json');
      echo json_encode(array('last_events' => $last_events, 'events' => $events, 'banners' => $banners));
}

function updateToken($token, $user_id)
{
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql1 = "UPDATE users SET token='$token' WHERE id='$user_id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function addAlbum($user_id, $photo)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");

        $result5 = mysqli_query($con,"INSERT INTO albums (user_id, photo) VALUES 
                                    ('$user_id', '$photo')"); 
       
        
            if($result5)
            {
                echo json_encode(array($status1));
                
            }
            else 
            {
                echo json_encode(array($status0));
            } 
    
}

function addEvent($user_id, $title, $description, $image, $event_type, $startTime, $time, $address)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
    $latLng = $lat.','.$lng;
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");
        
        $result5 = mysqli_query($con,"INSERT INTO events (user_id, title, description, image, type, eventDate, eventTime, address) VALUES 
                                ('$user_id', '$title', '$description', '$image', '$event_type', '$startTime', '$time', '$address')");
         
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            } 
       
    
    
}

function updateVotes($ans_id)
{
    $counter=0;$rate=0.0;
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) or die ("charset in DB didn\'t change");
      
    $result4 = mysqli_query($con,"SELECT id FROM votes WHERE ans_id = '$ans_id' ");
     while($fetch4 = mysqli_fetch_assoc($result4))
     {
         $rate = $rate + (float) $fetch4['id'];
         $counter++;
     }
     if($counter==0)
     {
        
     }
     else
     {
        $rateAvg1 = round($rate/$counter, 1);
        $result6 = mysqli_query($con,"UPDATE poll_answers SET vote_count = '$counter' WHERE id = '$ans_id'");
        
         $rateAvg  = array("avgRate"=>$rateAvg1);
         $counter1 = array("totalRating"=>$counter);
     }
   
}

function addPhone($full_name, $phone, $user_id)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    $statSQL= 'SET CHARACTER SET utf8';
      
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
    $latLng = $lat.','.$lng;
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");

        $result5 = mysqli_query($con,"INSERT INTO phones (user_id, full_name, phone) VALUES 
                                    ('$user_id', '$full_name', '$phone')"); 
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            } 
       
    
    
}

function loginuser($username, $password)
    {
      $status;    
      $userDetail  = array();
       $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
       $statSQL= 'SET CHARACTER SET utf8';
      
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
      $sql1 = "SELECT * FROM users WHERE email = '$username' AND status != '0'";
      $result1= mysqli_query($con,$sql1);
      
      while($fetch = mysqli_fetch_assoc($result1))
     {
         $userDetail = $fetch;
         $user_id = $fetch[id];
         $status = 1; //password_verify($password,$fetch['password']);
        
     }
       if($status && sizeof($userDetail) > 0)
       {
           header('Content-type: application/json');
           $temp['userDetail'] = $userDetail;
           $user_type = $userDetail['user_type'];
           $cycles = $temp;
           echo json_encode($cycles);
       }
        else
        {
          $sql2 = "SELECT * FROM users WHERE phone ='$username'";
          $result2= mysqli_query($con,$sql2);
          while($fetch = mysqli_fetch_assoc($result2))
         {
             $userDetail = $fetch;
             $user_id = $fetch[id];
             $status = password_verify($password,$fetch['password']);
         }
         if($status && sizeof($userDetail) > 0)
           {
               header('Content-type: application/json');
               $temp['userDetail'] = $userDetail;
               $user_type = $userDetail['user_type'];
                $cycles = $temp;
               echo json_encode($cycles);
           }
          else {
             $userDetail = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode($userDetail); 
          }
        }
    }

function updaterecord($username, $address, $country, $email, $city, $id, $phone, $image)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql1 = "UPDATE users SET name='$username', address='$address', email='$email', phone='$phone', city='$city', country='$country', photo='$image' WHERE id='$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function updaterecord2($username, $address, $country, $email, $city, $id, $phone)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
      $sql1 = "UPDATE users SET name='$username', address='$address', email='$email', phone='$phone', city='$city', country='$country' WHERE id='$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function updatePassword($id, $password)
    {
        $pass= password_hash($password, PASSWORD_DEFAULT);
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "UPDATE users SET password = '$pass' WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
    }
    
function register($name, $phone, $email, $password)
{
    $buyers=array();
    $buyers1=array();
    $plans=array();
    $id;
    $plan;
    $notexist = true;
    $planexist = false;
    $pass= password_hash($password, PASSWORD_DEFAULT);
    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    $statSQL= 'SET CHARACTER SET utf8';
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
    header('Content-type: application/json');
        $status1 = array("id"=>"1");
        $status0 = array("id"=>"-1");
        $status2 = array("id"=>"Email already exist");
        
        if($email != '') {
           $sql10 = "SELECT * FROM users WHERE email = '$email'"; 
        }
        else if($email == '') {
            $sql10 = "SELECT * FROM users WHERE phone = '$phone'"; 
        }
        $result10= mysqli_query($con,$sql10);
            while($fetch = mysqli_fetch_assoc($result10))
            {
                $buyers1[] = $fetch;
            }
            if(sizeof($buyers1) > 0)
            {
                $notexist = false;
               
            }
            
        if($notexist)
        {
            
         $result5 = mysqli_query($con,"INSERT INTO users (full_name, email, password, phone) VALUES 
                                    ('$name', '$email', '$pass', '$phone')");
        
            if($result5)
            {
                $sql11 = "SELECT * FROM users WHERE email='$email'";
                $result11= mysqli_query($con,$sql11);
                    while($fetch = mysqli_fetch_assoc($result11))
                    {
                        $buyers1[] = $fetch;
                    }
            
                echo json_encode($buyers1);
            }
            else 
            {
                echo json_encode(array($status0));
            } 
        }
        else {
            echo json_encode(array($status2));
        }
    
    
}

?>