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

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

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

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

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

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

        {include file="includes/catalog-breadcrumb.html"}

        {hook name="categories.top" location="categories_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="category_list">
                             <caption>
                                 {* display parent category name, and get current cat ID *}
                                 {loop name="category_title" type="category" visible="*" id=$category_id}
                                     {intl l="Categories in %cat" cat={$TITLE}}
                                 {/loop}
                                 {elseloop rel="category_title"}
                                     {intl l="Top level categories"}
                                 {/elseloop}

                                 {hook name="categories.caption" location="category_list_caption" }

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

                             {ifloop rel="category_list"}
                                 <thead>
                                     <tr>
                                         <th class="object-title">
                                         {admin_sortable_header
                                            current_order=$category_order
                                            order='id'
                                            reverse_order='id_reverse'
                                            path={url path='/admin/catalog' category_id=$category_id}
                                            request_parameter_name='category_order'
                                            label="{intl l='ID'}"
                                         }
                                         </th>

                                         <th class="object-image">&nbsp;</th>

                                         <th class="object-title">
                                         {admin_sortable_header
                                            current_order=$category_order
                                            order='alpha'
                                            reverse_order='alpha_reverse'
                                            path={url path='/admin/catalog' category_id=$category_id}
                                            request_parameter_name='category_order'
                                            label="{intl l='Category title'}"
                                         }
                                         </th>

                                         {hook name="categories.header" location="category_list_header" }

                                         <th>
                                         {admin_sortable_header
                                            current_order=$category_order
                                            order='visible'
                                            reverse_order='visible_reverse'
                                            path={url path='/admin/catalog' category_id=$category_id}
                                            request_parameter_name='category_order'
                                            label="{intl l='Online'}"
                                         }
                                         </th>

                                         <th>
                                         {admin_sortable_header
                                            current_order=$category_order
                                            order='manual'
                                            reverse_order='manual_reverse'
                                            path={url path='/admin/catalog' category_id=$category_id}
                                            request_parameter_name='category_order'
                                            label="{intl l='Position'}"
                                         }
                                         </th>

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

                                 <tbody>
                                 {loop name="category_list" type="category" visible="*" parent=$category_id order=$category_order backend_context="1" lang=$lang_id return_url=false}
                                 <tr>
                                     <td>{$ID}</td>

                                     <td>
                                     {loop type="image" name="cat_image" source="category" source_id="$ID" limit="1" width="50" height="50" resize_mode="crop" backend_context="1"}
                                       <a href="{url path='admin/catalog' category_id=$OBJECT_ID}" title="{intl l='Browse this category'}"><img class="img-thumbnail" src="{$IMAGE_URL nofilter}" alt="{$TITLE}" /></a>
                                     {/loop}
                                     </td>

                                     <td class="object-title">
                                          <a href="{url path='admin/catalog' category_id=$ID}" title="{intl l='Browse this category'}">
                                            {$TITLE}
                                          </a>
                                     </td>

                                     {hook name="categories.row" location="category_list_row" category_id={$ID} }

                                     <td>
                                         {loop type="auth" name="can_change" role="ADMIN" resource="admin.category" access="UPDATE"}
                                             <div class="make-switch switch-small categoryVisibleToggle" 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="categoryVisibleToggle" {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>

                                     <td>
                                     {admin_position_block
                                         resource="admin.category"
                                         access="UPDATE"
                                         path={url path='admin/categories/update-position' category_id=$ID}
                                         url_parameter="category_id"
                                         in_place_edit_class="categoryPositionChange"
                                         position=$POSITION
                                         id=$ID
                                      }
                                     </td>

                                     <td class="actions">
                                        {include file='renderer/buttons.html' buttons=[
                                            [
                                                'type' => 'folder-open',
                                                'title' => {intl l='Browse this category'},
                                                'href' => {url path='admin/categories' category_id=$ID}
                                            ],
                                            [
                                                'type' => 'edit',
                                                'title' => {intl l='Edit this category'},
                                                'href' => {url path='/admin/categories/update' category_id=$ID},
                                                'auth' => ['resource' => 'admin.category']
                                            ],
                                            [
                                                'type' => 'delete',
                                                'class' => 'category-delete',
                                                'title' => {intl l='Delete this category'},
                                                'href' => '#category_delete_dialog',
                                                'data' => [
                                                    'id' => $ID,
                                                    'toggle' => 'modal'
                                                ],
                                                'auth' => ['resource' => 'admin.category']
                                            ]
                                        ]}
                                      </td>
                                 </tr>
                                 {/loop}
                                 </tbody>
                             {/ifloop}

                             {elseloop rel="category_list"}
                             <thead>
                                 <tr>
                                     <td class="message">
                                         <div class="alert alert-info">
                                         {loop type="auth" name="can_create" role="ADMIN" resource="admin.category" access="CREATE"}
                                             {intl l="This category has no sub-categories. To create a new one, click the + button above."}
                                         {/loop}

                                         {elseloop rel="can_create"}
                                            {intl l="This category has no sub-categories."}
                                         {/elseloop}
                                         </div>
                                     </td>
                                 </tr>
                             </thead>
                             {/elseloop}
                         </table>
                     </div>
                </div>
            </div>
        </div>

{* -- PRODUCT MANAGEMENT ---------------------------------------------------- *}

     {* No product on toplevel category, e.g. id_category = 0 *}

     {loop type="currency" name="product-currency" id=$edit_currency_id backend_context="1"}
        {$currency_symbol = $SYMBOL}
     {/loop}

     {if $category_id > 0}
     <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>
                         {* display parent category name *}
                         {loop name="category_title" type="category" visible="*" id=$category_id}
                             {intl l="Products in %cat" cat={$TITLE}}
                         {/loop}

                         {elseloop rel="category_title"}
                             {intl l="Top level Products"}
                         {/elseloop}

                         {hook name="products.caption" location="product_list_caption" }

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

                     {ifloop rel="product_list"}
                         <thead>
                             <tr>
                                 <th class="object-title">
                                 {admin_sortable_header
                                    current_order=$product_order
                                    order='id'
                                    reverse_order='id_reverse'
                                    path={url path='/admin/catalog' category_id=$category_id target='products'}
                                    request_parameter_name='product_order'
                                    label="{intl l='ID'}"
                                 }

                                 <th>&nbsp;</th>

                                 <th class="object-title">
                                 {admin_sortable_header
                                    current_order=$product_order
                                    order='ref'
                                    reverse_order='ref_reverse'
                                    path={url path='/admin/catalog' category_id=$category_id target='products'}
                                    request_parameter_name='product_order'
                                    label="{intl l='Reference'}"
                                 }
                                 </th>

                                 <th class="object-title">
                                 {admin_sortable_header
                                    current_order=$product_order
                                    order='alpha'
                                    reverse_order='alpha_reverse'
                                    path={url path='/admin/catalog' category_id=$category_id target='products'}
                                    request_parameter_name='product_order'
                                    label="{intl l='Product title'}"
                                 }

                                 {hook name="products.header" location="product_list_header" }

                                 <th class="text-right">
                                     {admin_sortable_header
                                     current_order=$product_order
                                     order='min_price'
                                     reverse_order='max_price'
                                     path={url path='/admin/catalog' category_id=$category_id target='products'}
                                     request_parameter_name='product_order'
                                     label="{intl l='Price'}"
                                     }
                                 </th>

                                 <th class="text-center">
                                     {admin_sortable_header
                                     current_order=$product_order
                                     order='visible'
                                     reverse_order='visible_reverse'
                                     path={url path='/admin/catalog' category_id=$category_id target='products'}
                                     request_parameter_name='product_order'
                                     label="{intl l='Online'}"
                                     }
                                 </th>

                                 <th class="text-center">
                                 {admin_sortable_header
                                    current_order=$product_order
                                    order='manual'
                                    reverse_order='manual_reverse'
                                    path={url path='/admin/catalog' category_id=$category_id target='products'}
                                    request_parameter_name='product_order'
                                    label="{intl l='Position'}"
                                 }
                                 </th>

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

                         <tbody>
                         {loop name="product_list" type="product" visible="*" category=$category_id order=$product_order page=$page backend_context="on" limit={config key="number_default_results_per_page.category_list" default=20} return_url=false}
                         <tr>
                             <td>{$ID}</td>

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

                             <td class="object-title">
                                 <a href="{url path='/admin/products/update' product_id=$ID page=$page}" title="{intl l='Edit this product'}">{$REF}</a>
                                 {if $VIRTUAL}<span class="glyphicon glyphicon-download text-muted" title="{intl l='Virtual product'}"></span>{/if}
                             </td>

                             <td class="object-title"><a href="{url path='/admin/products/update' product_id=$ID page=$page}" title="{intl l='Edit this product'}">{$TITLE}</a></td>

                             {hook name="products.row" location="product_list_row" product_id={$ID} }

                             <td class="text-right"><a href="{url path='/admin/products/update' product_id=$ID page=$page current_tab='prices'}" title="{intl l='Edit Prices'}">{format_money number=$BEST_PRICE symbol=$currency_symbol}</a></td>

                             <td class="text-center">
                                    {loop type="auth" name="can_change" role="ADMIN" resource="admin.product" access="UPDATE"}
                                        <div class="make-switch switch-small productVisibleToggle" 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="productVisibleToggle" {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>

                             <td class="text-center">
                             {admin_position_block
                                 resource="admin.product"
                                 access="UPDATE"
                                 path={url path='/admin/products/update-position' product_id=$ID category_id=$category_id}
                                 url_parameter="product_id"
                                 in_place_edit_class="productPositionChange"
                                 position=$POSITION
                                 id=$ID
                              }
                             </td>

                             <td class="actions">
                                 {include file='renderer/buttons.html' buttons=[
                                    [
                                        'type' => 'edit',
                                        'title' => {intl l='Edit this product'},
                                        'href' => {url path='/admin/products/update' product_id=$ID page=$page},
                                        'auth' => ['resource' => 'admin.product']
                                    ],
                                    [
                                        'type' => 'delete',
                                        'class' => 'product-delete',
                                        'title' => {intl l='Delete this product'},
                                        'href' => '#product_delete_dialog',
                                        'data' => [
                                            'id' => $ID,
                                            'toggle' => 'modal'
                                        ],
                                        'auth' => ['resource' => 'admin.product']
                                    ]
                                ]}
                              </td>
                         </tr>
                         {/loop}
                         </tbody>
                         <tfoot>
                         <tr>
                             <td colspan="100">
                                 {include
                                 file = "includes/pagination.html"

                                 loop_ref       = "product_list"
                                 max_page_count = 10
                                 page_url       = {url path="/admin/catalog" category_id=$category_id product_order=$product_order}
                                 }

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

                     {elseloop rel="product_list"}
                     <thead>
                         <tr>
                             <td class="message"><div class="alert alert-info">{intl l="This category doesn't contains any products. To add a new product, <strong>click the + button</strong> above."}</div></td>
                         </tr>
                     </thead>
                     {/elseloop}
                 </table>
                </div>
             </div>
            </div>
        </div>
        {else}
        <div class="alert alert-info">{intl l="To create a new product, select an existing category, or create a new one."}</div>
        {/if}

        {hook name="categories.bottom" location="categories_bottom" }

    </div>

    {hook name="categories.catalog-bottom" location="catalog_bottom" }

</div>


{* -- Adding a new category ------------------------------------------------- *}

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

    {* Capture the dialog body, to pass it to the generic dialog *}
    {capture "category_creation_dialog"}

        {form_hidden_fields exclude="parent,locale"}

        <input type="hidden" {form_field_attributes field='parent' value={$category_id}}>

        {* on success, redirect to the edition page, _ID_ is replaced with the created object ID, see controller  *}
        {render_form_field field="success_url" value={url path='/admin/categories/update' category_id='_ID_'}}

        {custom_render_form_field field="title"}
            {loop type="lang" name="default-lang" default_only="1"}
                <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>

                {* Switch edition to the current locale *}
                <input type="hidden" name="edit_language_id" value="{$ID}" />

                {render_form_field field="locale" value=$LOCALE}
            {/loop}
        {/custom_render_form_field}
        
        {form_field field="visible"}

        <label class="checkbox">
            <input type="checkbox" checked name="{$name}" value="1"> {$label}
        </label>
        {/form_field}

        {hook name="category.create-form" location="category_create_form" }

    {/capture}

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

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

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

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

{* -- Adding a new product -------------------------------------------------- *}

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

    {* Capture the dialog body, to pass it to the generic dialog *}
    {capture "product_creation_dialog"}

        {form_hidden_fields}

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

        {form_field field='success_url'}
           {* on success, redirect to the edition page, _ID_ is replaced with the created object ID, see controller  *}
           <input type="hidden" name="{$name}" value="{url path='/admin/products/update' product_id='_ID_'}" />
        {/form_field}

        {form_field field='default_category'}
           <input type="hidden" name="{$name}" value="{$category_id}" />
        {/form_field}

        {form_field field='ref'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </label>

                <div class="from-group">
                    <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="form-control" value="{$value}" title="{$label}" placeholder="{intl l='Reference'}">
                </div>

                <div class="help-block">{intl l='Enter here the product reference'}</div>
            </div>
        {/form_field}

        {form_field field='title'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} : </label>
                {loop type="lang" name="default-lang" default_only="1" backend_context="1"}
                    <div class="input-group">
                        <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="form-control" value="{$value}" title="{$label}" placeholder="{intl l='Title'}">
                        <span class="input-group-addon"><img src="{image file="assets/img/flags/$CODE.png"}" alt="{$TITLE}" /></span>
                    </div>

                    <div class="help-block">{intl l='Enter here the product name in the default language (%title)' title=$TITLE}</div>

                    {* Switch edition to the current locale *}
                    <input type="hidden" name="edit_language_id" value="{$ID}" />

                    {form_field field='locale'}
                        <input type="hidden" name="{$name}" value="{$LOCALE}" />
                    {/form_field}
                {/loop}
            </div>
        {/form_field}

        <div class="row">
            <div class="col-sm-6">
                {form_field field='tax_rule'}
                    <div class="form-group {if $error}has-error{/if}">
                        <label for="{$label_attr.for}" class="control-label">{$label} : </label>

                        <div class="form-group">
                            <select id="{$label_attr.for}" required="required" name="{$name}" class="form-control">
                                <option value="">{intl l="Select a tax tule"}</option>
                                {loop name="tax" type="tax-rule" backend_context="1"}
                                    <option value="{$ID}" {if $IS_DEFAULT}selected="selected"{/if}>{$TITLE}</option>
                                {/loop}
                            </select>
                        </div>

                        <div class="help-block">{intl l='Select here the tax applicable to this product'}</div>
                    </div>
                {/form_field}
            </div>

            <div class="col-sm-6">
                {* If there are some templates, display select template field *}
                {ifloop rel="product_template"}

                    {* Get category template *}
                    {loop type="category" name="new_product_category" visible="*" id=$category_id}
                        {assign var="product_template" value=$TEMPLATE}
                        {assign var="parent_category" value=$PARENT}
                    {/loop}

                    {* If current category has no template, get a parent's one *}
                    {if !$product_template}
                        {while $parent_category != 0 && !$product_template}
                            {loop type="category" name="parent_category" backend_context=1 visible="*" id=$parent_category}
                                {$parent_category = $PARENT}
                                {$product_template = $TEMPLATE}
                            {/loop}
                            {* Be sure to prevent infinite loops *}
                            {elseloop rel="parent_category"}
                                {$parent_category = 0}
                            {/elseloop}
                        {/while}
                    {/if}

                    {form_field field='template_id'}
                        <div class="form-group {if $error}has-error{/if}">
                            <label for="{$label_attr.for}" class="control-label">{$label} : </label>

                            <div class="form-group">
                                <select id="{$label_attr.for}" name="{$name}" class="form-control">
                                    <option value="">{intl l="Select a template"}</option>
                                    {loop name="product_template" type="product-template"}
                                        <option value="{$ID}" {if $product_template == $ID}selected{/if}>{$NAME}</option>
                                    {/loop}
                                </select>
                            </div>

                            <div class="help-block">{intl l='Select here a template for this product'}</div>
                        </div>
                    {/form_field}
                {/ifloop}
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                {form_field field='price'}
                    <div class="form-group {if $error}has-error{/if}">
                        <label for="{$label_attr.for}" class="control-label">{$label} : </label>
                        {loop type="currency" name="default-currency" default_only="1" backend_context="1"}

                            <div class="input-group">
                                <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="col-lg-2 form-control automatic_price_field" value="{$value}" title="{$label}" placeholder="{intl l='Product price'}" data-price-type="price-without-tax" data-rel-price="price_with_tax">
                                <span class="input-group-addon">{$SYMBOL}</span>
                            </div>

                            <div class="help-block">{intl l='Enter here the product price in %title' title={$NAME}}</div>

                            {form_field field='currency'}
                                <input type="hidden" name="{$name}" value="{$ID}" />
                            {/form_field}

                        {/loop}
                    </div>
                {/form_field}
            </div>

            <div class="col-sm-6">
                {form_field field='tax_price'}
                    <div class="form-group {if $error}has-error{/if}">
                        <label for="{$label_attr.for}" class="control-label">{$label} : </label>
                        {loop type="currency" name="default-currency" default_only="1" backend_context="1"}

                            <div class="input-group">
                                <input type="text" id="{$label_attr.for}" name="{$name}" class="col-lg-2 form-control automatic_price_field" value="{$value}" title="{$label}" placeholder="{intl l='Product tax price'}" data-price-type="price-with-tax" data-rel-price="price_without_tax">
                                <span class="input-group-addon">{$SYMBOL}</span>
                            </div>

                            <div class="help-block">{intl l='Enter here the product tax price in %title' title={$NAME}}</div>
                        {/loop}
                    </div>
                {/form_field}
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                {form_field field='weight'}
                    <div class="form-group {if $error}has-error{/if}">
                        <label for="{$label_attr.for}" class="control-label">{$label}: </label>

                        <div class="input-group">
                            <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{$label}" placeholder="{intl l='Product weight'}">
                            <span class="input-group-addon">{intl l="Kg"}</span>
                        </div>

                        <div class="help-block">{intl l='Enter here the product weight, in Kilogrammes'}</div>
                    </div>
                {/form_field}
                </div>

                <div class="col-sm-6">
                {form_field field='quantity'}
                    <div class="form-group {if $error}has-error{/if}">
                        <label for="{$label_attr.for}" class="control-label">{$label}: </label>

                        <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{$label}" placeholder="{intl l='Product stock'}">

                        <div class="help-block">{intl l='Enter here the product stock'}</div>
                    </div>
                {/form_field}
            </div>
        </div>

        {form_field field='virtual'}
        <div class="form-group {if $error}has-error{/if}">
            <div class="checkbox">
                <label>
                    <input type="checkbox" id="{$label_attr.for}" name="{$name}" value="1">
                    {$label}
                </label>
            </div>
        </div>
        {/form_field}

        {form_field field='visible'}
            <div class="form-group {if $error}has-error{/if}">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" id="{$label_attr.for}" name="{$name}" value="1" checked="checked">
                        {$label}
                    </label>
                </div>
            </div>
        {/form_field}

        {hook name="product.create-form" location="product_create_form" }

    {/capture}

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

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

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

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

{* -- Delete category confirmation dialog ----------------------------------- *}

{capture "category_delete_dialog"}
    <input type="hidden" name="category_id" id="category_delete_id" value="" />

    {hook name="category.delete-form" location="category_delete_form" }

{/capture}

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

    dialog_id       = "category_delete_dialog"
    dialog_title    = {intl l="Delete category"}
    dialog_message  = {intl l="Do you really want to delete this category and all its content ?"}

    form_action         = {token_url path='/admin/categories/delete'}
    form_content        = {$smarty.capture.category_delete_dialog nofilter}
}

{* -- Delete product confirmation dialog ------------------------------------ *}

{capture "product_delete_dialog"}
    <input type="hidden" name="product_id" id="product_delete_id" value="" />
    <input type="hidden" name="category_id" value="{$category_id}" />
    <input type="hidden" name="page" value="{$page}">

    {hook name="product.delete-form" location="product_delete_form" }

{/capture}

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

    dialog_id       = "product_delete_dialog"
    dialog_title    = {intl l="Delete product"}
    dialog_message  = {intl l="Do you really want to delete this product and all it's components (images, documents)?<br>This can't be canceled."}

    form_action         = {token_url path='/admin/products/delete'}
    form_content        = {$smarty.capture.product_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}
    {javascripts file='assets/js/jquery.typewatch.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    <script>
        $(function() {

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

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

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

            {include
                file      = "includes/generic-js-dialog.html"
                dialog_id = "product_creation_dialog"
                form_name = "thelia.admin.product.creation"
            }

            {* Toggle object visibility *}

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


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

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

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

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

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

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

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

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

            {* Change default status *}

            $('.change-default').change(function(ev) {
                var url = "{url path='/admin/categories/set-default' category_id='__ID__'}";

                // Perform ID subtitutions
                url = url.replace('__ID__', $(this).val());

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

            function update_price(price, price_type, dest_field_id) {
                var tax_rule_id = $('#tax_rule_field').val();

                if (tax_rule_id != "") {

                    var operation;

                    if (price_type.indexOf('with-tax') != -1)
                        operation = 'from_tax';
                    else if (price_type.indexOf('without-tax') != -1)
                        operation = 'to_tax';
                    else
                        operation = '';

                    $.ajax({
                        url      : '{url path="/admin/product/calculate-raw-price"}',
                        data     : {
                            price      : price,
                            action     : operation,
                            tax_rule   : tax_rule_id
                        },
                        type     : 'get',
                        dataType : 'json',
                        success  : function(json) {
                            $('#' + dest_field_id).val(json.result);
                        },
                        error : function(jqXHR, textStatus, errorThrown) {
                            alert("{intl l='Failed to get prices. Please try again.'} (" +errorThrown+ ")");
                        }
                   });
                }
            }

            // Automatic update of price fields: any change in the taxed (resp. untaxed) price
            // will update the untaxed (resp. taxed) one
            $('.automatic_price_field').typeWatch({
                captureLength: 1,
                wait         : 300,
                callback     : function () {
                    var price = $(this).val();
                    $(this).val(sanitizeFloat(price));
                    update_price($(this).val(), $(this).data('price-type'), $(this).data('rel-price'));
                }
            });

            function sanitizeFloat(numVal) {
                return numVal.replace(",", ".");
            };

        });
    </script>
{/block}

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