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

{block name="after-bootstrap-css"}

{/block}

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

{block name="page-title"}Configuration CustomerFamily{/block}

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

{block name="main-content"}

<div class="general-block-decorator" id="CustomerFamily">
    <div class="title">
        {intl l="Customer Family" d='customerfamily.bo.default'}
    </div>

    <div class="row">
        <div class="col-md-12">
            <!-- Tab menu -->
            <ul id="tabbed-menu" class="nav nav-tabs">
                <li class="active"><a data-toggle="tab" href="#families">{intl l="Families" d='customerfamily.bo.default'}</a> </li>
                <li><a data-toggle="tab" href="#prices_definition">{intl l="Prices definition" d='customerfamily.bo.default'}</a></li>
                <li><a data-toggle="tab" href="#category_restrictions">{intl l="Category restrictions" d='customerfamily.bo.default'}</a></li>
                <li><a data-toggle="tab" href="#brand_restrictions">{intl l="Brand restrictions" d='customerfamily.bo.default'}</a></li>
            </ul>

            <div class="tab-content">

                <!-- Families managing tab -->
                <div id="families" class="tab-pane active form-container">
                    <div class="row">
                        <div class="col-md-6">
                            {if $hide_flags != true}
                                <ul class="nav nav-pills">
                                    {loop name="lang_list" type="lang"}
                                        <li {if $ID == $edit_language_id}class="active"{/if}>
                                            {if $current_tab}
                                                {$lang_url = {url path="/admin/module/CustomerFamily" edit_language_id=$ID current_tab=$current_tab}}
                                            {else}
                                                {$lang_url = {url path="/admin/module/CustomerFamily" edit_language_id=$ID}}
                                            {/if}
                                            <a class="language-change-button" data-language-id="{$ID}" href="{$lang_url nofilter}" title="{intl l='Edit information in %lng' lng={$TITLE}}">
                                                <img src="{image file="assets/img/flags/$CODE.png"}" alt="{$TITLE}" />
                                            </a>
                                        </li>
                                    {/loop}
                                </ul>
                            {/if}
                        </div>

                        {form name="customer.family.delete.form"}
                            {if $form_error}
                                <div class="alert alert-danger">{$form_error_message}</div
                            {/if}
                        {/form}

                        <div class="col-md-12">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>
                                            {intl l="ID"}
                                        </th>
                                        <th>
                                            {intl l="Code"}
                                        </th>
                                        <th>
                                            {intl l="Title"}
                                        </th>
                                        <th>
                                            {intl l="Default"}
                                        </th>
                                        <th>
                                            {intl l="Action"}
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {ifloop rel="customer_family"}

                                        <!-- Default family -->
                                        {form name='customer_family_update_default_form'}
                                            <form action="{url path='/admin/module/CustomerFamily/update-default'}" method="post" id="update-default-family-form">

                                                {form_hidden_fields form=$form}

                                                {form_field form=$form field="customer_family_id"}
                                                    <input type="hidden" name="{$name}" id="default_family_id">
                                                {/form_field}
                                            </form>
                                        {/form}

                                        <!-- Families list -->
                                        {loop type="customer_family" name="customer_family" lang=$edit_language_id  force_return="on"}
                                            <tr>
                                                <td>
                                                    {$CUSTOMER_FAMILY_ID}
                                                </td>
                                                <td>
                                                    {$CODE}
                                                </td>
                                                <td>
                                                    {$TITLE_CUSTOMER_FAMILY}
                                                </td>
                                                <td>
                                                    <div class="switch-small update-default-family" data-id="{$CUSTOMER_FAMILY_ID}" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok-circle'></i>" data-off-label="<i class='glyphicon glyphicon-remove-circle'></i>">
                                                        <input type="radio" name="is_default" {if $IS_DEFAULT}checked{/if} />
                                                    </div>
                                                </td>
                                                <td>
                                                    <a class="btn btn-info pull-left js-action-update" data-id="{$CUSTOMER_FAMILY_ID}" data-code="{$CODE}" data-title="{$TITLE_CUSTOMER_FAMILY}" style="margin-right: 5px;">{intl l="Edit" d='customerfamily.bo.default'}</a>
                                                    <a class="btn btn-danger pull-left js-action-delete" data-id="{$CUSTOMER_FAMILY_ID}" style="margin-right: 5px;">{intl l="Delete"}</a>
                                                </td>
                                            </tr>
                                        {/loop}
                                    {/ifloop}
                                    {elseloop rel="customer_family"}
                                        <tr>
                                            <td colspan="4" class="text-center">
                                                {intl l="No family" d='customerfamily.bo.default'}
                                            </td>
                                        </tr>
                                    {/elseloop}
                                </tbody>
                                <tfoot class="text-center">
                                    <tr>
                                        <td colspan="4">
                                            {form name="customer.family.create.form"}
                                                <form action="{url path="/admin/module/CustomerFamily/create"}" method="POST" class="form-inline">
                                                    {if $form_error}
                                                        <div class="alert alert-danger">{$form_error_message}</div
                                                    {/if}
                                                    {form_hidden_fields form=$form}
                                                    {form_field form=$form field="locale"}
                                                        <input type="hidden" name="{$name}" value="{$edit_language_locale}" />
                                                    {/form_field}
                                                    {form_field form=$form field="code"}
                                                        <div class="form-group{if $error} has-error{/if}">
                                                            <input class="form-control" type="text" required placeholder="{$label}" name="{$name}" value="{$value}" />
                                                        </div>
                                                    {/form_field}
                                                    {form_field form=$form field="title"}
                                                        <div class="form-group{if $error} has-error{/if}">
                                                            <input class="form-control" type="text" required placeholder="{$label}" name="{$name}" value="{$value}" />
                                                        </div>
                                                    {/form_field}
                                                    <div class="form-group">
                                                        <button type="submit" class="btn btn-primary js-action-create">{intl l="Add"}</button>
                                                    </div>
                                                </form>
                                            {/form}
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>

                    {form name="customer.family.delete.form"}
                        <div class="modal js-modal-delete" tabindex="-1" role="dialog" aria-labelledby="Delete family" aria-hidden="true">
                            <div class="modal-dialog">
                                <form action="" method="POST">
                                    {form_hidden_fields form=$form}
                                    <div class="modal-content">
                                        <div class="modal-body">
                                            {intl l="Do you really want to delete this customer family ?" d='customerfamily.bo.default'}
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">{intl l="No"}</button>
                                            <button type="submit" type="button" class="btn btn-danger js-action-delete-confirm">{intl l="Yes"}</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    {/form}

                    {form name="customer.family.update.form"}
                        <div class="modal js-modal-update" tabindex="-1" role="dialog" aria-labelledby="Update family" aria-hidden="true">
                            <div class="modal-dialog">
                                <form action="" method="POST">
                                    {form_hidden_fields form=$form}
                                    <div class="modal-content">
                                        <div class="modal-body">
                                            {form_field form=$form field="locale"}
                                                <input type="hidden" name="{$name}" value="{$edit_language_locale}" />
                                            {/form_field}
                                            {form_field form=$form field="code"}
                                                <div class="form-group{if $error} has-error{/if}">
                                                    <label>{$label}</label>
                                                    <input class="form-control js-field-code" type="text" required placeholder="{$label}" name="{$name}" value="{$value}" />
                                                </div>
                                            {/form_field}
                                            {form_field form=$form field="title"}
                                                <div class="form-group{if $error} has-error{/if}">
                                                    <label>{$label}</label>
                                                    <input class="form-control js-field-title" type="text" required placeholder="{$label}" name="{$name}" value="{$value}" />
                                                </div>
                                            {/form_field}
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">{intl l="Cancel"}</button>
                                            <button type="submit" type="button" class="btn btn-primary js-action-delete-confirm">{intl l="Update"}</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    {/form}
                    <div>
                        {form name="customer_family_price_mode"}
                            <form action="{url path="/admin/CustomerFamily/selectPriceMode"}" method="post">
                                {form_hidden_fields form=$form}
                                {form_field form=$form field="price_mode"}
                                    <input type="checkbox" name="{$name}" id="{$name}" value="{$value}" {if $checked}checked{/if} onchange="this.form.submit()">
                                    <label for="{$name}">{$label}</label>
                                {/form_field}
                                <div class="help-block">
                                    <span>{$label_attr.help}</span>
                                </div>
                            </form>
                        {/form}
                    </div>
                </div>

                <!-- Prices definition tab -->
                <div id="prices_definition" class="tab-pane form-container">
                    <br>

                    <!-- Configure prices equation -->
                    <div class="row">
                        <div class="col-md-12">

                            <h4 class="text-center">
                                {intl l="Define if your prices are the products' ones or if they are calculated by the equation with your parameters." d='customerfamily.bo.default'}
                            </h4>
                            <h4 class="text-center">
                                {intl l="Not using the equation will display the fix product price to customers." d='customerfamily.bo.default'}
                            </h4>
                            <h3 class="text-center">
                                {intl l='Equation: ( ( product_purchase_price + fix_amount_1 ) x factor ) + fix_amount_2' d='customerfamily.bo.default'}
                            </h3>
                            <br>
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>{intl l='Customer family' d='customerfamily.bo.default'}</th>
                                        <th>{intl l='Use equation' d='customerfamily.bo.default'}</th>
                                        <th>{intl l='Fix amount 1' d='customerfamily.bo.default'}</th>
                                        <th>{intl l='Factor' d='customerfamily.bo.default'}</th>
                                        <th>{intl l='Fix amount 2' d='customerfamily.bo.default'}</th>
                                        <th>{intl l='Show taxes' d='customerfamily.bo.default'}<br>{intl l='(need specific integration)' d='customerfamily.bo.default'}</th>
                                        <th>{intl l='Save' d='customerfamily.bo.default'}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {loop type='customer_family' name='customerfamily_loop'}

                                        {assign var='amount_added_before' value=0}
                                        {assign var='coefficient' value=1}
                                        {assign var='amount_added_after' value=0}
                                        {assign var='is_taxed' value=1}
                                        {assign var='use_equation' value=0}

                                        <!-- Standard price equation -->
                                        <tr>
                                            {form name='customer_family_price_update'}
                                                <form action="{url path='/admin/module/CustomerFamily/update-price-calculation'}" method="post">

                                                    {form_hidden_fields form=$form}

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

                                                    {loop type='customer_family_price' name='customer_family_price_loop' promo=0 customer_family_id=$CUSTOMER_FAMILY_ID}
                                                        {assign var='amount_added_before' value=$AMOUNT_ADDED_BEFORE}
                                                        {assign var='coefficient' value=$COEFFICIENT}
                                                        {assign var='amount_added_after' value=$AMOUNT_ADDED_AFTER}
                                                        {assign var='is_taxed' value=$IS_TAXED}
                                                        {assign var='use_equation' value=$USE_EQUATION}
                                                    {/loop}

                                                    <td>
                                                        {$TITLE_CUSTOMER_FAMILY}
                                                        {form_field form=$form field="customer_family_id"}
                                                            <input type="hidden" name="{$name}" value="{$CUSTOMER_FAMILY_ID}">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="use_equation"}
                                                            <input type="checkbox" name="{$name}" {if $use_equation}checked{/if}>
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="amount_added_before"}
                                                            <input type="number" name="{$name}" maxlength="16" step="any" value="{$amount_added_before}" class="form-control">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="coefficient"}
                                                            <input type="number" name="{$name}" maxlength="16" step="any" value="{$coefficient}" class="form-control">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="amount_added_after"}
                                                            <input type="number" name="{$name}" maxlength="16" step="any" value="{$amount_added_after}" class="form-control">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="is_taxed"}
                                                            <input type="checkbox" name="{$name}" {if $is_taxed}checked{/if}>
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>
                                                    </td>
                                                </form>
                                            {/form}
                                        </tr>

                                        {assign var='amount_added_before' value=0}
                                        {assign var='coefficient' value=1}
                                        {assign var='amount_added_after' value=0}
                                        {assign var='is_taxed' value=1}
                                        {assign var='use_equation' value=0}

                                        <!-- Promo price equation -->
                                        <tr>
                                            {form name='customer_family_price_update'}
                                                <form action="{url path='/admin/module/CustomerFamily/update-price-calculation'}" method="post">

                                                    {form_hidden_fields form=$form}

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

                                                    {loop type='customer_family_price' name='customer_family_price_promo_loop' promo=1 customer_family_id=$CUSTOMER_FAMILY_ID}
                                                        {assign var='amount_added_before' value=$AMOUNT_ADDED_BEFORE}
                                                        {assign var='coefficient' value=$COEFFICIENT}
                                                        {assign var='amount_added_after' value=$AMOUNT_ADDED_AFTER}
                                                        {assign var='is_taxed' value=$IS_TAXED}
                                                        {assign var='use_equation' value=$USE_EQUATION}
                                                    {/loop}

                                                    <td>
                                                        {$TITLE_CUSTOMER_FAMILY} - {intl l='Promo' d='customerfamily.bo.default'}
                                                        {form_field form=$form field="customer_family_id"}
                                                            <input type="hidden" name="{$name}" value="{$CUSTOMER_FAMILY_ID}">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="use_equation"}
                                                            <input type="checkbox" name="{$name}" {if $use_equation}checked{/if}>
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="amount_added_before"}
                                                            <input type="number" name="{$name}" maxlength="16" step="any" value="{$amount_added_before}" class="form-control">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="coefficient"}
                                                            <input type="number" name="{$name}" maxlength="16" step="any" value="{$coefficient}" class="form-control">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="amount_added_after"}
                                                            <input type="number" name="{$name}" maxlength="16" step="any" value="{$amount_added_after}" class="form-control">
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        {form_field form=$form field="is_taxed"}
                                                            <input type="checkbox" name="{$name}" {if $is_taxed}checked{/if}>
                                                        {/form_field}
                                                    </td>
                                                    <td>
                                                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>
                                                    </td>
                                                </form>
                                            {/form}
                                        </tr>
                                    {/loop}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <div id="category_restrictions" class="tab-pane form-container">
                    <div class="row">
                        {loop type='customer_family' name='customerfamily_loop'}
                            <div class="col-md-12 general-block-decorator">
                                <form method="post" action="{url path="/admin/module/CustomerFamily/category_restriction/%customerFamilyId" customerFamilyId=$CUSTOMER_FAMILY_ID}">
                                    <div class="title title-without-tabs">
                                        {$TITLE_CUSTOMER_FAMILY}
                                    </div>
                                    <label for="category_restriction_{$CUSTOMER_FAMILY_ID}">
                                        <input name="restriction_enabled" class="restriction_toggler" id="category_restriction_{$CUSTOMER_FAMILY_ID}" type="checkbox" {if $CATEGORY_RESTRICTION_ENABLED}checked{/if}>
                                        <span>{intl l="Enable category restriction for this customer family" d='customerfamily.bo.default'}</span>
                                    </label>
                                    <div class="restriction_toggled toggled_by_category_restriction_{$CUSTOMER_FAMILY_ID}">
                                        <p>{intl l="Choose what categories will be available for this customer family" d='customerfamily.bo.default'}</p>
                                        <select class="form-control" name="available_categories[]" id="" multiple>
                                            {foreach from=$categoryRestrictions[$CUSTOMER_FAMILY_ID] item=categoryRestriction}
                                                <option value="{$categoryRestriction.id}" {if $categoryRestriction.available}selected{/if}>{$categoryRestriction.title}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                                    <input type="submit" class="btn-block btn btn-success"/>
                                </form>
                            </div>
                        {/loop}
                    </div>
                </div>
                <div id="brand_restrictions" class="tab-pane form-container">
                    <div class="row">
                        {loop type='customer_family' name='customerfamily_loop'}
                            <div class="col-md-12 general-block-decorator">
                                <form method="post" action="{url path="/admin/module/CustomerFamily/brand_restriction/%customerFamilyId" customerFamilyId=$CUSTOMER_FAMILY_ID}">
                                    <div class="title title-without-tabs">
                                        {$TITLE_CUSTOMER_FAMILY}
                                    </div>
                                    <label for="brand_restriction_{$CUSTOMER_FAMILY_ID}">
                                        <input name="restriction_enabled" class="restriction_toggler" id="brand_restriction_{$CUSTOMER_FAMILY_ID}" type="checkbox" {if $BRAND_RESTRICTION_ENABLED}checked{/if}>
                                        <span>{intl l="Enable brand restriction for this customer family" d='customerfamily.bo.default'}</span>
                                    </label>
                                    <div class="restriction_toggled toggled_by_brand_restriction_{$CUSTOMER_FAMILY_ID}">
                                        <p>{intl l="Choose what brands will be available for this customer family" d='customerfamily.bo.default'}</p>
                                        <select class="form-control" name="available_categories[]" id="" multiple>
                                            {foreach from=$brandRestrictions[$CUSTOMER_FAMILY_ID] item=brandRestriction}
                                                <option value="{$brandRestriction.id}" {if $brandRestriction.available}selected{/if}>{$brandRestriction.title}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                                    <input type="submit" class="btn-block btn btn-success"/>
                                </form>
                            </div>
                        {/loop}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="update-default-family-failed" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h3>{intl l="An error occured" d='customerfamily.bo.default'}</h3>
            </div>
            <div class="modal-body" id="update-default-family-failed-body">

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

{block name="javascript-initialization"}
    <script>
        $('.restriction_toggler').each(function () {
            triggerRestrictionToggler($(this));
        });

        $('.restriction_toggler').on('change', function () {
            triggerRestrictionToggler($(this));
        });

        function triggerRestrictionToggler(restrictionToggler) {
            var toggled = $('.toggled_by_'+restrictionToggler.attr('id'));
            if (restrictionToggler.is(":checked")) {
                toggled.show();
            } else {
                toggled.hide();
            }
        }
    </script>
{/block}