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

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

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

{block name="check-resource"}admin.configuration.tax{/block}
{block name="check-access"}update{/block}

{block name="main-content"}

<div class="taxes-rules edit-taxes-rules">

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

        <ul class="breadcrumb">
            <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
            <li><a href="{url path='/admin/configuration'}">{intl l="Configuration"}</a></li>
            <li><a href="{url path='/admin/configuration/taxes_rules'}">{intl l="Taxes"}</a></li>
            <li>{intl l='Editing tax'}</li>
        </ul>

        {loop type="tax" name="tax" id=$tax_id backend_context="1" lang=$edit_language_id}

        {hook name="tax-edit.top" tax_id=$tax_id}

        <div class="row">
            <div class="col-md-12 general-block-decorator clearfix">

                <div class="form-container">

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

                <form method="POST" action="{url path="/admin/configuration/taxes/save"}" {form_enctype} >

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

                        page_url  = {url path="/admin/configuration/taxes/update/%id" id=$tax_id}
                        close_url = {url path="/admin/configuration/taxes_rules"}
                    }

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

                    {form_hidden_fields}

                    {form_field field='success_url'}
                        <input type="hidden" name="{$name}" value="{url path="/admin/configuration/taxes_rules"}" />
                    {/form_field}

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

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

                    {form_field field='title'}
                    <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}" required="required" title="{intl l='Title'}" placeholder="{intl l='Title'}" class="form-control" value="{if $form_error}{$value}{else}{if $IS_TRANSLATED == 1}{$TITLE}{/if}{/if}">
                    </div>
                    {/form_field}

                    {form_field field='description'}
                    <div class="form-group {if $error}has-error{/if}">
                        <label for="{$label_attr.for}" class="control-label">
                            {$label} :
                            <span class="label-help-block">{intl l="The detailed description."}</span>
                        </label>

                        <textarea name="{$name}" id="{$label_attr.for}" rows="10" class="form-control wysiwyg">{if $form_error}{$value}{else}{if $IS_TRANSLATED == 1}{$DESCRIPTION}{/if}{/if}</textarea>
                    </div>
                    {/form_field}

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

                            <div class="form-group">
                                <select name="{$name}" class="js-change-tax-type" data-toggle="selectpicker">
                                    {foreach $choices as $choice}
                                        <option value="{$choice->value}" {if !$form_error && $choice->value == $ESCAPED_TYPE || $form_error && $choice->value == $value}selected="selected" {/if}>{$choice->label}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        {$typeValue = $value}
                    {/form_field}

                    {form_tagged_fields tag='requirements'}
                        <div class="form-group {if $error}has-error{/if} js-tax-requirements" data-tax-type="{$attr_list.tax_type}" {if !$form_error && $attr_list.tax_type != $ESCAPED_TYPE || $form_error && $attr_list.tax_type != $typeValue}style="display: none"{/if}>
                            <label for="{$label_attr.for}" class="control-label">
                                {$label}
                            </label>
                            {if $formType == 'choice'}
                                <select name="{$name}" data-toggle="selectpicker" {if !$form_error && $attr_list.tax_type != $ESCAPED_TYPE || $form_error && $attr_list.tax_type != $typeValue}disabled="disabled"{/if}>
                                    {foreach $choices as $choice}
                                        <option value="{$choice->value}" {if !$form_error && $REQUIREMENTS[$label_attr.type] == $choice->value || $form_error && $value == $choice->value}selected="selected" {/if}>{$choice->label}</option>
                                    {/foreach}
                                </select>
                            {/if}
                            {if $formType == 'text'}
                                <input type="text" {if !$form_error && $attr_list.tax_type != $ESCAPED_TYPE || $form_error && $attr_list.tax_type != $typeValue}disabled="disabled"{/if} id="{$label_attr.for}" name="{$name}" required="required" class="form-control" value="{if $form_error}{$value}{else}{$REQUIREMENTS[$label_attr.type]}{/if}">
                            {/if}
                        </div>
                    {/form_tagged_fields}

                    <div class="row">
                        <div class="col-md-12">
                            <div class="control-group">
                                <label>&nbsp;</label>
                                <div class="controls">
                                    <p>{intl l='Tax created on %date_create. Last modification: %date_change' date_create={format_date date=$CREATE_DATE} date_change={format_date date=$UPDATE_DATE}}</p>
                                </div>
                             </div>
                        </div>
                    </div>

                    </form>
                    {/form}
                </div>

            </div>
        </div>

        {hook name="tax-edit.bottom" tax_id=$tax_id}

        {/loop}

    </div>

</div>

{/block}

{block name="javascript-initialization"}

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

    <script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

    <script>
        $(function() {
            $('.js-change-tax-type').change(function(e){
                $('.js-tax-requirements').children('select, input').attr('disabled', true);
                $('.js-tax-requirements').hide();
                $('.js-tax-requirements[data-tax-type="' + $(this).val() + '"]').children('select, input').attr('disabled', false);
                $('.js-tax-requirements[data-tax-type="' + $(this).val() + '"]').children('select').selectpicker("refresh");
                $('.js-tax-requirements[data-tax-type="' + $(this).val() + '"]').show();
            });
        });

    </script>

{/block}

{block name="javascript-last-call"}
    {hook name="tax.edit-js" location="tax-edit-js" }
    {hook name="wysiwyg.js" location="wysiwyg-tax-edit-js" }
{/block}