<?
@session_start();
$base_path='../';
require_once('../connect/connect.php');
include_once('../lib/lib.php');
require('pdk.php');

if(isset($_SESSION['ses_s']['id_spisok'])) unset($_SESSION['ses_s']['id_spisok']);

$Action='spisok.php';

$spisok_arr[1]="Одноуровневый список";
$spisok_arr[2]="Двухуровневый";

if ($_POST['del'] && $_POST['del_spisoks']) 
{ 
$raz = mysql_query ("SELECT id_element FROM element WHERE id_spisok='".$_POST["del"]."'") or die(mysql_error());
$n_raz = mysql_num_rows($raz);
if($n_raz)
	{
	$so=" IN (0";
	while($r_raz=mysql_fetch_array($raz))
		{
		$so .=",";
		$so .= $r_raz['id_element'];
		}
	$so .=") ";

	mysql_query ("DELETE FROM element WHERE id_element ".$so) or die(mysql_error());
	mysql_query ("DELETE FROM element_two WHERE id_element ".$so) or die(mysql_error());
	}
	
mysql_query ("DELETE FROM spisok WHERE id_spisok='".$_POST['del']."'") or die(mysql_error());

if(file_exists("../cashe_n/_svod_element_".$_POST["del"].".php"))	
unlink("../cashe_n/_svod_element_".$_POST["del"].".php");

if(file_exists("../cashe_n/_svod_element_two_".$_POST["del"].".php"))	
unlink("../cashe_n/_svod_element_two_".$_POST["del"].".php");

echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
exit();
}


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "edit_spisok")) 
{ 
$spisok = trim($_POST['spisok']);
if(!$spisok) $err .='Укажите название списка<br>';
if ($_POST['color']) $color=1; else $color=0;

if(!$err)
	{
	if(!$color)
		{
		$_SESSION['ses_s']['id_spisok']=$_POST['id_spisok'];
		mysql_query("UPDATE element SET color_fon='', color_text='' WHERE id_spisok='".$_POST['id_spisok']."'");
		include('_svod_element.php');
		if(isset($_SESSION['ses_s']['id_spisok'])) unset($_SESSION['ses_s']['id_spisok']);
		}
	
	mysql_query("UPDATE spisok SET spisok='".$spisok."',
	color='".$color."'
	WHERE id_spisok='".$_POST['id_spisok']."'");
	echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
	exit();
	}
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) 
{ 
$spisok = trim($_POST['spisok']);
$level = $_POST['level'];
if ($_POST['color']) $color=1; else $color=0;

if(!$spisok) $err .='Укажите название списка<br>';
if(!$level) $err .='Укажите тип списка<br>';
	
if(!$err)
	{
	mysql_query("INSERT INTO spisok (spisok, level, color) VALUES ('".$spisok."', '".$level."', '".$color."')");
	echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
	exit();
	}
}

$titl="Списки подстановки";
require ('top.php'); 

if($err) echo '<p><font color=red>'.$err.'</font></p>';

if ($_GET['edit_spisok'])
{
$rm = mysql_query ("SELECT * FROM spisok WHERE id_spisok='".$_GET['edit_spisok']."'");
$rrm=mysql_fetch_array($rm);
?>
<form action="<? echo $Action.'?edit_spisok='.$_GET['edit_spisok']; ?>" method="post" name="edit_spisok" >
	<input name="id_spisok" type="hidden" value="<? echo $rrm['id_spisok']; ?>">
	<table><tr><td>
	Название списка
	<br>
	<input name="spisok" type="text" value="<? echo $rrm['spisok']; ?>" size="40">
	</td></tr></table>
	<?
	if($rrm['level']==1)
		{
		echo '<input type="checkbox" name="color" ';
		if($rrm['color']) echo 'checked';
		echo ' >  с цветовой гаммой<br>';
		}
	?>
	<input class="submit" type="submit" name="Submit" value="Изменить">
	<input type="hidden" name="MM_insert" value="edit_spisok">
</form>
<?
 
}

elseif($_GET['del_spisok'])
{
$del=intval($_GET['del_spisok']);
$rm = mysql_query ("SELECT * FROM spisok WHERE id_spisok='".$del."'");
$rrm=mysql_fetch_array($rm);

echo '<table width=250 bordercolor=red border=1 cellSpacing=0 cellPadding=5><tr><td>
<p>Вы действительно хотите удалить список <b>'.$rrm['spisok'].'</b> ?</p>';
?>
<form action="<? echo $Action; ?>" method="post" name="del_spisok" >
	<input name="del" type="hidden" value="<? echo $del; ?>">
	<input class="submit" type="submit" name="del_spisoks" value="ДА">
	<input class="submit" type="submit" name="not" value="НЕТ">
</form>
<?
echo '</td></tr></table>';
}

else {
?>
<form name="form" method="POST" action="<? echo $Action; ?>">
	<table><tr><td>
	Название списка
	<br>
	<input name="spisok" type="text" value="<? echo $spisok; ?>" size="40">
	</td><td>
	Тип списка
	<br>
	<select name="level" onChange="color_spis(this.value)" >
		<option value="0"  ></option>
		<? foreach($spisok_arr as $index=>$sp_a) { ?>
		<option value="<?=$index?>" <? if($level==$index) echo 'selected'; ?> ><?=$sp_a?></option>
		<? } ?>
	</select>
	</td></tr></table>
	<span id="color_check"></span>
	<input class="submit" type="submit" name="Submit" value="Добавить">
	<input type="hidden" name="MM_insert" value="form">
</form>
<? } 

$r = mysql_query ("SELECT * FROM spisok ORDER BY spisok") or die(mysql_error());
$nr = mysql_num_rows ($r);

?>	
		
<p><b><? if ($nr <1) echo "Перечень списков пуст";?></b></p>
<? 
if ($nr >0)
{
$i=0;
echo '<TABLE width="100%" style="BORDER-COLLAPSE: collapse" cellpadding="3" cellspacing="3" border="1" bordercolor="#E2E2E2">';
	echo '<tr align=center>';
	echo '<td>';
	echo '<b>Название списка</b>';  
	echo '<td>';
	echo '<b>Тип списка</b>';  
	echo '<td>';
	echo '<td>';
	echo '<td>';

	while ($rr = mysql_fetch_assoc($r)) 
	{ 
	$i++;
	echo '<tr>';
	echo '<td>';
	echo $rr['spisok'];  
	
	echo '<td align=center >';
	if($rr['color'])
	echo '<font color=red>'.$spisok_arr[$rr['level']].'</font>'; 
	else 
	echo $spisok_arr[$rr['level']];  
	
	echo '<td width="5%">';
	echo '<a href = "element.php?id_spisok='.$rr['id_spisok'].'"><img hspace="2" src="../icon/search.png" alt="" border="0" ></a>';
	
	echo '<td width="5%">';
	echo '<a href = "'.$Action.'?edit_spisok='.$rr['id_spisok'].'"><img hspace="2" src="../icon/edit.png" alt="Изменить название" border="0" ></a>';
	echo '<td  width="5%">';
 	echo '<a href = "'.$Action.'?del_spisok='.$rr['id_spisok'].'"><img hspace="2"  src="../icon/del.png" alt="Удалить" border="0" ></a>'; 
	
	}  
echo '</TABLE>';
}

require ('bottom.php'); 
?>
<script type="text/javascript">
function color_spis(a)
	{
	if(a==1)
	var b = '<input type="checkbox" name="color"> с цветовой гаммой<br>';
	else if (a==2)
	var b = '';
	document.getElementById('color_check').innerHTML = b; 
	}
</script>