{extends file="admin-layout.tpl"}

{block name="page-title"}{intl l='Modules'}{/block}

{block name="check-resource"}admin.configuration.module{/block}
{block name="check-access"}view{/block}

{block name="main-content"}
<div class="modules">

    <div id="wrapper" class="container">

        <div class="clearfix">
            <ul class="breadcrumb pull-left">
                <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
                <li><a href="{url path='/admin/modules'}">{intl l="Modules"}</a></li>
            </ul>
{*            {loop type="auth" name="can_create" role="ADMIN" resource="admin.module" access="UPDATE"}
                <a class="btn btn-default btn-primary pull-right" title="{intl l='Install a new module'}" href="#install_dialog" data-toggle="modal">
                    <span class="glyphicon glyphicon-import"></span> {intl l="Install a new module"}
                </a>
            {/loop}
*}
        </div>

        {module_include location='modules_top'}

        <div class="row">
            <div class="col-md-12">
            {if $error_message}<div class="alert alert-danger">{$error_message}</div>{/if}
            {include file="includes/module-block.html" module_type="1" caption_title={intl l='Classic modules'}}
            {include file="includes/module-block.html" module_type="2" caption_title={intl l='Delivery modules'}}
            {include file="includes/module-block.html" module_type="3" caption_title={intl l='Payment modules'}}

            </div>
        </div>

        {module_include location='modules_bottom'}

    </div>
</div>

{* Delete module confirmation dialog *}

{capture "delete_module_dialog"}
    <input type="hidden" name="module_id" id="delete_module_id" value="" />
{/capture}

{include
    file = "includes/generic-confirm-dialog.html"

    dialog_id       = "delete_module_dialog"
    dialog_title    = {intl l="Delete a module"}
    dialog_message  = {intl l="Do you really want to delete this module ?"}

    form_action         = {url path='/admin/configuration/modules/delete'}
    form_content        = {$smarty.capture.delete_module_dialog nofilter}
}

<div class="modal fade" id="module-failed" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3>{intl l="An error occured"}</h3>
            </div>
            <div class="modal-body" id="module-failed-body">

            </div>
        </div>
    </div>
</div>

{/block}

{block name="javascript-initialization"}

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

    <script>
        $(document).ready(function(){
           var url_management = "{url path="/admin/configuration/modules/toggle-activation/"}";
           $(".module-activation").on("switch-change", function(e, data){
               $('body').append('<div class="modal-backdrop fade in" id="loading-event"><div class="loading"></div></div>');
               $.ajax({
                    url: url_management+$(this).data('id')
               }).done(function(){
                   $("#loading-event").remove();
               })
                .fail(function(jqXHR, textStatus, errorThrown){
                    $("#loading-event").remove();
                    $('#module-failed-body').html(jqXHR.responseJSON.error);
                    $("#module-failed").modal("show");
               });

           });

           $(".module-delete-action").click(function(){
                $("#delete_module_id").val($(this).data("id"));
           });
        });
    </script>
{/block}