<?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')
    {
        getUsers();
    }
    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')
    {
        categories();
    }
    else if($_POST['userStep']=='6')
    {
       register($_POST['fullName'],$_POST['phone'],$_POST['address'],$_POST['email'], $_POST['password'], $_POST['user_type']);
    }
    else if($_POST['userStep']=='7')
    {
        checkUser($_POST['email'], $_POST['phone']);
    }
    else if($_POST['userStep']=='8')
    {
        deleteUser($_POST['id']);
    }
    else if($_POST['userStep']=='9')
    {
        updateToken($_POST['id'], $_POST['token']);
    }
    else if($_POST['userStep']=='10')
    {
        updateStatus($_POST['id'], $_POST['status']);
    }
    else if($_POST['userStep']=='11')
    {
        updaterecord2($_POST['full_name'], $_POST['address1'], $_POST['country'], $_POST['address2'], $_POST['city'], $_POST['id']);
    }
    
    else if($_POST['userStep']=='12')
    {
       updateEvent($_POST['id'], $_POST['title'],$_POST['description'], $_POST['image'], $_POST['event_type'], $_POST['startTime'],$_POST['time'],
                $_POST['address']);
    }
    else if($_POST['userStep']=='13')
    {
        banners();
    }
    else if($_POST['userStep']=='14')
    {
        getAdmin();
    }
    else if($_POST['userStep']=='15')
    {
        addEventType($_POST['type']);
    }
    else if($_POST['userStep']=='16')
    {
        addPost($_POST['title'], $_POST['text'],$_POST['images'], $_POST['id'], $_POST['video']);
    }
    else if($_POST['userStep']=='17')
    {
        speicalCycles();
    }
    else if($_POST['userStep']=='18')
    {
        introPage();
    }
    else if($_POST['userStep']=='19')
    {
        getPhones();
    }
    else if($_POST['userStep']=='20')
    {
        contactUs();
    }
    else if($_POST['userStep']=='21')
    {
        getSubCategories($_POST['id']);
    }
    else if($_POST['userStep']=='22')
    {
        addBanner($_POST['image'], $_POST['name']);
    }
    else if($_POST['userStep']=='23')
    {
       EditCycle($_POST['name'], $_POST['condition'],$_POST['make'], $_POST['model'], $_POST['mileage'], $_POST['fuel'], $_POST['engine'], $_POST['transmission'], $_POST['drive'], $_POST['ext_color'], $_POST['int_color'], $_POST['registered'],$_POST['address'], $_POST['lat'], $_POST['lng'], $_POST['images'], $_POST['note'], $_POST['price'], $_POST['user_id'], $_POST['category'], $_POST['id']);
    }
    else if($_POST['userStep']=='24')
    {
        deleteType($_POST['id']);
    }
    else if($_POST['userStep']=='25')
    {
        delBanner($_POST['id']);
    }
    else if($_POST['userStep']=='26')
    {
        deleteEvent($_POST['id']);
    }
    else if($_POST['userStep']=='27')
    {
        addCat($_POST['image'], $_POST['name'], $_POST['id'], $_POST['type']);
    }
    else if($_POST['userStep']=='29')
    {
        addIntro($_POST['image'], $_POST['text'], $_POST['id'], $_POST['type']);
    }
    else if($_POST['userStep']=='30')
    {
        deleteCat($_POST['id']);
    }
    else if($_POST['userStep']=='31')
    {
        addSubCat($_POST['name'], $_POST['id']);
    }
    else if($_POST['userStep']=='32')
    {
        deleteSubCat($_POST['id']);
    }
    else if($_POST['userStep']=='33')
    {
        deleteNews($_POST['id']);
    }
    else if($_POST['userStep']=='34')
    {
        addNews($_POST['image'], $_POST['title'], $_POST['id'], $_POST['type'], $_POST['news_type'], $_POST['text']);
    }
    
//functions details

