<?php 
$page = (!empty($_GET['page-id'])) ? $_GET['page-id'] : 1;
$filter_keyword = (!empty($_GET['query'])) ? Secure($_GET['query']) : '';
$filter_type    = '';
$db->pageLimit  = 50;

if (!empty($filter_keyword)) {
    $sql   = " {$filter_type} (username LIKE '%$filter_keyword%' OR email LIKE '%$filter_keyword%' OR id = '$filter_keyword' OR name LIKE '%$filter_keyword%') ";
	$users = $db->where($sql)->orderBy('id', 'DESC')->objectbuilder()->paginate(T_USERS, $page);
} 

else {

	$users = $db->objectbuilder()->orderBy('id', 'DESC')->paginate(T_USERS, $page);
}

$get_online_users = $db->where('last_active', time() - 60, '>')->getValue(T_USERS, 'COUNT(*)');

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
	header("Location: " . LoadAdminLinkSettings('manage-users'));
	exit();
}
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Users > Manage Users</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage & Edit Users</h2>
                </div>
                <div class="body">
                   <div class="row">
                       <div class="col-md-6" style="margin-bottom:0;">
                            <form method="get" action="<?php echo LoadAdminLinkSettings('manage-users'); ?>">
                              <div class="row">
                              	<div class="col-md-11">
	                              	<div class="form-group form-float">
	                                    <div class="form-line">
	                                        <input type="text" name="query" id="query" class="form-control" value="<?php echo (!empty($_GET['query'])) ? $_GET['query']: '';?>">
	                                        <label class="form-label search-form">Search for ID, Keyword, E-mail, Username, First Name, Last Name</label>
	                                    </div>
	                                </div>
	                              </div>
	                              <div class="col-md-1">
	                              	 <button class="btn btn-info">Search</button>
	                              </div>
                              </div>

                              <div class="clearfix"></div>
                           </form>
                       </div>
                   </div>
                   <div class="clearfix"></div>
                   <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                      <th>ID</th>
					                  <th>Username</th>
                                      <th>E-mail</th>
                                      <th>User Type</th>
					                  <th>IP address</th>
					                  <th>Status</th>
					                  <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                foreach ($users as $key => $user) {
                                	$user = UserData($user, array('data' => 1));
                                	$status = ($user->active == 1) ? '<span class="label label-success">Active</span>' : '<span class="label label-danger">Pending</span>';
                                	echo LoadAdminPage('manage-users/list', array('USER_DATA' => $user, 'SETTINGS_LINK' => UrlLink('settings/' . $user->username . '/general'), 'STATUS' => $status));
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="pull-left">
                    	<?php echo "Showing $page out of " . $db->totalPages; ?>
                    </div>
                    <div class="pull-right">
                    	<nav>
                            <ul class="pagination">
                            	<li>
                                    <a href="<?php echo LoadAdminLinkSettings('manage-users?page-id=1'); ?>" class="waves-effect" title='First Page'>
                                        <i class="material-icons">first_page</i>
                                    </a>
                                </li>
                                <?php if ($page > 1) {  ?>
                                <li>
                                    <a href="<?php echo LoadAdminLinkSettings('manage-users?page-id=' . ($page - 1)); ?>" class="waves-effect" title='Previous Page'>
                                        <i class="material-icons">chevron_left</i>
                                    </a>
                                </li>
                                <?php  } ?>
                                <?php 
                                $nums = 0;
                                $nums_pages = ($page > 4) ? ($page - 4) : $page;
                                for ($i=$nums_pages; $i <= $db->totalPages; $i++) { 
                                	if ($nums < 20) {
                                	?>
                                	<li class="<?php echo ($page == $i) ? 'active' : ''; ?>"><a href="<?php echo LoadAdminLinkSettings('manage-users?page-id=' . ($i)); ?>" class="waves-effect"><?php echo $i ?></a></li>
                                	<?php
                                	}
                                	$nums++;
                                }
                                ?>
                                <?php if ($db->totalPages > $page) { ?>
                                <li>
                                    <a href="<?php echo LoadAdminLinkSettings('manage-users?page-id=' . ($page + 1)); ?>" class="waves-effect" title="Next Page">
                                        <i class="material-icons">chevron_right</i>
                                    </a>
                                </li>
                                <?php } ?>
                                <li>
                                    <a href="<?php echo LoadAdminLinkSettings('manage-users?page-id=' . ($db->totalPages)); ?>" class="waves-effect" title='Last Page'>
                                        <i class="material-icons">last_page</i>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
<div id="delete-modal" class="modal fade" role="dialog" data-id="">
  <div class="modal-dialog">
    <div class="modal-content modal-col-red">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">DELETE USER</h4>
      </div>
      <div class="modal-body">
        <p>Are you sure you want to continue? this action can't be undo</p>
      </div>
      <div class="modal-footer">
      	<button type="button" class="btn btn-link waves-effect delete-button" data-dismiss="modal" data-type="user">DELETE</button>
        <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
      </div>
    </div>
  </div>
</div>


    <div class="modal fade" id="FollowersForm" role="dialog">
        <div class="modal-dialog">

            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Add Followers</h4><br>
                </div>
                <div class="modal-body">

                    <div class="alert alert-danger site-settings-alert"></div>
                    <form class="add-followers" method="POST">
                        <div class="form-group form-float">
                            <div class="form-line">
                                <input type="number" id="followers" name="followers" class="form-control">
                                <label class="form-label">How many followers you want to add? You can add between 1 and <?php echo $db->getValue(T_USERS, "COUNT(*)") - 1;?>.</label>
                            </div>
                        </div>
                        <input type="hidden" id="user_id" name="user_id">
                        <input type="hidden" name="hash_id" value="<?php echo createMainSession();?>">
                        <button type="submit" id="submitLikes" class="btn btn-primary m-t-15 waves-effect">Start</button>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" id="closeLikesForm" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>
    </div>


<script>
    function getUserId(user_id) {
        $('#user_id').val(user_id);
    }

$(function () {
	$('.delete-content').on('click', function(event) {
		event.preventDefault();
		$('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
	});
});

    $(function() {
        var form_site_settings = $('form.add-followers');
        form_site_settings.ajaxForm({
            url: '{{CONFIG ajax_url}}/ap/add_followers',
            beforeSend: function() {
                $('.site-settings-alert.alert-danger').empty();
                $('#submitLikes').text('Please wait..');
            },
            success: function(data) {
                if (data['status'] == 200) {
                    $('#submitLikes').text('Add');
                    $('#closeLikesForm').click();
                    $("html, body").animate({ scrollTop: 0 }, "slow");
                    $('.site-settings-alert.alert-success').html('<i class="fa fa-check"></i> Followers are being added in the background, this process might take few mins.');
                    setTimeout(function () {
                        $('.site-settings-alert').empty();
                    }, 10000);
                } else if (data['status'] == 500) {
                    $('#submitLikes').text('Add');
                    $('.site-settings-alert.alert-danger').html('<i class="fa fa-fw fa-exclamation-triangle"></i> ' + data['error']);
                }
            }
        });
    });
    $('#FollowersForm').on('hidden.bs.modal', function () {
        $('.site-settings-alert.alert-danger').empty();
    })

</script>