<?php 
$filter_keyword = (!empty($_GET['query'])) ? PT_Secure($_GET['query']) : '';
$link = "";
if (!empty($filter_keyword)) {
    $link .= "&query=".$filter_keyword;
    $sql   = " (name LIKE '%$filter_keyword%' OR ad_link LIKE '%$filter_keyword%') ";
    $db->where($sql);
} 
$sort_link = $link;
$sort_array = array('DESC_i' => array('id' , 'DESC'),
                    'ASC_i'  => array('id' , 'ASC'),
                    'DESC_n' => array('name' , 'DESC'),
                    'ASC_n'  => array('name' , 'ASC'),
                    'DESC_c' => array('clicks' , 'DESC'),
                    'ASC_c'  => array('clicks' , 'ASC'),
                    'DESC_v' => array('views' , 'DESC'),
                    'ASC_v'  => array('views' , 'ASC'));
if (!empty($_GET['sort']) && in_array($_GET['sort'], array_keys($sort_array))) {
    $db->orderBy($sort_array[$_GET['sort']][0],$sort_array[$_GET['sort']][1]);
    $link .= "&sort=".PT_Secure($_GET['sort']);
}
else{
    $_GET['sort'] = 'DESC_i';
    $db->orderBy('id', 'DESC');
}
$get_ads = $db->get(T_VIDEO_ADS);
?>
<div class="container-fluid">
    <div>
        <h3>Manage Video Ads</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Admin Panel</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Advertisement</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Manage Video Ads</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="card-body">
					<div class="btn-group pull-right">
                        <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                            Create New Ad <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-right">
                            <li><a href="<?php echo PT_LoadAdminLinkSettings('create-video-ad?type=video'); ?>" class=" dropdown-item">Video Ad</a></li>
                            <li><a href="<?php echo PT_LoadAdminLinkSettings('create-video-ad?type=image'); ?>" class=" dropdown-item">Image Ad</a></li>
                            <li><a href="<?php echo PT_LoadAdminLinkSettings('create-video-ad?type=vast'); ?>" class=" dropdown-item">Vast / Vpaid</a></li>
                        </ul>
                    </div>
                  <h6 class="card-title">Manage Video Ads</h6>
                  
                    <div class="clearfix"></div>
                    <br>
                    <div class="body">
                        <div class="row">
                           <div class="col-md-6" style="margin-bottom:0;">
                                <form method="get" action="<?php echo PT_LoadAdminLinkSettings('manage-video-ads'); ?>">
                                  <div class="row">
                                    <div class="col-md-8">
                                        <div class="form-group form-float">
                                            <div class="form-line">
                                                <label class="form-label search-form">Search for Keyword</label>
                                                <input type="text" name="query" id="query" class="form-control" value="<?php echo(!empty($_GET['query']) ? $_GET['query'] : '') ?>">
                                                
                                            </div>
                                        </div>
                                      </div>
                                      <div class="col-md-1">
                                        <label>&nbsp;</label>
                                    <div></div>
                                         <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 width="50"><input type="checkbox" id="check-all" class="filled-in check-all" ><label for="check-all"></label></th>
                                          <th width="130">ID 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_i') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=ASC_i") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=DESC_i") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                          <th>Name 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_n') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=ASC_n") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=DESC_n") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                          <th>Type</th>
                                          <th>Clicks 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_c') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=ASC_c") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=DESC_c") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                          <th>Views 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_v') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=ASC_v") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-video-ads?page-id=1').$sort_link."&sort=DESC_v") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                          <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    foreach ($get_ads as $key => $video_ad) {
                                        $type = 'Video Ad';
                                        if (!empty($video_ad->ad_image)) {
                                            $type = 'Image Ad';
                                        }
                                        if (!empty($video_ad->vast_xml_link)) {
                                            $type = 'Vast Ad';
                                        }
                                        ?>
                                        <tr id="list-<?php echo $video_ad->id ?>" data-id="<?php echo $video_ad->id ?>" data_selected="<?php echo $video_ad->id ?>">
                                            <td><input type="checkbox" id="check-data-<?php echo $video_ad->id ?>" class="delete-checkbox filled-in"><label for="check-data-<?php echo $video_ad->id ?>"></label></td>
                                            <td><?php echo $video_ad->id ?></td>
                                            <td><?php echo $video_ad->name ?></td>
                                            <td><?php echo $type ?></td>
                                            <td><?php echo number_format($video_ad->clicks) ?></td>
                                            <td><?php echo number_format($video_ad->views) ?></td>
                                            <td>
                                                <a class="btn bg-success admn_table_btn" href="<?php echo PT_LoadAdminLinkSettings('edit-video-ad?id=' . $video_ad->id); ?>">Edit</a>
                                                <button class="btn bg-danger admn_table_btn" data-id="<?php echo $video_ad->id ?>" onclick="DeleteVideoAd('<?php echo $video_ad->id ?>','hide')">Delete</button>
                                            </td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="clearfix"></div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3">
                                <span>&nbsp;</span>
                                <button type="button" class="btn btn-info waves-effect delete-selected d-block" disabled>Delete Selected<span></span></button>
                            </div>
                        </div>
                    </div>
                </div>
                    
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->

<div class="modal fade" id="SelectedDeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Delete Ad?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure that you want to remove the selected ad(s)?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="DeleteSelected()" data-dismiss="modal">Delete</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="DeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Delete Ad?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete this ad?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">Delete</button>
            </div>
        </div>
    </div>
</div>
<script>
function DeleteVideoAd(id,type = 'show') {
  if (!id) {
      return false;
  }
  if (type == 'hide') {
    $('#DeleteModal').find('.btn-primary').attr('onclick', "DeleteVideoAd('"+id+"')");
    $('#DeleteModal').modal('show');
    return false;
  }
  $.post('{{LINK aj/ap/delete-video_ad}}', {id: id}, function(data, textStatus, xhr) {
        if (data.status == 200) {
            $('#list-'+id).slideUp('slow');
            setTimeout(function () {
                $('#list-'+id).remove();
            },2000);
        }
    });
}
$('.check-all').on('click', function(event) {
    $('input:checkbox').not(this).prop('checked', this.checked);
});
$('.delete-checkbox, .check-all').change(function(event) {
    $('.delete-selected').attr('disabled', false);
    $('.delete-selected').find('span').text(' (' + $('.delete-checkbox:checked').length + ')');
});
$('.submit-selected').on('click', function(event) {
    event.preventDefault();
    $('#SelectedStatusModal').modal('show');
});

$('.delete-selected').on('click', function(event) {
    event.preventDefault();
    $('#SelectedDeleteModal').modal('show');
});
function DeleteSelected() {
    data = new Array();
    $('td input:checked').parents('tr').each(function () {
        data.push($(this).attr('data_selected'));
    });
    $('.delete-selected').attr('disabled', true);
    $('.delete-selected').text('Please wait..');
    $.post("{{LINK aj/ap/remove_multi_ad}}", {ids: data}, function () {
        $.each( data, function( index, value ){
            $('#list-' + value).remove();
        });
        $('.delete-selected').text('Delete Selected');
    });
}
</script>