<?php $__env->startSection('content'); ?>
    <div>
        <h3>Kategorijas</h3>
    </div>
    <div class="col-xs-12">
        <div class="row header-row">
            <button id="new" class="btn btn-info">Pievienot</button>
        </div>
        <div class="admin-table" id="genre-table">
            <?php echo $__env->renderEach('admin.genre.row', $genres, 'genre'); ?>
        </div>
    </div>
    <script type="text/javascript">
        $( document ).ready(function() {
            showNewFormBind();
            editBind();
            removeBind();
            showNewSubFormBind();

            function removeBind() {
                var $button = $("button.btn-remove");
                $button.tooltip();
                $button.click(function () {
                    var id = $(this).attr('data-id');
                    var url = "/admin/genre/"+id;
                    $.ajax({
                        url: url,
                        type: 'DELETE',
                        success: function(result,status,xhr) {
                            $(".content-row[data-id = '"+id+"']").replaceWith(result);
                        },
                        complete: function(xhr,status) {
                            restoreBind();
                        }
                    });
                });
            }

            function restoreBind() {
                var $button = $("button.btn-restore");
                $button.tooltip();
                $button.click(function () {
                    var id = $(this).attr('data-id');
                    var url = "/admin/genre/"+id;
                    $.ajax({
                        url: url,
                        type: 'GET',
                        success: function(result,status,xhr) {
                            $(".content-row[data-id = '"+id+"']").replaceWith(result);
                        },
                        complete: function(xhr,status) {
                            editBind();
                            removeBind();
                            showNewSubFormBind();
                        }
                    });
                });
            }

            function editBind() {
                var $button = $("button.btn-edit");
                $button.tooltip();
                $button.click(function() {
                    var id = $(this).attr('data-id');
                    var url = "/admin/genre/"+id+"/edit";
                    $.ajax({
                        url: url,
                        type: 'GET',
                        success: function(result,status,xhr) {
                            $(".content-row[data-id = '"+id+"']").replaceWith(result);
                        },
                        complete: function(xhr,status) {
                            submitBind();
                            $(".btn-remove-form").click(function() {
                                $("form[data-id = '"+id+"']").remove();
                            });
                            $("form[data-id = '"+id+"']").find("input#name").focus();
                        }
                    });
                });
            }

            function submitBind() {
                var $button = $("button.btn-submit");
                $button.tooltip();
                $(".edit-form").submit(function () {
                    var $form = $(this);
                    var formData = $form.serializeArray();
                    var url = $form.attr('action');
                    $.ajax({
                        url: url,
                        type: 'PUT',
                        data: formData,
                        success: function(result,status,xhr) {
                            $form.replaceWith(result);
                        },
                        complete: function(xhr,status) {
                            editBind();
                            removeBind();
                            submitBind();
                            showNewSubFormBind();
                            showNewSubFormBind();
                        }
                    });

                    return false;
                });
            }

            function newFormSubmit() {
                $("#new-form").submit(function(event) {
                    var $form = $(this);
                    var formData = $form.serializeArray();
                    var parentId = $('#parent_id').val();
                    var url = $form.attr('action');
                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: formData,
                        success: function(result,status,xhr) {
                            $form.remove();
                            if (parentId) {
                                $('#genre-table #'+parentId).append(result);
                            } else {
                                $('#genre-table').append(result);
                            }
                        },
                        complete: function(xhr,status) {
                            $(".btn-remove-new").click(function() {
                                $("#new-form").remove();
                            });
                            editBind();
                            removeBind();
                            newFormSubmit();
                            showNewSubFormBind();
                        }
                    });
                    return false;
                });
            }

            function showNewFormBind() {
                $("button#new").click(function() {
                    $("#new-form").remove();
                    if (!$("#new-form").length) {
                        $.ajax({
                            url: "/admin/genre/create",
                            type: 'GET',
                            success: function(result,status,xhr) {
                                $('#genre-table').append(result);
                            },
                            complete: function(xhr,status) {
                                $(".btn-remove-new").click(function() {
                                    $("#new-form").remove();
                                });
                                newFormSubmit();
                                $("#new-form #name").focus();
                                $("#new-form").find("button").tooltip();
                            }
                        });
                    } else {
                        $("#new-form #name").focus();
                    }
                });
            }

            function showNewSubFormBind() {
                $("button.btn-sub").unbind( "click" );
                $("button.btn-sub").click(function() {
                    var parentId = $(this).attr('data-id');

                    $("#new-form").remove();
                    if (!$("#new-form").length) {
                        $url = "/admin/genre/create/"
                        if (parentId) {
                            $url = $url+$(this).attr('data-id');
                        }
                        $.ajax({
                            url: $url,
                            type: 'GET',
                            success: function(result,status,xhr) {
                                $('#genre-table #'+parentId).append(result);
                            },
                            complete: function(xhr,status) {
                                $(".btn-remove-new").click(function() {
                                    $("#new-form").remove();
                                });
                                newFormSubmit();
                                $("#new-form #name").focus();
                                $("#new-form").find("button").tooltip();
                            }
                        });
                    } else {
                        $("#new-form #name").focus();
                    }
                });
            }

        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>