<div class="col-lg-9 col-md-8 col-sm-12">
	<form method="POST" name="form_task" id="formedit" enctype="multipart/form-data">
		<div class="row" style="margin-top:15px;">
			<div class="col-lg-8 col-xs-8">
				<h1><a href="/<?php echo $folder; ?>/tasks"><i class="fa fa-fw fa-angle-double-left" aria-hidden="true" title="Вернуться к списку"></i></a> 
				<?php if(isset($_GET['new'])) {?>Добавление <span class="hidden-xs">задачи</span><?php } ?>
				<?php if(isset($_GET['edit'])) {?>Редактирование <span class="hidden-xs">задачи</span><?php } ?>
				</h1>
			</div>
			<div class="col-lg-4 col-xs-4">
				<div class="btn-group btn-fixed" style="float:right;">
					<?php if(isset($_GET['new'])) {?>
					<button type="submit" name="TaskAdd" class="btn btn-md btn-info" style="color:#fff;"><span class="hidden-lg hidden-md hidden-sm"><i class="fa fa-fw fa-plus" aria-hidden="true"></i></span><span class="hidden-xs text">Добавить</span></button>
					<?php } ?>
					<?php if(isset($_GET['edit'])) {?>
					<a href="/<?php echo $folder; ?>/tasks/?view=<?php echo $TaskId; ?>" class="btn btn-md btn-default"><span class="hidden-lg hidden-md hidden-sm"><i class="fa fa-fw fa-eye" aria-hidden="true"></i></span><span class="hidden-xs">Просмотр</span></a>
					<button type="submit" name="TaskEdit" class="btn btn-md btn-info" style="color:#fff;"><span class="hidden-lg hidden-md hidden-sm"><i class="fa fa-fw fa-save" aria-hidden="true"></i></span><span class="hidden-xs text">Сохранить</span></button>
					<?php } ?>
				</div>
			</div>	
		</div>
		<div class="row" style="margin-top:15px;">
			<div class="form-group col-lg-6">
				<div class="input-group template">
					<span class="input-group-addon" title="Название"><i class="fa fa-fw fa-check" aria-hidden="true"></i></span>
					<input style="width:70%;" type="text" class="form-control" name="inputName" value="<?php if(isset($TaskName)){echo $TaskName;}?>" placeholder="Название задачи" maxlength="150" required>
					<div style="width:30%;" class="input-group">
						<select class="form-control task_type" name="selectType">
							<option value="task" selected>Задача</option>
							<option value="call">Звонок</option>
							<option value="mail">Письмо</option>
							<option value="visit">Встреча</option>
						</select>
					</div>
				</div>
				<input type="text" class="hidden" name="inputId" value="<?php if(isset($TaskId)){echo $TaskId;} ?>"/>
				<input type="text" class="hidden" name="inputTimeInWork" value="<?php if(isset($TaskTimeInWork)){echo $TaskTimeInWork;} ?>"/>
				<select class="hidden" name="selectStatus">
					<option value="inwork">В работе</option>
					<option value="done">Выполнена</option>
				</select>
			</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_c = $xpath_d->query('/deals/deal[@type="deal-c" and @id="'.$_GET['deal'].'"]');
						$deals_o = $xpath_d->query('/deals/deal[@type="offer" and @id="'.$_GET['deal'].'"]');
						?>
						<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.'">' . $nCRMdeals . ' '.$DealNumber.' - '.get_client_name($DealClient).' - "'.$DealSubject.'"</option>';
						}
						?>
						</optgroup> 
						<?php if($deals_c->length > 0){?>
						<optgroup label="Завершенные сделки">
						<?
						foreach ($deals_c 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>
						<?php } ?>
						<?php if($deals_o->length > 0){?>
						<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>
						<?php } ?>
					</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-user" aria-hidden="true"></i></span>
					<select class="form-control" name="selectUser" id="selectUser">
						<?php if (isset($Login) AND $LoginCheck == $Login or $GroupCheck != 'user') { ?><option value="">--- пусто ---</option><?php } ?>
						<?
							$dom = new DOMDocument;
							$dom->load(ROOT . '/crmdata/database/users.xml');
							$xpath = new DOMXPath($dom);
							$users = $xpath->query('/users/user');
							if($GroupCheck == 'guest'){
								$curentUserSurName		= $auth->getUserSurName();
								$curentUserName 		= mb_substr($auth->getUserName(), 0, 1,'utf-8').'.';
								echo '<option value="'.$LoginCheck.'">'.$curentUserName.' '.$curentUserSurName.'</option>';
							}elseif($GroupCheck == 'user'){
								foreach ($users as $user) {
									$UserSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
									$UserName 		= mb_substr($user->getElementsByTagName("username")->item(0)->nodeValue, 0, 1,'utf-8').'.';
									$UserLogin 		= $user->getAttribute('login');
									$UserGroup 		= $user->getAttribute('group');
									if($UserGroup == 'user'){
										echo '<option value="'.$UserLogin.'">'.$UserName.' '.$UserSurName.'</option>';
									}
								}
							}else{
								foreach ($users as $user) {
									$UserSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
									$UserName 		= mb_substr($user->getElementsByTagName("username")->item(0)->nodeValue, 0, 1,'utf-8').'.';
									$UserLogin 		= $user->getAttribute('login');
									echo '<option value="'.$UserLogin.'">'.$UserName.' '.$UserSurName.'</option>';
								}
							}
						?>
					</select>
					<input type="text" class="hidden" name="inputUserS" value="<?php if(isset($TaskUserS)){echo $TaskUserS;}?>"/>
				</div>
			</div>
			<div class="form-group col-lg-3 col-xs-6">
				<div class="input-group date-pick" style="position:relative;">
					<span class="input-group-addon" title="Дата добавления"><i class="fa fa-fw fa-calendar-plus-o" aria-hidden="true"></i></span>
					<input type="text" class="form-control dateS" name="inputDateStart" placeholder="ГГГГ-ММ-ДД" value="<?php if(isset($TaskDateStart)){echo $TaskDateStart;} else {echo date("Y-m-d");} ?>" autocomplete="off" required />
				</div>
			</div>
			<div class="form-group col-lg-3 col-xs-6">
				<div class="input-group date-pick" style="position:relative;">
					<span class="input-group-addon" title="Дата завершения"><i class="fa fa-fw fa-calendar-check-o" aria-hidden="true"></i></span>
					<input type="text" class="form-control dateE" name="inputDateEnd" placeholder="ГГГГ-ММ-ДД" value="<?php if(isset($TaskDateEnd)){echo $TaskDateEnd;}?>" autocomplete="off" />
				</div>
			</div>
			<div class="form-group col-lg-3 col-xs-6">
				<div class="input-group">
					<span class="input-group-addon" title="Важность задачи"><i class="fa fa-fw fa-exclamation-triangle" aria-hidden="true"></i></span>
					<select class="form-control" name="selectPriority">
						<option value="none">--- пусто ---</option>
						<option value="high">Высокая</option>
						<option value="medium">Средняя</option>
						<option value="low">Низкая</option>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-3 col-xs-6">
				<div class="input-group">
					<span class="input-group-addon" title="Процент выполнения"><i class="fa fa-fw fa-line-chart" aria-hidden="true"></i></span>
					<select class="form-control" name="selectPercent">
						<option value="0">0 %</option>
						<option value="10">10 %</option>
						<option value="20">20 %</option>
						<option value="30">30 %</option>
						<option value="40">40 %</option>
						<option value="50">50 %</option>
						<option value="60">60 %</option>
						<option value="70">70 %</option>
						<option value="80">80 %</option>
						<option value="90">90 %</option>
						<option value="100">100 %</option>
					</select>
				</div>
			</div>
			<div class="form-group col-lg-12 editor-form" style="width: 100%!important;">
				<div class="input-group template">
					<textarea class="form-control" name="inputComment" id="inputComment" rows="10" placeholder="Комментарий к задаче"><?php if(isset($TaskComment)){echo $TaskComment;}?></textarea>
				</div>
			</div>
			<div class="form-group col-lg-6">
				<div class="input-group">
					<span class="input-group-addon" title="Ссылка"><i class="fa fa-fw fa-link" aria-hidden="true"></i></span>
					<input type="text" class="form-control" name="inputLink" value="<?php if(isset($TaskLink)){echo $TaskLink;}?>" placeholder="Ссылка"/>
				</div>
			</div>
			<div class="form-group col-lg-6">
				<?php if ($GroupCheck == 'admin' or $GroupCheck == 'manager') { ?>
				<input id="NewTempl" type="checkbox" value="yes" name="NewTempl">
				<label for="NewTempl" style="margin: 4px 0px;">Сохранить как шаблон</label>
				<?php } ?>
			</div>
		</div>
	</form>
</div>
<div class="col-lg-3 col-md-4 col-sm-12">
	<?php require_once(ROOT . 'templates/side-block.php'); ?>
</div>
<script type="text/javascript">
	function get_templ(id){
		$.ajax({
			type: "post",
			url: "/<? echo $folder; ?>/modules/templs/get_templ.php",
			data: {templid:id},
			beforeSend: function(){},
			success: function(html) {
                var result = JSON.parse(html);
				if(result.status == "ok"){
					$("#inputComment, .nicEdit-main").html(result.templ);
					$("#inputName").val(result.name);
				} else {}
			},
			error: function(xhr) {$(".page_block .title").append(' <i class="fa-exclamation-circle clr_high"></i>');},
			complete: function() {}
		});
	}
	function del_templ(id){
		$.ajax({
			type: "post",
			url: "/<? echo $folder; ?>/modules/templs/del_templ.php",
			data: {templid:id},
			beforeSend: function(){},
			success: function(html) {
                var result = JSON.parse(html);
				if(result.status == "ok"){$("#templ_" + id).fadeOut();}
			},
			error: function(xhr) {$(".page_block .title").append(' <i class="fa-exclamation-circle clr_high"></i>');},
			complete: function() {}
		});
	}
	$(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("selectDeal");
		sorted("selectUser");
		<?php if(isset($_GET['edit'])) {?>
		$('form[name=form_task] select[name=selectType]').val('<?php echo $TaskType;?>');
		$('form[name=form_task] select[name=selectDeal]').val('<?php echo $TaskDeal;?>');
		$('form[name=form_task] select[name=selectPriority]').val('<?php echo $TaskPriority;?>');
		$('form[name=form_task] select[name=selectStatus]').val('<?php echo $TaskStatus;?>');
		$('form[name=form_task] select[name=selectUser]').val('<?php echo $TaskUser;?>');
		$('form[name=form_task] select[name=selectPercent]').val('<?php echo $TaskPercent;?>');
		<?php } elseif(isset($_GET['new'])) { ?>
		$('form[name=form_task] select[name=selectDeal]').val('<?php if(isset($_GET["deal"])){echo $_GET["deal"];}?>');
		$('form[name=form_task] select[name=selectUser]').val('<?php if(isset($_GET["user"])){echo $_GET["user"];}else{echo $_SESSION["login"];}?>');
		<?php } ?>
	});
</script>
