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

{block name="after-admin-css"}
    <link rel="stylesheet" href="{stylesheet file='assets/js/image-picker/image-picker.css'}">
{/block}

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

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

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

{block name="main-content"}
    <div class="brand edit-brand">
        <div id="wrapper" class="container">
            {loop name="brand_edit" type="brand" visible="*" id="{$brand_id}" backend_context="1" lang="$edit_language_id" with_prev_next_info="true"}

            <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/brand'}">{intl l='Brands'}</a></li>
                    <li>{intl l='Editing brand "%title"' title="{$TITLE}"}</li>
                </ul>
            </nav>

            {hook name="brand-edit.top" brand_id=$brand_id}

            <div class="row">
                <div class="col-md-12 general-block-decorator">
                    <div class="row">
                        <div class="col-md-7 title">
                            {intl l='Edit brand %title' title={$TITLE}}
                        </div>

                        <div class="col-md-5 actions">

                            {if $HAS_PREVIOUS != 0}
                                <a href="{url path="/admin/brand/update/%previous" previous=$PREVIOUS}" class="btn btn-default" title="{intl l='Edit previous brand'}"><span class="glyphicon glyphicon-arrow-left"></span></a>
                            {else}
                                <a href="#" disabled="disabled" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span></a>
                            {/if}

                            <a href="{$URL nofilter}" target="_blank" class="btn btn-default" title="{intl l='Preview brand page'}"><span class="glyphicon glyphicon-eye-open"></span></a>

                            {if $HAS_NEXT != 0}
                                <a href="{url path="/admin/brand/update/%next" next=$NEXT}" class="btn btn-default" title="{intl l='Edit next brand'}"><span class="glyphicon glyphicon-arrow-right"></span></a>
                            {else}
                                <a href="#" disabled="disabled" class="btn btn-default"><span class="glyphicon glyphicon-arrow-right"></span></a>
                            {/if}
                        </div>

                    </div>

                    <div class="row">
                        <div class="col-md-12">

                            {hookblock name="brand.tab" brand_id="{$brand_id}" fields="id,title,href,content"}

                                {capture "brand_tab_tab"}
                                    {forhook rel="brand.tab"}
                                    <li>
                                        <a href="#{$id}"
                                           {if $href}data-href="{$href}"{/if}
                                           data-toggle="tab">{$title}
                                        </a>
                                    </li>
                                    {/forhook}
                                {/capture}

                                {capture "brand_tab_content"}
                                    {forhook rel="brand.tab"}
                                    <div class="tab-pane fade" id="{$id}">
                                        {if $href}
                                            {* ajax *}
                                            <div class="text-center"><span class="loading">{intl l="Please wait, loading"}</span></div>
                                        {else}
                                            {$content nofilter}
                                        {/if}
                                    </div>
                                    {/forhook}
                                {/capture}

                            {/hookblock}
                            <ul class="nav nav-tabs" id="tabbed-menu">
                                <li class="active">
                                    <a href="#general" data-toggle="tab">{intl l="General description"}</a>
                                </li>
                                <li><a href="#seo" data-toggle="tab">{intl l="SEO"}</a></li>
                                <li><a href="#images" data-toggle="tab">{intl l="Images"}</a></li>
                                <li><a href="#documents" data-toggle="tab">{intl l="Documents"}</a></li>
                                {$smarty.capture.brand_tab_tab nofilter}
                                <li><a href="#modules" data-toggle="tab">{intl l="Modules"}</a></li>
                            </ul>

                            <div class="tab-content">

                                <div class="tab-pane fade active in" id="general">

                                    <div class="form-container">

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

                                            <form method="POST" action="{url path="/admin/brand/save/%id" id=$ID}" {form_enctype} class="clearfix">

                                                {include file="includes/inner-form-toolbar.html" close_url={url path='/admin/brand'}}

                                                <input type="hidden" name="current_tab" value="general"/>

                                                {* Be sure to get the currency ID, even if the form could not be validated *}
                                                <input type="hidden" name="brand_id" value="{$ID}" />

                                                {form_hidden_fields}

                                                {render_form_field field="success_url" value={url path="/admin/brand"}}
                                                {render_form_field field="locale" value={$edit_language_locale}}

                                                {if $form_error}
                                                    <div class="alert alert-danger">{$form_error_message}</div>
                                                {/if}

                                                <div class="row">
                                                    <div class="col-md-8">
                                                        {include file="includes/standard-description-form-fields.html"}
                                                    </div>

                                                    <div class="col-md-4">
                                                        {hook name="brand.modification.form-right.top" brand_id=$brand_id}

                                                        {render_form_field field="visible"}

                                                        {custom_render_form_field field='logo_image_id'}
                                                            <select {form_field_attributes field='logo_image_id' extra_class='brand-image-selector'} >
                                                                <option value="">{intl l="No logo image"}</option>

                                                                {loop name="brand-images" type="image" brand=$ID width="90" height="90" resize_mode="crop"}
                                                                    <option value="{$ID}" data-img-src="{$IMAGE_URL nofilter}" {if $LOGO_IMAGE_ID == $ID}selected="selected"{/if}>{$TITLE}</option>
                                                                {/loop}
                                                            </select>
                                                        {/custom_render_form_field}

                                                        {hook name="brand.modification.form-right.bottom" brand_id=$brand_id}
                                                    </div>
                                                </div>

                                                {hook name="brand.update-form" location='brand_update_form'}

                                                {include
                                                    file="includes/inner-form-toolbar.html"
                                                    hide_submit_buttons = false
                                                    hide_flags = true

                                                    close_url={url path="/admin/brand"}
                                                }

                                                <small>{intl l='Brand created on %date_create. Last modification: %date_change' date_create={format_date date=$CREATE_DATE} date_change={format_date date=$UPDATE_DATE} }</small>
                                            </form>
                                        {/form}
                                    </div>
                                </div>

                                <div class="tab-pane fade" id="seo">
                                    {include file = "includes/seo-tab.html"
                                        formAction  = {url path='/admin/brand/seo/save'}
                                        pageUrl     = {url path='/admin/brand/update/%id' id=$brand_id}
                                        closeUrl    = {url path='/admin/brand'}
                                        success_url = {url path='/admin/brand'}
                                        current_id  = $brand_id
                                        seoType     = 'brand'
                                        parentId    = $brand_id
                                    }
                                </div>

                                 <div class="tab-pane fade" id="images">
                                    {include file='includes/image-upload-form.html' imageType='brand' parentId=$brand_id resource="admin.brand"}
                                </div>

                                <div class="tab-pane fade" id="documents">
                                    {include file='includes/document-upload-form.html' documentType='brand' parentId=$brand_id resource="admin.brand"}
                                </div>

                                {$smarty.capture.brand_tab_content nofilter}

                                <div class="tab-pane fade" id="modules">
                                    <div class="form-container">
                                        {include
                                            file = "includes/inner-form-toolbar.html"
                                            hide_submit_buttons = true
                                            page_url = $pageUrl
                                            close_url = {$closeUrl}
                                            current_tab = "modules"
                                        }
                                    </div>
                                    {* bad fix : {hook name="brand.tab-content"} *}
                                    {include file="includes/module-tab-content.html" hook="brand.tab-content" location="brand-edit" id={$brand_id} view="brand"}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            {hook name="brand-edit.bottom" brand_id=$brand_id}

            {/loop}
        </div>
    </div>
 {/block}



