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

{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"}

        {module_include 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}
                                     {$cat_id = $ID}
                                 {/loop}
                                 {elseloop rel="category_title"}
                                     {intl l="Top level categories"}
                                 {/elseloop}

                                 {module_include location='category_list_caption'}

                                 {loop type="auth" name="can_create" role="ADMIN" resource="admin.category" access="CREATE"}
                                 <a class="btn btn-default btn-primary action-btn" title="{intl l='Add a new category'}" href="#category_creation_dialog" data-toggle="modal">
                                     <span class="glyphicon glyphicon-plus-sign"></span>
                                 </a>
                                 {/loop}
                             </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/categories' id_category=$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/categories' id_category=$category_id}
                                            request_parameter_name='category_order'
                                            label="{intl l='Category title'}"
                                         }
                                         </th>

                                         {module_include location='category_list_header'}

                                         <th>
                                         {admin_sortable_header
                                            current_order=$category_order
                                            order='visible'
                                            reverse_order='visible_reverse'
                                            path={url path='/admin/categories' id_category=$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/categories' id_category=$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}
                                 <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}" 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>

                                     {module_include location='category_list_row'}

                                     <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">
                                         <div class="btn-group">
                                             <a class="btn btn-default btn-xs" title="{intl l='Browse this category'}" href="{url path='admin/categories' category_id=$ID}"><i class="glyphicon glyphicon-folder-open"></i></a>

                                             {loop type="auth" name="can_change" role="ADMIN" resource="admin.category" access="UPDATE"}
                                                <a class="btn btn-default btn-xs" title="{intl l='Edit this category'}" href="{url path='/admin/categories/update' category_id=$ID}"><i class="glyphicon glyphicon-edit"></i></a>
                                             {/loop}

                                             {loop type="auth" name="can_delete" role="ADMIN" resource="admin.category" access="DELETE"}
                                                <a class="btn btn-default btn-xs category-delete" title="{intl l='Delete this category and all its contents'}"  href="#category_delete_dialog" data-id="{$ID}" data-toggle="modal"><i class="glyphicon glyphicon-trash"></i></a>
                                             {/loop}
                                         </div>
                                      </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 ---------------------------------------------------- *}

     <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}

                         {module_include location='product_list_caption'}

                         <a class="btn btn-default btn-primary action-btn" title="{intl l='Add a new product'}" href="#product_creation_dialog" data-toggle="modal">
                             <span class="glyphicon glyphicon-plus-sign"></span>
                         </a>
                     </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/categories' id_category=$category_id target='products'}
                                    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/categories' id_category=$category_id target='products'}
                                    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/categories' id_category=$category_id target='products'}
                                    label="{intl l='Product title'}"
                                 }

                                 {module_include location='product_list_header'}

                                 <th>
                                 {admin_sortable_header
                                    current_order=$product_order
                                    order='visible'
                                    reverse_order='visible_reverse'
                                    path={url path='/admin/categories' id_category=$category_id target='products'}
                                    label="{intl l='Online'}"
                                 }
                                 </th>

                                 <th>
                                 {admin_sortable_header
                                    current_order=$product_order
                                    order='manual'
                                    reverse_order='manual_reverse'
                                    path={url path='/admin/categories' id_category=$category_id target='products'}
                                    label="{intl l='Position'}"
                                 }
                                 </th>

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

                         <tbody>
                         {loop name="product_list" type="product" visible="*" category_default=$category_id order=$product_order}
                         <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}" title="{intl l='Edit this product'}">
                                 <img src="{$IMAGE_URL}" alt="{$TITLE}" />
                               </a>
                             {/loop}

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

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

                             {module_include location='product_list_row'}

                             <td>
                                    {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>
                             {admin_position_block
                                 resource="admin.product"
                                 access="update"
                                 path={url path='/admin/products/update-position' product_id=$ID}
                                 url_parameter="product_id"
                                 in_place_edit_class="productPositionChange"
                                 position=$POSITION
                                 id=$ID
                              }
                             </td>

                             <td class="actions">
                                 <div class="btn-group">
                                 {loop type="auth" name="can_change" role="ADMIN" resource="admin.product" access="UPDATE"}
                                    <a class="btn btn-default btn-xs" title="{intl l='Edit this product'}" href="{url path='/admin/products/update' product_id=$ID}"><i class="glyphicon glyphicon-edit"></i></a>
                                {/loop}

                                 {loop type="auth" name="can_delete" role="ADMIN" resource="admin.product" access="DELETE"}
                                       <a class="btn btn-default btn-xs product-delete" title="{intl l='Delete this product'}"  href="#product_delete_dialog" data-id="{$ID}" data-toggle="modal"><i class="glyphicon glyphicon-trash"></i></a>
                                 {/loop}
                                 </div>
                              </td>
                         </tr>
                         {/loop}
                         </tbody>
                     {/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>

        {module_include location='categories_bottom'}

    </div>

    {module_include 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 form=$form}

        {form_field form=$form 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/categories/update' category_id='_ID_'}" />
        {/form_field}

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

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

                    <div class="help-block">{intl l='Enter here the category 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 form=$form field='locale'}
                        <input type="hidden" name="{$name}" value="{$LOCALE}" />
                    {/form_field}
                {/loop}
            </div>
        {/form_field}

        {form_field form=$form 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}

        {module_include 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=$form}
        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 form=$form}

        {* 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 form=$form 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 form=$form field='default_category'}
           <input type="hidden" name="{$name}" value="{$category_id}" />
        {/form_field}

        {form_field form=$form 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 form=$form field='title'}
            <div class="form-group {if $error}has-error{/if}">
                <label for="{$label_attr.for}" class="control-label">{$label} : </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}.gif"}" 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 form=$form field='locale'}
                        <input type="hidden" name="{$name}" value="{$LOCALE}" />
                    {/form_field}
                {/loop}
            </div>
        {/form_field}

        {form_field form=$form 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="row">
	                    <div class="col-lg-4">
						    <div class="input-group">
						        <input type="text" id="{$label_attr.for}" required="required" name="{$name}" class="col-lg-2 form-control" value="{$value}" title="{$label}" placeholder="{intl l='Product price'}">
						        <span class="input-group-addon">{$SYMBOL}</span>
						    </div>
					    </div>
					</div>

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

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

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

        {form_field form=$form 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}

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

                <div class="row">
                    <div class="col-lg-4">
		                <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='Product weight'}">
		                    <span class="input-group-addon">{intl l="Kg"}</span>
		                </div>
		            </div>
		        </div>

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

        {form_field form=$form 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}

        {module_include 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=$form}
        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="" />

    {module_include 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         = {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}" />

    {module_include 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 ?"}

    form_action         = {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}

    <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__'}";

                    // 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;
            });

        });
    </script>
{/block}