<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.php");
include('include/functions.php');

$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);

$action = $_GET['act'];


	switch($action){
		
		case 'insert_country':
			if($_POST['submit']){
				$db->execute("insert into country SET country_name = '".$_POST['country_name']."', country_description = '".$_POST['country_description']."', flag_id = '".$_POST['flag_id']."'");
			}
			header("location: country.php");
		break;


		case 'update_country':
			if($_POST['submit']){
				$db->execute("UPDATE country SET country_name = '".$_POST['country_name']."', country_description = '".$_POST['country_description']."', flag_id = '".$_POST['flag_id']."' WHERE country_id = '".(int)$_GET['lid']."'");
			}
			header("location: country.php");
		break;
		
		case 'delete':
			if($_GET['lid']){
				if(isset($_GET['confirm'])){
					$db->execute("DELETE FROM country WHERE country_id = '".(int)$_GET['lid']."'");
					header("location: country.php");
				}
			}
			
		break;


		case 'unjoin':
			if(isset($_GET['t2lid'])){
				if(isset($_GET['confirm'])){
					$db->execute("DELETE FROM team_to_country WHERE team_to_country_id = '".(int)$_GET['t2lid']."'");
					header("location: country.php?act=team_list&lid=".(int)$_GET['lid']);
				}
			}
			
		break;

		
		case 'team_list':
$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit = 20;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("SELECT t2l.team_to_country_id, t2l.win, t2l.loss, t2l.equal, t2l.vote, t.team_name FROM team_to_country t2l, team t where t2l.team_id = t.team_id and t2l.country_id = '".(int)$_GET['lid']."' ORDER BY t2l.vote DESC"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("موردی يافت نشد."); // bah, modify the "Not Found" error for your needs.
}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
		break;
		
		
		default:
$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit = 20;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("select * from country ORDER BY country_id DESC"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("موردی يافت نشد."); // bah, modify the "Not Found" error for your needs.
}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
		break;
	}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>مديريت چيتا سافت</title><link href="css/dropdown/dropdown.vertical.rtl.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/dropdown/themes/default/default.css" media="all" rel="stylesheet" type="text/css" />
<!--[if lt IE 7]>
<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="js/jquery/jquery.dropdown.js"></script>
<![endif]-->



