<br/>

<% row_index = 1 %>
<% total = 0 %>
<%

%>
<script type="text/javascript">
	productModifyUrl="<%= "#{url_for(controller: "wkdelivery", action: 'populateProductItemDD')}" %>";
</script>
<div class="wk-contextual">
	<h2 style="float:left;"><%= l(:label_item) %> </h2>
	 <span id="populate_items"><%= link_to l(:label_populate_items), "#", { onclick: "javascript:populateInvoice()", class: 'icon icon-report'} if params[:delivery_id].blank? %></span>
	<%= link_to l(:button_add_row), "#", { onclick: "javascript:invoiceAddRow('deliveryTable', 'totalrow');",  class: 'icon icon-add' } if params[:delivery_id].blank? %>
</div>
<div style="clear:both"></div>
<div class="autoscroll">
<table id="deliveryTable" class="list time-entries", style="width:100%;">
  <thead style="width:100%; word-wrap: break-word;">
         <tr>
			<th width="1%"></th>
			<th width="1%" >#</th>
			<th width="7%" ><%= l(:field_location) %></th>
			<th width="7%" ><%= l(:label_product) %></th>
			<th width="20%" ><%= l(:label_item) %></th>
			<th width="13%" colspan="2"><%= l(:label_serial_number) %></th>
			<th width="10%" ><%= l(:field_notes) %></th>
			<th width="7%" ><%= l(:field_currency) %></th>
			<th width="7%" ><%= l(:label_cost_price) %></th>
			<th width="7%" ><%= l(:label_selling_price) %></th>
			<th width="7%" ><%= l(:label_over_head_price) %></th>
			<th width="3%" ><%= l(:field_quantity) %></th>
			<th width="5%" ><%= l(:label_s_uom) %></th>
			<th width="7%" ><%= l(:label_project) %></th>

			<th width="2%"></th>
         </tr>
  </thead>
  <tbody>
  <% if !@deliveryItem.blank? %>
      <% @deliveryItem.each do |entry| %>
				<%= render partial: 'edit_delivery_row', locals: { row_index: row_index, entry: entry } %>
		    <% row_index = row_index + 1 %>
			<% end %>
	<% else %>
		<%= render partial: 'edit_delivery_row' %>
	<% end %>

   </tbody>
</table>

</div>
<div class="wk-contextual">
	<%= link_to l(:button_add_row), "#", { onclick: "javascript:invoiceAddRow('deliveryTable', 'totalrow');",  class: 'icon icon-add' } if params[:delivery_id].blank? %>
</div style="clear:both">
<%= hidden_field_tag("totalrow",  row_index-1 )  %>
<%= hidden_field_tag("delivery_id",  @shipment.id )  %>
<% unless @deliveryItem.blank?  %>
<% unless @deliveryItem.blank? && @deliveryItem[0].blank? %>
<%= hidden_field_tag("delivery_id",  @shipment.id )  %>
<% end %>
<% end %>
<%= hidden_field_tag("deletedids",  "" )  %>
<% if params[:delivery_id].present? %>
	<div class="contextual total-hours">
		<%= l(:label_total) %>: &nbsp;<%= @deliveryItem[0]&.currency %> <%= "%.2f" % @deliveryItem&.sum('total_quantity * selling_price')%>
	</div>
<%end%>
<div style="clear:both;"></div>
<% if params[:delivery_id].present? %>
	<% url = url_for(controller: 'wkdelivery', action: 'delivery_slip', delivery_id: params[:delivery_id]) %>
	<div style="float:right;">
		<%= link_to l(:label_delivery_slip), "javascript:void(window.open('#{url}', '_blank', 'location=yes,scrollbars=yes,status=yes'));",
		style: 'color:blue;text-decoration: underline;' %>
	</div>
<% end %>
<%= submit_tag l(:button_save),  hidden: false, id: 'items_save', disabled: false %>
<!-- For Geolocation -->
<% if isChecked('asset_save_geo_location') %>
	<%
		locations = []
		@shipment.wkstatus.map do |entry|
			marker = entry.status == 'L' ? "S" : (entry.status == 'IT' ? "T" : "E")
			locations << [ entry.longitude || "", entry.latitude || "",l(:field_status)+": "+ getDeliveryStatus[entry.status] +"\n"+l(:label_date)+": "+  entry.status_date.localtime.strftime("%Y-%m-%d"), marker]
		end
		locations = [["", ""]] if locations.length == 0
	%>
	<div style="padding: 10px">
		<div id="mapContainer" style="width:30%;height:300px;"></div>
		<%= render partial: "wkgeolocation/location_map", locals: { locations: locations } %>
	</div>
<% end %>