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

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

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

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

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

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

            <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/brand'}">{intl l="Brands"}</a></li>
            </ul>

            {hook name='brands.top' location='brands_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">
                               <caption class="clearfix">
                                    {intl l='Brands'}
                                    {include file='renderer/buttons.html' btn_group=false buttons=[
                                        [
                                            'type' => 'create',
                                            'class' => 'action-btn',
                                            'title' => {intl l='Add a new brand'},
                                            'href' => '#creation_dialog',
                                            'auth' => ['resource' => 'admin.brand'],
                                            'data' => [
                                                'toggle' => 'modal'
                                            ]
                                        ]
                                    ]}
                                </caption>

                                <thead>
                                    <tr>
                                        <th>
                                            {admin_sortable_header
                                            current_order=$order
                                            order='id'
                                            reverse_order='id-reverse'
                                            path='/admin/brand'
                                            label="{intl l='ID'}"
                                            }
                                        </th>

                                        <th>&nbsp;</th>

                                        <th>
                                            {admin_sortable_header
                                            current_order=$order
                                            order='alpha'
                                            reverse_order='alpha-reverse'
                                            path='/admin/brand'
                                            label="{intl l='Name'}"
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='manual'
                                            reverse_order='manual-reverse'
                                            path='/admin/brand'
                                            label="{intl l="Position"}"
                                            }
                                        </th>

                                        <th class="text-center">
                                            {admin_sortable_header
                                            current_order=$order
                                            order='visible'
                                            reverse_order='visible-reverse'
                                            path={url path='/admin/brand'}
                                            label="{intl l='Online'}"
                                            }
                                        </th>

                                        {hook name='brands.table-header' location='brands_table_header'}

                                        <th class="actions">{intl l='Actions'}</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    {loop name="brands" type="brand" visible="*" backend_context="1" lang=$lang_id order=$order return_url=false}
                                        <tr>
                                            <td>{$ID}</td>

                                            <td>
                                                {loop type="image" name="folder_image" source="brand" source_id="$ID" limit="1" width="50" height="50" resize_mode="crop" backend_context="1"}
                                                    <a href="{url path="admin/brand/update/$OBJECT_ID"}" title="{intl l='Edit this brand'}">
                                                        <img src="{$IMAGE_URL nofilter}" alt="{$TITLE}" />
                                                    </a>
                                                {/loop}
                                            </td>

                                            <td>
                                                {loop type="auth" name="can_change" role="ADMIN" resource="admin.brand" access="UPDATE"}
                                                    <a title="{intl l='Change this brand'}" href="{url path="/admin/brand/update/%id" id=$ID}">{$TITLE}</a>
                                                {/loop}
                                                {elseloop rel="can_change"}
                                                    {$TITLE}
                                                {/elseloop}
                                            </td>

                                            <td class="text-center">
                                                {admin_position_block
                                                    resource="admin.brand"
                                                    access="UPDATE"
                                                    path="/admin/brand/update-position"
                                                    url_parameter="brand_id"
                                                    in_place_edit_class="brandPositionChange"
                                                    position="$POSITION"
                                                    id="$ID"
                                                }
                                            </td>

                                            <td class="text-center">
                                                {loop type="auth" name="can_change" role="ADMIN" resource="admin.brand" access="UPDATE"}
                                                    <div class="make-switch switch-small visibleToggle" data-id="{$ID}"  data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
                                                        <input type="checkbox" class="visibleToggle" {if $VISIBLE == 1}checked="checked"{/if}>
                                                    </div>
                                                {/loop}

                                                {elseloop rel="can_change"}
                                                    <div class="make-switch switch-small" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
                                                        <input type="checkbox" class="disabled" disabled="disabled" {if $VISIBLE == 1}checked="checked"{/if}>
                                                    </div>
                                                {/elseloop}
                                            </td>

                                            {hook name='brands.table-row' location='brands_table_row' brand_id={$ID}}

                                            <td class="actions">
                                                {include file='renderer/buttons.html' buttons=[
                                                    [
                                                        'type' => 'edit',
                                                        'title' => {intl l='Change this brand'},
                                                        'href' => {url path="/admin/brand/update/%id" id=$ID},
                                                        'auth' => ['resource' => 'admin.brand']
                                                    ],
                                                    [
                                                        'type' => 'delete',
                                                        'class' => 'brand-delete',
                                                        'title' => {intl l='Delete this brand'},
                                                        'href' => '#delete_dialog',
                                                        'data' => [
                                                            'id' => $ID,
                                                            'toggle' => 'modal'
                                                        ],
                                                        'auth' => ['resource' => 'admin.brand']
                                                    ]
                                                ]}
                                            </td>
                                        </tr>
                                    {/loop}
                                    {elseloop rel="brands"}
                                        <tr>
                                            <td colspan="100">
                                                <div class="alert alert-info">
                                                    {intl l="No brand has been created yet. Click the + button to create one."}
                                                </div>
                                            </td>
                                        </tr>
                                    {/elseloop}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            {hook name='brands.bottom' location='brands_bottom'}

        </div>
    </div>


    {* Adding a new brand *}

    {form name="thelia.admin.brand.creation"}

        {* Capture the dialog body, to pass it to the generic dialog *}
        {capture "creation_dialog"}
			{form_hidden_fields exclude="locale"}

            {render_form_field field="success_url" value={url path='/admin/brand/update/_ID_'}}

            {custom_render_form_field field="title"}
                {loop type="lang" name="default-lang" default_only="1"}
                    {* Switch edition to the current locale *}
                    <input type="hidden" name="edit_language_id" value="{$ID}" />

                    {render_form_field field="locale" value=$LOCALE}

                    <div class="input-group">
                        <input type="text" {form_field_attributes field="title"}>
                        <span class="input-group-addon"><img src="{image file="assets/img/flags/$CODE.png"}" alt="{$TITLE}" /></span>
                    </div>
                {/loop}
            {/custom_render_form_field}

            {custom_render_form_field field="visible"}
                <input type="checkbox" checked {form_field_attributes field="visible"}> {$label}
            {/custom_render_form_field}

            {hook name='brand.create-form' location='brand_create_form'}

        {/capture}

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

            dialog_id    = "creation_dialog"
            dialog_title = {intl l="Create a new brand"}
            dialog_body  = {$smarty.capture.creation_dialog nofilter}

            dialog_ok_label     = {intl l="Create this brand"}

            form_action        = {url path='/admin/brand/create'}
            form_enctype       = {form_enctype}
            form_error_message = $form_error_message
        }
    {/form}


    {* Delete confirmation dialog *}

    {capture "delete_dialog"}
        <input type="hidden" name="brand_id" id="brand_delete_id" value="" />

        {hook name='brand.delete-form' location='brand_delete_form'}

    {/capture}

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

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

        form_action         = {token_url path='/admin/brand/delete'}
        form_content        = {$smarty.capture.delete_dialog nofilter}
    }
{/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>
        $(function() {

            // Set proper brand ID in delete from
            $('a.brand-delete').click(function(ev) {
                $('#brand_delete_id').val($(this).data('id'));
            });

            // JS stuff for creation form
            {include
                file      = "includes/generic-js-dialog.html"
                dialog_id = "creation_dialog"
                form_name = "thelia.admin.brand.creation"
            }

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

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

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

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

            {* Visibility toggle *}

            $(".visibleToggle").on('switch-change', function(event, data) {
                $.ajax({
                    url : "{url path='admin/brand/toggle-online'}",
                    data : {
                        brand_id : $(this).data('id'),
                        action : 'visibilityToggle'
                    }
                });
            });

        });
    </script>
{/block}

{block name="javascript-last-call"}
    {hook name='brand.js' location='brand_js'}
{/block}
