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

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

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

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

{block name="main-content"}
    {$coupon_page = {$smarty.get.page|default:1}}

    <div class="coupons">

    <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><a href="{url path='admin/coupon'}">{intl l='Coupons'}</a></li>
            </ul>
        </nav>

        {hook name="coupon.top" location="coupon_top" }

        <div class="row">
            <div class="col-md-12">
                <div class="general-block-decorator">
                    <div class="table-responsive">
                        <table class="table table-striped table-condensed" id="folder_list">

                            <caption>
                                {intl l='Coupons'}

                                {hook name="coupon.list-caption" location="coupon_list_caption" }

                                {include file='renderer/buttons.html' btn_group=false buttons=[
                                    [
                                        'type' => 'create',
                                        'class' => 'action-btn',
                                        'title' => {intl l='Create a new coupon'},
                                        'href' => {url path='/admin/coupon/create'},
                                        'auth' => ['resource' => 'admin.coupon'],
                                        'data' => [
                                            'toggle' => 'modal'
                                        ]
                                    ]
                                ]}
                            </caption>

                            <thead>
                                <tr>
                                    <th>{admin_sortable_header
                                        current_order=$coupon_order
                                        order='code'
                                        reverse_order='code-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Code'}"
                                    }
                                    </th>

                                    <th>{admin_sortable_header
                                        current_order=$coupon_order
                                        order='title'
                                        reverse_order='title-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Title'}"
                                    }
                                    </th>

                                    <th class="text-center">{admin_sortable_header
                                        current_order=$coupon_order
                                        order='enabled'
                                        reverse_order='enabled-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Status'}"
                                    }
                                    </th>

                                    <th class="text-center">{admin_sortable_header
                                        current_order=$coupon_order
                                        order='start-date'
                                        reverse_order='start-date-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Start Date'}"
                                        }
                                    </th>

                                    <th class="text-center">{admin_sortable_header
                                        current_order=$coupon_order
                                        order='expiration-date'
                                        reverse_order='expiration-date-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Expiration date'}"
                                    }
                                    </th>

                                    <th class="text-center">{admin_sortable_header
                                        current_order=$coupon_order
                                        order='days-left'
                                        reverse_order='days-left-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Days before expiration'}"
                                    }
                                    </th>

                                    <th class="text-center">{admin_sortable_header
                                        current_order=$coupon_order
                                        order='usages-left'
                                        reverse_order='usages-left-reverse'
                                        path={url path='/admin/coupon'}
                                        request_parameter_name='coupon_order'
                                        label="{intl l='Usages left'}"
                                    }
                                    </th>

                                    {hook name="coupon.table-header" location="coupon_table_header" }

                                    <th>{intl l="Actions"}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {loop type="coupon" name="list_coupon" page={$coupon_page} limit={config key="number_default_results_per_page.coupon_list" default=20} order={$coupon_order|default:'code'} backend_context="true"}
                                    <tr>
                                        <td>
                                            {loop type="auth" name="can_change" role="ADMIN" resource="admin.coupon" access="UPDATE"}
                                                <a title="{intl l='Change this coupon'}" href="{url path="/admin/coupon/update/%id" id=$ID}">{$CODE}</a>
                                            {/loop}
                                            {elseloop rel="can_change"}
                                                {$CODE}
                                            {/elseloop}
                                        </td>

                                        <td>{$TITLE}</td>

                                        <td class="text-center">
                                            {if $IS_ENABLED}
                                                <span class="label label-success">{intl l="Enabled"}</span>
                                            {else}
                                                <span class="label label-default">{intl l="Disabled"}</span>
                                            {/if}
                                        </td>

                                        <td class="text-center">
                                            {if $START_DATE == null}
                                                /
                                            {else}
                                                {format_date date=$START_DATE output="date"}
                                            {/if}
                                        </td>

                                        <td class="text-center">{format_date date=$EXPIRATION_DATE output="date"}</td>

                                        <td class="text-center">
                                            {if $DAY_LEFT_BEFORE_EXPIRATION <= 0}
                                                <span class="label label-default">{intl l='Expired'}</span>
                                            {else}
                                                {$DAY_LEFT_BEFORE_EXPIRATION}
                                            {/if}
                                        </td>

                                        <td class="text-center">
                                            {if $USAGE_LEFT == -1}
                                                {intl l="Unlimited"}
                                            {elseif $USAGE_LEFT}
                                                {$USAGE_LEFT}
                                            {else}
                                                <span class="label label-default">0</span>
                                            {/if}
                                        </td>

                                        {hook name="coupon.table-row" location="coupon_table_row" coupon_id={$ID} }

                                        <td class="actions">
                                            {include file='renderer/buttons.html' buttons=[
                                                [
                                                    'type' => 'edit',
                                                    'title' => {intl l='Change this coupon'},
                                                    'href' => {url path="/admin/coupon/update/%id" id=$ID},
                                                    'auth' => ['resource' => 'admin.coupon']
                                                ],
                                                [
                                                    'type' => 'delete',
                                                    'class' => 'delete_coupon_btn',
                                                    'title' => {intl l='Delete this coupon'},
                                                    'href' => '#delete_coupon',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal',
                                                        'target' => '#delete_coupon'
                                                    ],
                                                    'auth' => ['resource' => 'admin.coupon']
                                                ]
                                            ]}
                                        </td>
                                    </tr>
                                {/loop}
                                {elseloop rel="list_coupon"}
                                    <tr>
                                        <td colspan="100">
                                            <div class="alert alert-info text-center">
                                                <p>{intl l="No coupon is currently defined. Please use the (+) button above to create your first coupon."}</p>
                                            </div>
                                        </td>
                                    </tr>
                                {/elseloop}
                            </tbody>
                            {ifloop rel="list_coupon"}
                                <tfoot>
                                <tr>
                                    <td colspan="100">
                                        {include
                                        file = "includes/pagination.html"

                                        loop_ref       = "list_coupon"
                                        max_page_count = 10
                                        page_url       = {url path="/admin/coupon" orders_order=$coupon_order}
                                        }

                                    </td>
                                </tr>
                                </tfoot>
                            {/ifloop}
                        </table>
                    </div>
                </div>

                {hook name="coupon.bottom" location="coupon_bottom" }

            </div>
        </div>

    </div> <!-- #wrapper -->

    {* DELETE MODAL *}
    {capture "delete_dialog"}
        <input type="hidden" name="coupon_id" id="coupon_delete_id" value="" />

        {hook name="coupon.delete-form" }
    {/capture}

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

    dialog_id       = "delete_coupon"
    dialog_title    = {intl l="Delete coupon"}
    dialog_message  = {intl l="Do you really want to delete this coupon ?"}

    form_action         = {token_url path='/admin/coupon/delete'}
    form_content        = {$smarty.capture.delete_dialog nofilter}
    }
</div>

{/block}


{block name="javascript-initialization"}

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

    {javascripts file='assets/js/tablesorter/jquery.tablesorter.min.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}

    {javascripts file='assets/js/tablesorter/jquery.metadata.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}

    {javascripts file='assets/js/tablesorter/jquery.tablesorter.widgets.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}

    {javascripts file='assets/js/tablesorter/jquery.tablesorter.widgets-filter-formatter.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}

    <script>
        $(document).ready(function() {
            $(".delete_coupon_btn").click(function() {
                $("#coupon_delete_id").val($(this).data("id"));
            });
        });
    </script>
{/block}

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