{% extends "layout_default.phtml" %}
{% set active_menu = 'client' %}
{% block meta_title %}{{ client.first_name }} {{ client.last_name }}{% endblock %}

{% block content %}

<div class="widget simpleTabs nomargin">

    <ul class="tabs">
        <li><a href="#tab-info">{{ client.email|truncate(30) }}</a></li>
        <li><a href="#tab-profile">{% trans 'Profile' %}</a></li>
        <li><a href="#tab-orders">{% trans 'Orders' %}</a></li>
        <li><a href="#tab-invoice">{% trans 'Invoices' %}</a></li>
        <li><a href="#tab-support">{% trans 'Tickets' %}</a></li>
        <li><a href="#tab-balance">{% trans 'Payments' %}</a></li>
        <li><a href="#tab-history">{% trans 'Logins' %}</a></li>
        <li><a href="#tab-emails">{% trans 'Emails' %}</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-info">

            <div class="help">
                <a href="{{ client.email|gravatar }}?size=100" target="_blank"><img src="{{ client.email|gravatar }}?size=30" alt="{{ client.id }}" style="float: right;"/></a>
                <h3>{% trans 'Profile details' %}</h3>
            </div>

            <table class="tableStatic wide">
                <tbody>
                    <tr class="noborder">
                        <td>ID:</td>
                        <td align="right">{{ client.id }}</td>
                    </tr>

                    <tr>
                        <td>Name:</td>
                        <td align="right"><strong class="red">{{ client.first_name }} {{ client.last_name }}</strong></td>
                    </tr>

                    <tr>
                        <td>Company:</td>
                        <td align="right"><strong class="green">{{ client.company|default('-') }}</strong></td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <td align="right">{{ client.email }}</td>
                    </tr>
                    <tr>
                        <td>Status:</td>
                        <td align="right">{{ mf.status_name(client.status) }}</td>
                    </tr>
                    <tr>
                        <td>Group:</td>
                        <td align="right">{{ client.group|default('-') }}</td>
                    </tr>
                    <tr>
                        <td>Currency:</td>
                        <td align="right">{{ client.currency|default('-') }}</td>
                    </tr>
                    <tr>
                        <td>IP:</td>
                        <td align="right">{{ client.ip|default('-') }} {{ client.ip|ipcountryname }}</td>
                    </tr>
                    <tr>
                        <td>API Key:</td>
                        <td align="right">{{ client.api_token|default('-') }}</td>
                    </tr>
                    <tr>
                        <td>Address:</td>
                        <td align="right">{{ client.address_1 }} {{ client.address_2 }}</td>
                    </tr>
                    <tr>
                        <td>Registered at:</td>
                        <td align="right">{{ client.created_at|date('M d, Y') }}</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2">
                            <a href="#" title="" class="btnIconLeft mr10 mt5" onclick="api.get('admin/client/login',{id:{{client.id}}}, function(){ window.location = '{{ '/'|link }}'  }); return false;"><img src="images/icons/dark/adminUser.png" alt="" class="icon"><span>Login to client area</span></a>
                            <a href="admin/client/delete?id={{client.id}}" data-api-confirm="Are you sure?" data-api-redirect="client" class="btnIconLeft mr10 mt5 api-link" ><img src="images/icons/dark/trash.png" alt="" class="icon"><span>Delete</span></a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-profile">

            <div class="help">
                <h3>{% trans 'Client profile details' %}</h3>
            </div>

            <form method="post" action="admin/client/update" class="mainForm api-form save" data-api-msg="{% trans 'Client Profile updated' %}">
                <fieldset>
                    <div class="rowElem">
                        <label>{% trans 'Status' %}:</label>
                        <div class="formRight noborder">
                            <input type="radio" name="status" value="active"{% if client.status == 'active' %} checked="checked"{% endif %}/><label>{% trans 'Active' %}</label>
                            <input type="radio" name="status" value="suspended"{% if client.status == 'suspended' %} checked="checked"{% endif %} /><label>{% trans 'Suspended' %}</label>
                            <input type="radio" name="status" value="canceled"{% if client.status == 'canceled' %} checked="checked"{% endif %} /><label>{% trans 'Canceled' %}</label>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Group' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('group_id', admin.client_group_get_pairs, client.group_id, 0, 'Select group') }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Email' %}:</label>
                        <div class="formRight">
                            <input type="text" name="email" value="{{client.email}}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Name' %}:</label>
                        <div class="formRight moreFields">
                            <ul>
                                <li style="width: 200px"><input type="text" name="first_name" value="{{client.first_name}}" required="required"/></li>
                                <li class="sep"></li>
                                <li style="width: 200px"><input type="text" name="last_name" value="{{client.last_name}}" /></li>
                            </ul>
                        </div> 
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Company' %}:</label>
                        <div class="formRight">
                            <input type="text" name="company" value="{{client.company}}" />
                        </div>
                        <div class="fix"></div>
                    </div>
                </fieldset>
                <fieldset>
                    <legend>{% trans 'Address and contact details' %}</legend>
                    <div class="rowElem">
                        <label>{% trans 'Address Line 1' %}:</label>
                        <div class="formRight">
                            <input type="text" name="address_1" value="{{client.address_1}}" />
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Address Line 2' %}:</label>
                        <div class="formRight">
                            <input type="text" name="address_2" value="{{client.address_2}}" />
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Country' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('country', guest.system_countries, client.country, 0, 'Select country') }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'State' %}:</label>
                        <div class="formRight">
                            {# mf.selectbox('state', guest.system_states, client.state, 0, 'Select state') #}
                            <input type="text" name="state" value="{{ client.state }}" />
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'City' %}:</label>
                        <div class="formRight">
                            <input type="text" name="city" value="{{client.city}}" />
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Postcode' %}:</label>
                        <div class="formRight">
                            <input type="text" name="postcode" value="{{client.postcode}}" />
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Phone' %}:</label>
                        <div class="formRight moreFields">
                            <ul>
                                <li><input type="text" name="phone_cc" value="{{client.phone_cc}}" /></li>
                                <li class="sep"></li>
                                <li style="width: 200px;"><input type="text" name="phone" value="{{client.phone}}" /></li>
                            </ul>  
                        </div> 
                        <div class="fix"></div>   
                    </div>
                </fieldset>
                
                <fieldset>
                    <legend>{% trans 'Additional settings' %}</legend>
                    <div class="rowElem">
                        <label>{% trans 'Alternative ID' %}:</label>
                        <div class="formRight">
                            <input type="text" name="aid" value="{{client.aid}}" placeholder="{% trans 'Used to identify client on foreign system. Usually used by importers' %}"/>
                        </div> 
                        <div class="fix"></div>   
                    </div>
                    
                    <div class="rowElem">
                        <label>{% trans 'Currency' %}:</label>
                        <div class="formRight">
                            {{ mf.selectbox('currency', guest.currency_get_pairs, client.currency, 0, 'Select currency') }}
                        </div> 
                        <div class="fix"></div>
                    </div>
                    
                    <div class="rowElem">
                        <label>{% trans 'Exempt from tax' %}:</label>
                        <div class="formRight">
                            <input type="radio" name="tax_exempt" value="1"{% if client.tax_exempt %} checked="checked"{% endif %}/><label>Yes</label>
                            <input type="radio" name="tax_exempt" value="0"{% if not client.tax_exempt %} checked="checked"{% endif %} /><label>No</label>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Signed up time' %}:</label>
                        <div class="formRight">
                            <input type="text" name="created_at" value="{{client.created_at}}" />
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Notes' %}:</label>
                        <div class="formRight">
                            <textarea name="notes" cols="5" rows="5">{{client.notes}}</textarea>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <input type="submit" value="{% trans 'Update profile' %}" class="greyishBtn submitForm" />
                    <input type="hidden" name="id" value="{{ client.id }}"/>
                </fieldset>
            </form>
            
            <div class="help">
                <h3>{% trans 'Change password' %}</h3>
            </div>
            <form method="post" action="admin/client/change_password" class="mainForm api-form" data-api-msg="{% trans 'Password changed' %}">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Password' %}</label>
                        <div class="formRight">
                            <input type="password" name="password" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Password confirm' %}</label>
                        <div class="formRight">
                            <input type="password" name="password_confirm" value="" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Change password' %}" class="greyishBtn submitForm" />
                    <input type="hidden" name="id" value="{{ client.id }}"/>
                </fieldset>
            </form>

            <div class="help">
                <h3>{% trans 'Custom fields' %}</h3>
                <p>{% trans 'Use these fields to define custom client details' %}</p>
            </div>
            
            <form method="post" action="admin/client/update" class="mainForm api-form save" data-api-msg="{% trans 'Client Profile custom fields updated' %}">
                <fieldset>
                    {% for i in 1..10 %}
                    {% set fn = 'custom_'~i %}
                    <div class="rowElem{% if loop.first%} noborder{% endif %}">
                        <label>{% trans 'Custom field' %} {{i}}</label>
                        <div class="formRight">
                            <textarea name="custom_{{i}}" cols="5" rows="5">{{client[fn]}}</textarea>
                        </div>
                        <div class="fix"></div>
                    </div>
                    {% endfor %}

                    <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
                    <input type="hidden" name="id" value="{{ client.id }}"/>
                </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-balance">
            <div class="help">
                <h3>{% trans 'Client payments history' %}</h3>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <th style="width: 15%">{% trans 'Amount' %}</th>
                        <th>{% trans 'Description' %}</th>
                        <th style="width: 20%">{% trans 'Date' %}</th>
                        <th style="width: 5%">&nbsp;</th>
                    </tr>
                </thead>

                <tbody>
                    {% set transactions = admin.client_balance_get_list({"per_page":20, "client_id":client.id}) %}
                    {% for i, tx in transactions.list %}
                    <tr>
                        <td>{{mf.currency_format(tx.amount, tx.currency)}}</td>
                        <td>{{tx.description}}</td>
                        <td>{{tx.created_at|date("Y-m-d H:i")}}</td>
                        <td>
                            <a class="bb-button btn14 bb-rm-tr api-link" data-api-confirm="Are you sure?" href="admin/client/balance_delete?id={{tx.id}}"><img src="images/icons/dark/trash.png" alt=""></a>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="4">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>

                <tfoot>
                    <tr>
                        <th class="currency" colspan="4">{% trans 'Balance' %}: {{ mf.currency_format(client.balance, client.currency) }}</th>
                    </tr>
                </tfoot>
            </table>
            
            <div class="help">
                <h3>{% trans 'Add funds for client' %}</h3>
            </div>

            <form method="post" action="admin/client/balance_add_funds" class="mainForm api-form save" data-api-msg="{% trans 'Funds added' %}">
                <fieldset>
                    <div class="rowElem">
                        <label>{% trans 'Amount' %}:</label>
                        <div class="formRight noborder">
                            <input type="text" name="amount" value="" style="width: 100px;" required="required"/> {{ client.currency }}
                        </div>
                        <div class="fix"></div>
                    </div>
                    
                    <div class="rowElem">
                        <label>{% trans 'Description' %}:</label>
                        <div class="formRight noborder">
                            <input type="text" name="description" value=""  required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Add funds' %}" class="greyishBtn submitForm" />
                    <input type="hidden" name="id" value="{{ client.id }}"/>
                </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-orders">
            <div class="help">
                <h3>{% trans 'Client orders' %}</h3>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="40%">Title</td>
                        <td width="20%">Amount</td>
                        <td width="20%">Period</td>
                        <td width="20%">Status</td>
                        <td>&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    {% set orders = admin.order_get_list({"per_page":"20", "hide_addons":1, "client_id":client.id}) %}
                    {% for order in orders.list %}
                    <tr>
                        <td>{{order.id}}</td>
                        <td>{{order.title|truncate(30) }}</td>
                        <td>{{ mf.currency_format( order.total, order.currency) }}</td>
                        <td>{{ mf.period_name( order.period) }}</td>
                        <td>{{ mf.status_name(order.status) }}</td>
                        <td class="actions"><a class="bb-button btn14" href="{{ '/order/manage'|alink }}/{{order.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="6">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
                
                <tfoot>
                    <tr>
                        <td colspan="6">
                            <a href="{{ 'order'|alink}}?client_id={{client.id}}#tab-new" class="btnIconLeft mr10 mt5"><img src="images/icons/dark/money.png" alt="" class="icon"><span>New order</span></a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-invoice">
            <div class="help">
                <h3>{% trans 'Client invoices' %}</h3>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="15%">Amount</td>
                        <td width="30%">Issued at</td>
                        <td width="30%">Paid at</td>
                        <td width="15%">Status</td>
                        <td>&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                    {% set invoices = admin.invoice_get_list({"per_page":"20", "client_id":client.id}) %}
                    {% for invoice in invoices.list %}
                    <tr>
                        <td>{{invoice.id}}</td>
                        <td>{{ mf.currency_format( invoice.total, invoice.currency) }}</td>
                        <td>{{ invoice.created_at|date('Y-m-d') }}</td>
                        <td>{% if invoice.paid_at %}{{ invoice.paid_at|date('Y-m-d') }}{% else %}-{% endif %}</td>
                        <td>{{ mf.status_name(invoice.status) }}</td>
                        <td class="actions"><a class="bb-button btn14" href="{{ '/invoice/manage'|alink }}/{{invoice.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="6">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
                
                <tfoot>
                    <tr>
                        <td colspan="6">
                            <a href="admin/invoice/prepare?client_id={{client.id}}" class="btnIconLeft mr10 mt5 api-link" data-api-jsonp="onAfterInvoicePrepared" ><img src="images/icons/dark/money.png" alt="" class="icon"><span>New invoice</span></a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-support">
            <div class="help">
                <h3>{% trans 'Client support tickets' %}</h3>
            </div>
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td width="60%">Subject</td>
                        <td width="15%">Help desk</td>
                        <td width="15%">Status</td>
                        <td>&nbsp;</td>
                    </tr>
                </thead>
                
                <tbody>
                    {% set tickets = admin.support_ticket_get_list({"per_page":"20", "client_id":client.id}) %}
                    {% for ticket in tickets.list %}
                    <tr>
                        <td>{{ ticket.id }}</td>
                        <td>{{ ticket.subject|truncate(30) }}</td>
                        <td>{{ ticket.helpdesk.name }}</td>
                        <td>{{ mf.status_name(ticket.status) }}</td>
                        <td class="actions"><a class="bb-button btn14" href="{{ '/support/ticket'|alink }}/{{ticket.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="5">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
                
                <tfoot>
                    <tr>
                        <td colspan="5">
                            <a href="{{ 'support'|alink}}?client_id={{client.id}}#tab-new" class="btnIconLeft mr10 mt5" ><img src="images/icons/dark/help.png" alt="" class="icon"><span>New support ticket</span></a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
        
        <div class="tab_content nopadding" id="tab-history">
            <div class="help">
                <h3>{% trans 'Logins history' %}</h3>
            </div>
            
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <th>{% trans 'IP' %}</th>
                        <th>{% trans 'Country' %}</th>
                        <th>{% trans 'Date' %}</th>
                    </tr>
                </thead>

                <tbody>
                    {% set logins = admin.client_login_history_get_list({"per_page":10, "page":request.page, "client_id":client.id}) %}
                    {% for i, login in logins.list %}
                    <tr>
                        <td>{{login.ip}}</td>
                        <td>{{login.ip|ipcountryname}}</td>
                        <td>{{login.created_at|date('l, d F Y')}}</td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="3">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-emails">
            <div class="help">
                <h3>{% trans 'Emails sent to client' %}</h3>
            </div>

            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <th>{% trans 'Email subject' %}</th>
                        <th>{% trans 'To' %}</th>
                        <th>{% trans 'Date sent' %}</th>
                        <th style="width: 5%">&nbsp;</th>
                    </tr>
                </thead>

                <tbody>
                    {% set emails = admin.email_email_get_list({"per_page":"20", "client_id":client.id}) %}
                    {% for i, email in emails.list %}
                    <tr>
                        <td>{{email.subject}}</td>
                        <td>{{ email.recipients }}</td>
                        <td>{{email.created_at|date('l, d F Y')}}</td>
                        <td class="actions"><a class="bb-button btn14" href="{{ 'email'|alink }}/{{email.id}}"><img src="images/icons/dark/pencil.png" alt=""></a></td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="4">{% trans 'The list is empty' %}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>


