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

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

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

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

{block name="main-content"}
<div class="customers edit-customer">

        <div id="wrapper" class="container">
            {loop name="customer_edit" type="customer" current="false" id="$customer_id" backend_context="1" with_prev_next_info="true" lang="$edit_language_id"}

                <ul class="breadcrumb">
                    <li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
                    <li><a href="{url path='/admin/customers'}">{intl l="Customers"}</a></li>
                    <li>{intl l="Edit customer %firstname %lastname" firstname={$FIRSTNAME} lastname={$LASTNAME}}</li>
                </ul>

            {hook name="customer-edit.top" customer_id=$customer_id}

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

                            <div class="col-md-6 title title-without-tabs">
                                {intl l="Edit customer %firstname %lastname (Ref : %ref)" firstname={$FIRSTNAME} lastname={$LASTNAME} ref={$REF}}
                            </div>

                            <div class="col-md-6 actions">

                                {if $HAS_PREVIOUS != 0}
                                <a href="{url path="/admin/customer/update?customer_id=%previous" previous=$PREVIOUS}" class="btn btn-default" title="{intl l='Edit previous customer'}"><span class="glyphicon glyphicon-arrow-left"></span></a>
                                {else}
                                <a href="#" disabled="disabled" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span></a>
                                {/if}
                                {if $HAS_NEXT != 0}
                                <a href="{url path="/admin/customer/update?customer_id=%next" next=$NEXT}" class="btn btn-default" title="{intl l='Edit next customer'}"><span class="glyphicon glyphicon-arrow-right"></span></a>
                                {else}
                                <a href="#" disabled="disabled" class="btn btn-default"><span class="glyphicon glyphicon-arrow-right"></span></a>
                                {/if}
                            </div>


                            <div class="form-container">
                                <div class="col-md-12">
                                    <br>
                                        {form name="thelia.admin.customer.update"}
                                        <form method="POST" action="{url path='/admin/customer/save'}" {form_enctype} class="clearfix">

                                            {include
                                                file="includes/inner-form-toolbar.html"
                                                hide_flags = true
                                                close_url  = {url path='/admin/customers'}
                                            }

                                            {form_hidden_fields}

                                            <input type="hidden" name="page" value="{$page}" />
                                            <input type="hidden" name="customer_id" value="{$customer_id}" />

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

                                            {if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}
                                            {loop name="address" type="address" customer="$customer_id" backend_context="1" default="true"}
                                            <div class="col-md-6">
                                                <p class="title title-without-tabs">{intl l="Customer informations"}</p>

                                                {custom_render_form_field field='title'}
                                                    <select {form_field_attributes field='title'} >
                                                        {loop type="title" name="title1"  backend_context="1"}
                                                            <option value="{$ID}"{if $value == $ID} selected{/if}>{$LONG}</option>
                                                        {/loop}
                                                    </select>
                                                {/custom_render_form_field}

                                                {render_form_field field='firstname'}
                                                {render_form_field field='lastname'}
                                                {render_form_field field='email'}

                                                {custom_render_form_field field='lang_id'}
                                                    <select {form_field_attributes field='lang_id'}>
                                                        {loop type="lang" name="lang.list" visible="*" order="alpha"}
                                                            <option value="{$ID}" {if {$value} == $ID}selected{/if}>{$TITLE}</option>
                                                        {/loop}
                                                    </select>
                                                {/custom_render_form_field}

                                                {render_form_field field='discount'}
                                                {render_form_field field='reseller'}
                                                {render_form_field field='password'}

                                                <p class="title title-without-tabs">{intl l="Default address"}</p>

                                                {render_form_field field='company'}
                                                {render_form_field field='address1'}
                                                {render_form_field field='address2' show_label=false}
                                                {render_form_field field='address3' show_label=false}
                                                {render_form_field field='zipcode'}
                                                {render_form_field field='city'}

                                                {custom_render_form_field field='country'}
                                                    <select {form_field_attributes field='country'}>
                                                        {loop type="country" name="country.list" visible="*" order="alpha"}
                                                            <option value="{$ID}" {if {$value} == $ID}selected{/if}>{$TITLE}</option>
                                                        {/loop}
                                                    </select>
                                                    {$countryFieldId=$label_attr.for}
                                                {/custom_render_form_field}

                                                <div class="group-state">
                                                    {custom_render_form_field field='state'}
                                                        <select {form_field_attributes field='state'}
                                                            data-thelia-state="1" data-thelia-country="#{$countryFieldId}" data-thelia-toggle=".group-state">
                                                            {loop type="state" name="state1" visible="*" order="alpha"}
                                                                <option value="{$ID}" {if {$value} == $ID}selected{/if} data-country="{$COUNTRY}">{$TITLE}</option>
                                                            {/loop}
                                                        </select>
                                                    {/custom_render_form_field}
                                                </div>

                                                {render_form_field field='phone'}
                                                {render_form_field field='cellphone'}
                                            </div>
                                            {/loop}

                                            <div class="col-md-6">
                                                <p class="title title-without-tabs clearfix">
                                                    {intl l="Other addresses"}
                                                    {include file='renderer/buttons.html' btn_group=false buttons=[
                                                        [
                                                            'type' => 'create',
                                                            'class' => 'pull-right',
                                                            'title' => {intl l='Add a new address'},
                                                            'href' => '#address_creation_dialog',
                                                            'data' => [
                                                                'toggle' => 'modal'
                                                            ]
                                                        ]
                                                    ]}
                                                </p>

                                                {ifloop rel="address"}
                                                <div class="table-responsive">
                                                    <table class="table table-bordered table-striped">
                                                        <thead>
                                                            <tr>
                                                                <th>{intl l="Address"}</th>
                                                                <th class="text-center">{intl l="Actions"}</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                        {loop name="address" type="address" customer="$customer_id" backend_context="1" default="0"}
                                                            <tr>
                                                                <td>
                                                                    {$LABEL}
                                                                    <address>
                                                                        {if $COMPANY}<strong>{$COMPANY}</strong><br />{/if}
                                                                        <strong>{loop name="address.title" type="title" id=$TITLE}{$SHORT}{/loop} {$FIRSTNAME} {$LASTNAME}</strong><br>
                                                                        {$ADDRESS1}
                                                                        {if $ADDRESS2}<br />{$ADDRESS2}{/if}
                                                                        {if $ADDRESS3}<br />{$ADDRESS3}{/if}
                                                                        <br>{$ZIPCODE} {$CITY}
                                                                        {loop name="address.country" type="country" id=$COUNTRY visible="*"}
                                                                            <br>{$TITLE}
                                                                        {/loop}
                                                                        {if $STATE}{loop type="state" name="address.state" id=$STATE}, {$TITLE}{/loop}{/if}

                                                                        {if $PHONE}
                                                                            <br>{intl l="Phone"}: {$PHONE}
                                                                        {/if}
                                                                        {if $CELLPHONE}
                                                                            <br>{intl l="Cellular phone"}: {$CELLPHONE}
                                                                        {/if}
                                                                    </address>
                                                                </td>
                                                                <td class="text-center">
                                                                    <div class="btn-group">

                                                                        <a class="btn btn-default btn-xs customer-update-address" title="{intl l='Edit this address'}" href="{url path='/admin/address/update' address_id=$ID}">
                                                                            <span class="glyphicon glyphicon-edit"></span>
                                                                        </a>

                                                                        <a class="btn btn-default btn-xs customer-address-use" title="{intl l='Use this address by default'}" href="#use_address_dialog" data-toggle="modal" data-id="{$ID}" data-toggle="modal" rel="tooltip">
                                                                            <span class="glyphicon glyphicon-pushpin"></span>
                                                                        </a>

                                                                        <a class="btn btn-default btn-xs customer-address-delete" title="{intl l='Delete this customer address'}"  href="#delete_address_dialog" data-id="{$ID}" data-toggle="modal">
                                                                            <span class="glyphicon glyphicon-trash"></span>
                                                                        </a>

                                                                    </div>
                                                                </td>
                                                            </tr>
                                                        {/loop}
                                                        </tbody>
                                                    </table>
                                                </div>
                                                {/ifloop}

                                                {elseloop rel="address"}
                                                    <div class="alert alert-info">
                                                    {intl l="This customer has not defined any delivery address"}
                                                </div>
                                                {/elseloop}
                                            </div>

                                        </form>
                                    {/form}

                                </div>
                            </div>

                        </div>
                    </div>

                </div>
                <div class="row">
                    <div class="col-md-12 general-block-decorator">
                        <div class="row">
                            <div class="col-md-12 title title-without-tabs">
                                {intl l="orders for this customer"}
                            </div>
                            <div class="col-md-12">
                                <div class="table-responsive">
                                    <table class="table table-striped table-left-aligned">
                                        <thead>
                                        <tr>
                                            <th>{intl l="Order n°"}</th>
                                            <th>{intl l="Date & Hour"}</th>
                                            <th class="text-right">{intl l="Amount"}</th>
                                            <th>{intl l="Status"}</th>
                                            {hook name="customer.orders-table-header" location="orders_table_header" customer_id=$customer_id }
                                        </tr>
                                        </thead>
                                        <tbody>
                                        {loop type="order" name="order-list" customer="{$customer_id}" backend_context="1" status='*' page={$page_order} limit={config key="number_default_results_per_page.order_list" default=20}}
                                        {loop type="order-status" name="order-status" id=$STATUS}
                                        {assign "orderStatus" $TITLE}
                                        {assign "orderStatusLabel" "order_$CODE"}
                                        {assign "orderStatusCodeColor" $COLOR}

                                        {/loop}
                                            <tr>
                                                <td><a href="{url path="/admin/order/update/%id" id=$ID}">{$REF}</a></td>
                                                <td>{format_date date=$CREATE_DATE}</td>
                                                <td class="text-right">{format_money number=$TOTAL_TAXED_AMOUNT}</td>
                                                <td>
                                                    <span class="label" style="background-color:{$orderStatusCodeColor};">{$orderStatus}</span>
                                                </td>
                                                {hook name="customer.orders-table-row" location="orders_table_row" customer_id=$customer_id order_id=$ID}
                                            </tr>
                                        {/loop}

                                        {elseloop rel="order-list"}
                                            <tr>
                                                <td colspan="4">
                                                    <div class="alert alert-info">
                                                        {intl l="No orders fo this customer."}
                                                    </div>
                                                </td>
                                            </tr>
                                        {/elseloop}

                                        </tbody>
                                        <tfoot>
                                        <tr>
                                            <td colspan="7">
                                                {include
                                                file = "includes/pagination.html"
                                                page_param_name = "order_page"
                                                loop_ref       = "order-list"
                                                max_page_count = 10
                                                page_url       = {url path="/admin/customer/update" customer_id=$customer_id page=$page}
                                                page_param_name = "page_order"
                                                }
                                            </td>
                                        </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            {hook name="customer.edit" location="customer-edit" customer_id=$customer_id}

            {hook name="customer-edit.bottom" customer_id=$customer_id}

    		{/loop}

            {elseloop rel="customer_edit"}
                <div class="row">
                    <div class="col-md-12">
                        <div class="alert alert-error">
                            {intl l="Sorry, customer ID=%id was not found." id={$customer_id}}
                        </div>
                    </div>
                </div>
            {/elseloop}

        </div>
    </div>

