<div class="col-lg-9 col-md-8 col-sm-12">
	<form method="POST" name="form_deal" id="formedit" enctype="multipart/form-data">
		<div class="row" style="margin-top:15px;">
			<div class="col-lg-8 col-xs-8">
				<h1><a href="javascript: ;" onclick="history.go(-2)"><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="DealAdd" 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; ?>/deals/?view=<?php echo $DealId; ?>" 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="DealEdit" 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">
					<span class="input-group-addon" title="Тип"><i class="fa fa-fw fa-briefcase" aria-hidden="true"></i></span>
					<select class="form-control" name="selectType" required>
						<option value="offer">Коммерческое предложение</option>
						<option value="deal-a">Активная сделка</option>
						<?php if(isset($_GET['edit'])) {?><option value="deal-c">Завершенная сделка</option><?php } ?>
					</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-file-text-o" aria-hidden="true"></i></span>
					<input type="text" class="form-control" name="inputNumber" value="<?php if(isset($DealNumber)){echo $DealNumber;} else {echo newDealNum();} ?>" placeholder="№ документа" maxlength="150" required>
				</div>
				<input type="text" class="hidden" value="<?php if(isset($DealId)){echo $DealId;} ?>"  name="inputId">
			</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-rub" aria-hidden="true"></i></span>
					<input type="text" class="form-control" name="inputPrice" placeholder="Сумма сделки" maxlength="150" value="<?php if(isset($Price)){echo $Price;} ?>" onkeyup="return checkdot(this);" onchange="return checkdot(this);" required>
				</div>
			</div>
			<div class="form-group col-lg-3 hidden">
				<div class="input-group">
					<span class="input-group-addon" title="Расходы"><i class="fa fa-fw fa-rub clr_red" aria-hidden="true"></i></span>
					<input type="text" class="form-control" name="inputCosts" placeholder="Расходы" maxlength="150" value="<?php if(isset($Costs)){echo $Costs;} ?>" onkeyup="return checkdot(this);" onchange="return checkdot(this);">
				</div>
			</div>
			<div class="form-group col-lg-3 hidden">
				<div class="input-group">
					<span class="input-group-addon" title="Оплачено"><i class="fa fa-fw fa-rub clr_green" aria-hidden="true"></i></span>
					<input type="text" class="form-control" name="inputPaid" placeholder="Оплачено" maxlength="150" value="<?php if(isset($Paid)){echo $Paid;} ?>" onkeyup="return checkdot(this);" onchange="return checkdot(this);">
				</div>
			</div>
			<div class="form-group col-lg-6">
				<div class="input-group template">
					<span class="input-group-addon" title="Метка"><i class="fa fa-fw fa-tag" aria-hidden="true"></i></span>
					<input list="all-dealstags" type="text" class="form-control" id="inputSubject" name="inputSubject" value="<?php if(isset($Subject)){echo $Subject;} ?>" placeholder="Метка сделки" maxlength="150" required>
					<datalist id="all-dealstags">
						<?php
						$dox = new DOMDocument;
						$dox->load($baseD);
						$xpathx = new DOMXPath($dox);
						$alltagsdeal = $xpathx->query('/deals/deal');
						$sourses_arr = array();
						$i = 0;
						foreach ($alltagsdeal as $deal) {
							if(is_object($deal->getElementsByTagName("subject")->item(0))){
								$listSourceName = $deal->getElementsByTagName("subject")->item(0)->nodeValue;
								$countSource = count($xmldeal->xpath('/deals/deal[subject="'.$listSourceName.'"]'));
								$sourses_arr[$listSourceName] =  $countSource;
								$i++;
							}
						}
						$sources = $sourses_arr;
						foreach ($sources as $key => $value){
							echo '<option value="' . $key . '"></option>';
						}
						//print_r($sourses_arr);
						?>
					</datalist>
				</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-address-book" aria-hidden="true"></i></span>
					<select class="form-control" name="selectClient" id="selectClient" required>
						<option value="">--- пусто ---</option>
						<?	/*
							$dom = new DOMDocument;
							$dom->load(ROOT . '/crmdata/database/clients.xml');
							$xpath = new DOMXPath($dom);
							$clients = $xpath->query('/clients/client');
							foreach ($clients as $client) {
								$ClientId = $client->getAttribute('id');
								$ClientName = $client->getElementsByTagName("clientname")->item(0)->nodeValue;
								echo '<option value="'.$ClientId.'">'.$ClientName.'</option>';
							}
							*/
						?>
					</select>
				</div>
				--->
				<div class="input-group">
					<?php
						$dom = new DOMDocument;
						$dom->load($baseC);
						$xpath = new DOMXPath($dom);
						$clients = $xpath->query('/clients/client');
					?>
					<span class="input-group-addon" title="Клиент"><i class="fa fa-fw fa-address-book" aria-hidden="true"></i></span>
					<input class="form-control" type="text" placeholder="Клиент" value="" name="selectClient" list="list-all-clients" required <?php if(isset($_GET['edit'])) {?>readonly<? } ?> />
				</div>
				<datalist id="list-all-clients">
					<?php
					foreach ($clients as $client) {
						$ClientName = $client->getElementsByTagName("clientname")->item(0)->nodeValue;
						echo '<option value="' . $ClientName . '"></option>';
					} ?>
				</datalist>
			</div>
			<div class="form-group col-lg-12 editor-form">
				<div class="input-group template" style="width: 100%!important;">
					<textarea class="form-control" name="inputComment" id="inputComment" rows="10" placeholder="Комментарий к сделке"><?php if(isset($Comment)){echo $Comment;}?></textarea>
				</div>
			</div>
			<div class="form-group col-lg-3">
				<div class="input-group date-pick" style="position:relative;">
					<span class="input-group-addon" style="cursor:pointer;" 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($DateStart)){echo $DateStart;} else {echo date("Y-m-d");} ?>" required />
				</div>
			</div>
			<div class="form-group col-lg-3">
				<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 dateE1" name="inputDateEnd" placeholder="ГГГГ-ММ-ДД" value="<?php if(isset($DateEnd)){echo $DateEnd;}?>" autocomplete="off" />
				</div>
			</div>
			<div class="form-group col-lg-6">
				<input id="NewTempl" type="checkbox" value="yes" name="NewTempl">
				<label for="NewTempl" style="margin: 4px 0px;">Сохранить как шаблон</label>
			</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);
					$("#inputSubject").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("selectClient");
		<?php if(isset($_GET['edit'])) {?>
		$('form[name=form_deal] select[name=selectType]').val('<?php echo $Type;?>');
		$('form[name=form_deal] input[name=selectClient]').val('<?php echo get_client_name($Client); ?>');
		<?php } elseif(isset($_GET['new'])) { ?>
		$('form[name=form_deal] input[name=selectClient]').val('<?php if(isset($_GET["client"])){echo get_client_name($_GET["client"]);}elseif(isset($_GET["client"]) AND $Client != ''){echo get_client_name($Client);} else {echo '';}?>');
		<?php } ?>
	});
</script>