<%= javascript_include_tag 'crmactivity', :plugin => "redmine_wktime" %>
<%= javascript_include_tag 'index', :plugin => "redmine_wktime" %>
<script type="text/javascript">
	actRelatedUrl="<%= "#{url_for(:controller => "wkcrm", :action => 'getActRelatedIds', :additionalContactType => controller.additionalContactType, :additionalAccountType => controller.additionalAccountType)}" %>";
</script>
<%
	hr = []
	for i in 0..23
		hr << (i < 10 ? ['0' + i.to_s,i] : [i,i])
	end
	relHash = relatedHash
	contactType = 'C'
	accountType = 'A'
	t = !@activityEntry.blank? ? @activityEntry[0].duration : 0
	mm, ss = t.divmod(60)
	hh, mm = mm.divmod(60)
	dd, hh = hh.divmod(24)
	accArr = options_for_wktime_account(false, accountType, controller.additionalAccountType)

	if params[:controller_from] == 'wksupplieraccount' || params[:controller_from] == 'wksuppliercontact'
		relHash.delete("WkOpportunity")
		relHash.delete("WkLead")
		contactType = 'SC'
		accountType = 'S'
	end

	if !@activityEntry.blank?
		entityType = @activityEntry[0].parent_type == 'WkCrmContact' ? contactType : accountType
		accArr = relatedValues(@activityEntry[0].parent_type, @activityEntry[0].parent_id, entityType, false, controller.additionalContactType, controller.additionalAccountType, (@isReferral ? "I" : nil))
  elsif !params[:parentType].blank?
		entityType = params[:parentType] == 'WkCrmContact' ? contactType : accountType
		accArr = relatedValues(params[:parentType], params[:parentId], entityType, false, controller.additionalContactType, controller.additionalAccountType, (@isReferral ? "I" : nil))
	end
 %>

