<?php


global $wpdb;

$citytablename = $wpdb->prefix.'findus_city'; 
$loctablename = $wpdb->prefix.'findus_location';


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

    $loc_name =  $_POST['loc_name'];
    $loc_image_name = '';
    $city_id = $_POST['city_id_add'];


    //Upload image
    $img = $_FILES['loc_image']['name'];
    $loc_image_name =time().$img;
    $folder = FIND_US_PATH."/images/".$loc_image_name;
    $tmp = $_FILES['loc_image']['tmp_name'];
    move_uploaded_file($tmp, $folder);
    //END | Upload image

    $data=array('loc_name' => $loc_name , 'loc_image' => $loc_image_name , 'city_id' => $city_id );
    //var_dump($data);
    $wpdb->insert( $loctablename , $data );


    echo "<div id='message' class='updated'><p><strong>Location added successfully</strong></p></div>";

}

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

        $i = 0;

        foreach($_POST['loc_id_del'] as $cityData){

            $loc_id_del =  $_POST['loc_id_del'][$i]; 
            $delete_command = "DELETE FROM " . $loctablename ." where id=".$loc_id_del ;
            $result = $wpdb->query($delete_command);
            $i++;

        }

        echo "<div id='message' class='updated'><p><strong>Location deleted successfully</strong></p></div>";

}

$cities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $citytablename"), ARRAY_A);


?>

    <div class="postbox">

        <div class="city-drop">
            <form action="" method="post">
                <table>
                    <tr>
                        <td><label>City Name</label></td>
                        <td>
                            <select name="findus_city" id="findus_city">
                                <option value="0">Choose City</option>
                                <?php 
                                    foreach ($cities as $city ) {
                                        ?>
                                        <option value="<?=$city['id']?>"><?=$city['city_name']?></option>
                                        <?php
                                    }
                                ?>
                            </select>
                        </td>
                    </tr>
                </table>
                
            </form>
        </div>


        <img src="<?=site_url()?>/wp-content/uploads/2021/09/ajax-loader.gif" id="loader">
        <div id="location-data"></div>

<style type="text/css">
    #loader{
        display: none;
    }
</style> 
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 
<script>
    jQuery(document).ready(function(){
        
        jQuery("#findus_city").change(function(){
                
                jQuery("#location-data").html('');
                jQuery("#loader").show();
                
                var getCityID = jQuery(this).val();
                
                if(getCityID != '0')
                {
                    jQuery.ajax({
                        type: 'POST',
                        url: '<?=FIND_US_URL?>/backend/ajaxlocations.php',
                        data: {city_id:getCityID},
                        success: function(data){
                            console.log('1111');
                            jQuery("#loader").hide();
                            jQuery("#location-data").html(data);                            
                        }
                    });
                }
                else
                {
                    jQuery("#location-data").html('');
                    jQuery("#loader").hide();
                }
        });
        

    });


    function gettest(city_id){
        var getCityID = city_id;
                
                if(getCityID != '0')
                {
                    jQuery.ajax({
                        type: 'POST',
                        url: '<?=FIND_US_URL?>/backend/ajaxlocations.php',
                        data: {city_id:getCityID},
                        success: function(data){
                            console.log('222');
                            jQuery("#loader").hide();
                            jQuery("#location-data").html(data);
                        }
                    });
                }
                else
                {
                    jQuery("#location-data").html('');
                    jQuery("#loader").hide();
                }
            return false;
    }


</script>