<?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>Creating users</h1>
                </div>
                <div class="breadcrumb">
                    
                    <ul>
                        <li><a href="<?php echo $this->url(array('controller'=>'Utilisateur','action'=>'index'),null, true);?>">Back to list</a></li>
                        
                    </ul>
                </div>
             
                <div class="table-responsive">
               <form role="form" id="form" method='post' action='' class="col-lg-12">
                   <div class="row">
                                        <div class="col-md-6 form-group mb-3">
                                            <label for="login">Login</label><label style=" color : Red;">&nbsp; *&nbsp; </label>
                                            <input class="form-control" id="login" type="text" placeholder="Enter your login" required=""/>
                                        </div>
                                        <div class="col-md-6 form-group mb-3">
                                            <label for="password">Password</label>
                                            <input class="form-control" id="password" type="password" placeholder="Enter your password" />
                                        </div>
                                        <div class="col-md-6 form-group mb-3">
                                            <label for="name">Name</label><label style=" color : Red;">&nbsp; *&nbsp; </label>
                                            <input class="form-control" id="name" type="text" placeholder="Enter your name" required="" />
                                        </div>
                                        <div class="col-md-6 form-group mb-3">
                                            <label for="phone">Phone</label><label style=" color : Red;">&nbsp; *&nbsp; </label>
                                            <input class="form-control" id="phone" placeholder="Enter phone" required=""/>
                                        </div>
<!--                                        <div class="col-md-6 form-group mb-3">
                                            <label for="cni">CNI</label><label style=" color : Red;">&nbsp; *&nbsp; </label>
                                            <input class="form-control" id="cni" placeholder="Cni" required=""/>
                                        </div>
                                        <div class="col-md-6 form-group mb-3">
                                            <label for="picker2">Birth date</label>
                                            <input class="form-control datepicker" id="birthdate" placeholder="dd/mm/yyyy" name="dp" />
                                        </div>-->
                                        <div class="col-md-6 form-group mb-3">
                                            <label for="picker1">Sexe</label>
                                            <select class="form-control" id="sexe">
                                                <option>Masculin</option>
                                                <option>Feminin</option>
                                            </select>
                                        </div>
                                         <div class="col-md-6 form-group mb-3">
                                            <label for="picker1">Role</label>
                                            <input type="hidden" id="role_id" name="inputParent_id" class="idgroupe"   />
                                            <input class="form-control" id="role" name="role" type="text" placeholder="Role" required />
                                        </div>
                                    </div>
            
                   <div class="row">
<!--                       <div id="site" class="col-md-6 transfer-demo"> <h1>Sites</h1>&nbsp;</div>
                        <div id="labo" class="col-md-6 transfer-demo"> <h1>Laboratoires</h1>&nbsp;</div>-->
                  </div>
                                 
               
                <hr> 
                &nbsp; &nbsp;
                <div class="row">
                    <div class="col-sm-2"></div>
                     <div class="col-sm-6">
                         <button type="button" id="submission" name="submit" class="btn btn-info m-1">CREATE USER </button>
                </div> 
                <div class="col-sm-2">
                    <button type="reset" id="resetBtn" name="reset" class="btn btn-warning m-1">RESET </button>
                </div>
                    <div class="col-sm-2"></div>
                </div>
                
                <br>
                <div class="clearfix"></div>
            </form>
                           </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.transfer.js?v=0.0.6');?>"></script>
      <script  src="<?php echo $this->baseUrl('script/jquery.autocomplete.js'); ?>"></script>
      <script  src="<?php echo $this->baseUrl('js/plugins/calendar/moment.min.js'); ?>"></script>
       <script  src="<?php echo $this->baseUrl('script/bootstrap-datepicker.js'); ?>"></script>
       
    
   <script type="text/javascript">
        
        $(document).ajaxSend(function(event, jqxhr, settings){
            $.LoadingOverlay("show");
        });
        
        $(document).ajaxComplete(function(event, jqxhr, settings){
            $.LoadingOverlay("hide");
        });
        $('.datepicker').datepicker();
      
    $('#role').keyup(function () {
        var params = {parent: $('#role').val()};
        $.ajax({
            url: "<?php echo $this->url(array('controller' => 'Role', 'action' => 'allrole')); ?>",
            dataType: "json",
            type: "POST",
            data: params,
            beforeSend: function () {
                //$("#inputParent").LoadingOverlay("show");
            },
            success: function (response) {

                $('#role').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};
                        $('#role_id').val(JSON.stringify(info.id));

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

            }
        });

    });
    
   listSites = [];
   listLabo = [];
   function getSites() {
        var params = {parent: "level"};
        $.ajax({
            url: "<?php echo $this->url(array('controller' => 'Site', 'action' => 'findsiteuser')); ?>",
            dataType: "json",
            type: "POST",
            data: params,
            beforeSend: function () {
                //$("#inputParent").LoadingOverlay("show");
            },
            success: function (response) {
                
               var settings = {
                    dataArray: response,
                    callable: function (items) {
                        listSites = items;
                        console.log(listSites);
                 }
                };

        $("#site").transfer(settings);
            },
            complete: function () {
               
            },
            error: function () {

            }
        });

    }
    
    //getSites();
    
    
    function getLabos() {
        var params = {parent: "level"};
        $.ajax({
            url: "<?php echo $this->url(array('controller' => 'Labo', 'action' => 'findlabouser')); ?>",
            dataType: "json",
            type: "POST",
            data: params,
            beforeSend: function () {
                //$("#inputParent").LoadingOverlay("show");
            },
            success: function (response) {
                
               var settings = {
                    dataArray: response,
                    callable: function (items) {
                        listLabo = items;
                        console.log(listLabo);
                 }
                };

        $("#labo").transfer(settings);
            },
            complete: function () {
               
            },
            error: function () {

            }
        });

    }
    
   // getLabos();
    
            $("#submission").click(function(e) {
                e.preventDefault();
                //$("#submission").hide();
                params = {login: $("#login").val(), password: $("#password").val(),name:$("#name").val(),phone:$("#phone").val(),role:$("#role_id").val(),sexe:$("#sexe").val()}
                
                console.log(params);
                $.ajax({
                    url: "<?php echo $this->url(array('controller' => 'Utilisateur', 'action' => 'enregistrer')); ?>",
                    data: params, // {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' => 'Utilisateur', 'action' => 'index')); ?>");
                        }
                    },
                    complete: function() { // Info Debuggage si erreur         
                        //$("#loadueclass").removeClass("fa fa-spinner fa-spin");
                    }
                });
                //---
                return false;
            });
            
           
            
           
	</script>
</body>

</html>