<?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 S_ADDRESS_CHECK() {

	require_once dirname(__FILE__) . '/' . 'se-lib' . '/' . 'Lib.php';
	Lib::loadClass('V');
	Lib::loadClass('User');
	Lib::loadClass('Config');
	Lib::loadClass('DB');

	SEF('MENU');
	MENU();

	echo '<h1>' . __FUNCTION__ . '</h1>';

	$addr_check = new AddressCheck();
	$addr_check->set_filters($_GET);
	$buildings_rows = $addr_check->get_buildings();
	$mieszkania_rows = $addr_check->get_mieszkania();

echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;display:none;">buildings_rows (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($buildings_rows);echo'</pre>';
echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;display:none;">mieszkania_rows (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($mieszkania_rows);echo'</pre>';

?>
	<style type="text/css">
.tbl-view td {vertical-align:top;}
.tbl-view td {font-size:small;}

#tbl_S_ADDRESS_CHECK {}
 #tbl_S_ADDRESS_CHECK .tbl-actions {text-align:left;}
 #tbl_S_ADDRESS_CHECK .klatki-0 {background:#FFCFCF;}
 #tbl_S_ADDRESS_CHECK .mieszk-0 {background:#FFCFCF;}
 #tbl_S_ADDRESS_CHECK .err-mieszk-count .mieszk-cnt {background:#FFCFCF;}

.tbl-actions .all {display:none;}
.tbl-actions .with_offers_only {display:inline;}
.with_offers_only .tbl-actions .all {display:inline;}
.with_offers_only .tbl-actions .with_offers_only {display:none;}

.with_offers_only .building- {display:none;}
.with_offers_only .building-WAITING {display:none;}
.with_offers_only .building-NORMAL {}
.with_offers_only .building-MONITOR {display:none;}
.with_offers_only .building-WARNING {}
.with_offers_only .building-OFF {display:none;}
.with_offers_only .building-TO_OFF {display:none;}
.with_offers_only .building-DELETED {display:none;}
	</style>

	<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery('#tbl_S_ADDRESS_CHECK').find('.tbl-actions button').click(function(e){
		console.log(e);
		var n=jQuery(e.target);
		if (n.hasClass('with_offers_only')) {
			jQuery('#tbl_S_ADDRESS_CHECK').removeClass('all');
			jQuery('#tbl_S_ADDRESS_CHECK').addClass('with_offers_only');
		} else {
			jQuery('#tbl_S_ADDRESS_CHECK').removeClass('with_offers_only');
			jQuery('#tbl_S_ADDRESS_CHECK').addClass('all');
		}
	})
});
	</script>

	<table cellspacing="0" cellpadding="0" border="1" class="tbl-view" id="tbl_S_ADDRESS_CHECK">
		<caption class="tbl-actions">
			<button class="all">Wszystkie budynki</button>
			<button class="with_offers_only">Tylko budynki z ofertami</button>
		</caption>
	<thead>
		<tr>
			<th colspan="5">BUILDING</th>
			<th>USERS2_MARKETING</th>
		</tr>
		<tr>
			<th>A_STATUS</th>
			<th>P_ADDRESS_STREET</th>
			<th>S_ADDRESS_STREET</th>
			<th>KLATKI</th>
			<th>MIESZKANIA</th>
			<th>Ilość mieszkań</th>
		</tr>
	</thead>
	<tbody>