{block name="javascript-initialization"}
    {javascripts file='assets/js/dropzone.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    {javascripts file='assets/js/image-upload.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    {javascripts file='assets/js/document-upload.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    {javascripts file='assets/js/jquery-ui-1.10.3.custom.min.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    {javascripts file='assets/js/image-picker/image-picker.min.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    <script>
        $(function() {
            // Atomatic ajax tab load, if data-href is defined.
            $('.nav-tabs a[data-href]').on('shown.bs.tab', function(ev) {
                var $this = $(this);
                $($this.attr('href')).load($this.data('href'), function(ev) {
                    if($this.data('callback')) {
                        eval($this.data('callback') + '();');
                    }
                });

            });

            // Load active tab

            $.imageUploadManager.initImageDropZone();
            $.documentUploadManager.initDocumentDropZone();

            $('.use_default_rewriten_url').click(function(ev) {
                alert("Not functionnal");

                ev.preventDefault();
            });

            // Show proper tab, if defined
            {if ! empty($current_tab)}
            $('.nav-tabs a[href="#{$current_tab}"]').trigger("click");
            {/if}

            $('.brand-image-selector').imagepicker();
        });
    </script>
{/block}

{block name="javascript-last-call"}
    {hook name='brand.edit-js' location='brand-edit-js' brand_id={$brand_id}}
    {hook name="wysiwyg.js" location="wysiwyg-brand-edit-js" }
{/block}
