<?php
/**
 * Edit all matchs for bet tables
 *
 * @package PHP Competition System
 * @author  Eric GALLENE <okrome@free.fr>
 * @link	http://www.phpcompet.com
 * @version $Id: edit_betlist.php,v 1.00 2006/10/26 19:47:13
 */

require_once './core/functions.php';
require_once './core/setupfunctions.php';


if ($save) {

	$user_id = $_COOKIE['PCSuserid'];
	
	$SELECT = 'SELECT '.TBL_BETTABLE.'.cost, '.TBL_BETTABLE.'.nbr_prono
	FROM '.TBL_BETTABLE.'
	WHERE '.TBL_BETTABLE.'.id = '.$id;				 
	$bettable = runSQL($SELECT);
	
	if ($user_id) {
		for ($x=1; $x<$bettable[0]['nbr_prono']+1; $x++) {
			$DELETE = 'DELETE FROM '.TBL_BET.' WHERE id_betlist=\''.${'betlist_'.$x}.'\' AND id_user=\''.$user_id.'\'';
			runSQL($DELETE);
			$INSERT = 'REPLACE INTO '.TBL_BET.' (id_betlist, pronostic, id_user) VALUES (\''.${'betlist_'.$x}.'\',\''.${'r_'.$x}.'\',\''.$user_id.'\')';
			$bet = runSQL($INSERT); 
		}
	}
	count_points($user_id,'-'.$bettable[0]['cost']);

	redirect('pronostics.php');
}

if ($cancel) {

	$user_id = $_COOKIE['PCSuserid'];
	
	$SELECT = 'SELECT '.TBL_BETTABLE.'.cost, '.TBL_BETTABLE.'.nbr_prono
	FROM '.TBL_BETTABLE.'
	WHERE '.TBL_BETTABLE.'.id = '.$id;				 
	$bettable = runSQL($SELECT);
	
	if ($user_id) {
		for ($x=1; $x<$bettable[0]['nbr_prono']+1; $x++) {
			$DELETE = 'DELETE FROM '.TBL_BET.' WHERE id_betlist=\''.${'betlist_'.$x}.'\' AND id_user=\''.$user_id.'\'';
			runSQL($DELETE);
		}
	}

	count_points($user_id, $bettable[0]['cost']);
	redirect('pronostics.php');
}


