<?php include_once PUBLIC_PATH. '/files_to_include/header_content.php'; ?>
<div class="main-content-wrap sidenav-open d-flex flex-column">
            <!-- ============ Body content start ============= -->
            <div class="main-content">
                <div class="breadcrumb">
                    <h1>Editing roles</h1>
                </div>
                <div class="breadcrumb">
                    
                    <ul>
                        <li><a href="<?php echo $this->url(array('controller'=>'Site','action'=>'index'),null, true);?>">Back to list</a></li>
                        
                    </ul>
                </div>
             
                <div class="table-responsive">
                 <div class="card-body">
                                <form role="form" id="form" method='post'>
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label" for="inputName">Name<i style=" color : Red;">&nbsp; *&nbsp;</i></label>
                                        <div class="col-sm-9">
                                            <input type="hidden" name="code" value="<?php echo $this->site->getCode();  ?>" />
                                            <input class="form-control" id="inputName" name="name" required="" type="text" placeholder="Name" value="<?php echo $this->site->getName();  ?>"  />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label" for="inputLocalisation">Localisation</label>
                                        <div class="col-sm-9">
                                            <input class="form-control" id="inputLocalisation" name="localisation" type="text" placeholder="Localisation"  value="<?php echo $this->site->getLocalisation();  ?>" />
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label" for="inputDescription" >Description</label>
                                        <div class="col-sm-9">
                                            <textarea class="form-control" id="inputDescription" name="description" type="text" placeholder="Description" ><?php echo $this->site->getDescription();  ?></textarea>
                                            
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-sm-3 col-form-label" for="inputParent">Parent site<i style=" color : Red;">&nbsp; *&nbsp;</i></label>
                                        <div class="col-sm-9">
                                            <input type="hidden" id="inputParent_id" name="inputParent_id" class="idgroupe"   />
                                            <input class="form-control" id="inputParent" name="parent" type="text" required="" placeholder="Parent" value="<?php if(is_object($this->site->getParent())) echo $this->site->getParent()->getName();  ?>" />
                                        </div>
                                    </div>
                                    <br/>
                                    <div class="form-group row">
                                        <div class="col-sm-6">
                                            <button class="btn btn-info" type="submit">EDIT</button>
                                        </div>
                                        <div class="col-sm-6">
                                        <button type="reset" id="resetBtn" name="reset" class="btn btn-warning m-1">RESET </button>
                                        </div>
                                    </div>
                                     
                                </form>
                            </div>
              
                           </div>
            </div><!-- Footer Start -->
            <div class="flex-grow-1"></div>
            <div class="app-footer">
                <div class="footer-bottom border-top pt-3 d-flex flex-column flex-sm-row align-items-center">
                    <span class="flex-grow-1"></span>
                    <div class="d-flex align-items-center">
                        <img class="logo" src="<?php echo $this->baseUrl('images/logo.png');?>" alt="">
                        <div>
                            <p class="m-0">&copy; 2021 ASLM</p>
                            <p class="m-0">All rights reserved</p>
                        </div>
                    </div>
                </div>
            </div>
            <!-- fotter end -->
        </div>
    </div><!-- ============ Search UI Start ============= -->
    
    <!-- ============ Search UI End ============= -->
    <script src="<?php echo $this->baseUrl('js/plugins/jquery-3.3.1.min.js');?>"></script>
     <script src="<?php echo $this->baseUrl('js/plugins/bootstrap.bundle.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('js/plugins/perfect-scrollbar.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('js/scripts/script.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('js/scripts/sidebar.large.script.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('js/plugins/datatables.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('js/plugins/loadingoverlay.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/jquery.toast.min.js');?>"></script> 
     <script  src="<?php echo $this->baseUrl('script/jquery.autocomplete.js'); ?>"></script>
   <script type="text/javascript">
        
        $(document).ajaxSend(function(event, jqxhr, settings){
            $.LoadingOverlay("show");
        });
        
        $(document).ajaxComplete(function(event, jqxhr, settings){
            $.LoadingOverlay("hide");
        });
        
         $('#inputParent').keyup(function () {
        var params = {parent: $('#inputParent').val()};
        $.ajax({
            url: "<?php echo $this->url(array('controller' => 'Site', 'action' => 'find')); ?>",
            dataType: "json",
            type: "POST",
            data: params,
            beforeSend: function () {
                //$("#inputParent").LoadingOverlay("show");
            },
            success: function (response) {

                $('#inputParent').autocomplete({
                    selectFirst: true,
                    autoFocus: true,
                    lookup: response.data,
                    onSearchComplete: function (suggestions) {
                        console.log(suggestions);
                    },
                    onSelect: function (suggestion) {
                        var info = {id: suggestion.id, type: suggestion.value};
                        $('#inputParent_id').val(JSON.stringify(info.id));

                    },
                    showNoSuggestionNotice: true,
                    noSuggestionNotice: 'Aucun résultat trouvé'
                });
            },
            complete: function () {
                //$("#inputParent").LoadingOverlay("hide");
                $('#inputParent').focus();
            },
            error: function () {

            }
        });

    });
   
            $("#form").submit(function(e) {
                e.preventDefault();
                //$("#submission").hide();
                $tdata = $("#form").serialize();
                $.ajax({
                    url: "<?php echo $this->url(array('controller' => 'Site', 'action' => 'editer')); ?>",
                    data: $tdata, // {name: name, tableauIdL: tableauIdL, tableauIdE: tableauIdE},
                    cache: false,
                    method: "POST",
                    dataType: "json",
                    error: function(request, error) { 
                    },
                    success: function(data) {
                        // Informe l'utilisateur que l'opération est terminée et renvoie le résultat
                       //$("#submission").show("slow");
                       // var top = $("#submission").offset().top;
                        //var left = $("#submission").offset().left;
                         
                        if (data.type == "error") {
                           $.toast({
                               heading : "statut",
                               text : data.msg,
                               hideAfter : false,
                               stack: false,
                               icon : "error",
                           });
                            
                            
                        } else {
                           $.toast({
                               heading : "statut",
                               text : data.msg,
                               hideAfter : false,
                               stack : false,
                               icon : "success",
                           });
                           //$("#resetBtn").trigger("click");
                           // window.location.replace("<?php //echo $this->url(array('controller' => 'Role', 'action' => 'index')); ?>");
                        }
                    },
                    complete: function() { // Info Debuggage si erreur         
                        //$("#loadueclass").removeClass("fa fa-spinner fa-spin");
                    }
                });
                //---
                return false;
            });
            
            $(".checkall").on("click", function() {
            var $allCheck = $(".checkin[concerne='" + $(this).attr("concerne") + "']");
            for (var i = 0; i < $allCheck.length; i++) {
                if ($(this).prop('checked')) {
                    $allCheck.eq(i).prop('checked', true);
                } else {
                    $allCheck.eq(i).prop('checked', false);
                }
            }
            });
            
           
	</script>
</body>

</html>