<script type="text/javascript" src="js/js_body.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 5]>
<style>body {text-align: center;}#wrapper {text-align: left;}.menulist a {float: left; clear: both;}</style>
<![endif]-->
<!--[if lte IE 7]>
<style>#wrapper, #footer, #masthead, #column_wrapper {zoom: 1;}</style>
<![endif]-->
<style type="text/css">
.list th {background-color:#8080C0;color:#FFF;}
.list td {background-color:#DCDCED;}
</style>
</head>
<body id="p7bod" onload="P7_Uberlink('p7uberlink','p7bod')">

<div id="wrapper">

  <div id="masthead">
    <div class="content" align="center"> مديريت چيتا سافت </div>
  </div>
  
  
  <div id="column_wrapper">
    <div id="left">
      <div class="content">
<?php

	switch($action){
		
		case 'new_country':
			
			if(isset($_GET['lid'])){
				$form_action = "update_country&lid=".$_GET['lid'];
				$submit = " ويرايش ";
				$sql = $db->query("SELECT * FROM country WHERE country_id = '".(int)$_GET['lid']."'");
				$res = $db->fetchNextArray($sql);
			}else{
				$form_action = "insert_country";
				$submit = " ثبت ";
			}
?>
<form action="country.php?act=<?=$form_action?>" method="post" >
	<table width="600" border="0">
    	<tr>
        	<td>نام كشور: </td><td><input type="text" name="country_name" value="<?=$res['country_name']?>" /></td>
        </tr>
    	<tr>
        	<td> پرچم : </td>
            <td>
<?php
    echo '<select name="flag_id">';
    echo '<option value="">پرچم را انتخاب كنيد</option>';
    
    for($i = 0; $i <= 226; $i++) {
			if($res['flag_id'] == $i){
				$selected = ' selected="selected"';
			}else{
				$selected = '';
			}
            echo '<option value="'.$i.'" style="background:url(\'images/flags.gif\') no-repeat scroll 0 -'.(($i*24)+24).'px;padding-right:18px; padding-top:5px;padding-bottom:2px" '.$selected.'>&nbsp;</option>';
    }
    echo '</select>';
?>
		</td>
        </tr>
    	<tr>
        	<td>توضيحات: </td><td><textarea name="country_description"><?=$res['country_description']?></textarea></td>
        </tr>
        <tr>
        	<td></td><td><input type="submit" name="submit" value="<?=$submit?>" /></td>
        </tr>
    </table>
</form>
<?php
		break;


		case 'add_team':
			
			if(isset($_GET['lid'])){
				if(isset($_POST['tid'])){
					$db->execute("INSERT INTO team_to_country SET team_id = '".(int)$_POST['tid']."', country_id = '".(int)$_GET['lid']."'");
					echo 'تيم مورد نظر به كشور اضافه شد. <br />
							<a href ="country.php?act=add_team&lid='.(int)$_GET['lid'].'"> افزودن تيم جديد </a> | <a href ="country.php?act=team_list&lid='.(int)$_GET['lid'].'"> مشاهده همه تيمهای اين كشور </a> |
							<a href ="country.php"> مشاهده ليست كشورها </a>';
				}else{
					$sql = $db->query("SELECT * FROM country WHERE country_id = '".(int)$_GET['lid']."'");
					$res = $db->fetchNextArray($sql);
?>
<form action="country.php?act=add_team&lid=<?=(int)$_GET['lid']?>" method="post" >
	<table width="600" border="0">
    	<tr>
        	<td>نام كشور: </td><td><?=$res['country_name']?></td>
        </tr>
    	<tr>
        	<td>نام تيم: </td><td><select name="tid">
            					  		<?php
                                        $sql = $db->query("select * from team order by team_name");
										while($res = $db->fetchNextArray($sql)){
											echo '<option value="'.$res['team_id'].'">'.$res['team_name'].'</option>'."\n\t\t\t";
										}
										?>
                                  </select>
                             </td>
        </tr>
        <tr>
        	<td></td><td><input type="submit" name="submit" value=" ثبت تيم " /></td>
        </tr>
    </table>
</form>
<?php
				}
			}else{
				echo ' خطایی رخ داده. ';
			}

		break;


		case 'game_list':
			if(isset($_GET['lid'])){
				
				$sql = $db->query("SELECT t2l.team_id, t.team_name FROM team_to_country t2l, team t where t2l.team_id = t.team_id and t2l.country_id = '".(int)$_GET['lid']."'");
				while($res = $db->fetchNextArray($sql)){
					while($subres = $db->fetchNextArray($sql)){
						echo 'res = '.$res['team_name'].'res2 = '.$subres['team_name'].'<br />';
					}
				}
				
			}else{
				echo ' خطایی رخ داده. ';
			}
			
		break;
		
		
		case 'team_list':
			if(isset($_GET['lid'])){
				if(isset($_POST['submit'])){
					$db->execute("UPDATE team_to_country SET win = '".(int)$_POST['win']."', 
															loss = '".(int)$_POST['loss']."', 
															equal = '".(int)$_POST['equal']."', 
															in_goal = '".(int)$_POST['in_goal']."', 
															out_goal = '".(int)$_POST['out_goal']."', 
															vote = '".(int)$_POST['vote']."'
															 WHERE team_to_country_id = '".(int)$_GET['tid']."'");
				}
?>
<table width="100%" border="0">
 <tr>
  <td width="50%" align="right">
موارد يافت شده:  <b><?=$first?></b> تا <b><?=$last?></b> از <b><?=$numrows?></b> مورد
  </td>
  <td width="50%" align="left">
صفحه <b><?=$current?></b> از <b><?=$total?></b> صفحه
  </td>
 </tr>
 <tr>
  <td colspan="2" align="left">&nbsp;

  </td>
 </tr>
 <tr>
  <td colspan="2" align="left">
چند مورد نشان داده شود: <a href="<?=$PHP_SELF?>?act=team_list&lid=<?=(int)$_GET['lid']?>&query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?act=team_list&lid=<?=(int)$_GET['lid']?>&query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?act=team_list&lid=<?=(int)$_GET['lid']?>&query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?act=team_list&lid=<?=(int)$_GET['lid']?>&query=<?=$query?>&page=<?=$page?>&limit=50">50</a>
  </td>
 </tr>
</table>
<?
//Go back into PHP mode.
// Now we can display results.
$sql = $db->query("SELECT t2l.country_id, t2l.team_to_country_id, t2l.win, t2l.loss, t2l.equal, t2l.in_goal, t2l.out_goal, t2l.vote, t.team_name FROM team_to_country t2l, team t where t2l.team_id = t.team_id and t2l.country_id = '".(int)$_GET['lid']."' ORDER BY t2l.vote DESC  LIMIT $page, $limit");
?>
<table class="list" width="600" dir="rtl" style="margin-left:100px;" cellspacing="3">
	<tr>
    	<th> كد تبم </th><th> نام تيم </th><th> برد </th><th> تساوی </th><th> باخت </th><th> گل زده </th><th> گل خورده </th><th> امتياز </th><th> حذف </th></tr>
<?php
	$i = 1;
	while($data=$db->fetchNextArray($sql)){
?>
	<tr height="25" align="center">
    	<form action="country.php?act=team_list&lid=<?=$_GET['lid']?>&tid=<?=$data['team_to_country_id']?>" method="post">
    	<td><b> <?=$i?> </b></td>
        <td> <?=$data['team_name']; ?> </td>
        <td><input name="win" value="<?=$data['win']; ?>" size="3" /></td>
        <td><input name="equal" value="<?=$data['equal']; ?>" size="3" /></td>
        <td><input name="loss" value="<?=$data['loss']; ?>" size="3" /></td>
        <td><input name="out_goal" value="<?=$data['out_goal']; ?>" size="3" /></td>
        <td><input name="in_goal" value="<?=$data['in_goal']; ?>" size="3" /></td>
        <td><input name="vote" value="<?=$data['vote']; ?>" size="3" /></td>
        <td> <input name="submit" type="submit" value="بروزرسانی" /> <a href="country.php?act=unjoin&lid=<?=$data['country_id']; ?>&t2lid=<?=$data['team_to_country_id']; ?>" title="حذف"><img src="images/delete_icon.png" border="0" /></a></td>
        </form>
    </tr>
<?php
		$i++;
	}
?>
</table>
<p align="center" style="margin-top:20px;">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?act=team_list&lid=".(int)$_GET['lid']."&query=$query&page=$back_page&limit=$limit\" class=\"pages\">قبلي</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b class=\"pages_on\">".Convertnumber2farsi($i)."</b> \n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?qact=team_list&lid=".(int)$_GET['lid']."&uery=$query&page=$ppage&limit=$limit\" class=\"pages\">".Convertnumber2farsi($i)."</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo(" <a href=\"$PHP_SELF?act=team_list&lid=".(int)$_GET['lid']."&query=$query&page=$next_page&limit=$limit\" class=\"pages\">بعدی</a>\n");}
?>
</p>
<a href="country.php"> ليست كشورها </a>
<?php

			}else{
				echo ' خطایی رخ داده. ';
			}


		break;
		
		case 'delete':
			if($_GET['lid']){
				if(!isset($_GET['confirm'])){
					echo 'آيا شما مطمئنيد؟ <br /> <a href="country.php?act=delete&lid='.(int)$_GET['lid'].'&confirm"> بله </a> | <a href="country.php"> خير </a> ';
				}
			}
			
		break;
		
		case 'unjoin':
			if($_GET['t2lid']){
				if(!isset($_GET['confirm'])){
					echo 'آيا شما مطمئنيد؟ <br /> <a href="country.php?act=unjoin&t2lid='.(int)$_GET['t2lid'].'&lid='.(int)$_GET['lid'].'&confirm"> بله </a> | <a href="country.php?act=team_list&lid='.(int)$_GET['lid'].'"> خير </a> ';
				}
			}
			
		break;

		default:
?>
<table width="100%" border="0">
 <tr>
  <td width="50%" align="right">
موارد يافت شده:  <b><?=$first?></b> تا <b><?=$last?></b> از <b><?=$numrows?></b> مورد
  </td>
  <td width="50%" align="left">
صفحه <b><?=$current?></b> از <b><?=$total?></b> صفحه
  </td>
 </tr>
 <tr>
  <td colspan="2" align="left">&nbsp;

  </td>
 </tr>
 <tr>
  <td colspan="2" align="left">
چند مورد نشان داده شود: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5"><?=Convertnumber2farsi(5)?></a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10"><?=Convertnumber2farsi(10)?></a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20"><?=Convertnumber2farsi(20)?></a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50"><?=Convertnumber2farsi(50)?></a>
  </td>
 </tr>
</table>
<?
//Go back into PHP mode.

// Now we can display results.
$sql = $db->query("SELECT * FROM country ORDER BY country_id DESC LIMIT $page, $limit");
?>
<table class="list" width="600" dir="rtl" style="margin-left:100px;" cellspacing="3">
	<tr>
    	<th> كد كشور </th><th> نام كشور </th><th> توضبحات </th><th> ويرايش </th></tr>
<?php
	while($data=$db->fetchNextArray($sql)){
?>
	<tr height="25" align="center">
    	<td><b> <?php echo $data['country_id']; ?> </b></td>
        <td> <?php echo '<span style="background:url(\'images/flags.gif\') no-repeat scroll 0 -'.(($data['flag_id']*24)+24).'px; padding:2px 12px; float:right">&nbsp;</span> '.$data['country_name']; ?> </td>
        <td><?php echo $data['country_description']; ?></td>
        <td> <a href="country.php?act=delete&lid=<?php echo $data['country_id']; ?>" title="حذف"><img src="images/delete_icon.png" border="0" /></a> | 
        	<a href="country.php?act=new_country&lid=<?php echo $data['country_id']; ?>" title="ويرايش"><img src="images/confirm_32.png" border="0" /></a>
        </td>
    </tr>
<?php
}
?>
</table>
<p align="center" style="margin-top:20px;">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\" class=\"pages\">قبلی</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b class=\"pages_on\">".Convertnumber2farsi($i)."</b> \n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\" class=\"pages\">".Convertnumber2farsi($i)."</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\" class=\"pages\">بعدی</a>\n");}
?>
</p>
<a href="country.php?act=new_country"> اضافه كردن كشور جديد </a>
<?php
		break;
	}
?>
        
      </div>
    </div>
    
    
    <div id="right">
      <div class="content">
        <h4>منو </h4>
<?php include("include/header.php");?>
      </div>
    </div>
    
    <br class="clearfloat" />
  </div>
  
  <div id="footer"><?php include("include/footer.php");?>  </div>
  
</div>
</body>
</html>
<?php } ?>