<?php

if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
if (!defined('APP_PATH_ROOT')) define('APP_PATH_ROOT', dirname(__FILE__));
if (!defined('APP_PATH_WWW')) define('APP_PATH_WWW', dirname(__FILE__));
if (!defined('APP_PATH_CONFIG')) define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');

function WYNIKI_ANKIETY_FUNC() {

	require_once APP_PATH_ROOT . DS . 'se-lib' . DS . 'Lib.php';
	Lib::loadClass('V');
	Lib::loadClass('User');
	Lib::loadClass('Config');
	Lib::loadClass('DB');


	SEF('MENU');
	MENU();

	echo '<h1>' . "Wyniki ankiet" . '</h1>';

	$task = V::get('task', '', $_GET);

	$controller = new WynikiAnkiety();
	$controller->run($task);
}


class WynikiAnkiety {

	var $allowed_monsths = array();
	var $month = null;
	var $user = null;

	function __construct() {
		
		$cur_y = date('Y');
		$cur_m = date('n');
		while ($cur_m > 0) {
			$this->allowed_monsths []= sprintf('%d-%02d', $cur_y, $cur_m);
			$cur_m--;
		}
		for ($i = 1; $i <= 12; $i++) {
			$this->allowed_monsths []= sprintf('%4d-%02d', $cur_y - 1, $i);
		}

		$this->month = V::get('month', '', $_GET);
		if (!in_array($this->month, $this->allowed_monsths)) {
			$this->month = '';
		}

		$this->user = V::get('user', '', $_GET);
	}

	function run($task) {
		$this->task_default();

		if ($task != '') {
			$task_fun = 'task_' . $task;
			if (method_exists($this, $task_fun)) {
				$this->$task_fun();
			} else {
				echo '<p>' . "Error: wrong task. " . '<a href="' . "?MENU_INIT=WYNIKI_ANKIETY_FUNC" . '">' . "WYNIKI_ANKIETY_FUNC" . '</a>' . '</p>';
			}
		}
	}

	function task_default() {
		echo '<form action="" method="GET">';
			echo '<input type="hidden" name="MENU_INIT" value="WYNIKI_ANKIETY_FUNC" />';
			echo "Miesiąc: ";
			echo '<select name="month">';
				echo '<option>' . " [ Wybierz ] " . '</option>';
				foreach ($this->allowed_monsths as $v_month) {
					$sel = (!empty($this->month) && $this->month == $v_month)? ' selected="selected"' : '';
					echo '<option value="' . $v_month . '"' . $sel . '>' . $v_month . '</option>';
				}
			echo '</select>';
			echo ' ';
			echo '<input type="submit" value="' . "Wybierz" . '" />';
		echo '</form>';

		if (!empty($this->month)) {
			$stats = $this->get_stats_by_month($this->month);

			if (empty($stats)) {
				echo '<p>' . "Brak danych" . '</p>';
			} else {
				echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view" style="margin:10px 0;">';
				echo '<tr>';
					echo '<th>' . "T_WORKPOINTS_USER" . '</th>';
					echo '<th>' . "ilość" . '</th>';
				echo '</tr>';
				foreach ($stats as $v_stat) {
					echo '<tr>';
						echo '<td>';
						if (!empty($v_stat->T_WORKPOINTS_USER)) {
							echo '<a href="' . "?MENU_INIT=WYNIKI_ANKIETY_FUNC&month=" . $this->month . "&user=" . $v_stat->T_WORKPOINTS_USER . '">' . $v_stat->T_WORKPOINTS_USER . '</a>';
						}
						echo '</td>';
						echo '<td>' . $v_stat->cnt . '</td>';
					echo '</tr>';
				}
				echo '</table>';
			}

			if (!empty($this->user)) {
				$this->show_user();
			}
		}
	}

