<%= javascript_include_tag 'settings', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'payrollsettings', :plugin => "redmine_wktime" %>

<%= title l(:label_leave_setting) %>
<div class="contextual">
	<%= render :partial => 'sidebar' %>
</div>
<%= render partial: "wkattendance/render_hr_tabs" %>
<script type="text/javascript">
	attendanceAlertMsg = "<%= l(:text_are_you_sure) %>";
	selectListAlertMsg = "<%=l(:actionview_instancetag_blank_option) %>";
	issueExistsAlertMsg = "<%=l(:label_wk_leave_exists) %>";
	issueUrl="<%= "#{url_for(:controller => 'wkattendance', :action => 'getIssuesByProject')}" %>";
	projectUrl="<%= "#{url_for(:controller => 'wkattendance', :action => 'getProjectByIssue')}" %>";
	lblInvalid= " <%= (CGI.unescapeHTML l('activerecord.errors.messages.invalid')).html_safe %>";
	lblDaysPerMonth = "<%=l(:label_hours_per_month) %>";
	lblAccrual = "<%=l(:label_wk_leave_accrual) %>";
	lblAccrualAfter = "<%=l(:label_wk_leave_accrual_after) %>";
	lblAccrualMultiplier = "<%=l(:label_accrual_multiplier) %>";
	lblLeaveIssue = "<%=l(:label_wk_leave_issue) %>";
	lblYear = "<%=l(:label_year) %>";
</script>
<%
	leave = []
	issue_list = Issue.order('subject')
	unless issue_list.blank?
		issueslist = issue_list.collect {|issue| [issue.subject, issue.id] }
		issuehash = Hash[issue_list.map { |u| [u.id, u.subject] }]
	else
		issueslist = Array.new
		issueslist << [ "", 0]
	end
	projects = Project.where("#{Project.table_name}.status not in(#{Project::STATUS_CLOSED},#{Project::STATUS_ARCHIVED})").order('name')
	wktime_helper = Object.new.extend(WktimeHelper)
	projArr = wktime_helper.options_for_wktime_project(projects,false)

	if @leaveSettings.blank?
		leave = ''
	else
		@leaveSettings.each do |element|
			listboxArr = element.split('|')
			issueId = listboxArr[0]
			issueName = issuehash[issueId.to_i].blank? ? issueId : issuehash[issueId.to_i]
			leaveAccrual = listboxArr[1].blank? ? "" : listboxArr[1]
			accrualAfter = listboxArr[2].blank? ? "" : listboxArr[2]
			optionText = leaveAccrual.blank? ? issueName : issueName + " : "  + leaveAccrual + " " + l(:label_hours_per_month) + " " + l(:label_wk_leave_accrual_after) + " " + accrualAfter + " " + l(:label_year)
			leave << [optionText,element]
		end
	end
%>

<%= form_tag({:controller => controller_name, :action => 'leavesettings'}, :method => :post) do %>
  <p>
		<label><%= l(:label_wk_leave) %></label>
			<%=  select_tag('settings[leave_settings]',
				options_for_select(leave, :selected => 'settings[leave_settings]'), :multiple=> true, :style => "min-width:200px; height:100px;")%>

		</p>
		<p>
			<%= link_to l(:button_add), "javascript:showLeaveDialog('Add');", :class => "button"%>  |
			<%= link_to l(:button_edit), "javascript:showLeaveDialog('Edit');", :class => "button"%>  |
			<%= link_to l(:button_delete), "javascript:removeSelectedValue('settings_leave_settings');", :class => "button" %>
		</p>
	<%= submit_tag l(:button_save), :id => 'wk_leave_save', :name => 'leave_save', :disabled => false %>
<% end %>
	<div id="leave-dlg" title="<%= l(:label_wk_leave) %>">
		<table>
			<tr>
				<th align="left"><label><%= l(:label_project) %></label></th>
				<td class="project" align="left"><%=h select_tag('leave_project', options_for_select(projArr),:onchange => "projectChanged(this,-1);") %></td>
			</tr>
			<tr>
				<th align="left"><label><%= l(:label_wk_leave_issue) %></label><span style="margin-left:0.8em;"></span></th>
				<td class="issue" align="left"><%=h select_tag('leave_issue', options_for_select(issueslist)) %></td>
			</tr>
			<tr>
				<th align="left"><label><%= l(:label_wk_leave_short_name) %></label></th>
				<td><%= text_field_tag 'wk_leave_short_name','', :size => 4, :maxlength => 4 %></td>
			</tr>
			<tr>
				<th align="left"><label><%= l(:label_wk_leave_accrual) %></label></th>
				<td><%= text_field_tag('leave_accrual','', :size => 2.5, :maxlength => 6) %>&nbsp<%= l(:label_hours_per_month) %></td>
			</tr>
			<tr>
				<th align="left"><label><%= l(:label_accrual_multiplier) %></label></th>
				<td><%= text_field_tag 'leave_accrual_multiplier','1', :size => 2.5, :maxlength => 6 %></td>
			</tr>
			<tr>
				<th align="left"><label><%= l(:label_wk_leave_accrual_after) %></label></th>
				<td><%= text_field_tag 'leave_accrual_after','', :size => 2.5, :maxlength => 6 %>&nbsp<%= l(:label_year) %></td>
			</tr>
			<tr>
				<th align="left"><label><%= l(:label_reset_month) %></label></th>
				<td><%=h select_tag('wk_attn_leave_reset_month', options_for_select(l('date.month_names').collect.with_index.to_a)) %></td>
			</tr>
			<tr>
				<%= hidden_field_tag :hdnAction, "" %>
			</tr>
		</table>
	</div>