<%= stylesheet_link_tag 'wk-time', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'invoice', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'accountproject', :plugin => "redmine_wktime" %>
<h2><%= l(:label_acc_projects) %></h2>
<% wkexpense_helper = Object.new.extend(WkexpenseHelper) %>
<%= form_tag({:controller => "wkaccountproject", :action => 'update'}, :method => :post, :id => 'query_form') do %>
	<%	projects = Project.where("#{Project.table_name}.status not in(#{Project::STATUS_CLOSED},#{Project::STATUS_ARCHIVED})").order('name') %>
	<%	projArr = options_for_wktime_project(projects,false) %>
	<%  billing_type = { 'TM'  => l(:label_time_material), 'FC' =>  l(:label_fixed_cost) } %>
	<%= hidden_field_tag "accountProjectId", @accProjEntry.blank? ? "" : @accProjEntry.id %>
	<%
	if params[:account_id].present?
		parentId = params[:account_id]
		parentType = "WkAccount"
	elsif params[:contact_id].present?
		parentId = params[:contact_id]
		parentType = "WkCrmContact"
	elsif params[:lead_id].present?
		parentId = params[:lead_id]
		parentType = "WkLead"
	else
		parentId = ""
		parentType = ""

	end
	%>
	<div class="div-left">

	<table>
		<%= render partial: "wkcrm/view_polymorphic", locals: { parentObject: @accProjEntry.blank? ? nil : @accProjEntry, :parentType => parentType, :parentId => parentId, :index => false, :needParentProject => false, :padding => nil, :needblankType => false } %>
		<tr>
			<th align="left"><label><%= l(:label_project) %></label></th>
			<td style="padding-left:40px;"><%=h select_tag('project_id', options_for_select(projArr, :selected =>  @accProjEntry.blank?  ?  '' : @accProjEntry.project_id)) %></td>
		</tr>
		<tr>
			<th align="left"><label><%= l(:label_itemized_bill) %></label></th>
			<td style="padding-left:40px;"><%= check_box_tag('itemized_bill', 1, (@accProjEntry.blank? ? false : @accProjEntry.itemized_bill)) %></td>
		</tr>
		<tr>
			<th align="left"><label><%= l(:label_include_expense) %></label></th>
			<td style="padding-left:40px;"><%= check_box_tag('include_expense', 1, (@accProjEntry.present? && @accProjEntry&.include_expense)) %></td>
		</tr>
		<tr>
			<th align="left"><label><%= l(:label_billing_type) %></label></th>
			<td style="padding-left:40px;"><%=h select_tag('billing_type', options_for_select(billing_type.invert, :selected => (@accProjEntry.blank? ? 'TM' : @accProjEntry.billing_type) ), :onchange => "showorHide((this.value == 'FC' ? true : false), 'billingschdules', null);") %></td>
		</tr>
	</table>
	</div>
	<div id="rightMenu" class="div-right">
	<table>
	<tr><td><label><%= l(:label_apply_tax) %></label></td><td><%= check_box_tag('applytax', 1, (@accProjEntry.blank?  ? false : @accProjEntry.apply_tax  ),
	:onchange => "showorHide((this.checked ? true : false), 'applicable_taxes', null);") %> </td></tr>
	<tr id="applicable_taxes"><td><label><%= l(:label_applicable_taxes) %></label></td><td><%=h select_tag('tax_id', options_for_select((@taxentry.blank? ? [] : @taxentry),  :selected => @selectedtax), :size => 5, :multiple=> true) %></td></tr>
		</table>
	</div>
	<div style="clear:both"></div>
	<div id="billingschdules" class="autoscroll">
	<div class="wk-contextual">
		<h2 style="float:left;"><%= l(:label_billing_schedules) %></h2>
		<%= link_to l(:button_add_row), "#", { :onclick => "javascript:invoiceAddRow('milestoneTable', 'mtotalrow');",  :class => 'icon icon-add' }%>
	</div>
	<div style="clear:both"></div>
	<% row_index = 1 %>
	<table id="milestoneTable" class="list time-entries">
	<thead>
		<tr>
			<th></th>
			<th class="lbl-txt-align" >#</th>
			<th class="lbl-txt-align"><%= l(:label_milestone) %></th>
			<th class="lbl-txt-align"><%= l(:field_currency) %></th>
			<th class="lbl-txt-align"><%= l(:field_amount) %></th>
			<th class="lbl-txt-align"><%= l(:label_bill_date) %></th>
			<th></th>
		</tr>
	</thead>
	<tbody>
	<% if !@wkbillingschedule.blank? %>
		<% @wkbillingschedule.each do |entry| %>
			<tr onclick="  row_id  = this.rowIndex ">
				<td class="lbl-txt-align" headers="milestone_id">
					<%=h hidden_field_tag("milestone_id_#{row_index}",   entry.id )  %>
				</td>
				<td class="lbl-txt-align" headers="item_index">
					<label id="item_index_<%= row_index.to_s %>"><%= row_index  %></label>
				</td>
				<td class="lbl-txt-align" headers="milestone" >
					<%= text_field_tag "milestone_#{row_index}",  entry.milestone ,  :required => false,  :style => "width:350px;" %>
				</td>
				<td class="lbl-txt-align" headers="currency" >
					<%=h select_tag("currency_#{row_index}",options_for_select(wkexpense_helper.options_for_currency,:selected => entry.currency)) %>
				</td>
				<td class="lbl-txt-align" headers="amount">
					<%= text_field_tag "amount_#{row_index}",  ("%.2f" % entry.amount)  , :required => false, :maxlength => 14,  :style => "width:150px;"  %>
				</td>
				<td class="lbl-txt-align" headers="billdate">
					<%= text_field_tag "billdate_#{row_index}",  entry.bill_date, :required => false  , :class => "date",  :style => "width:150px;" %>
				</td>
				<td>
					<%= link_to image_tag('delete.png'), "javascript:deleteRow('milestoneTable', 'mtotalrow');", :id => row_index,
							:data => {:confirm => l(:text_are_you_sure)},
							:title => l(:button_delete) %>
				</td>
			</tr>
			<% row_index = row_index + 1 %>
		<% end %>
	<% else %>
		<tr onclick="  row_id  = this.rowIndex ">
			<td class="lbl-txt-align" headers="milestone_id">
				<%=h hidden_field_tag("milestone_id_#{row_index}",   "" )  %>
			</td>
			<td class="lbl-txt-align" headers="item_index">
				<label id="item_index_<%= row_index.to_s %>"><%= row_index  %></label>
			</td>
			<td class="lbl-txt-align" headers="milestone" >
				<%= text_field_tag "milestone_#{row_index}",  "" , :required => false,  :style => "width:350px;" %>
			</td>
			<td class="lbl-txt-align" headers="currency" >
				<%=h select_tag("currency_#{row_index}",options_for_select(wkexpense_helper.options_for_currency)) %>
			</td>
			<td class="lbl-txt-align" headers="amount">
				<%= text_field_tag "amount_#{row_index}",  "" , :required => false, :maxlength => 14, :style => "width:150px;"  %>
			</td>
			<td class="lbl-txt-align" headers="billdate">
				<%= text_field_tag "billdate_#{row_index}",  ""  , :required => false, :class => "date",  :style => "width:150px;" %>
			</td>
			<td>
				<%= link_to image_tag('delete.png'), "javascript:deleteRow('milestoneTable', 'mtotalrow');", :id => row_index,
						:data => {:confirm => l(:text_are_you_sure)},
						:title => l(:button_delete) %>
			</td>
		</tr>
		<% row_index = row_index + 1 %>
	<% end %>
	</tbody>
	</table>
	<div class="wk-contextual">
		<%= link_to l(:button_add_row), "#", { :onclick => "javascript:invoiceAddRow('milestoneTable', 'mtotalrow');",  :class => 'icon icon-add' }%>
	</div style="clear:both">
	</div>
	<div style="clear:both"></div>
	<% if @invoiceComp.present? %>
	<div style="padding-top: 10px">
	<h2><%= l(:label_invoice_components) %></h2>
	<fieldset>
		<table>
			<thead>
				<tr>
					<th class="lbl-txt-align" >#</th>
					<th class="lbl-txt-align" style="width: 150px"><%= l(:field_name) %></th>
					<th class="lbl-txt-align"><%= l(:field_value) %></th>
				</tr>
			</thead>
			<tbody>
				<% @invoiceComp.each do |comp| %>
					<%= hidden_field_tag("acc_inv_comp_id_#{comp.ic_id}",  comp.id )  %>
					<tr>
						<td><%= check_box_tag("invoice_components[][invoice_component_id]", comp.ic_id, comp.id.present?, {id: "invoice_components_id_#{comp.ic_id}", onchange: "overrideComponents(this);"} ) %>
						<td style="width: 150px"><%= comp.name %>
						<td><%= text_field_tag("invoice_components[][value]", comp.value.present? ? comp.value : comp.ic_value,  {disabled: comp.id.blank?, id: "invoice_components_value_#{comp.ic_id}"}) %>
					</tr>
				<% end %>
			</tbody>
		</table>
	</fieldset>
	</div>
	<% end %>
	<%=h hidden_field_tag("mtotalrow",  row_index-1 )  %>
	<%=h hidden_field_tag("tax_updated_id",  "" )  %>
	<%= submit_tag l(:button_save), :class => "small", :name => nil %>
<% end %>