<% use Mojo::JSON qw/decode_json encode_json/; %>
<% my $server_time = &subs::rightNow(); %>

<div class="detail_area" app="<%= $a %>" style="overflow:scroll;">
	<span style="display:none;" class="detail_json" app="<%= $a %>">
		<%= $appts->{'__specs'}->{'data'} %>
	</span>
	<b><span class="detail_quantity" app="<%= $a %>"><%= scalar @{$details} %></span> app<% unless (scalar @{$details} == 1) { %>s<% } %></b>
	<% if ($appts->{$a}->{'total_amount'}) { %>
		<span><u>Amount:</u> <%= $appts->{$a}->{'total_amount'} %></span><br>
	<% } %>
	<% if ($appts->{$a}->{'total_quantity'}) { %>
		<span><u>Quantity:</u> <%= $appts->{$a}->{'total_quantity'} %></span><br>
	<% } %>
	<% my ($db,$database,$sql) = &subs::database_grabber(); %>
	<% foreach my $l ( reverse @{$details} ) { %>
		<div style="border:solid;" last_update="<%= $l->{'server_time'} %>" class="appointment_detail" uuid="<%= $l->{'uuid'} %>" app="<%= $l->{'app'} %>" id="app_<%= $l->{'app'} %>_<%= $l->{'timestamp'} %>">
			<table style="width:100%;">
				<thead>
					<th>Type</th>
					<th>Edit</th>
					<th>TS</th>
					<th>Dur</th>
					<% if ($l->{'stop_timestamp'}) { %>
						<th>Stop</td>
					<% } %>
					<th>Warr</th>
					<th>OST</th>

				</thead>
				<tbody>
					<tr>
						<% if ($l->{'pseudonym'}->{'icon'}) { %>
						<td><img style="left:0px;"><img class="little_thumb" src="<%= $l->{'pseudonym'}->{'icon'} %>"></td>
						<% } else { %>
							<td><%= $l->{'formatted_type'} %></td>
						<% } %>

						<td><img src="/icons/edit.png" uuid="<%= $l->{'uuid'} %>" app="<%= $a %>" timestamp="<%= $l->{'timestamp'} %>" 
							class="little_thumb edit_app hover"	server_time="<%= $l->{'server_time'} %>">
						</td>




						<td class="time" style="font-size:16px;" timestamp="<%= $l->{'timestamp'} %>"></td>
						<td><%= $l->{'formatted_duration'} %></td>
						<% if ($l->{'stop_timestamp'}) { %><td class="time" timestamp="<%= $l->{'stop_timestamp'} %>"></td><% } %>
						<td class="time" timestamp="<%= $l->{'warranty'} %>"></td>
						<td class="time" timestamp="<%= $l->{'ost'} %>"></td>
					</tr>
				</tbody>
			</table>
			<div class="appointment_edit_area" app="<%= $l->{'app'} %>" uuid="<%= $l->{'uuid'} %>" style="display:none;">
				EDITOR!
			</div>
			<div class="appointment_details_buttons">
				<td><img src="/icons/trash.png" app="<%= $a %>" timestamp="<%= $l->{'timestamp'} %>" 
					class="delete_app little_thumb hover" formatted_name="<%= $appts->{$a}->{'formatted_name'} %>" 
					formatted_time="<%= $l->{'formatted_time'} %>"
					app_type="<%= $l->{'type'} %>"
					server_time="<%= $l->{'server_time'} %>"
					formatted_duration="<%= $l->{'formatted_duration'} %>" 
					uuid="<%= $l->{'uuid'} %>" />
				</td>
				<td><img class="little_thumb upload_file app_upload hover" src="/images/make believe/up.png"></td>

				<% if ($device eq 'mobile' || grep { $_->{'device'} eq 'mobile' } @{$remote_machines} ) { %>
					<td>
						<img class="little_thumb take_picture hover" camera="0" type="camera" app="<%= $a %>" src="/images/decipherable/camera back.png">
					</td>
					<td>
						<img class="little_thumb take_picture hover" camera="1" type="camera" app="<%= $a %>" src="/images/decipherable/camera front.png">
					</td>
				<% } %>
				<% if ($device eq 'computer' || $device eq 'server' || grep { $_->{'device'} eq 'computer' || $_->{'device'} eq 'server' } @{$remote_machines} ) { %>
					<td>
						<img class="little_thumb scan_document hover" type="scan" feed="flatbed" app="<%= $a %>" src="/images/decipherable/flatbed.png">
					</td>
					<td>
						<img class="little_thumb scan_document hover" type="scan" feed="adf" app="<%= $a %>" src="/images/decipherable/feeder.png">
					</td>
					<td>
						<img class="little_thumb take_picture hover" camera="webcam" type="camera" app="<%= $a %>" src="/images/make believe/eye.png">
					</td>
					<br>
				<% } %>
				<% if ($l->{'start_notes'} eq 'redacted' || $l->{'notes'} eq 'redacted' || $l->{'end_notes'} eq 'redacted') { %>
					<img src="/icons/note_button.png" server_time="<%= $l->{'server_time'} %>" class="little_thumb notes_toggle hover" app="<%= $l->{'app'} %>" timestamp="<%= $l->{'timestamp'} %>">
					<span class="notespace"></span>
				<% } %>
				
				<% if ($l->{'type'} eq 'web') { %>
					<div><iframe class="web" src="<%= $l->{'content'} %>"><%= $l->{'data'} %></iframe></div>					
				<% } elsif ($l->{'type'} eq 'msg') { %> 
					<div><%= $l->{'data'} %></div>
				<% } %>
				<% if ($l->{'file'}) { %>
					<% foreach my $f (@{$l->{'files'}}) { %>
						<% unless ( -e $f->{'file'} || $l->{'type'} eq 'snapshot' ) { 
							next;
						} %>

						<img style="float:left;" src="/icons/trash.png" class="little_thumb delete delete_file" timestamp="<%= $l->{'timestamp'} %>" app="<%= $l->{'app'} %>" app_uuid="<%= $l->{'uuid'} %>" file_uuid="<%= $f->{'uuid'} %>">
						<img src="/images/icons/download.png" class="little_thumb download" timestamp="<%= $l->{'timestamp'} %>" uuid="<%= $l->{'uuid'} %>" server_time="<%= $l->{'server_time'} %>" app="<%= $l->{'app'} %>" file="<%= $f->{'file'} %>">
						<img src="/icons/lymeboard/I.png" class="tiny_thumb file_information_grabber" app_uuid="<%= $l->{'uuid'} %>" file_uuid="<%= $f->{'uuid'} %>" file="<%= $f->{'file'} %>" app="<%= $l->{'app'} %>">						
						<% if ($f->{'file_type'} eq 'snd' || $l->{'type'} eq 'audio') { %>
						<!--	<button class="file_toggle" timestamp="<%= $l->{'timestamp'} %>" file="<%= $l->{'file'} %>?action=start"><%= $l->{'type'} %> <%= $f->{'file_type'} %></button><br> -->
							<br><%= $f->{'file'} %><br>
							<audio controls src="/file_open?app=<%= $l->{'app'} %>&file=<%= $f->{'file'} %>&timestamp=<%= $l->{'server_time'} %>"></audio>
						<% }  elsif ($f->{'file_type'} eq 'img') { %>
								<select class="image_function_selection" uuid="<%= $f->{'uuid'} %>" value="<%= $f->{'function'} %>" style="vertical-align:top;">
									<% foreach my $func ( qw/image background diagram map receipt signatorial/ ) { %>
										<option value="<%= $func %>" <% if ($func eq $f->{'function'}) { %>selected<% } %>><%= &subs::format_name($func) %></option>
									<% } %>
								</select>
								<img style="float:right;" class="little_thumb main_image_selector" 
									<% if ($settings->{'main_image'} eq $f->{'file'}) { %>
										main_image="yes"
									<% } else { %>
										main_image="no"
									<% } %>

									app="<%= $l->{'app'} %>" file="<%= $f->{'file'} %>" src="/images/make believe/explosion.png"
									style="<% if ($settings->{'main_image'} eq $f->{'file'}) { %>border-color:orange;border-width:6px;<% } %>"
								>
								<% if ($f->{'file'} !~ /;base64/gi) { %>
									<img class="little_thumb palette_toggle" src="/images/decipherable/palette.png" file="<%= $f->{'file'} %>">
									<div class="palette" style="border:solid;border-width:3px;border-radius:5px;display:none;" uuid="<%= $f->{'uuid'} %>" file="<%= $f->{'file'} %>">
										<input class="palette_input" style="width:50px;vertical-align:top;" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="fill" src="/images/palette/fill.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="rotate" src="/images/make believe/renew.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="crop" src="/images/palette/crop.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="modulate" src="/images/palette/brightness.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="level" src="/images/palette/contrast.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="black-threshold" src="/images/palette/black level.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="gaussian-blur" src="/images/palette/blur.png" file="<%= $f->{'file'} %>">
										<img class="little_thumb palette_duty" duty="sharpen" src="/images/palette/sharpen.png" file="<%= $f->{'file'} %>">

										<img class="little_thumb palette_duty" duty="copy" src="/images/palette/duplicate.png" file="<%= $f->{'file'} %>">

										<img class="little_thumb palette_duty" src="/images/decipherable/diskette.png" duty="save" file="<%= $f->{'file'} %>" style="display:none;">
										<img class="little_thumb palette_duty" src="/images/decipherable/stop sign.png" duty="clear" file="<%= $f->{'file'} %>" style="display:none;">

									</div>
								<% } %>
								<img class="detail_image" 
									<% if ($l->{'file'} =~ /;base64/gi) { %>
										src="<%= $l->{'file'} %>" type="snapshot"
									<% } else { %>
										file="<%= $f->{'file'} %>"
										src="file_open?app=<%= $l->{'app'} %>&file=<%= $f->{'file'} %>&timestamp=<%= $l->{'server_time'} %>"
									<% } %> timestamp="<%= $l->{'server_time'} %>" 
									style="height:90%;width:95%" app_uuid="<%= $l->{'uuid'} %>" file_uuid="<%= $f->{'uuid'} %>" id="<%= $l->{'uuid'} %>_<%= $f->{'uuid'} %>"
										
								/>
								<canvas style="display:none;" app="<%= $l->{'app'} %>" file_uuid="<%= $f->{'uuid'} %>" app_uuid="<%= $l->{'uuid'} %>" id="<%= $l->{'uuid'} %>_<%= $f->{'uuid'} %>_canvas"></canvas>
								<%= include 'apps/image_functions/' . $f->{'function'}, file => $f, appt => $l %>
								<input class="image_name_input" app_uuid="<%= $l->{'uuid'} %>" file_uuid="<%= $f->{'uuid'} %>" app="<%= $l->{'app'} %>" value="<%= &subs::format_name($f->{'name'}) %>" placeholder="Name" style="width:150px;vertical-align:top;">
						<% } elsif ($f->{'file_type'} eq 'vid') { %>
							<video controls loop src="/file_open?app=<%= $l->{'app'} %>&file=<%= $f->{'file'} %>&timestamp=<%= $l->{'server_time'} %>" style="height:90%;width:95%;"></video>
						<% } elsif ($f->{'file_type'} eq 'pdf') { %>
							<% my $embed_string = &subs::random_string_creator(25); %>
							<iframe class="resizable" type="application/pdf" src="/file_open?app=<%= $l->{'app'} %>&file=<%= $f->{'file'} %>&timestamp=<%= $l->{'server_time'} %>" style="height:500px;width:95%;" id="<%= $embed_string %>">

							</iframe>
						<% } %>
						<br>
						<% if ($device eq 'mobile' && $l->{'data'} eq '') { %>
							<img src="/icons/edit.png" class="little_thumb share" type="edit" file="<%= $f->{'file'} %>">
							<img src="/icons/view.png" class="little_thumb share" type="view" file="<%= $f->{'file'} %>">
							<img src="/icons/share.png" class="little_thumb share" type="send" file="<%= $f->{'file'} %>">
						<% } %>

					<% } %>
					<br>
				<% } %>
			</div>

			<table style="align:center;width:100%;">
				<thead>
					<th>Proj</th>
					<% if ($l->{'src'}) { %><th>Src</th><% } %>
					<% if ($l->{'manufacturer'}) { %><th>Man</th><% } %>
					<th>POS</th>
					<% if ($l->{'quantity'}) { %><th>Quantity</th><% } %>
					<% if ($l->{'distance'}) { %><th><button class="appointment_list_toggle hover" list="distance">Dist</button></th><% } %>
					<% if ($l->{'duties'}) { %>
						<% if (scalar @{$l->{'duties'}} > 0) { %>
							<th><button class="appointment_list_toggle hover" list="duties">Duties</button></th>
						<% } %>
					<% } %>
					<% if ($l->{'measures'}) { %>
						<% if (scalar @{$l->{'measures'}} > 0) { %>
							<th><button class="appointment_list_toggle hover" list="measures">Measures</button</th>
						<% } %>
					<% } %>
					<% if ($l->{'type'} eq 'record') { %>
						<th>Rec</th>
					<% } %>
				</thead>
				<tbody>
					<tr>
						<td><%= $l->{'formatted_project'}  %></td>
						<% if ($l->{'src'}) { %><td class="hover" onclick="appointmentGrabber('<%= $l->{'src'}->{'app'} %>')"><%= &subs::format_name($l->{'src'}->{'app'}) %></td><% } %>
						<% if ($l->{'manufacturer'}) { %><td><%= &subs::format_name($l->{'manufacturer'}) %></td><% } %>
						
						<td class="appointment_pos"><%= &subs::format_name($l->{'pos'}) %></td>
						<% if ($l->{'quantity'}) { %><td><%= $l->{'quantity'} %><%= $l->{'unit'} %></td><% } %>
						<% if ($l->{'distance'}) { %><td><%= $l->{'total_distance'} %>km</td><% } %>
						<% if ($l->{'duties'}) { %>
							<% if (scalar @{$l->{'duties'}} > 0) { %>
								<td></td>
							<% } %>
						<% } %>
						<% if ($l->{'measures'}) { %>
							<% if (scalar @{$l->{'measures'}} > 0) { %>
								<td></td>
							<% } %>
						<% } %>
						<% if ($l->{'type'} eq 'record') { %>
							<td><%= &subs::format_name($l->{'data'}->{'recorder'}) %></td>
						<% } %>
					</tr>
				</tbody>
			</table>
			<% if ($l->{'measures'}) { %>
				<span class="appointment_list" list="measures" style="display:none;">
					<% my $mtotals = {}; %>
					<% my $maverage = {}; %>
					<% if (eval { scalar @{$l->{'measures'}} } ) { %>
						<% foreach my $me ( @{$l->{'measures'}} ) { %>
								<% foreach my $m ( sort keys %{$me} ) { %>

									<% if ($m eq 'uuid') { next; } %>
									<span class="app_measure_instance" uuid="<%= $me->{'uuid'} %>">
									<b><%= &subs::format_name($m) %>:</b> 
									<% if (grep { $_ eq $m } qw/timestamp server_time ts ost st/) { %>
										<span class="time" timestamp="<%= $me->{$m} %>"></span>

									<% } else { %>
										<% if ($me->{$m} =~ /[a-zA-Z]/) { %>
											<%= &subs::format_name($me->{$m}) %>
										<% } else { %>
											<%= $me->{$m} %>
										<% } %>
										<% if ($me->{$m} =~ /[0-9]/gi) { 
											$me->{$m} =~ s/[^0-9.]//gi;
											$mtotals->{$m} += $me->{$m};
											$maverage->{$m}->{'count'} += 1;
											$maverage->{$m}->{'av'} = $mtotals->{$m} / $maverage->{$m}->{'count'};
										} %>
									</span>
								<% } %>

							<% } %>
							<img src="/icons/trash.png" class="tiny_thumb app_measure_delete hover" uuid="<%= $me->{'uuid'} %>">
							<br>
						<% } %>
					<% } %>
					<% if (scalar keys %{$mtotals}) { %>
						<br>
						<% foreach my $mt (sort keys %{$mtotals}) { %>
							<u>
								<b>Total <%= &subs::format_name($mt) %>: </b><%= $mtotals->{$mt} %>
								<b>Average <%= &subs::format_name($mt) %>: </b><%= sprintf("%.2f", $maverage->{$mt}->{'av'}) %>
							</u>

							<br>
						<% } %>
					<% } %>
				</span>
			<% } %>
			<% if ($l->{'duties'}) { %>
				<span class="appointment_list" list="duties" style="display:none;">
					<% foreach my $d ( @{$l->{'duties'}} ) { %>
						<span class="appointment_duty_container" duuid="<%= $d->{'duuid'} %>">
							<img class="little_thumb"
								<% if ($d->{'camera'}) { %>
									<% if ($d->{'camera'} eq '0') { %>
										src="/images/decipherable/camera back.png"
									<% } elsif ($d->{'camera'} eq '1') { %>
										src="/images/decipherable/camera front.png"
									<% } elsif ($d->{'camera'} eq 'webcam') { %>
										src="/images/make believe/eye.png"
									<% } %>
								<% } elsif ($d->{'feed'}) { %>
									<% if ($d->{'feed'} eq 'flatbed') { %>
										src="/images/decipherable/flatbed.png"
									<% } elsif ($d->{'feed'} eq 'adf') { %>
										src="/images/decipherable/feeder.png"
									<% } %>
								<% } %>
							>
							<span class="time" timestamp="<%= $d->{'timestamp'} %>"></span>
							<img class="tiny_thumb delete_duty hover" server_time="<%= $server_time %>" duuid="<%= $d->{'duuid'} %>" src="/icons/trash.png"><br>
						</span>
					<% } %>
				</span>
			<% } %>
			<% if ($l->{'distance'}) { %>
				<span class="appointment_list" list="distance" style="display:none;">
					<% foreach my $d ( @{$l->{'distance'}} ) { %>
						<span class="appointment_distance_container" uuid="<%= $d->{'uuid'} %>" server_time="<%= $d->{'server_time'} %>">
							<span class="appointment_distance hover" app="<%= $a %>" app_uuid="<%= $l->{'uuid'} %>" uuid="<%= $d->{'uuid'} %>">
								<%= $d->{'distance'} %>km
							</span> 
							<% if ( scalar @{$l->{'distance'}} > 1) { %>
								<span class="time" timestamp="<%= $d->{'timestamp'} %>"></span>
							<% } %>
							<img class="tiny_thumb delete_location hover" server_time="<%= $server_time %>" uuid="<%= $d->{'uuid'} %>" src="/icons/trash.png"><br>
						</span>
					<% } %>
				</span>
			<% } %>
			<span class="appointment_model_selector_container" attribute="<%= $settings->{'pos'} %>">
				<%= include 'apps/transaction/models', settings => $settings, models => $models, l => $l, source => 'list' %>
			</span>
			<%= include 'apps/transaction/options', settings => $settings, options => $options, l => $l, source => 'list' %>


			<% if ($l->{'type'} eq 'transaction' || $l->{'type'} eq 'purchase' ) { %>
				<%= include 'apps/app_transaction', app => $l %>
				<br>
			<% } elsif ($l->{'type'} eq 'invoice' || $l->{'type'} eq 'quote') { %>
				<%= include 'apps/information', app => $l %>
			<% } %>
		</div>
	<% } %>
<div>

