<?php
include "include/db.php";
include "include/auth.php";

if(isset($_REQUEST['data'])){
    if(strlen($_REQUEST['data']) >0 && strlen($_REQUEST['data']) < 6){
        $zip_input = $_REQUEST['data'];

        $zip_input = ltrim($zip_input, '0');
        $zip_input = trim($zip_input);
        $zip_input =  mysqli_real_escape_string($conn, $zip_input);
        
        $fetch_details_query = "SELECT * FROM us_zip WHERE zip = $zip_input";
        $fetch_details_run = mysqli_query($conn, $fetch_details_query);

        if(mysqli_affected_rows($conn) == 1){
            $fetch_details_row = mysqli_fetch_array($fetch_details_run);
            $state = $fetch_details_row['state_name'];
            $state_id = $fetch_details_row['state_id'];
            $city = $fetch_details_row['city'];
            $county = $fetch_details_row['county_name'];
            $country = 'US';

            $response = array("status"=>"yes","s_id"=>"$state_id","state"=>"$state","city"=>"$city","county"=>"$county","country"=>"$country");
            
            echo json_encode($response);
        }else{
            echo "no";
        }    
    }else{
        echo "no"; 
    }
}else{
    echo "no"; 
}    