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

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

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

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

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

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

            <div class="clearfix">
                <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 rules"}</a></li>
                </ul>
            </div>

            {hook name="taxes-rules.top" location="taxes_rules_top" }

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

                    <div class="alert alert-info">
                        <p>{intl l="In order to manges your shop taxes you can manage"} <strong>{intl l="taxes"}</strong> {intl l="and"} <strong>{intl l="tax rules"}</strong>.</p>
                        <p>{intl l="Taxes define the amount of money which is added to a bought product."}</p>
                        <p>
                            {intl l="Example :"}
                        <ul>
                            <li>{intl l="French 19.6% VAT is a tax which add a 19.6% tax to the product price."}</li>
                            <li>{intl l="Ecotax is a tax wich add a defined amount (throug a product feature) to the product price."}</li>
                        </ul>
                        </p>
                        <p>{intl l="Tax rules are combination of different taxes."}</p>
                        <p>
                            {intl l="Example :"}
                        <ul>
                            <li>{intl l="French 19.6% VAT with ecotax is the applicance of the ecotax (on the product price) then the applicance of the 19.6% tax (on the product price + the ecotax amount)."}</li>
                        </ul>
                        </p>
                        <p>{intl l="you can combine taxes in tax rules and chose if they are applied one after the other or at the same time : it allows to apply taxes on an already taxed price or not."}</p>
                    </div>


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

                    <div class="general-block-decorator">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-left-aligned">
                                <caption class="clearfix">
                                    {intl l="Taxes"}
                                    {include file='renderer/buttons.html' btn_group=false buttons=[
                                        [
                                            'type' => 'create',
                                            'class' => 'action-btn',
                                            'title' => {intl l='Create a new tax'},
                                            'href' => '#tax_create_dialog',
                                            'auth' => ['resource' => 'admin.configuration.tax'],
                                            'data' => [
                                                'toggle' => 'modal'
                                            ]
                                        ]
                                    ]}
                                </caption>
                                <thead>
                                <tr>
                                    <th class="col-md-3">{intl l="Name"}</th>
                                    <th class="col-md-5">{intl l="Description"}</th>
                                    <th class="col-md-1 actions">{intl l="Actions"}</th>
                                </tr>
                                </thead>
                                <tbody>

                                {loop type="tax" name="taxes" backend_context="1"}

                                    <tr>
                                        <td>
                                            {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.tax" access="UPDATE"}
                                                <a title="{intl l='Change this tax'}" href="{url path="/admin/configuration/taxes/update/%id" id=$ID}">{$TITLE}</span></a>
                                            {/loop}
                                            {elseloop rel="can_change"}
                                                {$TITLE}
                                            {/elseloop}
                                        </td>
                                        <td class="description">{$DESCRIPTION nofilter}</td>
                                        <td class="actions">
                                            {include file='renderer/buttons.html' buttons=[
                                                [
                                                    'type' => 'edit',
                                                    'title' => {intl l='Change this tax'},
                                                    'href' => {url path="/admin/configuration/taxes/update/%id" id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.tax']
                                                ],
                                                [
                                                    'type' => 'delete',
                                                    'class' => 'js-delete-tax',
                                                    'title' => {intl l='Delete this tax'},
                                                    'href' => '#tax_delete_dialog',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ],
                                                    'auth' => ['resource' => 'admin.configuration.tax']
                                                ]
                                            ]}
                                        </td>
                                    </tr>

                                {/loop}

                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="general-block-decorator">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-left-aligned">
                                <caption class="clearfix">
                                    {intl l="Taxes rules"}
                                    {include file='renderer/buttons.html' btn_group=false buttons=[
                                        [
                                            'type' => 'create',
                                            'class' => 'action-btn',
                                            'title' => {intl l='Create a new tax rule'},
                                            'href' => '#tax_rule_create_dialog',
                                            'auth' => ['resource' => 'admin.configuration.tax', 'access' => 'UPDATE'],
                                            'data' => [
                                                'toggle' => 'modal'
                                            ]
                                        ]
                                    ]}
                                </caption>
                                <thead>
                                <tr>
                                    <th class="col-md-3">{intl l="Name"}</th>
                                    <th class="col-md-4">{intl l="Description"}</th>
                                    <th class="col-md-1 text-center">{intl l="Default"}</th>
                                    <th class="col-md-1 actions">{intl l="Actions"}</th>
                                </tr>
                                </thead>
                                <tbody>

                                {loop type="tax-rule" name="taxes-rules" backend_context="1"}

                                    <tr>
                                        <td>
                                            {loop type="auth" name="can_change-tr" role="ADMIN" resource="admin.configuration.tax" access="UPDATE"}
                                                <a title="{intl l='Change this tax rule'}" href="{url path="/admin/configuration/taxes_rules/update/%id" id=$ID}">{$TITLE}</a>
                                            {/loop}
                                            {elseloop rel="can_change-tr"}
                                                {$TITLE}
                                            {/elseloop}
                                        </td>
                                        <td class="description">{$DESCRIPTION nofilter}</td>
                                        <td class="text-center">
                                            {if $IS_DEFAULT == 1}
                                                <span class="glyphicon glyphicon-star"></span>
                                            {/if}
                                        </td>
                                        <td class="actions">
                                            {$class = ""}
                                            {if $IS_DEFAULT == 1}{$class = "disabled"}{/if}
                                            {include file='renderer/buttons.html' buttons=[
                                                [
                                                    'type' => 'edit',
                                                    'title' => {intl l='Change this tax rule'},
                                                    'href' => {url path="/admin/configuration/taxes_rules/update/%id" id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.tax']
                                                ],
                                                [
                                                    'type' => 'warning',
                                                    'class' => $class,
                                                    'title' => {intl l='Set as default tax rule'},
                                                    'href' => {url path="/admin/configuration/taxes_rules/update/set_default/%id" id=$ID},
                                                    'auth' => ['resource' => 'admin.configuration.tax'],
                                                    'icon' => ['class' => 'glyphicon glyphicon-star']
                                                ],
                                                [
                                                    'type' => 'delete',
                                                    'class' => 'js-delete-tax-rule',
                                                    'title' => {intl l='Delete this tax rule'},
                                                    'href' => '#tax_rule_delete_dialog',
                                                    'data' => [
                                                        'id' => $ID,
                                                        'toggle' => 'modal'
                                                    ],
                                                    'auth' => ['resource' => 'admin.configuration.tax']
                                                ]
                                            ]}
                                        </td>
                                    </tr>

                                {/loop}

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            {hook name="taxes-rules.bottom" location="taxes_rules_bottom" }

        </div>
    </div>

{* -- Add tax confirmation dialog ----------------------------------- *}
    {form name="thelia.admin.tax.add"}

    {if $form_error_message}
        {$taxCreateError = true}
    {else}
        {$taxCreateError = false}
    {/if}

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

        {form_hidden_fields exclude="locale"}

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

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

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

        {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">
                        {$typeValueWithError = $value}
                        {foreach from=$choices key=key item=choice}
                            {if $key == 0}
                                {$typeValue = $choice->value}
                            {/if}
                            <option value="{$choice->value}" {if $form_error && $choice->value == $value || !$form_error && $key == 0}selected="selected" {/if}>{$choice->label}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
        {/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 != $typeValue || $form_error && $attr_list.tax_type != $typeValueWithError}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 != $typeValue || $form_error && $attr_list.tax_type != $typeValueWithError}disabled="disabled"{/if}>
                        {foreach $choices as $choice}
                            <option value="{$choice->value}" {if !$form_error && $REQUIREMENTS.$label == $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 != $typeValue || $form_error && $attr_list.tax_type != $typeValueWithError}disabled="disabled"{/if} id="{$label_attr.for}" name="{$name}" required="required" class="form-control" value="{if $form_error}{$value}{else}{$REQUIREMENTS.$label}{/if}">
                {/if}
            </div>
        {/form_tagged_fields}

        {hook name="tax.create-form" location="tax_create_form" }

    {/capture}

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

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

    dialog_ok_label     = {intl l="Create"}
    dialog_cancel_label = {intl l="Cancel"}

    form_action        = {url path="/admin/configuration/taxes/add"}
    form_enctype       = {form_enctype}
    form_error_message = $form_error_message
    }

    {/form}

{* -- Delete tax confirmation dialog ----------------------------------- *}

    {capture "tax_delete_dialog"}
        <input type="hidden" name="tax_id" id="tax_delete_id" value="" />

        {hook name="tax.delete-form" location="tax_delete_form" }

    {/capture}

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

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

form_action         = {token_url path='/admin/configuration/taxes/delete'}
form_content        = {$smarty.capture.tax_delete_dialog nofilter}
}

