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

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

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

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

{block name="main-content"}
    {if $category_order != "manual"}
        {assign url_category "category_order="|cat:$category_order}
    {/if}
    {if $export_order != "manual"}
        {assign url_export "export_order="|cat:$export_order}
    {/if}

    <div id="export-modal"></div>

    <div class="configuration">

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

            <nav>
                <ul class="breadcrumb">
                    <li><a href="{url path='admin/home'}">{intl l='Home'}</a></li>
                    <li><a href="{url path='admin/tools'}">{intl l='Tools'}</a></li>
                    <li>{intl l="Exports"}</li>
                </ul>
            </nav>

            {hook name="exports.top" location="exports_top" }

            {loop name="export-category" type="export-category" order=$category_order}
                {ifloop rel="export-categ-list"}
                    {assign category_title $TITLE}
                    <div class="row">
                        <div class="col-md-12">
                            <div class="general-block-decorator">
                                <div class="table-responsive">
                                    <table class="table table-striped table-condensed table-left-aligned">
                                        <caption class="clearfix">
                                            {admin_position_block
                                                resource="admin.export"
                                                access="UPDATE"
                                                path={url path="admin/export/position/category"}
                                                url_parameter="id"
                                                in_place_edit_class="exportCategoryPositionChange"
                                                position=$POSITION
                                                id=$ID
                                            }
                                            {$TITLE}
                                        </caption>
                                        <thead>
                                            <tr>
                                                <th class="col-md-1">
                                                    {if $export_order == "id"}
                                                        <i class="glyphicon glyphicon-chevron-up"></i>
                                                    {elseif $export_order == "id_reverse"}
                                                        <i class="glyphicon glyphicon-chevron-down"></i>
                                                    {/if}
                                                    <a href="{url path="/admin/export"}?{if $url_category}{$url_category nofilter}&{/if}export_order=id{if $export_order == "id"}_reverse{/if}#category_{$category_title}">
                                                        {intl l="ID"}
                                                    </a>
                                                </th>
                                                <th class="col-md-2">
                                                    {if $export_order == "alpha"}
                                                        <i class="glyphicon glyphicon-chevron-up"></i>
                                                    {elseif $export_order == "alpha_reverse"}
                                                        <i class="glyphicon glyphicon-chevron-down"></i>
                                                    {/if}
                                                    <a href="{url path="/admin/export"}?{if $url_category}{$url_category nofilter}&{/if}export_order=alpha{if $export_order == "alpha"}_reverse{/if}#category_{$category_title}">
                                                        {intl l="Name"}
                                                    </a>
                                                </th>
                                                <th class="col-md-6">
                                                    {intl l="Description"}
                                                </th>
                                                <th class="col-md-2">
                                                    {if $export_order == "manual"}
                                                        <i class="glyphicon glyphicon-chevron-up"></i>
                                                    {elseif $export_order == "manual_reverse"}
                                                        <i class="glyphicon glyphicon-chevron-down"></i>
                                                    {/if}
                                                    <a href="{url path="/admin/export"}?{if $url_category}{$url_category nofilter}&{/if}export_order=manual{if $export_order == "manual"}_reverse{/if}#category_{$category_title}">
                                                        {intl l="Position"}
                                                    </a>
                                                </th>
                                                {hook name="export.table-header"}
                                                <th class="col-md-1">
                                                    {intl l="Actions"}
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {loop name="export-categ-list" type="export" order=$export_order category=$ID}
                                                <tr>
                                                    <td>
                                                        {$ID}
                                                    </td>
                                                    <td>
                                                        <a class="btn-show-export-modal" href="#category_{$category_title}" data-id="{$ID}" data-url="{$URL nofilter}">{$TITLE}</a>
                                                    </td>
                                                    <td>
                                                        {$DESCRIPTION nofilter}
                                                    </td>
                                                    <td>
                                                        {admin_position_block
                                                            resource="admin.export"
                                                            access="UPDATE"
                                                            path={url path="admin/export/position"}
                                                            url_parameter="id"
                                                            in_place_edit_class="exportPositionChange"
                                                            position=$POSITION
                                                            id=$ID
                                                        }
                                                    </td>
                                                    {hook name="export.table-row" export_id=$ID}
                                                    <td>
                                                        {include file='renderer/buttons.html' buttons=[
                                                            [
                                                                'type' => 'default',
                                                                'class' => 'btn-show-export-modal',
                                                                'title' => {intl l="Do this export"},
                                                                'data' => [
                                                                    'id' => $ID,
                                                                    'url' => {$URL nofilter}
                                                                ],
                                                                'icon' => ['class' => 'glyphicon glyphicon-open']
                                                            ]
                                                        ]}
                                                    </td>
                                                </tr>
                                            {/loop}
                                            {hook name="exports.row" category=$ID }
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                    </div>
                {/ifloop}
            {/loop}

            {elseloop rel="export-category"}
                <div class="alert alert-info">
                    {intl l="You don't have any export"}
                </div>
            {/elseloop}


            {hook name="exports.bottom" location="exports_bottom" }
        </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}

{/block}

{block name="javascript-last-call"}
    <script>
        $(document).ready(function() {

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

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

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

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

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

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

            var export_modal = $("#export-modal");

            $(".btn-show-export-modal").click(function() {

                var export_id = $(this).data("id");

                /**
                 * If we can't load the modal form, redirect to a page where the form is too.
                 */
                $.ajax(
                    $(this).data("url")
                ).success(function(data) {
                    export_modal.html(data);

                    var compression_switch = $("#export-compression-switch", export_modal);
                    var compression_row = $(".export-compression-selection-row", export_modal);

                    compression_switch.on("switch-change", function(e, data) {
                        var is_checked = data.value;

                        if (is_checked) {
                            compression_row.show("slow");
                        } else {
                            compression_row.hide("slow");
                        }
                    });

                    if ($("input[type=checkbox]", compression_switch).is(":checked")) {
                        compression_row.show();
                    } else {
                        compression_row.hide();
                    }

                    $(".make-switch", export_modal).bootstrapSwitch();
                    $("#real-export-modal").modal();

                }).fail(function() {
                    window.location.replace("{url path='/admin/export/'}"+export_id);
                });
            });
        });
    </script>
    {hook name="exports.js" location="exports-js" }
{/block}
