<%= javascript_include_tag 'edit', :plugin => "redmine_wktime" %>
<%= form_tag({:controller => 'wkattendance', :action => 'saveClockInOut'}, :method => :post, :id => 'save_ClockInOut') do %>
	<%= hidden_field_tag :page, params[:page]%>
	<% isDisable = !(params[:user_id].to_i != User.current.id &&  validateERPPermission('A_ATTEND')) %>
	<h2><%= l(:label_clock_summary) %></h2>
	<table>
		<tr>
			<th align="left"><%=l(:field_user)%></th>
			<td><%= @wkattnEntries[0].firstname + ' ' + @wkattnEntries[0].lastname if !@wkattnEntries[0].blank? %></td>
		</tr>
		<tr>
			<th align="left"><%=l(:label_wk_attn_for)%></th>
			<td>
			<%= params[:date].to_date  %>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				 <%= link_to("\xc2\xab " + l(:label_previous ), {:action => 'clockedit', :user_id => params[:user_id], :date => (Date.parse params[:date])-1, :tab => controller.controller_name, id: nil},
						  :title => l(:label_wk_prev_week_tip)) %>
				<% datetime = DateTime.now  %>
				<% if (Date.parse params[:date]).past? %>
					|
					<%= link_to(l(:label_next) + " \xc2\xbb", {:controller => controller.controller_name, :action => 'clockedit', :user_id => params[:user_id], :date => (Date.parse params[:date])+1, :tab => controller.controller_name, id: nil}, :title => l(:label_next_week)) %>
				<%end%>
			</td>
		</tr>
	</table>
	<br/>

	<div class="autoscroll">
		<div  style="float:left;width=100%;">
			<table class="list time-entries">
				<thead>
					<tr>
					<th class="lbl-txt-align"><%= l(:label_clock_in) %></th>
					<th class="lbl-txt-align"><%= l(:label_clock_out) %></th>
					<th class="lbl-txt-align"><%= l(:field_hours) %></th>
					</tr>
					<tr></tr>
				</thead>
				<% entry_id = Array.new %>
				<% start_time = Array.new %>
				<% end_time = Array.new %>
				<% hours = Array.new %>
				<% @wkattnEntries.each do |entry| %>
				<% entry_id << entry.id %>
				<% start_time << (entry.start_time.blank? ? '' : entry.start_time.localtime.strftime('%R')) %>
				<% end_time << (entry.end_time.blank? ? '' : entry.end_time.localtime.strftime('%R')) %>
				<% hours <<  entry.hours %>
				<% end %>
				<%=h hidden_field_tag("break_time" , Setting.plugin_redmine_wktime['wktime_break_time'] )  %>
				<%=h hidden_field_tag('attnDayEntriesCnt', start_time.blank? ? 1 : start_time.length) %>
				<%=h hidden_field_tag('startdate', params[:date] ) %>
				<%=h hidden_field_tag('user_id', params[:user_id] ) %>
				<% j = 0  %>
				<% totalhours = 0  %>
				<% loop do  %>
				<tr class="time-entry <%= cycle("odd", "even") %>">
					<%=h hidden_field_tag("attnEntriesId#{j}",  (entry_id.blank? ? "" : entry_id[j]) )  %>
					<td><%=h text_field_tag("attnstarttime#{j}", start_time.blank? ? "" : start_time[j] ,:onchange => "#{} ", :size => 10, :disabled => isDisable, :required => true,  :onchange => "validateHr(this,#{(j)}, ['attnstarttime#{j}', 'attnendtime#{j}', 'hoursdiff#{j}', #{true}]);") %>
					</td>
					<td><%=h text_field_tag("attnendtime#{j}", end_time.blank? ? "" : end_time[j],:size => 10, :disabled => isDisable, :onchange => "validateHr(this,#{(j)}, ['attnstarttime#{j}', 'attnendtime#{j}', 'hoursdiff#{j}', #{true}]);") %>
					</td>
					<td><%=h text_field_tag("hoursdiff#{j}", hours.blank? ? 0 : hours[j].to_f,:size => 10, :disabled => isDisable, :required => true) %>
					</td>
					<% totalhours += hours.blank? ? 0 : hours[j].to_f  %>
					<% break if j ==  start_time.length-1 || start_time.blank?  %>
					<% j += 1  %>
				</tr>
				<% end %>
				<tr class="time-entry <%= cycle("odd", "even") %>" >
					<td></td><td style="font-weight: bold; "><label><%= l(:label_total_hours) %></label></td>
					<td><%=h text_field_tag("tothours", totalhours.round(2),:size => 10, :disabled => isDisable, :required => true) %>
					</td>
				</tr>
			</table>
			<span style="margin-left:5.0em;">
				<%  if validateERPPermission('A_ATTEND') %>
					<label style = "float:left;"><%= l(:label_hours_note) %></label>
				<% end %>
			</span>
			<br>
			<p style="float: left; padding-left: 90px;">
				<%= submit_tag l(:button_save),  :hidden => isDisable, :id => 'wkattendance_save', :isDisable => false %>
			</p>
		</div>
		<% if isChecked('att_save_geo_location') %>
			<% 	startLocations = @wkattnEntries.map{ |entry|
						(!entry.id || entry.s_longitude == entry.e_longitude && entry.s_latitude == entry.e_latitude) ? ["",""] : [ entry.s_longitude || "", entry.s_latitude || "",
						l(:label_clock_in)+": "+entry.start_time.localtime.strftime('%R')+"\n"+l(:label_clock_out)+": "+
						(entry.end_time ? entry.end_time.localtime.strftime('%R') : "")+"\n"+l(:field_hours)+": "+entry.hours.to_s, "S" ]
					}
					endLocations = @wkattnEntries.map{ |entry|
						(!entry.id) ? ["",""] : [ entry.e_longitude || "", entry.e_latitude || "", l(:label_clock_in)+": "+entry.start_time.localtime.strftime('%R')+"\n"+
						l(:label_clock_out)+": "+(entry.end_time ? entry.end_time.localtime.strftime('%R') : "")+"\n"++"\n"+l(:field_hours)+": "+entry.hours.to_s, "E" ]
					}
					locations = startLocations + endLocations
					locations = [["", ""]] if locations.length == 0
			%>
			<div  id="mapContainer" style="float: right;width: 30%;height: 300px"></div>
			<%= render partial: "wkgeolocation/location_map", locals: { locations: locations } %>
		<% end %>
	</div>
<% end %>