<%= stylesheet_link_tag 'font-awesome.min', :plugin => "redmine_wktime" %>
<% wktime_helper = Object.new.extend(WktimeHelper) %>
<% notification_helper = Object.new.extend(WknotificationHelper) %>
<% if wktime_helper.isChecked('te_save_geo_location') ||
		wktime_helper.isChecked('att_save_geo_location') ||
		wktime_helper.isChecked('crm_save_geo_location') ||
		wktime_helper.isChecked('asset_save_geo_location') %>
			<%= javascript_include_tag 'geo_location', :plugin => "redmine_wktime" %>
<% end %>
<%
   lastAttnEntries = wktime_helper.findLastAttnEntry(true)
   if !lastAttnEntries.blank?
  	  @lastAttnEntry = lastAttnEntries[0]
   end
%>
<%= javascript_include_tag 'timer', :plugin => "redmine_wktime" %>

<!-- For label -->
<script>
var label_no_data = "<%= l(:label_no_data) %>";
</script>

<style>
	#clockINOUT, #issueLog ,#notification{
		float: right;
		display: flex;
		flex-direction: row-reverse;
		cursor: pointer;
		margin-left: 10px;
	}
	#appendlabel, #issue-time, #issue-tracker{
		padding-top: 5px;
		padding-left: 4px;
		font-size: 13px;
	}
	#startdiv, #enddiv{
		float: right;
	}
	#issueLog .drdn-items > * {
		color: #555 !important;
	}
	#notification .drdn-items > * {
		color: #555 !important;
		padding: 10px;
		border: none;
		white-space: inherit;
	}
	#issueLog .drdn-content {width:280px;}
	#notification .drdn-content {
		min-height: 84px;
		max-height:400px;
		width:345px;
  		overflow: auto;
		-moz-box-shadow: 0 0 8px #888888;
		-webkit-box-shadow: 0 0 8px #888888;
		box-shadow: 0 0 8px #888888;
	}
	#notification .padding {
		padding: 10px;
    	border-bottom: 1px solid #ccc;
	}
	#notification .drdn-content h2 {
		margin: 0px;
	}
	#notification .drdn-content .drdn-items .unseen {
		background-color: #e9e9e9;
	}
	#notification .drdn-content .drdn-items .seen {
		font-weight: lighter;
	}
	#notification .drdn-content .drdn-items a:hover {
	background-color: #dbdbdb;
	}
	#notification .notify-img {
  	position: relative;
	}
	#notification .count {
    background-color: red;
    font-size: xx-small;
    position: absolute;
    padding: 2px;
    top: -2px;
    left: 11px;
    border-radius: 25px;
	}
	#notification .no-items {
		padding-left: 10px;
	}
	#notification .drdn-content .drdn-items .fa:before {
    	font-size: 30px;
		color: steelblue;
	}
	#notification .drdn-trigger .fa:before {
    font-size: 20px;
	}
</style>
<% if wktime_helper.isChecked("wktime_enable_clock_in_out") && wktime_helper.isChecked("wktime_enable_attendance_module") && User.current.logged? && wktime_helper.checkViewPermission %>
	<% if !@lastAttnEntry.blank?
			if(@lastAttnEntry.end_time.blank? && @lastAttnEntry.start_time > 24.hour.ago)
				hideStart = true
				hideEnd = false
				imglabel = "end"
				remaininghr =  (wktime_helper.computeWorkedHours(@lastAttnEntry.start_time, Time.now.localtime, false))
			else
				hideStart = false
				hideEnd = true
			end
			imglabel = (@lastAttnEntry.end_time.blank? && @lastAttnEntry.start_time > 24.hour.ago ? "end" : "start")
		else
			hideStart = false
			hideEnd = true
		end

		imgname = "#{imglabel}" == "start" ? "clockin1.jpg" : "clockout1.jpg"
		totalhours = (wktime_helper.totalhours)*3600
		totalhours = (hideStart ? ( !remaininghr.blank? ? remaininghr.round(0)+totalhours : totalhours) : totalhours )
		totalhours1 = Time.at(totalhours).utc.strftime("%H:%M")
		host_with_subdir = wktime_helper.getHostAndDir(request)
	%>
	<%= javascript_tag do %>
		$(function(){
			<% if @lastAttnEntry && @lastAttnEntry.end_time.blank? && @lastAttnEntry.start_time > 24.hour.ago %>
				startTimer('start');
			<% end %>
			$("#clockINOUT").click(function(){
				if($('#clockout').is(':visible')){
					startTimer('start');
				}else{
					startTimer('pause');
				}
			});
		});
		function startTimer(str){
			$("#appendlabel").timer({
					action: str,
					seconds: <%= totalhours.to_i %>
			});
		}
	<% end %>
	<%=h hidden_field_tag('clockinout_url',  "#{url_for(:controller => 'wkbase', :action => 'updateClockInOut', :host => host_with_subdir, :only_path => true)}" )  %>
	<div style="clear: both;"></div>
	<div id="clockINOUT">
		<span id="appendlabel"><%= totalhours1 %></span>
		<div id="startdiv">
			<%= image_tag("widgetclockin.png", :id => 'clockin' , :plugin => "redmine_wktime", :style => hideStart ? "display:none;" : "display:block;" ) %>
		</div>
		<div id="enddiv">
			<%= image_tag("widgetclockout.png", :id => 'clockout', :plugin => "redmine_wktime", :style => hideEnd ? "display:none;" : "display:block;") %>
		</div>
	</div>
	<div style="clear: both;"></div>