<div>
	<fieldset class="box tabular">
		<legend><%= l(:label_activity_information) %></legend>
		<%=h hidden_field_tag('controller_from', params[:controller_from] ) %>
		<%=h hidden_field_tag('action_from', params[:action_from] ) %>
		<%=h hidden_field_tag('crm_activity_id', (!@activityEntry.blank? ? @activityEntry[0].id : "") ) %>
		<%=h hidden_field_tag('load_act_type', ( @isReferral ? "I" : @activityEntry&.first&.activity_type || 'C') ) %>
		<% interview_type = @activityEntry&.first&.interview_type_id || nil %>
		<table >
			<% if @isReferral %>
				<tr>
					<th align="left" ><%= l(:label_interview_type) %></th>
					<td align="left" style="padding-left:40px;">
						<%= select_tag('interview_type', options_for_select(options_for_enum_select('IT', interview_type, true))) %>
					</td>
				</tr>
			<% end %>
			<% if @isReferral %>
				<%= hidden_field_tag('activity_type', "I" ) %>
			<% else %>
				<tr>
					<th align="left" ><%= l(:label_activity_type) %></th>
					<td  align="left" style="padding-left:40px;">
							<%=h select_tag("activity_type", options_for_select(acttypeHash.invert, :selected => !@activityEntry.blank? ? @activityEntry[0].activity_type : 'C'),
							:style => "width:190px;", :onchange => "loadStatus(this.id)") %>
					</td>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
				</tr>
			<% end %>

			<tr>
				<th align="left"><%= l(:field_subject) %><span style="color:red;">*</span></th>
				<td align="left" style="padding-left:40px;"><%=h text_field_tag("activity_subject",(!@activityEntry.blank? ? @activityEntry[0].name : ""),:size => 20, :style => "width:180px;", maxlength: 255) %> </td>
				<td></td>
				<th align="left" style="padding-left:150px;"><label id="lblStatus"><%= l(:field_status) %></label></th>
				<td align="left" style="padding-left:40px;">
					<% if @isReferral %>
						<%= select_tag("activity_status", options_for_select(rf_status.invert, selected: @activityEntry&.first&.status), style: "width:190px;") %>
					<% else %>
						<%=h select_tag("activity_direction", options_for_select(directionHash.invert, :selected => (!@activityEntry.blank? ? @activityEntry[0].direction : 'I') ), :style => "float:left;margin-right: 3px;") %>
						<%=h select_tag("activity_status", options_for_select(meetCallStatusHash.invert, :selected =>(!@activityEntry.blank? ? @activityEntry[0].status : 'P') ), :style=>"float:right;") %>
						<%=h select_tag("task_status", options_for_select(taskStatusHash.invert, :selected => (!@activityEntry.blank? ? @activityEntry[0].status : 'NS') )) %>
					<% end %>
				</td>
				<td></td>
			</tr>

			<tr>
				<th align="left"><%= l(:label_start_date_time) %></th>
				<td align="left" style="padding-left:40px;" >
					<%= date_field_tag('activity_start_date', !@activityEntry.blank? && !@activityEntry[0].start_date.blank? ?
						(@activityEntry[0].start_date.localtime).strftime("%Y-%m-%d") : Date.today, {:id => 'activity_start_date',
						:size => 12, :style => "width:160px;"}) + calendar_for('activity_start_date') %>
				</td>
				<td>
					<%= select_tag("start_hour", options_for_select(hr,
						:selected => !@activityEntry.blank? && !@activityEntry[0].start_date.blank? ? ((@activityEntry[0].start_date.localtime).strftime("%H")).to_i : 00 ))%> :
					<%= select_tag("start_min", options_for_select([['00',00], [15, 15], [30, 30], [45, 45]],
						:selected => !@activityEntry.blank? && !@activityEntry[0].start_date.blank? ? (@activityEntry[0].start_date.localtime).strftime("%M") : 0))%>
				</td>
				<% unless @isReferral %>
					<th align="left" style="padding-left:150px;"><label id="lblEndDate"><%= l(:label_end_date_time) %></label></th>
					<td align="left" style="padding-left:40px;">
						<div id="enddatediv" style="display: inline-block; vertical-align: top;" >
							<%= date_field_tag('activity_end_date', !@activityEntry.blank? && !@activityEntry[0].end_date.blank? ?
								(@activityEntry[0].end_date.localtime).strftime("%Y-%m-%d") :  Date.today, {:id => 'activity_end_date', :size => 12,
								:style => "width:160px;"}) + calendar_for('activity_end_date') %>
						</div>
					</td>
					<td align="left">
						<%= select_tag("end_hour", options_for_select(hr, :selected => !@activityEntry.blank? && !@activityEntry[0].end_date.blank? ?
							((@activityEntry[0].end_date.localtime).strftime("%H")).to_i : 0), :style => "width: 50px;")%>
					</td>
					<td align="left">
						<%= select_tag("end_min", options_for_select([['00',00], [15, 15], [30, 30], [45, 45]],
							:selected => !@activityEntry.blank? && !@activityEntry[0].end_date.blank? ? (@activityEntry[0].end_date.localtime).strftime("%M") : 0),
							:style => "width:50px;")%>
						<div style="clear: both;"></div>
					</td>
				<% else %>
					<th style="text-align: end;"><%= l(:label_rating) %></th>
					<td style="padding-left:40px;">
						<%= select_tag("rating", options_for_select((1..10).to_a.unshift(""), selected: @activityEntry&.first&.rating || ''), :style => "width: 50px;")%>
					</td>
				<% end %>
			</tr>

			<tr>
				<th align="left"><span id="lblduration"><%= l(:label_duration) %></span></th>
				<td align="left" style="padding-left:40px;">
					<%=h text_field_tag("activity_duration",(!@activityEntry.blank? ? hh  : ""),:size => 20, :style => "width:180px;", maxlength: 255) %>
				</td>
				<td>
					<%= select_tag("activity_duration_min", options_for_select([['00',00], [15, 15], [30, 30], [45, 45]],
						:selected => !@activityEntry.blank? ? mm : 00))%>
				</td>
				<% unless @isReferral %>
					<th align="left" style="padding-left:150px;"><label id="lblLocation"><%= l(:field_location) %></label></th>
					<td align="left" style="padding-left:40px;">
						<%=h text_field_tag("location", !@activityEntry.blank? && !@activityEntry[0].location.blank? ? @activityEntry[0].location :  "",
							:style => "width:190px;", maxlength: 255) %>
					</td>
				<% end %>
				<td></td>
			</tr>

			<tr>
				<th align="left" ><%= l(@isReferral ? :label_referral : :label_relates_to) %></th>
				<% if @isReferral %>
					<%= hidden_field_tag('related_to', "WkLead" ) %>
				<% else %>
					<td align="left" style="padding-left:40px;">
						<%=h select_tag("related_to", options_for_select(relHash.invert, :selected => (!@activityEntry.blank? ? @activityEntry[0].parent_type :
							(params[:parentType].blank? ? "" : params[:parentType]))), :onchange => "actRelatedDd(#{User.current.id}, false, false, '#{accountType}', '#{contactType}', false)",
							:style => "width:200px;") %>
					</td>
				<% end %>
				<td align="left" style="<%= @isReferral ? 'padding-left:40px;' : ''%>" >
					<%=h select_tag("related_parent", options_for_select(accArr, :selected => (!@activityEntry.blank? ? @activityEntry[0].parent_id : (params[:parentId].blank? ? "" : params[:parentId]))),
						:style => "width:200px;") %>
				</td>
				<td></td>
				<td></td>
				<td></td>
			</tr>

			<tr>
				<th align="left" ><%= @isReferral ? l(:label_interviewer) : l(:field_assigned_to) %></th>
				<td align="left" style="padding-left:40px;">
					<% if @isReferral %>
						<%= select_tag('assigned_user_id', options_for_select(get_active_users, selected: @activityEntry&.first&.assigned_user_id || User.current.id), disabled: !controller.deletePermission) %></td>
						<%= hidden_field_tag('assigned_user_id', User.current.id) unless controller.deletePermission %>
					<% else %>
						<%=h select_tag('assigned_user_id',
							options_for_select(groupOfUsers, :selected => !@activityEntry.blank? ? @activityEntry[0].assigned_user_id : ""),:required => true) %>
					<% end %>
				</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>

			<tr>
				<th align="left" ><%= l(:field_description) %></th>
				<td colspan="4" align="left" style="padding-left:40px;"><%= text_area_tag 'activity_description', (!@activityEntry.blank? ? @activityEntry[0].description : ""), :style => "min-width:700px; min-height: 150px" %></td>
			</tr>

		</table>
	</fieldset>
</div>

<%= render partial: "wkdocument/attachment_form", locals: { container: @activityEntry&.first, container_type: "WkCrmActivity", deletable: validateERPPermission("A_CRM_PRVLG") } %>
<!-- For  CRM Geolocation -->
<% if isChecked("crm_save_geo_location") %>
	<% locations = [[ @activityEntry&.first&.longitude || "", @activityEntry&.first&.latitude || ""]] %>
	<div style="padding-left: 10px;padding-bottom: 20px">
		<div id="mapContainer" style="width:30%;height:300px;"></div>
		<%= render partial: "wkgeolocation/location_map", locals: { locations: locations } %>
	</div>
<% end %>