<style type="text/css">
@media print
{
.noprint {display:none;}
}
</style>
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <button type="button" onclick="openaddnew();" class="btn btn-custom waves-effect waves-light" >Add <span class="m-l-5"><i class="fa fa-plus"></i></span></button>
                    
                </div>
                <h4 class="page-title"><?php if(!empty($city_areas)){echo $city_areas[0]['city']; }?> City Areas</h4>
            </div>
            
            
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box table-responsive">
                    
                    <h4 class="header-title m-t-0 m-b-30">&nbsp;</h4>

                    <table id="datatable-buttons" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                
                                <th>ID</th>
                                <th>City</th>
                                <th>Area</th>
                                <th class='noprint'>Edit</th>                                
                            </tr>
                        </thead>

                        <tbody>
                            <?php if(isset($city_areas)) { foreach ($city_areas as $city_area) { ?>
                            <tr>
                                
                                <td><?php echo $city_area['id_city_areas']; ?></td>
                                <td><?php echo $city_area['city']; ?></td>
                                <td><?php echo $city_area['city_area']; ?></td>
                                <td class='noprint' > 
                                    <button id='btnedit' onclick="openupdate(<?php echo $city_area['id_city_areas']; ?>);" class="btn btn-icon waves-effect waves-light btn-info m-b-5"> <i class="fa fa-keyboard-o"></i> </button> 
                                </td>
                            </tr>
                            <?php }} ?>
                        </tbody>
                    </table>
                </div>
            </div><!-- end col -->
        </div>
        <!-- end row -->
        
        <!--Modals-->
        
        <!--Add Service Category Modal-->
        <div id="addcityarea" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addcity" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Add a New Area</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtcity" class="control-label">City</label>
                                    <select class="form-control" placeholder="City" id="txtcity" name="txtcity">
                                        <?php if(!empty($cities)) foreach($cities as $city){?>
                                        <option <?php if(!empty($city_areas)){if($city_areas[0]['city_id']==$city['id_cities']){echo "selected='selected'";}}?> value="<?php echo $city['id_cities'];?>"><?php echo $city['city'];?></option>
                                        <?php } ?>
                                    </select>
                                </div> 
                            </div> 
                        </div>
                         
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtcityarea" class="control-label">Area</label>
                                    <input class="form-control" placeholder="Area" id="txtcityarea" name="txtcityarea" >
                                </div> 
                            </div> 
                        </div>
                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button onclick="addnew();" type="button" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!--End Service Category Modal-->
        
        <!--Edit Businenss cities Modal-->
        <div id="editcityarea" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editcity" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Edit City Area</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditidcityarea" class="control-label">ID</label>
                                    <input readonly="readonly" type="text" class="form-control" id="txteditidcityarea" name="txteditidcityarea">
                                </div> 
                            </div> 
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditcityarea" class="control-label">Name</label>
                                    <input type="text" class="form-control" placeholder="City Area" id="txteditcityarea" name="txteditcityarea">
                                </div> 
                            </div> 
                        </div>
                         <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txteditcity" class="control-label">City</label>
                                    <select class="form-control" placeholder="City" id="txteditcity" name="txteditcity">
                                        <?php if(!empty($cities)) foreach($cities as $city){?>
                                        <option value="<?php echo $city['id_cities'];?>"><?php echo $city['city'];?></option>
                                        <?php } ?>
                                    </select>
                                </div> 
                            </div> 
                        </div>
                        
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button onclick="update();" type="button" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!--End Edit Service Category Modal-->
        
        
        
        <script>
           $(document).ready(function() {

                $('#datatable-buttons').DataTable({
                    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
                    stateSave: true,
                    dom: "Bfrtlip",
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"}, {
                            extend: "excel",
                            className: "btn-sm btn-warning btn-trans"
                        }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                    responsive: !0
                });
                
                toastr.options = {
                   "closeButton": true,
                   "debug": true,
                   "newestOnTop": true,
                   "progressBar": true,
                   "positionClass": "toast-bottom-center",
                   "preventDuplicates": true,
                   "onclick": null,
                   "showDuration": "300",
                   "hideDuration": "1000",
                   "timeOut": "5000",
                   "extendedTimeOut": "1000",
                   "showEasing": "swing",
                   "hideEasing": "linear",
                   "showMethod": "fadeIn",
                   "hideMethod": "fadeOut"
               }
                
            });
            
            
            function openaddnew(){
                //alert('ok');
                $("#addcityarea").modal('show');
            }
            
            
            function addnew(){
                if($("#txtcityarea").val()!== ""){
                $.ajax({
                        type: 'POST',
                        url: '<?php echo base_url();?>cities_controller/add_city_area',
                        data: {
                            city: $("#txtcity option:selected").text(),
                            city_id: $("#txtcity option:selected").val(),
                            city_area: $("#txtcityarea").val()                                                     
                        },
                        success: function(data) {
                            var result = data.split("|");
                            if (result[0] === "success") {
                                toastr.success(result[1], ' New City Area Added');
                                location.reload();
                            }
                        }
                    });
                }
            }
            
            
            function openupdate(id_city_areas){
                
                $.ajax({
                    type: 'POST',
                    url: '<?php echo base_url(); ?>cities_controller/get_a_city_area',
                    data: {id_city_areas: id_city_areas},
                    dataType: "json",
                    cache: false,
                    async: true,
                    success: function(data){
                        //console.log(data);
                        for(x=0; x<data.length;x++){
                            //console.log(data[x]);
                            $("#txteditidcityarea").val(data[x].id_city_areas);
                            $("#txteditcity").val(data[x].id_cities);
                            $("#txteditcityarea").val(data[x].city_area);
                            
                        }
                        $("#editcityarea").modal('show');
                        
                    }
                });
            }
            function update(){
                if($("#txteditidcities").val()!== ""){
                $.ajax({
                        type: 'POST',
                        url: '<?php echo base_url(); ?>cities_controller/update_city_area',
                        data: {
                            id_city_areas: $("#txteditidcityarea").val(), 
                            city: $("#txteditcity option:selected").text(),
                            city_id: $("#txteditcity option:selected").val(),
                            city_area: $("#txteditcityarea").val()
                        },
                        success: function(data) {
                            var result = data.split("|");
                            if (result[0] === "success") {
                                toastr.success(result[1], ' City Area Updated');
                                location.reload();
                            }
                        }
                    });
                }
            }
          

        </script>