// get data (id may be empty on a empty database)
if (!empty($id))
{
	$SELECT = 'SELECT '.TBL_BETTABLE.'.title, '.TBL_BETLIST.'.id as betlist, '.TBL_BETTABLE.'.cost, '.TBL_BETTABLE.'.nbr_prono,  '.TBL_BETTABLE.'.date_deb AS check_deb_bet, DATE_FORMAT('.TBL_BETTABLE.'.date_deb,\'%d/%m/%y\') AS date_deb,  DATE_FORMAT('.TBL_BETTABLE.'.date_fin,\'%d/%m/%y\') AS date_fin,  '.TBL_BETTABLE.'.date_fin_bet AS check_fin_bet, DATE_FORMAT('.TBL_BETTABLE.'.date_fin_bet,\'%d/%m/%y\') AS date_fin_bet,
	'.TBL_BETTABLE.'.winprize, '.TBL_BETTABLE.'.winrank, '.TBL_BETTABLE.'.rankpercent, '.TBL_BETTABLE.'.paybet, '.TBL_BETWINNER.'.goodprono 
	FROM '.TBL_BETTABLE.'
	LEFT JOIN '.TBL_BETWINNER.' ON '.TBL_BETWINNER.'.id_bettable = '.TBL_BETTABLE.'.id
	JOIN '.TBL_BETLIST.' ON '.TBL_BETLIST.'.id_bettable = '.TBL_BETTABLE.'.id
	WHERE '.TBL_BETTABLE.'.id = '.$id;				 
	$bettable = runSQL($SELECT);
	

	if (strtotime($bettable[0]['check_deb_bet']) < time() && strtotime($bettable[0]['check_fin_bet']) > time()) {
		$openbet = 1;
	}
	$prize = $bettable[0]['winprize'];
	for ($x=1; $x<$bettable[0]['winrank']+1; $x++) {
		$winnertable[$x]['rank']  = ($bettable[0]['nbr_prono'] - $x)+1;
		$winnertable[$x]['prize'] =  $prize;
		$prize = ceil(($prize * $bettable[0]['rankpercent'])/100);
	}

	
	$user_id = $_COOKIE['PCSuserid'];
	
	$SELECT = 'SELECT '.TBL_USERCONFIG.'.value AS money FROM '.TBL_USERCONFIG.' WHERE '.TBL_USERCONFIG.'.user_id = '.$user_id.' AND opt=\'points\''; 
	$credits   = runSQL($SELECT);
	
	$SELECT = 'SELECT count('.TBL_BET.'.id) AS num FROM '.TBL_BET.'
			   JOIN '.TBL_BETLIST.' ON '.TBL_BETLIST.'.id = '.TBL_BET.'.id_betlist
				WHERE '.TBL_BET.'.id_user = '.$user_id.' AND '.TBL_BETLIST.'.id_bettable='.$id; 
	$betdone   = runSQL($SELECT);

	//Count bet grid maked
	$SELECT = 'SELECT DISTINCT ('.TBL_BET.'.id_user) AS count
				FROM '.TBL_BET.'
				JOIN '.TBL_BETLIST.' ON '.TBL_BETLIST.'.id = '.TBL_BET.'.id_betlist
				WHERE '.TBL_BETLIST.'.id_bettable = '.$id.'
				GROUP BY '.TBL_BET.'.id_user';
	$numbet   = runSQL($SELECT);

	$SELECT = 'SELECT count(P1.pronostic) AS num, pcs_betlist.id_match  
				FROM '.TBL_BETLIST.'
				LEFT JOIN '.TBL_BET.' AS P1 ON '.TBL_BETLIST.'.id = P1.id_betlist AND P1.pronostic LIKE "1"
				WHERE '.TBL_BETLIST.'.id_bettable = '.$id.'
				GROUP BY '.TBL_BETLIST.'.id_match
				ORDER BY '.TBL_BETLIST.'.id_match';
	$vic_bet   = runSQL($SELECT);

	$SELECT = 'SELECT count(P1.pronostic) AS num, pcs_betlist.id_match  
				FROM '.TBL_BETLIST.'
				LEFT JOIN '.TBL_BET.' AS P1 ON '.TBL_BETLIST.'.id = P1.id_betlist AND P1.pronostic LIKE "N"
				WHERE '.TBL_BETLIST.'.id_bettable = '.$id.'
				GROUP BY '.TBL_BETLIST.'.id_match
				ORDER BY '.TBL_BETLIST.'.id_match';
	$nul_bet   = runSQL($SELECT);
	
	$SELECT = 'SELECT count(P1.pronostic) AS num, pcs_betlist.id_match  
				FROM '.TBL_BETLIST.'
				LEFT JOIN '.TBL_BET.' AS P1 ON '.TBL_BETLIST.'.id = P1.id_betlist AND P1.pronostic LIKE "2"
				WHERE '.TBL_BETLIST.'.id_bettable = '.$id.'
				GROUP BY '.TBL_BETLIST.'.id_match
				ORDER BY '.TBL_BETLIST.'.id_match';
	$def_bet   = runSQL($SELECT);
		
		
	$SELECT = 'SELECT '.TBL_BETLIST.'.id, '.TBL_BET.'.pronostic, C1.shortname as dom, C2.shortname as ext, '.TBL_MATCHS.'.buts_dom, '.TBL_MATCHS.'.buts_ext, CO.shortname as compet
				FROM '.TBL_BETLIST.'
				LEFT JOIN  '.TBL_BET.' ON '.TBL_BET.'.id_betlist = '.TBL_BETLIST.'.id AND '.TBL_BET.'.id_user = '.$user_id.'
				JOIN '.TBL_MATCHS.' ON '.TBL_MATCHS.'.id = '.TBL_BETLIST.'.id_match
				JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
				JOIN '.TBL_COMPETITIONS.' AS CO ON CO.id = '.TBL_DAYS.'.id_compet
				JOIN '.TBL_CLUBS.' AS C1 ON '.TBL_MATCHS.'.id_candidate_dom = C1.id
				JOIN '.TBL_CLUBS.' AS C2 ON '.TBL_MATCHS.'.id_candidate_ext = C2.id 
				WHERE '.TBL_BETLIST.'.id_bettable ='.$id; 
	$res   = runSQL($SELECT);
	
    // is it editable/ copyable?
    $editable = localnet();
    $copyable = localnet();
    
    // multi-user permissions
    if ($config['multiuser'])
    {
        $smarty->assign('editable', check_permission(PERM_WRITE, $_COOKIE['PCSuserid']));
		if ($config['activecomments']) {
	    $smarty->assign('commentable', check_permission(PERM_MKCOMMENT, $_COOKIE['PCSuserid']));
		}
		if ($config['activegallery']) {
	    $smarty->assign('seegallery', check_permission(PERM_GALLERY, $_COOKIE['PCSuserid']));
		}
    }

}

$smarty->assign('id', $id);
$smarty->assign('vic_bet', $vic_bet);
$smarty->assign('nul_bet', $nul_bet);
$smarty->assign('def_bet', $def_bet);
$smarty->assign('numbet', count($numbet));
$smarty->assign('openbet', $openbet);
$smarty->assign('winnertable', $winnertable);
$smarty->assign('betdone', $betdone[0]['num']);
$smarty->assign('bettable', $bettable[0]);
$smarty->assign('points', $credits[0]['money']);

// prepare templates
tpl_page('detailview', $bettable[0]['title']);
tpl_mkbet($res);

// caching enabled?
if ($config['http_caching'])
{
    require_once('./core/httpcache.php');
    httpCacheCaptureStart();
}

// display templates
smarty_display('header.tpl');
smarty_display('actions_pronostics.tpl');
if (!$config['http_caching']) flush();
smarty_display('mkbet.tpl', $id);
smarty_display('footer.tpl');
// caching enabled?
if ($config['http_caching'])
{
    httpCacheOutput('mkbet'.$id, httpCacheCaptureEnd());
}

?>
