{% extends 'dashboard/layout.html' %}
{% load currency_filters %}
{% load i18n %}

{% block body_class %}orders{% endblock %}

{% block title %}
{% blocktrans with number=order.number %}Order {{ number }}{% endblocktrans %} | {{ block.super }}
{% endblock %}

{% block breadcrumbs %}
<ul class="breadcrumb">
    <li>
	<a href="{% url dashboard:index %}">{% trans "Dashboard" %}</a>
        <span class="divider">/</span>
    </li>
    <li>
	<a href="{% url dashboard:order-list %}">{% trans "Orders" %}</a>
        <span class="divider">/</span>
    </li>
	<li class="active"><a href=".">#{{ order.number }}</a></li>
</ul>
{% endblock %}

{% block header %}
<div class="page-header">
	<h1>{% blocktrans with number=order.number %}Order #{{ number }}{% endblocktrans %}</h1>
</div>
{% endblock header %}

{% block dashboard_content %}

<h3>{% trans "Customer Information" %}</h3>
	{% if order.user %}

    <div class="row-fluid">
        <div class="span4">
            <div class="well well-info">
				<h4>{% trans "Name" %}</h4>
                <p>{{ order.user.get_full_name|default:"-" }}</p>
            </div>
        </div>
        <div class="span4">
            <div class="well well-info">
				<h4>{% trans "Email address" %}</h4>
                <p>{{ order.user.email|default:"-" }}</p>
            </div>
        </div>
    </div>
	{% else %}
	<p>{% trans "Customer checked out anonymously." %}</p>
	{% endif %}
	<h3>{% trans "Order information" %}</h3>
    <div class="row-fluid">
        <div class="span2">
            <div class="well well-info">
				<h4>{% trans "Order Total" %}</h4>
                <p>{{ order.total_incl_tax|currency }}</p>
            </div>
        </div>
        <div class="span2">
            <div class="well well-info">
				<h4>{% trans "Date of purchase" %}</h4>
                <p>{{ order.date_placed|date:"d/m/y" }}</p>
            </div>
        </div>
        <div class="span2">
            <div class="well well-info">
				<h4>{% trans "Time of purchase" %}</h4>
                <p>{{ order.date_placed|date:"H:s" }}</p>
            </div>
        </div>
        <div class="span2">
            <div class="well well-info">
				<h4>{% trans "Status" %}</h4>
                <p>{{ order.status|default:"N/A" }}</p>
            </div>
        </div>
    </div>


<div class="sub-header">
	<h2>{% trans "Order Details" %}</h2>
</div>


