<?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="row">
                    <div class="col-md-11"></div>
                    <div class="col-md-1">
                        
                        <button id="addTypeInfoMaladie" class=" float i-Add" type="button">
                             
                    <span class="ul-btn__icon"></span>
                </button>
                    </div>
                </div>
                &nbsp;
                <div class="table-responsive">
                                    <table class="display table table-striped table-bordered" id="hidden_column_table" style="width:100%">
                                        <thead>
                                            <tr>
                                           <th>Code</th>
                                           <th>Nom</th>
                                             <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                               
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                           <th>Code</th>
                                           <th>Nom</th>
                                             <th>Actions</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                           </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('js/plugins/sweetalert2.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/jquery.toast.min.js');?>"></script>
    <script type="text/javascript">
          $(document).ajaxSend(function(event, jqxhr, settings){
            $.LoadingOverlay("show");
        });
        
        $(document).ajaxComplete(function(event, jqxhr, settings){
            $.LoadingOverlay("hide");
        });
       actions = JSON.parse($('#listRole').val());
        actionUser = actions.filter((auth) => auth.controller === 'TypeInformationMaladie')
        $.each( actionUser, function( key, value ) {
            if(!value.action.includes("add")){
                $('#addTypeInfoMaladie').remove();
            }
        });
        
     var myTable =    $('#hidden_column_table').DataTable({
                    responsive: true,
                     "language": {
                                    "zeroRecords": " aucune donnée disponible ",
                                    "infoEmpty": "Aucune donnée disponible",
                                    "search": "recherche",
                                    "infoFiltered": ""
                                },
                    "processing" : true,
                    "serverSide" : true,
                    "ajax" : {
                                         url : "<?php echo $this->url(array('controller' => 'TypeInformationMaladie', 'action' => 'datatable')); ?>",
                                         type : "POST"
                             },
                             columnDefs: [
        { targets: [1,2], visible: true},
        { targets: '_all', visible: false }
    ],
                    columns : [
                                                    null,
                                                    null,
                                                   { render : function(data, type, row, meta) {
                                                           chaine = "";
                                                           $.each( actionUser, function( key, value ) {
                                                                if(value.action.includes("edit")){
                                                                    chaine+='<a href="/aslm/public/TypeinformationMaladie/editer/code/' + row[0] + '"><i title="Edit" class="text-20 i-Pen-2"></i></a>  ';
                                                                }
                                                                if(value.action.includes("delete")){
                                                                    chaine+='<a href="#" class="deletePurpose" code="'+row[0]+'"><i title="Remove"  class=" text-20 i-Remove"></i></a>';
                                                                }
                                                             });
                                                           
                                                           
                                                            
                                                         return chaine;
                                                     }
                                                   }, 
                                                  
                                                    
                                              ],
                   
                });
       
     
    $("#addTypeInfoMaladie").click(function(e){
    e.preventDefault();
        location.replace( "<?php echo $this->url(array('controller' => 'TypeinformationMaladie', 'action' => 'enregistrer')); ?>");
    });
   $("body").on("click",".deletePurpose",function(e){
           e.preventDefault();
            var code = $(this).attr("code");
            var params = {"code": code};
            console.log(code);
             myTable.row($(this).parents('tr')).remove().draw();
             return;
           var parent = $(this).parent().parent();
                swal({
                  title: 'Are you sure?',
                  text: "You won't be able to revert this!",
                  type: 'warning',
                  showCancelButton: true,
                  confirmButtonColor: '#0CC27E',
                  cancelButtonColor: '#FF586B',
                  confirmButtonText: 'Yes, delete it!',
                  cancelButtonText: 'No, cancel!',
                  confirmButtonClass: 'btn btn-success mr-5',
                  cancelButtonClass: 'btn btn-danger',
                  buttonsStyling: false
                }).then(function () {
                  $.ajax({
                        url: "<?php echo $this->url(array('controller' => 'Labo', 'action' => 'delete')); ?>",
                        type: "POST",
                        dataType: "json",
                        data: params,
                        success: function(data) {
                            if (data.type === "success") {
                                $myTable.row(parent).remove().draw();
                            } else {
                                $.toast({
                                    heading: "erreur",
                                    text: "un problème est survenu lors de la suppression",
                                    icon: "error",
                                    hideAfter: false,
                                    stack: false,
                                    position: 'mid-center'
                                });
                            }
                        },
                        error: function() {
                            $.toast({
                                    heading: "serveut inactif",
                                    text: "ASLM-MANAGER n'arrive pas à accéder au serveur , si cela persiste veuillez contacter MELLCO",
                                    icon: "error",
                                    hideAfter: false,
                                    stack: false,
                                    position: 'mid-center'
                            });
                        }
                    });
                }, function (dismiss) {
                  // dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
                  if (dismiss === 'cancel') {
                    
                  }
                });
            
        });
        
    </script>
</body>

</html>