<?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} (`name` LIKE '%$filter_keyword%' OR `headline` LIKE '%$filter_keyword%' OR `description` LIKE '%$filter_keyword%' ";
	$user_ads = $db->where($sql)->orderBy('id', 'DESC')->objectbuilder()->paginate(T_USR_ADS, $page);
}

else {

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

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
	header("Location: " . LoadAdminLinkSettings('manage-user-ads'));
	exit();
}
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Advertisements &gt; Manage User Advertisements</h2>
    </div>
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage User Advertisements</h2>
                </div>
                <div class="body">
                	<div class="table-responsive1">
	                	<table class="table table-bordered table-striped table-hover">
		                	<thead>
		                        <tr role="row">
		                        	<th>Website</th>
		                        	<th>Title</th>
		                        	<th>User</th>
		                        	<th>Wallet</th>
		                        	<th>Published</th>
		                        	<th>Placement</th>
		                        	<th>Results</th>
									<th>Spent</th>
		                        	<th>Action</th>
		                        </tr>
		                    </thead>
		                    <tbody>
		                    	<?php 
	                                foreach ($user_ads as $key => $music->user_ad) {
		                                $currency         = "";
										if ($music->config->currency == "USD") {
											$currency     = "$";
										}
										else if($music->config->currency == "EUR"){
											$currency     = "€";
										}
	                                	$ad_owner = UserData($music->user_ad->user_id);
	                                	echo LoadAdminPage('manage-user-ads/list',array(
	                                		'AD_ID' => $music->user_ad->id,
	                                		'AD_WEBSITE' => url_domain(urldecode($music->user_ad->url)),
	                                		'AD_TITLE' => $music->user_ad->headline,
	                                		'AD_URL' => urldecode($music->user_ad->url),
	                                		'DATE' => date('Y-M-d',$music->user_ad->posted),
											'CLICKS' => getAdAction($music->user_ad->id, 'click'),
											'VIEWS' => getAdAction($music->user_ad->id,'view'),
	                                		'USER_NAME' => $ad_owner->name,
											'APPEARS' => ($music->user_ad->placement == 1) ? 'Track' : 'Paged',
											'SETTINGS_LINK' => UrlLink('settings/general/' . $ad_owner->username),
	                                		'WALLET' =>  number_format($ad_owner->wallet,2),
	                                		'CURRENCY'   => $currency,
	                                		'SPENT' => number_format(getAdSpent($music->user_ad->id),2)
	                                	));
	                                }
	                            ?>
		                    </tbody>
	                    </table>
                	</div>
                	<div class="acp-usrads-pagination">
	                	<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-user-ads?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-user-ads?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-user-ads?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-user-ads?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-user-ads?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>
        <div class="clearfix"></div>
    </div>
</div>
<style>
	.acp-usrads-pagination{
		width: 100%;
		overflow: hidden;
	}

	.acp-usrads-pagination ul{
		margin: 0px;
	}

	.acp-usrads-pagination div.pull-left{
		padding: 5px 0px;
	}
</style>
<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 AD</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-ad" data-id="">DELETE</button>
        <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
      </div>
    </div>
  </div>
</div>

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

    // $(function () {
    //     $('.delete-button').on('click', function(event) {
    //         event.preventDefault();
    //         var ajax_type = $(this).attr('data-type');
    //         var id = $('#delete-modal').attr('data-id');
    //         $('#list-' + id).remove();
    //         $.post('{{CONFIG ajax_url}}/ap/delete_ad', {id: id});
    //     });
    // });

	function PT_ManageVRequests(id,self) {
        $(self).text('Please wait..').attr("disabled","true");

        $.post('{{CONFIG ajax_url}}/ap/get_user_ad', {id:id}, function(data){
            if (data.status == 200) {
                swal({
                    html:true,
                    title:"",
                    text:data.html,
                    showConfirmButton:false,
                    allowOutsideClick:true,
                    customClass:"sw-review-data",
                    animation:false
                })
                $(self).text('VIEW').removeAttr('disabled');
            } 
         },dataType="json");
    }
</script>