<div class="row">
     <div class="col-sm-1.5" style="margin-bottom: 15px">
        <button type="button" id="btn_add" class="btn btn-rounded btn-primary" id="loadingModal">Nouveau</button>
    </div>
</div>

<style>
.modal-dialog{
   width: 80%;
   margin: auto;
}
</style>
<!-- Page-Title -->

<!--  Modal content for the above example -->
<div id="modal_role" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal_formLabel" aria-hidden="true" style="display: none;">
<!-- <div class="row">
	<div class="col-md-12"> -->
    	<div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="modal_formLabel">
                CONFIGURATION DES ROLES POUR <span style="color:#F00; text-transform:uppercase;" id="role_profil"></span>
                </h4>
                <button type="button" class="btn-close" data-dismiss="modal" aria-hidden="true">×</button>
            </div>
            <div class="modal-body" id="modal-body">
            </div>
        </div><!-- /.modal-content -->
    <!-- </div>
    </div> -->
</div>
    
    <!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="row">
     <div class="col-sm-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title mb-2">Liste des profils </h3>
            </div>

            <div class="card-body">
                <div class="table-responsive">
                    <table id="datatable-buttons" class="datatable table table-stripped table-bordered">
                        <thead>
                            <tr>
                                <th>Libelle</th>
                                <th width="10%">Roles</th>
                                <th width="5%">Etat</th>
                                <th width="10%">Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($all_data as $value)
                            {?>
                                <tr>
                                    <td><?=$value->libelle_type_profil; ?></td>
                                    <td>
                                    <a href="#" profil="<?php echo $value->libelle_type_profil;?>" id_profil="<?php echo $value->id_type_profil;?>" class="btn-role badge badge-soft-primary">
                                    <i class="fa fa-cog fa-lg"></i><span> Role</span>
                                    </a>
                                    </td>
                                    <td>
                                        <?php
                                        if ($value->etat == '1')
                                        { 
                                        ?>
                                        <a href="#">
                                        <i class="fa fa-toggle-on fa-lg" id="desactiver" id_profil="<?php echo $value->id_type_profil; ?>" style="color:green;"></i>
                                        </a> 
                                        
                                        <?php
                                        }else
                                        {
                                        ?> 
                                        <a href="#">
                                        <i class="fa fa-toggle-off fa-lg" id="activer" id_profil="<?php echo $value->id_type_profil; ?>" style="color:red;"></i>
                                        </a> 
                                        <?php 
                                        }
                                        ?>
                                        </td>
                                    <td>
                                        <a href="#" class="remove-btn btn_edit" id='<?php echo $value->id_type_profil; ?>'><i class="fe fe-edit"></i></a>
                                        &nbsp;
                                        <a href="#" class="remove-btn btn_delete" id='<?php echo $value->id_type_profil; ?>'><i class="fe fe-trash-2" style="color:red;"></i></a>
                                       
                                    </td>
                                </tr>
                            <?php
                    }
					?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div> <!-- End Row -->


<!-- sample modal content -->
<div id="modal_form" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="standard-modalLabel" aria-hidden="true">
        <form action="#" id="form">
            <div class="modal-dialog ">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="standard-modalLabel">Title</h4>
                        <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close">×</button>
                    </div>
                <div class="modal-body">
                    <input type="hidden" id="id_type_profil" name="id_type_profil"/>
					
                    <div class="form-body">
                        <div class="form-group row">
                            <label class="col-lg-3 col-form-label">Libelle</label>

                            <div class="col-lg-9">
                                <input name="libelle_type_profil" id="libelle_type_profil"
                                       class="form-control" type="text" required>
                            </div>
                        </div> 
                    </div>
                </div>
                <div class="modal-footer">
                <button type="button" id="close-btn" class="btn btn-danger float-left">Fermer</button>
                    <button type="submit" class="btn btn-primary">Enrégistrer</button>        
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </form>
</div><!-- /.modal -->


<script type="text/javascript">
 
	$(document).ready(function ()
	{
		
		$('#datatable-buttons').managing_ajax(
		{
			id_menu: 'menu_sys_profils', //id du menu dans le fichier de (navigation) dans notre cas left_side_bar
			id_modal_form: 'modal_form', //id du modal qui contient le formulaire

			id_form: 'form', //id du formulaire
            url_submit: "<?php echo site_url('sys/C_sys_profil/save')?>", //url du save (données envoyés par post)
			
			title_modal_add: 'Nouveau profil', //Title du modal à l'ouverture en mode ajout
			focus_add: 'libelle', //l'emplacement du focus en mode ajout

			title_modal_edit: 'Edition de profil', //Title du modal à l'ouverture en mode edit
			focus_edit: 'libelle',//l'emplacement du focus en mode edit

            url_edit: "<?php echo site_url('sys/C_sys_profil/get_record')?>", //url le fonction qui recupére la données de la ligne
            url_delete: "<?php echo site_url('sys/C_sys_profil/delete')?>", //url de la fonction supprimé
		});

        $('.btn-close').click(function(){
            //alert('ok');
            $('#modal_form').modal('hide');
            $('#modal_role').modal('hide');
        });
        
        $('#close-btn').click(function(){
            //alert('ok');
            $('#modal_form').modal('hide');
        });
		
		$('.btn-role').on('click', function (event)
		{
			var id_cur_profil 	= $(this).attr('id_profil');
			var cur_profil 		= $(this).attr('profil');

			//Appel controller/action/id
			$.ajax(
			{
                url: '<?php echo site_url('sys/C_sys_profil/get_menu_liste/') ?>' + id_cur_profil,
				type: "GET",
				dataType: "HTML",
				success: function (data) {
					//alert(data);
					$('#modal-body').html(data);
				},
				error: function (jqXHR, textStatus, errorThrown) {
					alert('Error adding / update data');
				}
			});

			$("#role_profil").text(cur_profil);
			$("#id_role_profil").val(id_cur_profil);
			$('#modal_role').modal('show');
		});

        $('#datatable-buttons tbody').on('click', '#desactiver', function() {
            var id = $(this).attr('id_profil');
            var etat = '-1';
            swal.fire({
                    title: 'Alerte',
                    text: "voulez vous désactiver ce profil ?",
                    type: 'warning',
                    showCancelButton: true,
                   // dangerMode: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    cancelButtonText: 'Non',
                    confirmButtonText: 'Oui',
                    closeOnConfirm: true
                    //}).then(function (isConfirm) {
                    }).then((result) => {
                        if (result.isConfirmed) {
                            var href = "<?= base_url() ?>sys/C_sys_profil/change_etat/" + id + '/' + etat;
                            callBack_menu(href);
                    }
                });
        });

        $('#datatable-buttons tbody').on('click', '#activer', function() {
            var id = $(this).attr('id_profil');
            var etat = '1';
            swal.fire({
                    title: 'Alerte',
                    text: "voulez vous activer ce profil ?",
                    type: 'warning',
                    showCancelButton: true,
                   // dangerMode: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    cancelButtonText: 'Non',
                    confirmButtonText: 'Oui',
                    closeOnConfirm: true
                    //}).then(function (isConfirm) {
                    }).then((result) => {
                        if (result.isConfirmed) {
                            var href = "<?= base_url() ?>sys/C_sys_profil/change_etat/" + id + '/' + etat;
                            callBack_menu(href);
                    }
                });
        });
	
	});
  
</script>