<%= stylesheet_link_tag 'wk-time', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'invoice', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'index', :plugin => "redmine_wktime" %>
<% hash_status = { 'o'  => l(:label_open_issues), 'c' =>  l(:label_closed_issues) } %>
<script type="text/javascript">
	actRelatedUrl="<%= "#{url_for(:controller => "wkcrm", :action => 'getActRelatedIds', :additionalContactType => controller.additionalContactType, :additionalAccountType => controller.additionalAccountType)}" %>";
	invoiceUrl="<%= "#{url_for(:controller => controller_name, :action => 'getSupplierInvoices')}" %>";
</script>
<%= form_tag({:controller => controller_name, :action => 'update'}, :method => :post, :id => 'query_form') do %>
	<% unless @shipment.blank? %>
		<%=h hidden_field_tag("parent_id",  @shipment.parent_id )  %>
		<%=h hidden_field_tag("parent_type",  @shipment.parent_type )  %>
		<%#=h hidden_field_tag("shipment_date",  @shipment.shipment_date )  %>
		<%=h hidden_field_tag("shipment_serial_number",  @shipment.serial_number )  %>
	<% end %>
	<% accountArr = options_for_wktime_account(false, controller.getOrderAccountType, controller.additionalAccountType)
		parentId = @shipment.parent_id
		parentType = @shipment.parent_type
		type = parentType == "WkCrmContact" ? controller.getOrderContactType : controller.getOrderAccountType
		if !parentId.blank? && !parentType.blank?
			accountArr = relatedValues(parentType, parentId, type, false, controller.additionalContactType, controller.additionalAccountType)
		elsif !params[:related_parent].blank? && params[:related_to]
			accountArr = relatedValues(params[:related_to], params[:related_parent], type, false, controller.additionalContactType, controller.additionalAccountType)
		end
		siArr = []
		siArr = WkInvoice.get_invoice_numbers(@shipment&.parent_type || 'WkAccount', @shipment&.parent_id || (accountArr.present? ? accountArr[0][1] : ''), 'SI').collect {|i| [i.invoice_number, i.id]  }
		siArr.unshift(["",''])
		invoiceID = @shipmentItem&.first&.supplier_invoice_id
		invoice_no = @shipmentItem&.first&.supplier_invoice&.invoice_number
		if params[:shipment_id].present? && invoiceID.present?
			isAdd = false
			siArr.map{|no, id| isAdd = true if id.to_i == invoiceID}
			siArr.push([invoice_no, invoiceID]) if !isAdd
		end
		%>
		<%=h hidden_field_tag("prev_si_id", invoiceID )  %>
	<table>
		<tr>
			<th style="text-align: left"><label><%= l(:field_type) %></label></th>
			<td style="text-align: left">
				<%= ": "%><%= select_tag('related_to', options_for_select([[l(:field_account), 'WkAccount'], [l(:label_contact), 'WkCrmContact']], selected: @shipment.blank? ? "" : @shipment.parent_type), onchange: "actRelatedDd(#{User.current.id}, false, false, 'S', 'SC', false, true, 'si_id');", style: "width: 150px") %>
			</td>
			<th style="text-align: left"><label><%= l(:field_name) %></label></th>
			<td style="text-align: left">
				<%= ": "%><%= select_tag('related_parent', options_for_select(accountArr, selected: @shipment.blank? ? "" : @shipment.parent_id), onchange: "getInvoiceNos(#{User.current.id}, 'si_id');", style: "width: 150px") %>
			</td>
			<th style="text-align: left"><label><%= l(:label_supplier_invoice) %></label></th>
			<td style="text-align: left">
				<%= ": "%><%= select_tag('si_id', options_for_select(siArr, selected: invoiceID), onchange: "populateSIInvoice();") %>
				<%= link_to(l(:label_invoice_details), {controller: 'wksupplierinvoice', action: 'edit', invoice_id: invoiceID, new_invoice: false, preview_billing: false, tab: 'wksupplierinvoice', id: invoiceID},{style:"padding-left: 5px"}) if params[:shipment_id].present? && invoice_no.present? %>
			</td>
		</tr>
		<tr><th style="text-align: left"><%= l(:label_serial_number) %></th>
			<td style="width: 200px;">
				<%= ": " %>
				<%# if !@shipment.blank? %>

						<%= text_field_tag "serial_number", @shipment.blank? ? "" : @shipment.serial_number, :style => "width:150px;" %>

				<% #end %>
			</td>
			<th style="text-align: left"><%= l(:label_shipment_date) %></th>
			<td style="width: 200px;"><%= ": "%> <%=h date_field_tag('shipment_date', (@shipment.blank? ? Date.today.to_s : @shipment.shipment_date.to_s), :size => 10, :required => true, :disabled => @preBilling) %> <%= calendar_for('shipment_date') %></td>

		</tr>

	</table>
	<% if !@shipmentItem.blank? %>
		<% if !@shipmentItem[0].blank? %>
			<%= render :partial => 'edit_shipment_items' %>
		<% end %>
	<% else %>
		<% if !@shipmentItem.blank?  %>
			<%= render :partial => 'edit_shipment_items' %>
		<% else  %>
			<br/>
			<h2 ><%= l(:label_shipment_items) %></h2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<label><b><%= l(:label_no_data) %></b></label>
		<% end %>
	<% end %>
<% end %>