function addNews($image, $title, $id, $type, $news_type, $text)
{

    $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");
        $date = date('Y/m/d');
        
        if($type == 'Add') {
          $result5 = mysqli_query($con,"INSERT INTO news (title, image, type, text, date, status) VALUES 
                                    ('$title', '$image', '$news_type', '$text', '$date', '1')"); 
        }
        else {
            $sql1 = "UPDATE news SET name = '$name', image = '$image' WHERE id = '$id'";
            $result5= mysqli_query($con,$sql1);
        }

        
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            }
    
}

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

function addSubCat($name, $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');
        
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        
        $result5 = mysqli_query($con,"INSERT INTO sub_categories (name, category_id) VALUES ('$name', '$id')");

        if($result5)
        {
            echo json_encode(array($status1));
        }
        else 
        {
            echo json_encode(array($status0));
        } 
       
    
    
}

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

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

function addIntro($image, $text, $id, $type)
{

    $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");
        $status2 = array("status"=>"Email already exist");
        
        if($type == 'Add') {
          $result5 = mysqli_query($con,"INSERT INTO intro (text, image) VALUES ('$text', '$image')");  
        }
        else {
            $sql1 = "UPDATE intro SET text = '$text', image = '$image' WHERE id = '$id'";
            $result5= mysqli_query($con,$sql1);
        }
         
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            } 
       
    
    
}

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');
        
      $sql1 = "SELECT * FROM banners ORDER BY id DESC";
      
      $result1= mysqli_query($con,$sql1);
      while($fetch = mysqli_fetch_assoc($result1))
      {
        $temp = $fetch;
        $banners[] = $temp;
      }
      
      $special_sql = "SELECT * FROM categories";
      $special_result= mysqli_query($con,$special_sql);
      while($cat_fetch = mysqli_fetch_assoc($special_result))
      {
        $categories[] = $cat_fetch;
      }
      
      header('Content-type: application/json');
      echo json_encode(array('categories' => $categories, 'banners' => $banners));
      
}

function addBanner($image, $name)
{

    $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 banners (name, image, status) VALUES 
                                    ('$name', '$image', '1')");
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            }
    
}

function addCat($image, $name, $id, $type)
{

    $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");
        
        if($type == 'Add') {
          $result5 = mysqli_query($con,"INSERT INTO categories (name, image, status) VALUES 
                                    ('$name', '$image', '1')"); 
        }
        else {
            $sql1 = "UPDATE categories SET name = '$name', image = '$image' WHERE id = '$id'";
            $result5= mysqli_query($con,$sql1);
        }

        
       
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            }
    
}

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

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

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

function EditCycle($name, $condition, $make, $model, $mileage, $fuel, $engine, $transmission, $drive, $ext_color, $int_color, $registered, $address, $lat, $lng, $images, $note, $price, $user_id, $category, $id)
{

    $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
    if($lat != '' || $lng != '') {
       $latLng = $lat.','.$lng; 
    }
    else {
        $latLng = '';
    }
    
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");
        
        $sql1 = "UPDATE cycles SET cycle_name='$name', price='$price', maker='$make', dealer_id='$user_id', images='$images', notes='$note', conditionn='$condition', category='$category', model='$model', mileage='$fuel', fuel_type='$user_id', enginee='$engine', transmission='$transmission', drive='$drive', exterior_color='$ext_color', interior_color='$int_color', registered='$registered', location='$address', lat_lng='$latLng' WHERE id='$id'";
        $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
       
    
    
}

