<% productSqlCond = "product_type is null or product_type = '#{controller.getItemType}'" %>
<%
	productId = 0
	brandId = 0
	unless @productItem.blank?
		productId = @productItem.product_id.to_i
		brandId = @productItem.brand_id.to_i
	else
		firstProduct = WkProduct.where(productSqlCond).order(:name).first
		productId = firstProduct.id unless firstProduct.blank?
	end
	brandSql = 'id in (select brand_id from wk_brand_products where product_id = ' + productId.to_s + ')'
	parentArr = parentArray(controller.getItemType, true, false, nil, @parentEntry&.id || nil)
%>
<div class="splitcontent">
	<fieldset class="box tabular">
		<legend><%=l(:label_product_item)%></legend>
		<div class="splitcontentleft">
			<table>
				<% if params[:parentId].present? && controller.getItemType == 'A' %>
					<tr>
						<th style="text-align: left"><%= l(:label_add_existing_asset) %></th>
						<td style="text-align: left;padding-left:40px;"><%= select_tag("existing_id", options_for_select(parentArr, selected: invntItem.blank? ? parentArr : invntItem.id), onchange: "populateAsset();")%></td>
					</tr>
				<% end %>
				<tr>
					<th align="left"><%= l(:label_product) %></th>
					<td align="left" style="padding-left:40px;">
						<%=h select_tag("product_id", generic_options_for_select(WkProduct, productSqlCond, 'name', 'name', 'id', @productItem.blank? ? '' : @productItem.product_id, false), :onchange => "if(#{ controller.showProductItem }) { productChanged(this.id, 'brand_id', #{User.current.id}, true, true, null, null);productChanged(this.id, 'product_attribute_id', #{User.current.id}, false, false, null, null); }") %>
					</td>
				</tr>
				<% if controller.showProductItem %>
					<tr>
						<th align="left"><%= l(:label_brand) %></th>
						<td align="left" style="padding-left:40px;">
							<%=h select_tag("brand_id", generic_options_for_select(WkBrand, brandSql, 'name', 'name', 'id', @productItem.blank? ? '' : @productItem.brand_id, true), :onChange => "productChanged(this.id,'product_model_id', #{User.current.id}, false, true, null, null);") %>
						</td>
					</tr>
					<tr>
						<th align="left"><%= l(:label_model) %></th>
						<td align="left" style="padding-left:40px;">
							<%=h select_tag("product_model_id", generic_options_for_select(WkProductModel, 'product_id = ' + productId.to_s + ' and brand_id = ' + brandId.to_s, 'name', 'name', 'id', @productItem.blank? ? '' : @productItem.product_model_id, true)) %>
						</td>
					</tr>
					<tr>
						<th align="left"><%= l(:label_part_number) %></th>
						<td align="left" style="padding-left:40px;"><%= text_field_tag "part_number",  @productItem.blank? ? '' : @productItem.part_number %></td>
					</tr>
				<% end  %>
			</table>
		</div>
	</fieldset>

	<% if isChecked('asset_save_geo_location') && params[:controller] == 'wkasset'%>
			<% 	locations = [!invntItem ? ["", ""] : [ invntItem.asset_property.try(:longitude) || '', invntItem.asset_property.try(:latitude) || '', l(:field_name)+": "+invntItem.asset_property.name+"\n"+l(:label_rate)+": "+invntItem.asset_property.rate.to_s+" "+invntItem.asset_property.rate_per+"\n"+l(:field_location)+": "+(invntItem.location_id ? invntItem.location_id.to_s : "")]]
					locations = [["", ""]] if locations.length == 0
			%>
		<div >
			<div id='mapContainer' style="width:350px;height:220px; padding-left: 100px"></div>
			<%= render partial: "wkgeolocation/location_map", locals: { locations: locations } %>
			<p style="font-weight: bold; padding-left: 100px">
				<label><%= check_box_tag("save_current_location", 1) %>  <%= l(:label_save_c_geolocation) %></label>
			</p>
		</div>
	<% end %>
</div>