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

{block name="no-return-functions"}
    {$admin_current_location = 'modules'}
{/block}

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

{block name="check-resource"}admin.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>{intl l="Modules"}</li>
            </ul>

        </div>

        {hook name="modules.top" location="modules_top" }

        <div class="row">

            {if $module_errors}
            {include file="includes/module-errors.html"}
            {/if}

            {loop type="auth" name="can_create" role="ADMIN" resource="admin.module" access="UPDATE"}
            {include file="includes/module-install.html"}
            {/loop}

            <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="2" caption_title={intl l='Delivery modules'}}
            {include file="includes/module-block.html" module_type="3" caption_title={intl l='Payment modules'}}
            {include file="includes/module-block.html" module_type="1" caption_title={intl l='Classic modules'}}

            </div>


            <div class="col-md-12">
                <div class="text-right">
                    <a href="{url path='/admin/hooks'}" class="btn btn-primary">{intl l="Manage hooks"} <span class="glyphicon glyphicon-chevron-right"></span></a>
                    <a href="{url path='/admin/module-hooks'}" class="btn btn-primary">{intl l="Manage modules attachements"}  <span class="glyphicon glyphicon-chevron-right"></span></a>
                </div>
            </div>
        </div>

        {hook name="modules.bottom" location="modules_bottom" }

    </div>
</div>

{* Delete module confirmation dialog *}

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

    <div class="row">
        <div class="col-md-12">
		    <div class="form-group">
		        <div class="checkbox">
		            <label>
		                <input type="checkbox" name="delete-module-data" value="1">
		                {intl l="Delete also module data"}
		            </label>
		        </div>
		    </div>
		</div>
	</div>

{/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         = {token_url path='/admin/module/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>

{* Module information dialog *}
<div class="modal fade" id="module-information" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3 id="module-information-title"></h3>
            </div>

            <div class="modal-body" id="module-information-body">
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">{intl l='Close'}</button>
            </div>
        </div>
    </div>
</div>

{/block}

{block name="javascript-initialization"}

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

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

    <script>
        $(document).ready(function() {

            $("#module-install").on("submit", function(event) {
                var $loader = $('<div class="loader"></div>');
                $('body').append($loader);
                $loader.show();
            });

           var url_management = "{url path="/admin/module/toggle-activation/"}";
           $(".module-activation").on("switch-change", function(e, data){

               var checkbox = $(this);
        	   var module_id = checkbox.data('id');
        	   var is_checked = data.value;

               $('body').append('<div class="modal-backdrop fade in" id="loading-event"><div class="loading"></div></div>');
               $.ajax({
                    url: url_management+$(this).data('id')
               }).always(function(){
                   $("#loading-event").remove();
               }).done(function() {
                   location.reload();
               }).fail(function(jqXHR, textStatus, errorThrown){
                    checkbox.bootstrapSwitch('toggleState', true);
                    $('#module-failed-body').html(jqXHR.responseJSON.error);
                    $("#module-failed").modal("show");
               });

           });

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

           {* Inline editing of object position using bootstrap-editable *}

           $('.modulePositionChange').editable({
               type        : 'text',
               title       : "{intl l="Enter new module position"}",
               mode        : 'popup',
               inputclass  : 'input-mini',
               placement   : 'left',
               success     : function(response, newValue) {
                   // The URL template
                   var url = "{url noamp='1' path='/admin/module/update-position' module_id='__ID__' position='__POS__'}";

                   // Perform subtitutions
                   url = url.replace('__ID__', $(this).data('id'))
                   .replace('__POS__', newValue);

                   // Reload the page
                   location.href = url;
               }
           });

            {* module errors *}

            $('.module-error-more').on('click', function(event){
                event.preventDefault();

                $(event.currentTarget).next('.more').toggleClass('hidden');
            });

            $('.module-information').click(function(event) {
                showModuleInformation(
                        '{url path='/admin/module/information/__ID__'}'.replace('__ID__', $(this).data('id'))
                );

                event.preventDefault();
            });

            $('.module-documentation').click(function(event) {
                showModuleInformation(
                        '{url path='/admin/module/documentation/__ID__'}'.replace('__ID__', $(this).data('id'))
                );

                event.preventDefault();
            });

            function showModuleInformation(url) {
                $('body').append('<div class="modal-backdrop fade in" id="loading-event"><div class="loading"></div></div>');

                $.ajax({
                    url: url
                }).always(function(){
                    $("#loading-event").remove();
                }).success(function(data) {
                    $('#module-information-title').html(data.title);
                    $('#module-information-body').html(data.body);
                    $("#module-information").modal("show");
                });
            }
        });
    </script>

{/block}

{block name="javascript-last-call"}
    {hook name="modules.js" location="modules-js" }
{/block}