<div class="tabbable dashboard">
	<ul class="nav nav-tabs">
	{% block nav_tabs %}
	<li class="{% if active_tab == 'lines' %}active{% endif %}"><a href="#lines" data-toggle="tab">{% trans "Order contents" %}</a></li>
	<li class="{% if active_tab == 'shipping' %}active{% endif %}"><a href="#shipping" data-toggle="tab">{% trans "Shipping" %}</a></li>
	<li class="{% if active_tab == 'payment' %}active{% endif %}"><a href="#payment" data-toggle="tab">{% trans "Payment" %}</a></li>
	<li class="{% if active_tab == 'discounts' %}active{% endif %}"><a href="#discounts" data-toggle="tab">{% trans "Discounts" %}</a></li>
	<li class="{% if active_tab == 'emails' %}active{% endif %}"><a href="#emails" data-toggle="tab">{% trans "Emails" %}</a></li>
	<li class="{% if active_tab == 'notes' %}active{% endif %}"><a href="#notes" data-toggle="tab">{% trans "Notes" %}</a></li>
	{% endblock nav_tabs %}
	</ul>

	<div class="tab-content">
		<div class="tab-pane {% if active_tab == 'lines' %}active{% endif %}" id="lines">
			<div class="sub-header">
				<h3 class="app-ico ico_shop_bag icon">{% trans "Items ordered" %}</h3>
			</div>
			<form action="." method="post" class="form-inline">
				{% csrf_token %}
				<table class="table table-striped table-bordered">
					<thead>
						<tr>
							<th>{% trans "Select" %}</th>
							<th>{% trans "Quantity" %}</th>
							<th>{% trans "Product" %}</th>
							<th>{% trans "UPC" %}</th>
							<th>{% trans "Status" %}</th>
							<th>{% trans "Supplier" %}</th>
							<th>{% trans "Supplier SKU" %}</th>
							<th>{% trans "Est. delivery date" %}</th>
							<th>{% trans "Price (before discounts)" %}</th>
							<th>{% trans "Actions" %}</th>
						</tr>
					</thead>
					<tbody>
						{% for line in order.lines.all %}
						<tr>
							<td>
								<input type="checkbox" name="selected_line" value="{{ line.id }}" />
								<input type="text" name="selected_line_qty" value="{{ line.quantity }}" class="span1" size="2" />
							</td>
							<td>{{ line.quantity }}</td>
							<td>{{ line.title }}</td>
							<td>{{ line.upc }}</td>
							<td>{{ line.status|default:"-" }}</td>
							<td>{{ line.partner_name }}</td>
							<td>{{ line.partner_sku }}</td>
							<td>{{ line.est_dispatch_date }}</td>
							<td>{{ line.line_price_before_discounts_incl_tax|currency }}</td>
							<td>
								<a href="{% url dashboard:order-line-detail order.number line.id %}" class="btn btn-info">{% trans "View" %}</a>
							</td>
						</tr>
						{% endfor %}
						<tr>
							<td colspan="7"></td>
							<th>{% trans "Discount" %}</th>
							<td>{{ order.total_discount_incl_tax|currency }}</td>
							<td></td>
						</tr>
						<tr>
							<td colspan="7"></td>
							<th>{% trans "Shipping charge" %}</th>
							<td>{{ order.shipping_incl_tax|currency }}</td>
							<td></td>
						</tr>
						<tr>
							<td colspan="7"></td>
							<th>{% trans "Total" %}</th>
							<td>{{ order.total_incl_tax|currency }}</td>
							<td></td>
						</tr>
					</tbody>
				</table>

				{% block line_actions %}
				<p>With selected lines:</p>

				<div class="control-group">
					<div class="controls">
						<label class="radio inline">
							<input type="radio" name="line_action" value="change_line_statuses" /> {% trans "Change status to" %}
						</label>
						<label class="radio inline">
							<select name="new_status">
								<option value=""> -- {% trans "choose new status" %} -- </option>
								{% for status in line_statuses %}
								<option>{{ status }}</option>
								{% endfor %}
							</select>
						</label>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<label class="radio inline">
							<input type="radio" name="line_action" value="create_shipping_event" /> {% trans "Create shipping event" %}
						</label>
						<label class="radio inline">
							<select name="shipping_event_type">
								<option value=""> -- {% trans "choose event type" %} -- </option>
								{% for event_type in shipping_event_types %}
								<option value="{{ event_type.code }}">{{ event_type.name }}</option>
								{% endfor %}
							</select>
						</label>
						<label class="radio inline">
							with reference <input type="text" name="reference" value="" />
						</label>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<label class="radio inline">
							<input type="radio" name="line_action" value="create_payment_event" /> {% trans "Create payment event" %}
						</label>
						<label class="radio inline">
							<select name="payment_event_type">
								<option value=""> -- {% trans "choose event type" %} -- </option>
								{% for event_type in payment_event_types %}
								<option value="{{ event_type.code }}">{{ event_type.name }}</option>
								{% endfor %}
							</select>
						</label>
						<label class="radio inline">
							with amount <input type="text" name="amount" value="" />
						</label>
					</div>
				</div>

				<div class="form-actions">
					<input type="submit" value="{% trans "Go!" %}" class="btn btn-primary" />
				</div>
			</form>
			{% endblock line_actions %}

			<div class="sub-header">
				<h3 class="app-ico ico_favourite icon">{% trans "Shipping Events" %}</h3>
			</div>
			{% with events=order.shipping_events.all %}
			{% if events %}
			<table class="table table-striped table-bordered">
				<thead>
					<tr>
						<th>{% trans "Date" %}</th>
						<th>{% trans "Event" %}</th>
						<th>{% trans "Lines" %}</th>
						<th>{% trans "Notes" %}</th>
					</tr>
				</thead>
				<tbody>
					{% for event in events %}
					{% with line_qtys=event.line_quantities.all %}
					<tr>
						<td rowspan="{{ line_qtys|length }}">{{ event.date }}</td>
						<td rowspan="{{ line_qtys|length }}">{{ event.event_type.name }}</td>
						<td>
							{% for line_qty in event.line_quantities.all %}
							{% trans "Product:" %} {{ line_qty.line.title }} - {% trans "quantity" %} {{ line_qty.quantity }}</br>
							{% endfor %}
						</td>
						<td>{{ event.notes }}</td>
					</tr>
					{% endwith %}
					{% endfor %}
				</tbody>
			</table>
			{% else %}
			<p>No shipping events.</p>
			{% endif %}
			{% endwith %}

			<div class="sub-header">
				<h3 class="app-ico ico_favourite icon">{% trans "Payment Events" %}</h3>
			</div>
			{% with events=order.payment_events.all %}
			{% if events %}
			<table class="table table-striped table-bordered">
				<thead>
					<tr>
						<th>{% trans "Date" %}</th>
						<th>{% trans "Event" %}</th>
						<th>{% trans "Amount" %}</th>
						<th>{% trans "Lines" %}</th>
					</tr>
				</thead>
				<tbody>
					{% for event in events %}
					{% with line_qtys=event.line_quantities.all %}
					<tr>
						<td >{{ event.date }}</td>
						<td >{{ event.event_type.name }}</td>
						<td >{{ event.amount|currency }}</td>
						<td>
							{% for line_qty in event.line_quantities.all %}
							{% trans "Product:" %} {{ line_qty.line.title }} - {% trans "quantity" %} {{ line_qty.quantity }}</br>
							{% endfor %}
						</td>
					</tr>
					{% endwith %}
					{% endfor %}
				</tbody>
			</table>
			{% else %}
			<p>{% trans "No payment events." %}</p>
			{% endif %}
			{% endwith %}

		</div>

		<div class="tab-pane {% if active_tab == 'shipping' %}active{% endif %}" id="shipping">
			{% block tab_shipping %}
				<table class="table">
					<tbody>
						<tr>
							<th>{% trans "Method" %}</th>
							<td>{{ order.shipping_method }}</td>
						</tr>
						<tr>
							<th>{% trans "Charge (incl tax)" %}</th>
							<td>{{ order.shipping_incl_tax|currency }}</td>
						</tr>
						<tr>
							<th>{% trans "Charge (excl tax)" %}</th>
							<td>{{ order.shipping_excl_tax|currency }}</td>
						</tr>
						<tr>
							<th>{% trans "Address" %}</th>
							<td>
								{% for field in order.shipping_address.active_address_fields %}
								{{ field }}<br/>
								{% endfor %}
							</td>
						</tr>
					</tbody>
				</table>
			{% endblock %}
		</div>

		<div class="tab-pane {% if active_tab == 'payment' %}active{% endif %}" id="payment">
			{% if order.billing_address %}
				<div class="sub-header">
					<h3 class="app-ico ico_mapmarker icon">{% trans "Billing address" %}</h3>
				</div>
				<p>
				{% for field in order.billing_address.active_address_fields %}
				{{ field }}<br/>
				{% endfor %}
				</p>
			{% endif %}

			{% with sources=order.sources.all %}
			<div class="sub-header">
				<h3 class="app-ico ico_mapmarker icon">Payment sources</h3>
			</div>
			{% if sources %}
				<table class="table table-striped table-bordered">
					<thead>
						<tr>
							<th>{% trans "Source" %}</th>
							<th>{% trans "Allocation" %}</th>
							<th>{% trans "Amount debited" %}</th>
							<th>{% trans "Amount refunded" %}</th>
						</tr>  
					</thead>	
					<tbody> 
						{% for source in sources %}
						<tr>
							<td>{{ source.source_type }}</td>
							<td>{{ source.amount_allocated|currency }}</td>
							<td>{{ source.amount_debited|currency }}</td>
							<td>{{ source.amount_refunded|currency }}</td>
						</tr>
						{% endfor %}
					</tbody>
				</table>
			{% else %}
			<p>{% trans "No payment sources" %}</p>
			{% endif %}
			{% endwith %}

			{% block tab_transactions %}
			<div class="sub-header">
				<h3 class="app-ico ico_mapmarker icon">{% trans "Transactions" %}</h3>
			</div>
			<p>{% trans "No transaction data available." %}</p>
			{% endblock %}

		</div>

		<div class="tab-pane {% if active_tab == 'discounts' %}active{% endif %}" id="discounts">
			{% block tab_discounts %}
				<div class="sub-header">
					<h3 class="app-ico ico_settings icon">{% trans "Discounts" %}</h3>
				</div>
				{% with discounts=order.discounts.all %}
				{% if discounts %}
					<table class="table table-striped table-bordered">
						<thead>
							<tr>
								<th>{% trans "Voucher code" %}</th>
								<th>{% trans "Offer name" %}</th>
								<th>{% trans "Amount" %}</th>
							</tr>
						</thead>
						<tbody>
						{% for discount in discounts %}
						<tr>
							<td>{{ discount.voucher_code|default:"-" }}</td>
							<td>{{ discount.offer.name }}</td>
							<td>{{ discount.amount|currency}}</td>
						</tr>
						{% endfor %}
						</tbody>
					</table>
				{% else %}
				<p>{% trans "No discounts were applied in this order." %}</p>
				{% endif %}
				{% endwith %}
			{% endblock %}
		</div>

		<div class="tab-pane {% if active_tab == 'emails' %}active{% endif %}" id="emails">
			{% block tab_emails %}
			<div class="sub-header">
				<h3 class="app-ico ico_email icon">{% trans "Emails" %}</h3>
			</div>
			<p>{% trans "No email data available." %}</p>
			{% endblock %}
		</div>

		<div class="tab-pane {% if active_tab == 'notes' %}active{% endif %}" id="notes">
			{% block tab_notes %}
			<div class="sub-header">
				<h3 class="app-ico ico_comment icon">{% trans "Notes" %}</h3>
			</div>
			{% with notes=order.notes.all %}
			{% if notes %}
			<table class="table table-striped table-bordered">
				<tr>
					<th>{% trans "Date" %}</th>
					<th>{% trans "User" %}</th>
					<th>{% trans "Type" %}</th>
					<th>{% trans "Message" %}</th>
					<th></th>
				</tr>
				{% for note in notes %}
				<tr>
					<td>{{ note.date_created }}</td>
					<td>{{ note.user }}</td>
					<td>{{ note.note_type }}</td>
					<td>{{ note.message|linebreaks }}</td>
					<td class="span2">
						{% if note.is_editable %}
						&nbsp;<a href="{% url dashboard:order-detail-note order.number note.id %}#notes" class="btn btn-info">{% trans "Edit" %}</a>
						<form action="." method="post" class="pull-left">
							{% csrf_token %}
							<input type="hidden" name="order_action" value="delete_note" />
							<input type="hidden" name="note_id" value="{{ note.id }}" />
							<input type="submit" value="Delete" class="btn btn-danger" />
						</form>
						{% endif %}
					</td>
				</tr>
				{% endfor %}
			</table>
			{% endif %}
			{% endwith %}

			<form action=".?note={{ note_id }}" method="post" class="fields-full">
				{% csrf_token %}
				<input type="hidden" value="save_note" name="order_action" />
				{% include "partials/form_fields.html" with form=note_form %}
				<!-- {{ note_form.as_p }} -->
				<div class="form-actions">
					<input type="submit" value="Save note" class="btn btn-primary" />
					{% trans "Notes are only editable for 5 minutes after being saved." %}
				</div>
			</form>
			{% endblock %}
		</div>

		{% block extra_tabs %}{% endblock %}
	</div>
</div>

{% endblock dashboard_content %}


{% block onbodyload %}
oscar.dashboard.orders.initTabs();
oscar.dashboard.orders.initTable();
{% endblock %}
