<?php
$page = (!empty($_GET['page-id'])) ? $_GET['page-id'] : 1;
$db->pageLimit  = 50;
$prices = $db->objectbuilder()->orderBy('id', 'ASC')->paginate(T_SONG_PRICE, $page);

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
header("Location: " . LoadAdminLinkSettings('manage-song-price'));
exit();
}
?>
<div class="container-fluid">
    <div>
        <h3>Manage Song Price</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Songs</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Manage Song Price</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="card-body">
					<button class="btn btn-info add-content pull-right" >Add new price</button>
					<h6 class="card-title">Manage Song Price</h6>
                    <div class="row">
                        <div class="col-md-12" style="margin-bottom:0;">
                            <div class=" add_category_form_alert"></div>
                            <div class="clearfix"></div>
                            <br>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                            <tr>
                                <th><input type="checkbox" id="check-all" class="filled-in check-all" ><label for="check-all"></label></th>
                                <th>ID</th>
                                <th>Price</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                                foreach ($prices as $key => $price) {
                                    echo LoadAdminPage('manage-song-price/list', array('PRICE_DATA' => $price));
                                }
                            ?>
                            </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-song-price?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-song-price?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-song-price?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-song-price?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-song-price?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 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 class="clearfix"></div>
    </div>
</div>
    <!-- #END# Vertical Layout -->
    <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 price?</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 price?
                </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>
    <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 price?</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 price?
                </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 id="edit-modal" class="modal fade" role="dialog" data-id="">
        <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">EDIT PRICE</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <div class="form-lins">
                            <label class="form-lasbel">Song Price:</label>
                            <input type="text" class="form-control" name="song_price" id="song_price" value="">
                        </div>
                    </div>
                </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" id="edit_price_button">EDIT</button>
                </div>
            </div>
        </div>
    </div>

    <div id="add-modal" class="modal fade" role="dialog" data-id="">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">ADD PRICE</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <div class="form-lins">
                            <label class="form-lasbel">Song Price:</label>
                            <input type="text" class="form-control" name="new_song_price" id="new_song_price" value="0.00">
                        </div>
                    </div>
                </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" id="add_price_button">ADD</button>
                </div>
            </div>
        </div>
    </div>

    <script>
$('.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 + ')');
});

$('.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("{{CONFIG ajax_url}}/ap/remove_multi_price", {ids: data}, function () {
        $.each( data, function( index, value ){
            $('#list-' + value).remove();
        });
        $('.delete-selected').text('Delete Selected');
    });
}
function Wo_DeletePrice(id,type = 'show') {
    if (id == '') {
    return false;
    }
    if (type == 'hide') {
      $('#DeleteModal').find('.btn-primary').attr('onclick', "Wo_DeletePrice('"+id+"')");
      $('#DeleteModal').modal('show');
      return false;
    }
    $.post('{{CONFIG ajax_url}}/ap/delete-song-price', {id: id}, function(data, textStatus, xhr) {
        $('#list-' + id).fadeOut(300, function () {
            $(this).remove();
        });
    });
}

        $(function () {
            $('.edit-content').on('click', function(event) {
                event.preventDefault();
                $('#edit-modal').attr('data-id', $(this).attr('data-id')).modal('show');
                $('#song_price').val($(this).attr('data-price'));
                $('#edit-modal').on('shown.bs.modal', function () {
                    $('#song_price').focus();
                    $('#song_price').select();
                })
            });
        });
        $(function () {
            $('.delete-content').on('click', function(event) {
                event.preventDefault();
                $('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
            });

            $('.add-content').on('click', function(event) {
                event.preventDefault();
                $('#add-modal').modal('show');
                $('#add-modal').on('shown.bs.modal', function () {
                    $('#new_song_price').focus();
                    $('#new_song_price').select();
                });
            });


            $('#edit_price_button').on('click', function(event) {
                event.preventDefault();
                let id = $('#edit-modal').attr('data-id');
                let new_value = $('#song_price').val();

                var data = new FormData();
                    data.append('hash_id','<?php echo createMainSession();?>');
                    data.append('id',id);
                    data.append('price',new_value);
                $.ajax({
                    url: '{{CONFIG ajax_url}}/ap/update_price',
                    type : 'POST',
                    data : data,
                    contentType:false,
                    cache: false,
                    processData:false,
                    success : function(data) {
                        if (data.status == 200) {
                            $('#price_text_'+id).html(new_value);
                        }
                    }
                });
            });

            $('#add_price_button').on('click', function(event) {
                event.preventDefault();
                let new_value = $('#new_song_price').val();
                if(isNaN(new_value)){
                    $('#new_song_price').val('0.00');
                    $('#new_song_price').focus();
                    $('#new_song_price').select();
                }
                var data = new FormData();
                    data.append('hash_id','<?php echo createMainSession();?>');
                    data.append('price',new_value);
                $.ajax({
                    url : "{{CONFIG ajax_url}}/ap/add_price",
                    type : "POST",
                    data : data,
                    contentType:false,
                    cache: false,
                    processData:false,
                    success : function(data) {
                        if (data.status == 200) {
                            location.reload();
                        }
                    }
                });
            });

        });
    </script>