function advancedSearch($make, $model, $condition, $type, $price1, $price2, $minYear, $maxYear, $engine1, $engine2)
{
    $cycles=array();
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM cycles WHERE maker LIKE '%$make%' AND model LIKE '%$model%' AND conditionn LIKE '%$condition%' AND category LIKE '%$type%' AND price BETWEEN  $price1 AND $price2 AND enginee Between $engine1 AND $engine2 AND registered BETWEEN '$minYear' AND '$maxYear'";
      $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 getSubCategories($id)
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM sub_categories WHERE category_id = '$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 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 introPage()
{
    $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 intro";
      $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 speicalCycles()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT cycles.*, users.full_name,users.phone FROM special_cycles, cycles, users WHERE special_cycles.status='1' AND cycles.id = special_cycles.cycle_id AND cycles.dealer_id=users.id ORDER BY special_cycles.id DESC";
      $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 addEventType($type)
{
    $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 event_types (name, status) 
                            VALUES ('$type', '1')");  
        
            if($result5)
            {
                echo json_encode(array($status1));
            }
            else 
            {
                echo json_encode(array($status0));
            }
}

function getAdmin()
{
    $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 users WHERE user_type = 'Admin'";
      $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 categories()
{
    $dealers;
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "SELECT * FROM categories 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 banners()
{
    $dealers;
      $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))
     {
         $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 deleteUser($id)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "DELETE FROM users WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}



function getUsers()
{
      $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");
      
      header('Content-type: application/json');
      
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0"); 
         $sql2 = "SELECT * FROM users";
       $result2= mysqli_query($con,$sql2);
       while($fetch4 = mysqli_fetch_assoc($result2))
       {
        $temp2 = $fetch4;
        $allQuestions[] = $temp2;
       }
       if(sizeof($allQuestions) > 0)
       {
           
           header('Content-type: application/json');
           echo json_encode($allQuestions);
       }
        else
        {
           $allQuestions = array(
               "id" => '-1'
               );
           header('Content-type: application/json');       
           echo json_encode(array($allQuestions));
        }
}

function updateToken($id, $token)
{
     $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 = '$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
}

function getPhones()
{
    $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 phones";
      $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 updateEvent($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");
       
      $sql1 = "UPDATE events SET title='$title', description='$description', image='$image', type='$event_type', eventDate='$startTime', eventTime='$time',
               address = '$address' WHERE id = '$id'";
      $result1= mysqli_query($con,$sql1);
       if($result1)
       {
           echo json_encode(array("status"=>"1"));
       }
        else
        {
           echo json_encode(array("status"=>"0"));
        }
       
    
    
}

function addPost($post_title, $post_text, $post_images, $user_id, $video)
{

    $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");
        $status2 = array("status"=>"Email already exist");

        $result5 = mysqli_query($con,"INSERT INTO my_posts (user_id, post_text, post_images, post_title, video, status) VALUES 
                                    ('$user_id', '$post_text', '$post_images', '$post_title', '$video', '1')"); 
       
        
            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 = 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' AND status != '0'";
          $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 updateStatus($id, $status)
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "UPDATE users SET status = '$status' 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, $address1, $country, $address2, $city, $id )
{
      $con  = new mysqli("localhost","prodcast_murtaza","Murtaza03317117456","prodcast_alroq");
      $sql1 = "UPDATE users SET full_name='$username', address_line_1='$address1', address_line_2='$address2', 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, $address, $email, $password, $user_type)
{
    $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");
    
    
        
    header('Content-type: application/json');
        $status1 = array("status"=>"1");
        $status0 = array("status"=>"0");
        $status2 = array("status"=>"Email already exist");
        
        $sql10 = "SELECT * FROM users WHERE email='$email'";
        $result10= mysqli_query($con,$sql10);
            while($fetch = mysqli_fetch_assoc($result10))
            {
                $buyers1[] = $fetch;
            }
            if(sizeof($buyers1) > 0)
            {
                $notexist = false;
               
            }
            
        if($notexist)
        {
            $statSQL= 'SET CHARACTER SET utf8';
      
      mysqli_query($con,$statSQL) 
        or die ("charset in DB didn\'t change");
        
            $result5 = mysqli_query($con,"INSERT INTO users (name, phone, address, email, username, password, user_type) VALUES 
                                    ('$name', '$phone', '$address', '$email', '$email', '$pass', '$user_type')");  
        
            if($result5)
            {
                $sql1 = "SELECT * FROM users WHERE email = '$email'";
                $result1 = mysqli_query($con,$sql1);
                while($fetch = mysqli_fetch_assoc($result1))
                {
                    $buyers1[] = $fetch;
                }
                if(sizeof($buyers1) > 0)
                {
                    echo json_encode($buyers1);
                   
                }
                
            }
            else 
            {
                echo json_encode(array($status0));
            } 
        }
        else {
            echo json_encode(array($status2));
        }
    
    
}


   
    



?>