<?php	foreach ($buildings_rows as $b_id => $building) {
		$miekszania = array();
		if (array_key_exists($building->S_ADDRESS_STREET, $mieszkania_rows)) {
			$miekszania = $mieszkania_rows[$building->S_ADDRESS_STREET];
			unset($mieszkania_rows[$building->S_ADDRESS_STREET]);
		}
		?>
		<tr class="<?php echo "building-{$building->A_STATUS}"; ?>">
			<td><?php echo $building->A_STATUS; ?></td>
			<td><?php echo $building->P_ADDRESS_STREET; ?></td>
			<td><?php echo $building->S_ADDRESS_STREET; ?>
				<a href="index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=16#EDIT/<?php echo $b_id; ?>">Edytuj <?php echo $b_id; ?></a>
			</td>
			<td<?php echo ($building->T_BUILDING_KLATKI == 0)? ' class="klatki-0"' : ''; ?>><?php echo $building->T_BUILDING_KLATKI; ?></td>
			<td<?php echo ($building->T_BUILDING_MIESZKANIA == 0)? ' class="mieszk-0"' : ''; ?>><?php echo $building->T_BUILDING_MIESZKANIA; ?></td>
			<td<?php echo ($building->T_BUILDING_MIESZKANIA > 0 && $building->T_BUILDING_MIESZKANIA != count($miekszania))? ' class="err-mieszk-count"' : ''; ?>>
		<?php
			if (empty($miekszania)) {
				echo '<b style="color:red" title="' . "Brak mieszkań na danym budynku!" . '" class="mieszk-cnt">' . "(0)" . '</b>';
			} else {
				echo '<b class="mieszk-cnt">' . "(" . count($miekszania) . ")" . '</b>';
				$m_street_err = array();
				foreach ($miekszania as $m_id => $m_street) {
					if (substr($m_street, 0, strlen($building->S_ADDRESS_STREET)) !== $building->S_ADDRESS_STREET) {
						$m_street_err []= $m_id;
					}
				}
				if (!empty($m_street_err)) {
					echo '<b style="color:red">' . " " . count($m_street_err) . " błędnych adresów" . '</b>';
				}
				$js = "if(this.nextSibling.style.display=='block'){this.nextSibling.style.display='none'}else{this.nextSibling.style.display='block'};return false;";
				echo '<button onclick="' . $js . '" style="margin:0 3px;padding:0;">' . "więcej" . '</button>';
				echo '<div style="display:none">';
				foreach ($miekszania as $m_id => $m_street) {
					if (in_array($m_id, $m_street_err)) {
						echo '<b style="color:red">' . "{$m_street}" . '</b>';
					} else {
						echo "{$m_street}";
					}
					echo " " . '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=23#EDIT/{$m_id}" . '">' . "Edytuj $m_id" . '</a>';
					echo '<br />';
				}
				echo '</div>';
			}
			echo '</td>';
		echo '</tr>';
	}
	if (!empty($mieszkania_rows)) {
		$bad_mieszkania_rows_cnt = count($mieszkania_rows);
echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;display:none;"> (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($mieszkania_rows);echo'</pre>';
		foreach ($mieszkania_rows as $b_street => $v_mieszkania) {
			echo '<tr>';
				echo '<td colspan="3" style="background:#FF9A9A;">' . '<b>' . ((empty($b_street))? "Brak danych" : $b_street) . '</b>' . '</td>';
				echo '<td colspan="3">';
					if (empty($v_mieszkania)) {
						echo '<b style="color:red" title="' . "Brak mieszkań na danym budynku!" . '">' . "(0)" . '</b>';
					} else {
						echo '<b>' . "(" . count($v_mieszkania) . ")" . '</b>';
						echo '<b style="color:red">' . " " . count($v_mieszkania) . " błędnych adresów" . '</b>';
						$js = "if(this.nextSibling.style.display=='block'){this.nextSibling.style.display='none'}else{this.nextSibling.style.display='block'};return false;";
						echo '<button onclick="' . $js . '" style="margin:0 3px;padding:0;">' . "więcej" . '</button>';
						echo '<div style="display:none">';
						foreach ($v_mieszkania as $m_id => $m_street) {
							echo '<b style="color:red">' . "{$m_street}" . '</b>';
							echo " " . '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=23#EDIT/{$m_id}" . '">' . "Edytuj $m_id" . '</a>';
							echo '<br />';
						}
						echo '</div>';
					}
				echo '</td>';
			echo '</tr>';
		}
	}
	echo '</tbody>';
	echo '</table>';

	echo '<p>' . "Podsumowanie:" . '<br />';
		if ($bad_mieszkania_rows_cnt > 0) {
			echo "$bad_mieszkania_rows_cnt błędnie przypisanych mieszkań";
		}
	echo '</p>';

	die('</body></html>');
}


class AddressCheck {

	var $_filters = array();

	function set_filters($filters) {
		$this->_filters = $filters;
	}

	function sort_mieszkania_callback($street_a, $street_b) {
		$a = array();
		$street_arr = explode('/', $street_a, 2);
		$nr_domu_a = intval( preg_replace('/[^0-9]/', '', end($street_arr)) );
		$b = array();
		$street_arr = explode('/', $street_b, 2);
		$nr_domu_b = intval( preg_replace('/[^0-9]/', '', end($street_arr)) );

		if ($nr_domu_a < $nr_domu_b) {
			return -1;
		} else if ($nr_domu_a > $nr_domu_b) {
			return 1;
		} else {
			return 0;
		}
	}

	function sort_buildings_callback($row_a, $row_b) {
		$a = array();
		$a['nr_domu'] = intval( preg_replace('/[^0-9]/', '', $row_a->S_ADDRESS_STREET) );
		$a['street_short'] = preg_replace('/[0-9]/', '', $row_a->S_ADDRESS_STREET);
		$b = array();
		$b['nr_domu'] = intval( preg_replace('/[^0-9]/', '', $row_b->S_ADDRESS_STREET) );
		$b['street_short'] = preg_replace('/[0-9]/', '', $row_b->S_ADDRESS_STREET);

		if ($a['street_short'] < $b['street_short']) {
			return -1;
		} else if ($a['street_short'] > $b['street_short']) {
			return 1;
		} else if ($a['nr_domu'] < $b['nr_domu']) {
			return -1;
		} else if ($a['nr_domu'] > $b['nr_domu']) {
			return 1;
		} else {
			return 0;
		}
	}

	function get_buildings() {
		$buildings = array();
		$db = DB::getDB();
		$sql_where = "b.`A_STATUS` not in ('DELETED')";
		$sql = "select
				b.`ID`
				, b.`A_STATUS`
				, b.`S_ADDRESS_STREET`
				, b.`P_ADDRESS_STREET`
				, b.`T_BUILDING_KLATKI`
				, b.`T_BUILDING_MIESZKANIA`
			from `BUILDINGS` as b
			where {$sql_where}
			order by b.`S_ADDRESS_STREET` ASC
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$r->S_ADDRESS_STREET = strtoupper($r->S_ADDRESS_STREET);
			$buildings [$r->ID]= $r;
		}
		uasort($buildings, array($this, 'sort_buildings_callback'));
		return $buildings;
	}

	function get_mieszkania() {
		$mieszkania = array();
		$db = DB::getDB();
		$sql = "select
				m.`ID`
				, m.`S_ADDRESS_STREET`
				, m.`T_TELBOX_BUILDING_IN`
			from `USERS2_MARKETING` as m
			where
				m.`A_STATUS` not in ('DELETED')
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$r->T_TELBOX_BUILDING_IN = strtoupper($r->T_TELBOX_BUILDING_IN);
			$r->S_ADDRESS_STREET = strtoupper($r->S_ADDRESS_STREET);
			$mieszkania [$r->T_TELBOX_BUILDING_IN] [$r->ID] = $r->S_ADDRESS_STREET;
		}
		foreach ($mieszkania as $b_street => $v_arr) {
			uasort($mieszkania[$b_street], array($this, 'sort_mieszkania_callback'));
		}
		return $mieszkania;
	}

}
