<style>
.table_nb tr:first-child td {
	border-top:none;
}
</style>

<div class="col-lg-12 col-md-12 col-sm-12">
	<h1><i class="fa fa-bookmark <?php if($priority == 'high'){echo 'clr_high';}else{echo 'hidden';}?>" aria-hidden="true"></i> <?php if($subject != ''){echo 'Тема: ' . $subject;} else {echo 'Без темы';} ?></h1>
	<div class="row">
		<div class="col-lg-3">
			<div class="row" style="margin-bottom:20px;">
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-4">
					<?php 
					if($fromid == ''){echo '<div class="big-photo empty" style="background:' . get_color2('WORKBox') . ';">WB</div>';}elseif(file_exists('./crmdata/upload/_user-photo/' . get_user_photo($fromid)) and get_user_photo($fromid) != false){
						echo '<div class="big-photo" style="background:url(\'/'. $folder . '/crmdata/upload/_user-photo/' . get_user_photo($fromid) . '?'.rand(1,1000).'        \') center center;"><img id="qrImage" src=""/></div>';
					} else {echo '<div class="big-photo empty" style="background:' . get_color2(get_user_login($fromid)) . ';">' . get_user_name($fromid, 'short') . '</div>';}
					?>
				</div>
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-8">
					<form method="POST" style="display:block;">
						<div class="btn-group btn-group-justified" style="width:100%;margin:0px 0px 10px 0px;">
							<a href="javascript: ;" class="btn btn-default" style="cursor:default;background:none;"><?php if($status == 'notread' and $toid != $_SESSION["userid"]){echo '<b>Не прочитано</b>';} else {echo '<span class="clr_low">Прочитано</span>';} ?></a>
							<a href="javascript: ;" id="print" class="btn btn-default">На печать</a>
						</div>
					</form>
				</div>
			</div>
		</div>
		<div class="col-lg-9">
			<div id="to_print">
			<?php if($text != '' && $text != '<br>'){ ?>
				<div class="panel panel-default" style="">
					<div class="panel-heading" style="background:none;">
						<div><i class="fa fa-angle-right" aria-hidden="true"></i> <b><div style="display:inline-block;width:40px;">От:</div> <?php if($fromid == ''){echo $fromname;}else{echo '<a href="/'.$folder.'/users/?view='.$fromid.'">'.$fromname.'</a>';}?></b><span style="float:right;"><?php echo $date; ?></span></div>
						
						<div><i class="fa fa-angle-right" aria-hidden="true"></i> <div style="display:inline-block;width:40px;">Кому:</div> <a href="/<?php echo $folder;?>/users/?view=<?php echo $toid;?>"><?php echo $toname; ?></a></div>
						
						<?php if($coppyid != ''){ ?><div><i class="fa fa-angle-right"></i> <div style="display:inline-block;width:40px;">Копия:</div> <a href="/<?php echo $folder;?>/users/?view=<?php echo $coppyid;?>"><?php echo $coppyname; ?></a></div><?php } ?> 
					</div>
					<div class="panel-body">
						<?php echo $text; ?>
						<?php if($taskid != '' or $clientid != '' or $dealid != '' or $eventid != ''){ ?>
						<hr style="margin:20px 0px;"/>
							<p><i class="fa fa-fw fa-paperclip" aria-hidden="true"></i> Прикрепленные материалы:</p>
							<span class="clr_vlow">
							<?php if($taskid != '' and $taskname != ''){ ?><i class="fa fa-fw fa-angle-right" aria-hidden="true"></i> Задача: <a href="/<?php echo $folder; ?>/tasks/?view=<?php echo $taskid; ?>"><?php echo $taskname;?></a><br/><?php } ?>
							<?php if($clientid != '' and $clientname != ''){ ?><i class="fa fa-fw fa-angle-right" aria-hidden="true"></i> Клиент: <a href="/<?php echo $folder; ?>/clients/?view=<?php echo $clientid; ?>"><?php echo $clientname;?></a><br/><?php } ?>
							<?php if($dealid != '' and $dealnum != ''){ ?><i class="fa fa-fw fa-angle-right" aria-hidden="true"></i> Сделка: <a href="/<?php echo $folder; ?>/deals/?view=<?php echo $dealid; ?>"><?php echo $dealnum;?></a><br/><?php } ?>
							<?php if($eventid != '' and $eventname != ''){ ?><i class="fa fa-fw fa-angle-right" aria-hidden="true"></i> Событие: <a href="/<?php echo $folder; ?>/events/?view=<?php echo $eventid; ?>"><?php echo $eventname;?></a><?php } ?>
							</span>
						<?php } ?>
						

					</div>
				</div>
			<?php } ?>
			</div>

			<?php if($fromid != $_SESSION["userid"] AND $fromid != '') { ?>
				<form method="POST">
					<input type="hidden" name="inputSubject" value="RE: <?php if($subject != ''){echo $subject;} else {echo 'Без темы';} ?>">
					<input type="hidden" name="selectUserTo" value="<?php echo $fromid; ?>">
					<input type="hidden" name="selectUserFrom" value="<?php echo $toid; ?>">
					<input type="hidden" name="inputTags" value="<?php echo $tags; ?>">
					<input type="hidden" name="selectPriority" value="<?php echo $priority; ?>">
					<div class="row">
						<div class="form-group col-lg-12 editor-form">
							<textarea class="form-control" name="inputComment" id="inputComment" rows="10" placeholder="Ответ на письмо">
							<br/><br/>
							<hr/>
							<i><?php echo $date; ?> > <?php echo $fromname; ?> > <?php echo $toname; ?>
							<br/><br/>
							<?php echo $text; ?>
							</i>
							</textarea>
						</div>
						<div class="form-group col-lg-3">
							<div class="input-group">
								<span class="input-group-addon" title="Прикрепить задачу"><i class="fa fa-fw fa-check" aria-hidden="true"></i></span>
								<select class="form-control" name="selectTask" id="selectTask">
									<option value="">--- пусто ---</option>
									<?
										$dom_t = new DOMDocument;
										$dom_t ->load($baseT);
										$xpath_t = new DOMXPath($dom_t);
										$l_tasks = $xpath_t->query('/tasks/task[@status="inwork"]');
										foreach ($l_tasks as $task) {
											$taskName 	= $task->getElementsByTagName("taskname")->item(0)->nodeValue;
											$taskID 	= $task->getAttribute('id');
											echo '<option value="'.$taskID.'">'.$taskName.'</option>';
										}
									?>
								</select>
							</div>
						</div>
						<div class="form-group col-lg-3">
							<div class="input-group">
								<span class="input-group-addon" title="Прикрепить клиента"><i class="fa fa-fw fa-address-book" aria-hidden="true"></i></span>
								<select class="form-control" name="selectClient" id="selectClient">
									<option value="">--- пусто ---</option>
									<?
										$dom_c = new DOMDocument;
										$dom_c ->load($baseC);
										$xpath_c = new DOMXPath($dom_c);
										$l_clients = $xpath_c->query('/clients/client');
										foreach ($l_clients as $client) {
											$ClientName 	= $client->getElementsByTagName("clientname")->item(0)->nodeValue;
											$ClientId 		= $client->getAttribute('id');
											echo '<option value="'.$ClientId.'">'.$ClientName.'</option>';
										}
									?>
								</select>
							</div>
						</div>
						<div class="form-group col-lg-3">
							<div class="input-group">
								<span class="input-group-addon" title="Прикрепить сделку"><i class="fa fa-fw fa-briefcase" aria-hidden="true"></i></span>
								<select class="form-control" name="selectDeal" id="selectDeal" <?php if ($GroupCheck == 'user') { echo 'disabled'; } ?> >
									<?
									$dom_d = new DOMDocument;
									$dom_d->load($baseD);
									$xpath_d = new DOMXPath($dom_d);
									$deals_a = $xpath_d->query('/deals/deal[@type="deal-a"]');
									$deals_o = $xpath_d->query('/deals/deal[@type="offer"]');
									?>
									<option value="">--- пусто ---</option>
									
									<optgroup label="Активные сделки">
									<?
									foreach ($deals_a as $deal) {
										$DealId 		= $deal->getAttribute('id');
										$DealNumber 	= $deal->getElementsByTagName("num")->item(0)->nodeValue;
										if(is_object($deal->getElementsByTagName("subject")->item(0))){
											$DealSubject 	= $deal->getElementsByTagName("subject")->item(0)->nodeValue;
										}
										$DealClient 	= $deal->getAttribute('client');
										$DealType 		= $deal->getAttribute('type');
										echo '<option value="'.$DealId.'">' . $nCRMoffers . ' '.$DealNumber.' - '.get_client_name($DealClient); if($DealSubject != ''){ echo ' - "'.$DealSubject.'"';} echo '</option>';
									}
									?>
									</optgroup> 
									<optgroup label="Предложения">
									<?
									foreach ($deals_o as $deal) {
										$DealId 		= $deal->getAttribute('id');
										$DealNumber 	= $deal->getElementsByTagName("num")->item(0)->nodeValue;
										if(is_object($deal->getElementsByTagName("subject")->item(0))){
											$DealSubject 	= $deal->getElementsByTagName("subject")->item(0)->nodeValue;
										}
										$DealClient 	= $deal->getAttribute('client');
										$DealType 		= $deal->getAttribute('type');
										echo '<option value="'.$DealId.'">' . $nCRMoffers . ' '.$DealNumber.' - '.get_client_name($DealClient); if($DealSubject != ''){ echo ' - "'.$DealSubject.'"';} echo '</option>';
									}
									?>
									</optgroup>
									
								</select>
							</div>
						</div>
						<div class="form-group col-lg-3">
							<div class="input-group">
								<span class="input-group-addon" title="Прикрепить событие"><i class="fa fa-fw fa-clock-o" aria-hidden="true"></i></span>
								<select class="form-control" name="selectEvent" id="selectEvent">
									<option value="">--- пусто ---</option>
									<?
										$dom_e = new DOMDocument;
										$dom_e ->load($baseE);
										$xpath_e = new DOMXPath($dom_e);
										$l_events = $xpath_e->query('/events/event');
										foreach ($l_events as $event) {
											$eventName 	= $event->getElementsByTagName("name")->item(0)->nodeValue;
											$eventID 	= $event->getAttribute('id');
											echo '<option value="'.$eventID.'">'.$eventName.'</option>';
										}
									?>
								</select>
							</div>
						</div>
						<div class="col-lg-12 centered" style="text-align:center;">		
							<button type="submit" style="width:auto;margin:15px auto;padding:6px 30px;" name="AnswerSend" class="btn btn-block btn-info">Отправить ответ</button>
						</div>
					</div>
				</form>
			<?php } ?>
		</div>
	</div>
</div>
<script type="text/javascript">

	$(document).ready(function() {
		function sorted(id){
			for(var c=document.getElementById(id),b=c.options,a=0;a<b.length;)
			if(b[a+1]&&b[a].text>b[a+1].text){c.insertBefore(b[a+1],b[a]);a=a>=1?a-1:a+1}else a++;
			b[0].selected=true; 
		};
		sorted("selectTask");
		sorted("selectClient");
		sorted("selectDeal");
		sorted("selectEvent");
	});
</script>