{* -- Add an Address -------------------------------------------------------- *}

{form name="thelia.admin.address.create"}

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

        {include file='includes/customer_address_form_fields.html' customer_id=$customer_id page=$page dialog_type="new"}

        {hook name="customer.address-create-form" location="customer_address_create_form" customer_id=$customer_id page=$page}

    {/capture}

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

        dialog_id    = "address_creation_dialog"
        dialog_title = {intl l="Create a customer address"}
        dialog_body  = {$smarty.capture.address_creation_dialog nofilter}

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

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

{/form}

{* -- Update an Address ----------------------------------------------------- *}

{if $address_id > 0}
	{form name="thelia.admin.address.update"}

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

	    {capture "address_update_dialog"}
	        <input type="hidden" name="address_id" id="address_update_id" value="{$address_id}" />

	        {include file='includes/customer_address_form_fields.html' customer_id=$customer_id page=$page dialog_type="update"}
            {hook name="customer.address-update-form" location="customer_address_update_form" customer_id=$customer_id page=$page}
	    {/capture}

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

	        dialog_id    = "address_update_dialog"
	        dialog_title = {intl l="Edit a customer address"}
	        dialog_body  = {$smarty.capture.address_update_dialog nofilter}

	        dialog_ok_label     = {intl l="Save this address"}
	        dialog_cancel_label = {intl l="Cancel"}

	        form_action        = {url path="/admin/address/save"}
	        form_enctype       = {form_enctype}
	        form_error_message = $form_error_message
	    }

    {/form}
{/if}