{#
<div class="widgets">
    <div class="left">
        <div class="widget">
            <div class="head">
                <h5 class="iDayCalendar">{{ client.first_name }} {{ client.last_name }}</h5>
                <div class="num"><span>Balance:</span><a href="#" class="blueNum">{{ mf.currency_format(client.balance, client.currency) }}</a></div>
            </div>
            <table class="tableStatic wide">
                <tbody>
                    <tr class="noborder">
                        <td>Company:</td>
                        <td align="right"><strong class="red">{{ client.company }}</strong></td>
                    </tr>
                    <tr>
                        <td>Registered at:</td>
                        <td align="right">{{ client.created_at|date('M d, Y') }}</td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <td align="right"><a href="#" title="">{{ client.email }}</a></td>
                    </tr>
                    <tr>
                        <td>Status:</td>
                        <td align="right"><a href="#" class="green">{{ mf.status_name(client.status) }}</a></td>
                    </tr>
                    <tr>
                        <td>IP:</td>
                        <td align="right"><span class="expire">{{ client.ip }} {{ client.ip|ipcountryname }}</span></td>
                    </tr>
                    <tr>
                        <td>Address:</td>
                        <td align="right">{{ client.address_1 }} {{ client.address_2 }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="right">

        <div class="widget">
            <div class="head"><h5 class="iMoney">2 Latest orders</h5><div class="num"><span>Total:</span><a href="{{ 'order'|alink }}?client_id={{ client.id }}" class="greenNum">{{ orders.total }}</a></div></div>

            {% set orders = admin.order_get_list({"per_page":"2", "hide_addons":1, "client_id":client.id}) %}
            {% for order in orders.list %}
            <div class="supTicket{% if loop.first %} nobg{% endif %}">
                <div class="issueType">
                    <span class="issueInfo"><a href="{{ 'order/manage'|alink }}/{{ order.id }}" title="">{{ order.title|truncate(45) }}</a></span>
                    <span class="issueNum"><a href="{{ 'order/manage'|alink }}/{{ order.id }}" title="">[ {{ order.id }} ]</a></span>
                    <div class="fix"></div>
                </div>

                <div class="issueSummary">
                    <a href="{{ 'order/manage'|alink }}/{{ order.id }}" title="" class="floatleft"><img src="{{ order.client.email|gravatar }}?size=35" alt=""></a>
                    <div class="ticketInfo">
                        <ul>
                            <li>Current order status:</li>
                            <li class="even"><strong class="green">[ {{ order.status }} ]</strong></li>
                            <li>User email:</li>
                            <li class="even"><a href="{{ 'client/manage'|alink }}/{{ order.client_id }}" title="">{{ order.client.email }}</a></li>
                        </ul>
                        <div class="fix"></div>
                    </div>
                    <div class="fix"></div>
                </div>
            </div>
            {% else %}
            <div class="supTicket{% if loop.first %} nobg{% endif %}">
                The list is empty
            </div>
            {% endfor %}
        </div>
    </div>
    <div class="fix"></div>
</div>
#}

{% endblock %}


{% block js%}
<script type="text/javascript">
$(function() {

});

function onAfterInvoicePrepared(id) {
    bb.redirect("invoice/manage/"+id);
}
</script>
{% endblock %}