<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<script src="<?= getBaseUrl() ?>assets/common/plugins/jquery/jquery.min.js"></script>
<!-- Content Header (Page header) -->
<?php App\Controllers\BackendController::createService()->showBreadCrumbs( $arrmixBreadCrumbData ) ?>
<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="js-show-alert-message">
            <?php App\Controllers\BackendController::createService()->showAlertMessage( $arrmixFlashData ) ?>
        </div>
        <!-- Small boxes (Stat box) -->
        <div class="card">
            <div class="card-header">
                <div class="card-tools">
                    <a href="<?= getBaseUrl() ?>users/add" class="btn btn-outline-success"> <i class="fas fa-plus-square"></i>&nbsp;&nbsp;Add User</a>
                </div>
            </div>
            <div class="card-body">
                <table class="table table-striped js-datatable">
                    <thead>
                        <tr>
                            <th>#</th>
                            <!-- <th>User Type</th> -->
                            <th>First Name</th>
                            <th>Last Name</th>
                            <th>Mobile Number</th>
                            <th>Email Id</th>
                            <th>DOB</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                            if( true == isValArr( $arrmixUserList ) ) {
                                $intCounter = 1;
                                foreach( $arrmixUserList as $arrmixUserDetails ) {
                        ?>
                                <tr>
                                    <td><?= $intCounter ?></td>
                                    <!-- <td><?php // $arrmixUserDetails['user_type_name'] ?></td> -->
                                    <td><?= $arrmixUserDetails['first_name']; ?></td>
                                    <td><?= $arrmixUserDetails['last_name']; ?></td>
                                    <td><?= $arrmixUserDetails['mobile_number']?></td>
                                    <td><?= $arrmixUserDetails['email_id']?></td>
                                    <td><?= $arrmixUserDetails['date_of_birth']?></td>
                                    <td class="project-actions">
                                        <a class="btn btn-outline-info btn-sm" href="<?= getBaseUrl() ?>users/edit?user_id=<?= $arrmixUserDetails['user_id']?>">
                                            <i class="fas fa-pencil-alt"></i>
                                        </a>
                                        <a class="btn btn-outline-danger btn-sm" href="javascript:void(0)" id="js-delete-user" data-user_id="<?= $arrmixUserDetails['user_id']?>" data-user_name="<?= $arrmixUserDetails['first_name'] . ' ' . $arrmixUserDetails['last_name']; ?>" onclick="showDeleteModal(this)">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </td>
                                </tr>
                        <?php 
                                    $intCounter++;
                                } 
                            } else { 
                        ?>    
                                <tr>
                                    <td colspan="7" class="text-center">No users found</td>
                                </tr>
                        <?php } ?>    
                    </tbody>
                </table>
            </div>
            <!-- /.card-body -->
        </div>
        <!-- /.row (main row) -->
    </div><!-- /.container-fluid -->
</section>
<!-- /.content -->

<script>
    $(document).ready(function () {
        
        $( '#js-modal-body-confirm-button' ).on( 'click', function() {
            
            var intUserId = $( "#js-modal-body-delete-confirm-hidden-id" ).val();
            
            $.ajax({
                type: "POST",
                url: "<?php echo getBaseUrl(); ?>" + "users/delete",
                dataType: 'json',
                data: { 'user_id' : intUserId },
                success: function( arrmixResponseData ) {
                    $( '#js-modal-delete-confirmation' ).modal( 'hide' );
                    
                    if( true == arrmixResponseData.success ) {
                        var strAlertMessage = $( '#js-modal-body-delete-confirm-header-name' ).text() + ' has been deleted successfully';
                        showAlertMessage( 'success', strAlertMessage )
                        
                        resetDeleteConfirmationModal();
                        
                        setTimeout(function(){ 
                            location.reload();
                        }, 2000);
                    } else {
                        showAlertMessage( 'danger', arrmixResponseData.message );
                    }
                }
            });
        });
    });
    
    function showDeleteModal(ths){
        var intUserId = $( ths ).data( 'user_id' );
        var strHeaderName = 'User <b>' + $( ths ).data( 'user_name' ) + '</b>';

        showDeleteConfirmationModal( strHeaderName, intUserId );
    }
</script>