{* -- Add tax rule confirmation dialog ----------------------------------- *}
    {form name="thelia.admin.taxrule.add"}

    {if $form_error_message}
        {$taxRuleCreateError = true}
    {else}
        {$taxRuleCreateError = false}
    {/if}

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

        {form_hidden_fields exclude="locale"}

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

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

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

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

        {hook name="tax-rule.create-form" location="tax_rule_create_form" }
    {/capture}

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

        dialog_id    = "tax_rule_create_dialog"
        dialog_title = {intl l="Create a new tax rule"}
        dialog_body  = {$smarty.capture.tax_rule_create_dialog nofilter}

    dialog_ok_label     = {intl l="Create"}
    dialog_cancel_label = {intl l="Cancel"}

    form_action        = {url path="/admin/configuration/taxes_rules/add"}
    form_enctype       = {form_enctype}
    form_error_message = $form_error_message
    }

    {/form}

{* -- Delete tax rule confirmation dialog ----------------------------------- *}

    {capture "tax_rule_delete_dialog"}
        <input type="hidden" name="tax_rule_id" id="tax_rule_delete_id" value="" />

        {hook name="tax-rule.delete-form" location="tax_rule_delete_form" }

    {/capture}

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

    dialog_id       = "tax_rule_delete_dialog"
    dialog_title    = {intl l="Delete tax rule"}
    dialog_message  = {intl l="Do you really want to delete this tax rule ?"}

    form_action         = {token_url path='/admin/configuration/taxes_rules/delete'}
    form_content        = {$smarty.capture.tax_rule_delete_dialog nofilter}
}

{/block}

{block name="javascript-initialization"}

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

    <script type="text/javascript">
        jQuery(function($) {

            {if $taxRuleCreateError == true}
            $('#tax_rule_create_dialog').modal();
            {/if}

            {if $taxCreateError == true}
            $('#tax_create_dialog').modal();
            {/if}

            $(".js-delete-tax-rule").click(function(e){
                $('#tax_rule_delete_id').val($(this).data('id'))
            });

            $(".js-delete-tax").click(function(e){
                $('#tax_delete_id').val($(this).data('id'))
            });

            $('.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="taxes-rules.js" location="taxes-rules-js" }
    {hook name="wysiwyg.js" location="wysiwyg-tax-rules-js" }
{/block}