{javascripts file="assets/js/bootstrap-switch/bootstrap-switch.js"}
    <script src="{$asset_url}"></script>
{/javascripts}

<script type="text/javascript">
    (function($, $module){

        //delete
        var $modalDelete = $module.find('.js-modal-delete');

        $module.on('click', '.js-action-delete', function(event){
            var id = $(this).data('id');
            $modalDelete.find('form').attr('action', '{url path="/admin/module/CustomerFamily/delete"}/'+id);
            $modalDelete.modal('show');
            event.preventDefault();
        });
        //end delete

        //update
        var $modalUpdate = $module.find('.js-modal-update');

        $module.on('click', '.js-action-update', function(event){
            var id = $(this).data('id');

            $modalUpdate.find('.js-field-code').val($(this).data('code'));
            $modalUpdate.find('.js-field-title').val($(this).data('title'));

            $modalUpdate.find('form').attr('action', '{url path="/admin/module/CustomerFamily/update"}/'+id);
            $modalUpdate.modal('show');
            event.preventDefault();
        });
        //end update

        $module.find(".switch-small").bootstrapSwitch();

        // Default family
        $module.find(".update-default-family").on("switch-change", function(e, data){

            var form = $module.find("#update-default-family-form");
            $('body').append('<div class="modal-backdrop fade in" id="loading-event"><div class="loading"></div></div>');

            $('#default_family_id').val($(this).data('id'));

            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: form.serialize()
            })
            .done(function(){
                $("#loading-event").remove();
                location.href = "{url path='/admin/module/CustomerFamily'}";
            })
            .fail(function(jqXHR, textStatus, errorThrown){
                $("#loading-event").remove();
                $('#update-default-family-failed-body').html(jqXHR.responseJSON.error);
                $("#update-default-family-failed").modal("show");
            });
        });

    })(jQuery, jQuery('#CustomerFamily'));

</script>