<?php
error_reporting(0);
$oForm = new cForm();
$oFunctions = new cFunctions($this->oDb, $this->oUrl, $this->oSession, $oForm);

$ret = "";

$ret.= '<form method="post" action="'.$this->oUrl->href().'" enctype="multipart/form-data"'.'>'."\n";

$sql= 'SELECT *
		FROM users
		WHERE id
		IN (

		SELECT trening.trener_id
		FROM `trening`
		WHERE trening.trener_id
		IN (

		SELECT id
		FROM `users`
		WHERE klub_id =?
		)
		ORDER BY `trening`.`datum` ASC
		)
		';

$q= $this->oDb->queryWithBids($sql, array($_SESSION['klubId']));
$hraci=$q->fetchAll();

$ret .= tableHead("<h1> Aktívny tréneri našeho klubu <h1> " ,Array(
		"Meno","priezvisko","Vek", "poc. tréningov", "zaplatit"
), false);

foreach ($hraci as &$hrac){
	$sql='SELECT *
			FROM `trening`
			WHERE trening.trener_id =? AND `trening`.`vymazany`=0';


	$q= $this->oDb->queryWithBids($sql, array($hrac['id']));

	$bR = $q->fetchAll();
	//pocet jeho treningov
	$pocetT= count($bR);

	$ret.= '<tr>'."\n";
	$ret .= '<td>'.$hrac['meno']. '</td>'."\n";
	$ret .= '<td>'.$hrac['priezvisko']. '</td>'."\n";
	$ret .= '<td>'.$hrac['vek']. '</td>'."\n";
	$ret .= '<td>'.$pocetT. '</td>'."\n";
	$ret .= '<td>';
	$ret .= $oForm->checkBox("","zaplatit".$hrac['id'],1);
	foreach ($bR as &$a){
		$ret .= $oForm->hiddenF("treningy".$hrac['id']."[]", $a['id']);
		$ret .= $oForm->hiddenF("cas".$hrac['id']."[]", $a['trvanie']);
	}
	$ret .= '</td>'."\n";

	$ret.= '</tr>'."\n";
}
unset($hrac);

$ret .= '</table>'."\n";
$ret .= 'Sadzba za';
$ret .= '<input type="radio" name="ako" id="sadzbaH" value="hod">  hodinu';
$ret .= '<input type="radio" name="ako" id="sadzbaT" value="trening"> trening';
$ret .= $oForm->inputBox("Sadzba", "sadzba", 164);
$ret .= '<div id="vypocet"></div>';
$ret .= $oForm->submitButton("vyplat", "Eviduj vyplatenie", False, "vyplat");
$ret .= '</form>'."\n";


if (isset($_POST['vyplat']) && !empty($_POST['vyplat']) && isset($_POST['sadzba']) && !empty($_POST['sadzba'])
		&& isset($_POST['ako']) && !empty($_POST['ako'])
		&& 	je_cislo($_POST['sadzba'])
)
{
	$vymaz= '( ';
	foreach ($hraci as &$hrac){

		if (!isset($_POST['zaplatit'.$hrac['id']]) || $_POST['zaplatit'.$hrac['id']] != 1) continue;
		$jeho= $_POST['treningy'.$hrac['id']];
		foreach ($jeho as &$jeden){
			$vymaz .=  $jeden.', ';
		}
	}
	$vymaz= trim($vymaz, ", ");
	$vymaz .= ' )';
	unset($hrac);

	$sql= 'UPDATE `trening` SET vymazany=1  WHERE id in '.$vymaz;
	if ($vymaz=='( )') 
		$ret = '<h2>Označený tréneri nemajú odtrénovaný ziadny tréning!</h2>'.$ret;
	else {
		$this->oDb->queryWithBids($sql, array() );
		$ret ='<h2>Tréningy označené ako zaplatené</h2><a href="javascript:history.go(-1);"><i id="backTrigger" class="icon-caret-left icon-3x"></i></a>';
	}
		
}




/*
 * TODO vyber casu na selectenie ktore trenigy zaplatit zaplatit
*/
echo $ret;

function tableHead($caption, $array, $operations = TRUE) {
	$zebraCounter = 0;
	$string = '<table cellspacing="1" cellpadding="0">'."\n";
	if (strlen($caption) > 0)
		$string .= '		<caption>'.$caption.'</caption>'."\n";
	$string .= '	<tr>'."\n";
	for ($i=0;$i<count($array);$i++)
		$string .= '		<th>'.$array[$i].'</th>'."\n";
	if ($operations)
		$string .= '		<th></th>'."\n";
	$string .= '	</tr>'."\n";
	return $string;
}

function je_cislo($text) {
	return preg_match("/^[0-9]{1,}$/", $text );
}

?>

<script>

$(function() {

	

	$('form').change(function() {
		str= '';
		kontrolaCislo('#sadzba');
		$('#treningDetail').remove();
		if ($('input[name=ako]:checked').val() && $('#sadzba').val()){

			$.each($('form table tr').next(), function(index, value) {

				//ci je oznaceny na platenie inak ho nevypisuj
				if (!$($($(this).children()[4]).children()[1]).is(':checked')) return;
				//meno
				//console.debug($($(this).children()[0]).html());

				//priezvisko
				//console.debug($($(this).children()[1]).html());

				//pocet treningov
				//console.debug($($(this).children()[3]).html());
		
				
				//sadzba za trening
				if ($('#sadzbaT').is(':checked')) 
					suma= $($(this).children()[3]).html()*$('#sadzba').val();

				//sadzba za hodinu
				if ($('#sadzbaH').is(':checked')){
					i=0
					pocHodin=0;
					$.each($($($(this).children()[4])).find('.nodisp'), function(index, value) {
						if (i%2==1)
							pocHodin+=parseInt($(this).val());
						i++;
						});
					suma= pocHodin*$('#sadzba').val();
				}

				
					str +='Vyplatit: <h2>'+$($(this).children()[0]).html()+', '+$($(this).children()[1]).html()+'</h2><br> ';
					str += 'Suma dokopy: <h2>'+suma+'&euro;</h2> <br> <br>';
				
				});
			
			
			if (suma != 0) {
			$('#vypocet').append('<div id="treningDetail" >'+str+'</div> ');
			}
		}
		
		 
		});
	
	});

</script>
