<% shifts = WkShift.where(:in_active => true, :is_schedulable => true).order(:name).pluck(:name, :id) %>
<%= form_tag({:controller => controller_name, :action => 'update'},  :method => :post, :id => 'query_form') do %>
<%= hidden_field_tag "isscheduled", scheduled %>
<table class="list time-entries">
	<thead>
		<tr>
			<th class="lbl-txt-align"><%= l(:field_user) %></th>
			<th class="lbl-txt-align"><%= l(:field_spent_on) %></th>
			<th class="lbl-txt-align"><%= l(:field_shift) %></th>
			<th class="lbl-txt-align"><%= l(:label_day_off) %></th>
			<th class="lbl-txt-align"></th>
		</tr>
	</thead>
	<tbody> 
		<% rowCount = 1 %>
		<% unless scheduleObj.blank? %>		
			<% userIds =  scheduleObj.pluck(:user_id) %>
			<% scheduleObj.each do |entry| %>
				<tr>
					<%= hidden_field_tag "schedule_id#{rowCount}", entry.id %>
					<td class="lbl-txt-align"><%= entry.user.name %></td>
					<%= hidden_field_tag "user_id#{rowCount}", entry.user_id %>
					<% if scheduled %>
						<td><%= entry.schedule_date.strftime("%F") unless entry.schedule_date.blank? %></td>
						<%= hidden_field_tag "scheduling_date#{rowCount}", entry.schedule_date.strftime("%F") unless entry.schedule_date.blank? %>
						<% if @editShiftSchedules %>
							<td class="lbl-txt-align"><%= select_tag "shifts#{rowCount}", options_for_select(shifts, :selected => entry.shift_id), :style=>"width:150px; "  %></td>
							<td><%= check_box_tag("day_off#{rowCount}", 1, (entry.schedule_as == 'W' ? false : true), :onchange => "showordisable((this.checked ? true : false), 'shifts#{rowCount}' )") %></td>
						<% else %>
							<td><%= entry.shift.name unless entry.shift.blank? %>
								<%= hidden_field_tag "shifts#{rowCount}", entry.shift_id %>
							</td>
							<td><%=h checked_image (entry.schedule_as == 'W' ? false : true) %></td>
						<% end %>
						
					<% else %>
						<td><%= entry.schedule_date.strftime("%F") unless entry.schedule_date.blank? %></td>
						<%= hidden_field_tag "scheduling_date#{rowCount}", entry.schedule_date.strftime("%F") unless entry.schedule_date.blank? %>
						
						<% if User.current.id == entry.user_id %>
							<td class="lbl-txt-align"><%=h select_tag "shifts#{rowCount}", options_for_select(shifts, :selected => entry.shift_id), :style=>"width:150px;"  %></td>
							<td class="lbl-txt-align"><%= check_box_tag("day_off#{rowCount}", 1, (entry.schedule_as == 'W' ? false : true), :onchange => "showordisable((this.checked ? true : false), 'shifts#{rowCount}' )") %></td>
						<% else %>
							<td><%= entry.shift.name %>
								<%= hidden_field_tag "shifts#{rowCount}", entry.shift_id %>
							</td>
							<td><%=h checked_image (entry.schedule_as == 'W' ? false : true) %></td>
						<% end %>
					<% end %>
					
					<td class="lbl-txt-align"></td>
				</tr>
				<% rowCount =  rowCount + 1 %>
			<% end %>
			<% if (!userIds.include? User.current.id) && (!User.current.wk_user.blank?) && (session[controller_name].try(:[], :location_id).to_i == User.current.wk_user.location_id || session[controller_name].try(:[], :location_id).blank?)  %>
				<tr>
					<%= hidden_field_tag "schedule_id#{rowCount}", "" %>
					<td class="lbl-txt-align"><%= User.current.name %></td>
					<%= hidden_field_tag "user_id#{rowCount}", User.current.id %>
					<td class="lbl-txt-align"><%= params[:date] %></td>
					<%= hidden_field_tag "scheduling_date#{rowCount}", params[:date] %>
					<td class="lbl-txt-align"><%= select_tag "shifts#{rowCount}", options_for_select(shifts), :style=>"width:150px;"  %></td>
					<td class="lbl-txt-align"><%= check_box_tag("day_off#{rowCount}", 1, false, :disabled => false, :onchange => "showordisable((this.checked ? true : false), 'shifts#{rowCount}' )") %></td>
					<td class="lbl-txt-align"></td>
				</tr>
				<% rowCount =  rowCount + 1 %>
			<% end %>
			
		<% else %>
			<tr>
				<%= hidden_field_tag "schedule_id#{rowCount}", "" %>
				<td class="lbl-txt-align"><%= User.current.name %></td>
				<%= hidden_field_tag "user_id#{rowCount}", User.current.id %>
				<td class="lbl-txt-align"><%= params[:date] %></td>
				<%= hidden_field_tag "scheduling_date#{rowCount}", params[:date] %>
				<td class="lbl-txt-align"><%= select_tag "shifts#{rowCount}", options_for_select(shifts), :style=>"width:150px;"  %></td>
				<td class="lbl-txt-align"><%= check_box_tag("day_off#{rowCount}", 1, false, :disabled => false, :onchange => "showordisable((this.checked ? true : false), 'shifts#{rowCount}' )") %></td>
				<td class="lbl-txt-align"></td>
			</tr>
			<% rowCount =  rowCount + 1 %>
		<% end %>
	</tbody>
</table>
<%= hidden_field_tag "rowCount", rowCount %>
<% if scheduled && @schedulesShift && @editShiftSchedules %>
<%= submit_tag l(:button_save), :class => "small", :name => "schedule_preference" %>
<% end %>
<% if !scheduled %>
<%= submit_tag l(:button_save), :class => "small", :name => "schedule_preference" %>
<% end %>
<% end %>