<% end %>

<!-- Notification-->
<% if User.current.logged?
	unRead = WkUserNotification.unreadNotification
	notifications = WkUserNotification.where(user_id: User.current.id).order(id: 'desc')
	newNotify = (+"").html_safe
	earlierNotify = (+"").html_safe
%>
	<div id="notification" class="drdn" title="notification">
		<div class="notify-img">
			<span class="drdn-trigger">
				<% if unRead.count > 0 %>
					<i class="fa fa-bell" style="padding-left: 0px;color:yellow;"></i>
					<div class="count"><%=unRead.count %></div>
				<% else %>
					<i class="fa fa-bell" style="padding-left: 0px;color:darkgray;"></i>
				<% end %>
			</span>
		</div>
		<div class="drdn-content">
			<div class="padding">
				<span style="font-size: 20px;font-weight: bold;"><%= l(:label_notification_plural) + (unRead.present?  ? " (" + unRead.count.to_s + ")" : '')%></span>
				<span id="mark_read" style="float: right;"><a style="color: blue;text-decoration: underline;" ><%= l(:label_mark_all_read) %></a></span>
			</div>
			<% if notifications.present? %>
				<% notifications.each do |notification| %>
					<% notifyTime = notification_helper.getnotifyDate(notification.created_at)
						seenClass = notification.seen ? "seen" : "unseen"
						notifyVal = notification_helper.formNotificationText(notification)
						isURLreturn = notifyVal['url'].present? ? "return true;" : "return false;"
					%>
					<div class="drdn-items">
						<a class='<%=seenClass%>' onclick='<%=isURLreturn%>' data-id='<%=notification.id%>' href=<%= url_for(notifyVal['url']) %> style="text-decoration: none;">
						<div>
							<div style="width: 20%; float:left; padding-top: 7px;">
									<i class='<%=notifyVal['icon'] %>'></i>
							</div>
							<div style="width: 80%; float:right">
								<%= notifyVal['text'] %><br><%= notifyTime %>
							</div>
						</div>
						</a>
					</div>
				<%end%>
			<% else %>
				<span class="no-items"><%= l(:text_no_notification) %></span>
			<% end %>
		</div>
	</div>
<% end %>

<!-- Issue Time Tracker-->
<% if wktime_helper.isChecked("label_enable_issue_logger") && User.current.logged? && wktime_helper.checkViewPermission
		lastIssueLog = WkSpentFor.getIssueLog.first
		imgName = lastIssueLog.present? ? "finish" : "start"
		image = image_tag(imgName + '.png', plugin: "redmine_wktime", style: "height: 25px")
		trigger = content_tag("span", image, id: 'issueImg', class: "drdn-trigger")
		issueURL = "/wktime/get_issue_loggers?autocomplete=true"
		issue = text_field_tag("issue", "", id: "issues-quick-search", class: "autocomplete", data: {automcomplete_url: issueURL},
			autocomplete: "off", placeholder: "Select Issue")
		content = content_tag("div", content_tag("div", issue, class: "quick-search") +
			content_tag("div", nil, class: "drdn-items issues"), class: "drdn-content", id: "issue-content", style: "width: max-content;" )
%>
	<%= hidden_field_tag('g_clock_action', lastIssueLog ? lastIssueLog.clock_action : nil ) %>
	<%= hidden_field_tag('projectID', @project ? @project.id : "" ) %>
	<%= content_tag("div", trigger + content, id: "issueLog", class: "drdn") %>
	<div style="clear: both;"></div>
<% end %>

<% if controller_name == "timelog" && action_name == "index" || action_name == "report"%>
	<input type="hidden" name="spentTypeSession" id="spentTypeSession" value="<%= session[:timelog] && session[:timelog][:spent_type] %>">
<% end %>
<!-- Spent time show on map -->
<% if controller_name == "timelog" && action_name == "index" && wktime_helper.isChecked('te_save_geo_location') %>
	<% show_on_map = session[:timelog] && session[:timelog][:show_on_map] %>
	<div id="show_map_filter" >
		<label style="padding-left:5px">
			<%= check_box_tag('show_on_map', true, show_on_map, onclick: '$("#query_form").submit(); return false;') %><%= l(:label_show_on_map) %>
		</label>
	</div>
	<script>
		$(function(){
			$('#spent_type').parent('td').after('<td id="showmap_header"></td>');
			$('#show_map_filter').appendTo('#showmap_header');
		});
	</script>
	<% if show_on_map && @entries.present? %>
		<script>
			$(function(){
				$('#content .autoscroll').prepend('<div id="map-wrapper"></div>');
				$('#map-wrapper').append('<div id="list-wrapper" style="width: 49%; float: left; overflow-x: scroll; padding-right: 5px"></div>');
				$('.list.odd-even.time-entries').appendTo('#list-wrapper');
				$('#mapheader').appendTo('#map-wrapper');
			});
		</script>
		<%= render partial: "wkgeolocation/show_on_map", locals: { entries: @entries, height: '96%', width: '49%', model: 'spent_for' } %>
	<% end %>
<% end %>