	function get_stats_by_month($month) {
		$ret = array();
		$db = DB::getDB();
		$sql = "SELECT COUNT( * ) AS  `cnt`, `T_WORKPOINTS_USER`
		--		, GROUP_CONCAT(DISTINCT `A_RECORD_UPDATE_AUTHOR`), GROUP_CONCAT(DISTINCT `T_WORKPOINTS_DATE`)
			FROM `USERS2_MARKETING_HIST`
			WHERE `T_WORKPOINTS_DATE` LIKE '{$month}-%' AND `T_WORKPOINTS_USER`!='N/S;' AND `T_WORKPOINTS_DATE`!='N/S;'
			GROUP BY `T_WORKPOINTS_USER`
			ORDER BY `cnt` DESC
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$ret []= $r;
		}
		return $ret;
	}

	function get_user_badania($user, $month) {
		$ret = array();
		$db = DB::getDB();
		$sql = "SELECT h.*
				, (select S_ADDRESS_STREET from `USERS2_MARKETING` as m where m.`ID`=h.`ID_USERS2`) as S_ADDRESS_STREET
			FROM `USERS2_MARKETING_HIST` as h
			WHERE h.`T_WORKPOINTS_DATE` LIKE '{$month}-%' AND h.`T_WORKPOINTS_USER`='{$user}' AND h.`T_WORKPOINTS_DATE`!='N/S;'
			order by h.`T_WORKPOINTS_DATE` DESC
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$ret []= $r;
		}
		return $ret;
	}

	function get_badania_cols() {
		$cols = array();
		$cols []= 'T_WORKPOINTS_USER';
		$cols []= 'T_WORKPOINTS_DATE';
		//$cols []= 'T_WORKPOINTS_TYPE';
		$cols []= 'I_BAD_DATE';
		$cols []= 'I_OPER';
		$cols []= 'I_OPER_ZAD';
		$cols []= 'I_OPER_FIN';
		$cols []= 'I_OPER_ZAINT';
		$cols []= 'S_OTHER_INFO';
		$cols []= 'T_BAD_DATE';
		$cols []= 'T_OPER';
		$cols []= 'T_OPER_ZAD';
		$cols []= 'T_OPER_FIN';
		$cols []= 'T_OPER_ZAINT';
		$cols []= 'TV_BAD_DATE';
		$cols []= 'TV_OPER';
		$cols []= 'TV_OPER_ZAD';
		$cols []= 'TV_OPER_FIN';
		$cols []= 'TV_OPER_ZAINT';
		$cols []= 'P_NAME';
		$cols []= 'P_PHONE';
		$cols []= 'P_FLOOR_CONTROL';
		return $cols;
	}

	function show_user() {
		if (!empty($this->month) && !empty($this->user)) {
			$badania = $this->get_user_badania($this->user, $this->month);

			if (empty($badania)) {
				echo '<p>' . "Brak danych" . '</p>';
			} else {
				$cols = $this->get_badania_cols();
				echo '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view" style="margin:10px 0;">';
				echo '<thead>';
				echo '<tr>';
					echo '<td>' . "ID" . '</td>';
					echo '<td>' . "S_ADDRESS_STREET" . '</td>';
					foreach ($cols as $v_col) {
						echo '<td>' . $v_col . '</td>';
					}
					echo '<td>' . "Ilość zmian" . '</td>';
				echo '</tr>';
				echo '</thead>';
				echo '<tbody>';
				$t = 0;
				foreach ($badania as $v_badanie) {
					echo '<tr class="row-'.($t = 1 - $t).'">';
						echo '<td>' . $v_badanie->ID_USERS2 . '</td>';
						echo '<td>' . $v_badanie->S_ADDRESS_STREET . '</td>';
						$zmian = 0;
						foreach ($cols as $v_col) {
							$val = $v_badanie->$v_col;
							if ($val == 'N/S;') {
								$val = '';
							}
							echo '<td>' . $val . '</td>';
							if (!empty($val)) {
								$zmian++;
							}
						}
						echo '<td>' . ($zmian - 2) . '</td>';// - pola T_WORKPOINTS_USER, T_WORKPOINTS_DATE
					echo '</tr>';
				}
				echo '</tbody>';
				echo '</table>';
			}
		}
	}

}