{* -- Confirm making an address the default --------------------------------- *}

{capture "use_address_dialog"}
    <input type="hidden" name="address_id" id="address_use_id" value="" />

    <input type="hidden" name="customer_id" value="{$customer_id}">
    <input type="hidden" name="page" value="{$page}">
{/capture}

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

    dialog_id       = "use_address_dialog"
    dialog_title    = {intl l="Use address by default"}
    dialog_message  = {intl l="Do you really want to use this address by default ?"}

    form_action         = {url path='/admin/address/use'}
    form_content        = {$smarty.capture.use_address_dialog nofilter}
}

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

{capture "delete_address_dialog"}
    <input type="hidden" name="address_id" id="address_delete_id" value="" />

    <input type="hidden" name="customer_id" value="{$customer_id}">
    <input type="hidden" name="page" value="{$page}">

    {hook name="customer.address-delete-form" location="customer_address_delete_form" customer_id=$customer_id page=$page}
{/capture}

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

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

    form_action         = {token_url path='/admin/address/delete'}
    form_content        = {$smarty.capture.delete_address_dialog nofilter}
}
{/block}


{block name="javascript-initialization"}
    {javascripts file='assets/js/thelia-states.js'}
        <script src="{$asset_url}"></script>
    {/javascripts}
    <script>
        $(function() {

                $("a.customer-address-delete").click(function(e){
                    $("#address_delete_id").val($(this).data("id"));
                });

                $("a.customer-address-use").click(function(e){
                    $("#address_use_id").val($(this).data("id"));
                });

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

                $(document).on("hidden.bs.modal", "#edit_address_dialog", function(ev){
                    $("#edit_address_dialog").remove();
                });

                // If $address_id is defined, show the edit dialog
                {if $address_id > 0}

                    // JS stuff for update form
                    {include
	                    file      = "includes/generic-js-dialog.html"
	                    dialog_id = "address_update_dialog"
	                    form_name = "thelia.admin.address.update"
	                }

                    $('#address_update_dialog').modal();
                {/if}
            });
    </script>
{/block}

{block name="javascript-last-call"}
    {hook name="customer.edit-js" location="customer-edit-js" customer_id=$customer_id}
{/block}
