<?
if( isset( $_POST['search'] ) ) {
	$dom = new DOMDocument;
	$dom->load($baseT);
	$xpath = new DOMXPath($dom);
	$query	= mb_strtolower(rawurldecode($_POST['query']), 'UTF-8');
	$tasks	= $xpath->query('/tasks/task[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]');
	$countTaskStatusInWork 	= count($xmltask->xpath('/tasks/task[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$query.'")]'));
} elseif ( isset( $_POST['TaskFilter'] ) ) {
	/* Список - после фильтрации */
	if( isset( $_POST["selectDeal"] ) ) {
		$filterDeal 	= ' and @deal="'.$_POST["selectDeal"].'"';
		$selectDeal 	= $_POST["selectDeal"];
		echo '<script>$(document).ready(function() {$("form#filter-list input[name=checkDeal]").prop("checked", true);$("form#filter-list select[name=selectDeal]").prop("disabled",false); $("form#filter-list option[value=\''.$_POST["selectDeal"].'\']").prop("selected", true);});</script>';
	} else {
		$filterDeal 	= '';
	}
	if( isset( $_POST["radioUser"] ) ) {
		$filterUser 	= ' and @deligated="'.$_POST["radioUser"].'"';
		$selectUser 	= $_POST["radioUser"];
		echo '<script>$(function() {$("form#filter-list input[value*=\''.$_POST["radioUser"].'\']").prop("checked", true); $(".filter-buttons").fadeIn("500");});</script>';
	} else {
		$filterUser 	= '';
	}
	if( isset( $_POST["radioPriority"] ) ) {
		$filterPriority = ' and @priority="'.$_POST["radioPriority"].'"';
		$selectPriority = $_POST["radioPriority"];
		echo '<script>$(function() {$("form#filter-list input[value*=\''.$_POST["radioPriority"].'\']").prop("checked", true); $(".filter-buttons").fadeIn("500");});</script>';
	} else {
		$filterPriority = '';
	}
	$dom = new DOMDocument;
	$dom->load($baseT);
	$xpath = new DOMXPath($dom);
	$tasks = $xpath->query('/tasks/task[@status="inwork"'.$filterDeal.$filterUser.$filterPriority.']');
	$countTaskStatusInWork 	= count($xmltask->xpath('/tasks/task[@status="inwork"'.$filterDeal.$filterUser.$filterPriority.']'));
} elseif( isset( $_GET['deal'] ) or isset( $_GET['user'] ) or isset( $_GET['status'] ) ) {
	/* Список - после фильтрации */
	if( isset( $_GET['status'] ) ) {
		$filterStatus 	= '@status="'.$_GET['status'].'"';
	} else {
		$filterStatus 	= '@status!=""';
	}
	if( isset( $_GET['deal'] ) ) {
		$filterDeal 	= ' and @deal="'.$_GET['deal'].'"';
	} else {
		$filterDeal 	= '';
	}
	if( isset( $_GET['user'] ) ) {
		$filterUser 	= ' and @deligated="'.$_GET['user'].'"';
	} else {
		$filterUser 	= '';
	}
	$dom = new DOMDocument;
	$dom->load($baseT);
	$xpath = new DOMXPath($dom);
	$tasks = $xpath->query('/tasks/task['.$filterStatus.$filterDeal.$filterUser.']');
	$countTaskStatusInWork 	= count($xmltask->xpath('/tasks/task['.$filterStatus.$filterDeal.$filterUser.']'));
} else {
	/* Список - стандартный вид */
	$dom = new DOMDocument;
	$dom->load($baseT);
	$xpath = new DOMXPath($dom);
	$tasks = $xpath->query('/tasks/task[@status="inwork"]');
	$countTaskStatusInWork 	= count($xmltask->xpath('/tasks/task[@status="inwork"]'));
	$selectUser 	= '';
	$selectDeal 	= '';
	$selectStatus 	= '';
	$selectPriority = '';
}
if(isset($_GET['edit']) AND $_GET['edit'] != '' OR isset($_GET['view']) AND $_GET['view'] != '') {
	$dom 		= new DOMDocument;
	$dom		->load($baseT);
	$xpath 		= new DOMXPath($dom);
	if(isset($_GET['edit']) AND $_GET['edit'] != '') {
		$tasks 		= $xpath->query('/tasks/task[@id="'.$_GET['edit'].'"]');
		$taskDir 	= ROOT . 'crmdata/upload/tasks_'.$_GET['edit'];
	}
	if(isset($_GET['view']) AND $_GET['view'] != '') {
		$tasks 		= $xpath->query('/tasks/task[@id="'.$_GET['view'].'"]');
		$taskDir 	= ROOT . 'crmdata/upload/tasks_'.$_GET['view'];
	}
	$taskFile 	= $taskDir . '/page_description.xml';
	
	foreach ($tasks as $task) {
		$TaskId 		= $task->getAttribute('id');
		$TaskName 		= $task->getElementsByTagName("taskname")->item(0)->nodeValue;
		$TaskStatus 	= $task->getAttribute('status');
		$TaskType 		= $task->getAttribute('type');
		$TaskPriority 	= $task->getAttribute('priority');
		$TaskPercent 	= $task->getAttribute('percent');
		$TaskUser 		= $task->getAttribute('deligated');
		$TaskDeal 		= $task->getAttribute('deal');
		$TaskTempl 		= $task->getAttribute('templ');
		$TaskCommentSm 	= $task->getElementsByTagName("comment")->item(0)->nodeValue;
		$TaskUserS 		= $task->getElementsByTagName("supervisor")->item(0)->nodeValue;
		$TaskLink 		= $task->getElementsByTagName("link")->item(0)->nodeValue;
		$TaskDateStart 	= $task->getElementsByTagName("datestart")->item(0)->nodeValue;
		$TaskDateEnd 	= $task->getElementsByTagName("dateend")->item(0)->nodeValue;
		$TaskTimeInWork = $task->getElementsByTagName("timeinwork")->item(0)->nodeValue;
		$UIP 			= $task->getElementsByTagName("uip")->item(0)->nodeValue;
		
		$date				= date("Y-m-d");
		if($TaskStatus == 'done'){
			$datetimeFrom 		= $TaskDateStart;
		} else {
			$datetimeFrom 		= $date;
		}
		$datetimeTo 		= $TaskDateEnd;
	}
	if( isset($_GET['view']) and $_GET['view'] != '' ) {
		if (!is_dir($taskDir)){
			mkdir($taskDir);
			file_put_contents($taskFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		} elseif(is_dir($taskDir) AND !file_exists($taskFile)) {
			file_put_contents($taskFile,'<?xml version="1.0" encoding="utf-8"?><item type="documentary"><text></text></item>');
		}
		
		$doc = new DOMDocument;
		$doc->load($baseD);
		$xpathc = new DOMXPath($doc);
		$deals = $xpathc->query('/deals/deal[@id="'.$TaskDeal.'"]');
		foreach ($deals as $deal) {
			$DealId 		= $deal->getAttribute('id');
			$DealNumber 	= $deal->getElementsByTagName("num")->item(0)->nodeValue;
			$DealClient 	= $deal->getAttribute('client');
			$doс = new DOMDocument;
			$doс->load(ROOT . '/crmdata/database/clients.xml');
			$xpathc = new DOMXPath($doс);
			$clients = $xpathc->query('/clients/client[@id="'. $DealClient .'"]');
			foreach ($clients as $client) {
				$ClientID 		= $client->getAttribute("id");
				$ClientName 	= $client->getElementsByTagName("clientname")->item(0)->nodeValue;
			}
		}
		$dom = new DOMDocument;
		$dom->load($baseU);
		$xpath 	= new DOMXPath($dom);
		$users 	= $xpath->query('/users/user[@login="'.$TaskUser.'"]');
		$userss = $xpath->query('/users/user[@login="'.$TaskUserS.'"]');
		foreach ($users as $user) {
			$UserID 		= $user->getAttribute("id");
			$UserSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
			$UserName 		= $user->getElementsByTagName("username")->item(0)->nodeValue;
		}
		foreach ($userss as $user) {
			$UserSID 		= $user->getAttribute("id");
			$UserSSurName 	= $user->getElementsByTagName("usersurname")->item(0)->nodeValue;
			$UserSName 		= $user->getElementsByTagName("username")->item(0)->nodeValue;
		}
	}
	
	if(file_exists($taskFile)){
		$doz 	= new DOMDocument;
		$doz	->load($taskFile);
		$xpathz = new DOMXPath($doz);
		$TaskComment = $xpathz->query('/item')->item(0)->getElementsByTagName("text")->item(0)->nodeValue;
	} else {
		$TaskComment = $TaskCommentSm;
	}
}
?>