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

$colorpicker=1;

$Action='element.php';

if($_GET['id_spisok'])
	{
	$_SESSION['ses_s']['id_spisok']=intval($_GET['id_spisok']);
	$cat=mysql_query("SELECT * FROM spisok WHERE id_spisok='".$_SESSION['ses_s']['id_spisok']."'");
	$r_cat=mysql_fetch_array($cat);
	$_SESSION['ses_s']['level']=$r_cat['level'];
	$_SESSION['ses_s']['spisok']=$r_cat['spisok'];
	$_SESSION['ses_s']['color']=$r_cat['color'];
	}

if(!$_SESSION['ses_s']['id_spisok']) exit("Не определен список");

if ($_GET['id_el'] && $_GET['per'])
	{
	if ($_GET['per']==1) $per=1; else $per=-1;
	sort_num('element', 'id_element', 'num', 'element', intval($_GET['id_el']), $per, " WHERE id_spisok='".$_SESSION['ses_s']['id_spisok']."'");
	include('_svod_element.php');
	}


if ($_POST["del"] && $_POST["del_element"]) 
	{ 
	mysql_query ("DELETE FROM element WHERE id_element IN (".$_POST["del_element"].") ") or die(mysql_error());
	include('_svod_element.php');
	if($_SESSION['ses_s']['level']==2)
		{
		mysql_query ("DELETE FROM element_two WHERE id_element IN (".$_POST["del_element"].") ") or die(mysql_error());
		include('_svod_element_two.php');
		}
	echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
	exit();
	}


if ($_POST["edit"] && $_POST['id_element']) 
	{ 
	$element = htmlspecialchars(rtrim($_POST['element']));
	if(!$element) $err .='Укажите элемент списка';
	
	$color_fon = htmlspecialchars(trim($_POST['color_fon']));
	$color_text = htmlspecialchars(trim($_POST['color_text']));
	
	if($color_fon)
		{
		$color_fon=str_replace("#","",$color_fon);
		$color_fon="#".$color_fon;
		}
		
	if($color_text)
		{
		$color_text=str_replace("#","",$color_text);
		$color_text="#".$color_text;
		}
	
	if(!$err)
		{
		mysql_query("UPDATE element SET element='".$element."', color_fon='".$color_fon."', color_text='".$color_text."' WHERE id_element='".$_POST['id_element']."'");
		include('_svod_element.php');
		echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
		exit();
		}
	}

if ($_POST["add"]) 
	{ 
	$element = $_POST['element'];
	if(!$element) $err .='Укажите элемент списка<br>';
	$arr_elements=explode("\n", $element);

	if(!$err)
		{
		foreach($arr_elements as $arr_ln)
			{
			if(trim($arr_ln))
				{
				mysql_query("INSERT INTO element (id_spisok, element) VALUES ('".$_SESSION['ses_s']['id_spisok']."', '".htmlspecialchars(rtrim($arr_ln))."')");
				$nd++;
				}
			}
	
		include('_svod_element.php');
		echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
		exit();
		}
	}

$titl="Список: ".$_SESSION['ses_s']['spisok'];
require ('top.php'); 

echo '<TABLE width=100%><TR><TD valign=top width=40%>';

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

if ($_REQUEST['edit'] && $_REQUEST['id_element'])
	{
	$rm = mysql_query ("SELECT * FROM element WHERE id_element='".intval($_REQUEST['id_element'])."'");
	$rrm=mysql_fetch_array($rm);
	?>
	<form action="<? echo $Action; ?>" method="post" name="form_edit" >
		<table width="100%" cellspacing="0" cellpadding="0" >
			<tr>
				<td width="29%" valign="top">
					<table width="100%" cellspacing="1" cellpadding="1" >
						<tr>
							<td>
  							<input name="element" type="text" value="<? echo $rrm['element']; ?>" size="40">
							<?
							if($_SESSION['ses_s']['level']==1 && $_SESSION['ses_s']['color'])
								{
								?>
						<tr>
							<td>
							<input type="text" name="color_fon" id="colorpickerField1" size="10" value="<? echo $rrm['color_fon']; ?>"> Цвет фона
						<tr>
							<td>
							<input type="text" name="color_text" id="colorpickerField1" size="10" value="<? echo $rrm['color_text']; ?>"> Цвет текста
								<?
								}
								?>
					</table>
				<td width="21%" valign="top" >
  				<input class="submit"  type="submit" name="edit" value="Изменить">
 				<input name="id_element" type="hidden" value="<? echo $rrm['id_element']; ?>">
		</table>
	</form>
	<?
	}


if($_POST['paket_del'] && $_POST['ch'])
	{
	$del_element = implode(",",$_POST['ch']);
	echo '<table width=300 bordercolor=red border=3 cellSpacing=0 cellPadding=5><tr><td>
	<p>Вы действительно хотите удалить элементы под номерами <b>'.$del_element.'</b> ?</p>';
	?>
	<form action="<? echo $Action; ?>" method="post" name="form_del" >
 	<input name="del_element" type="hidden" value="<? echo $del_element; ?>">
 	<input class="submit" type="submit" name="del" value="ДА">
 	<input class="submit" type="submit" name="not" value="НЕТ">
	</form>
	<?
	echo '</td></tr></table>';
	}
	

if (!$_REQUEST['id_element'] && !$_POST['ch']) 
	{
	?>
	<form name="form_add" method="POST" action="<? echo $Action; ?>">
		<table width="100%" cellspacing="0" cellpadding="0" >
			<tr>
				<td>
				<textarea name="element" cols="40" rows="8"></textarea>
				<br>
				<input class="submit" type="submit" name="add" value="Добавить элемент списка 1-го уровня">
				<p><font color=green>Примечание:<br>Вы можете добавлять целым списком. Единственное условие - каждый элемент списка должет начинаться с новой строки</font></p>
		</table>
	</form>
	<? 
	} 


echo '</TD><TD valign=top>';

$r = mysql_query ("SELECT * FROM element WHERE id_spisok='".$_SESSION['ses_s']['id_spisok']."' ORDER BY num, element ASC") or die(mysql_error());
$nr = mysql_num_rows ($r);
if ($nr <1) echo "<p>Список пуст</p>";

if ($nr >0)
	{
	echo '<form action="'.$Action.'" method=post >';
	echo '<TABLE cellpadding="1" cellspacing="1" >';
	$i=0;
	while ($rr = mysql_fetch_assoc($r)) 
		{ 
		$i++;
		echo '<tr>';
		echo '<td width="30">';
		echo '<input type="checkbox" name=ch[] value="'.$rr['id_element'].'" ';
		if($_POST['ch'] && in_array($rr['id_element'],$_POST['ch'])) echo 'checked';
		echo ' >';
	
		echo '<td  width="30">';
		echo '<a href = "'.$Action.'?id_element='.$rr['id_element'].'&edit=1"><img hspace="2" src="../icon/edit.png" alt="Изменить название" border="0" ></a>';
	
		echo '<td  width="30">';
		if($i>1)
		echo ' <a title="Переместить на одну позицию вверх" href = "?id_el='.$rr['id_element'].'&per=1"><img border=0 src="../icon/up.gif"></a>';
		
		echo '<td  width="30">';
		if($i<$nr)
		echo ' <a title="Переместить на одну позицию вниз" href = "?id_el='.$rr['id_element'].'&per=2"><img border=0 src="../icon/down.gif"></a>';
		
		echo '<td>';
		if($_SESSION['ses_s']['level']==2) 
 		echo '<a href = "element_two.php?id_element='.$rr['id_element'].'">'.$rr['element'].'</a>'; 
		else
			{
			if($_SESSION['ses_s']['color'])
				{
				if($rr['color_fon'] && $rr['color_text'])
				echo '<font style="background-color:'.$rr['color_fon'].'; color:'.$rr['color_text'].'; ">'.$rr['element'].'</font>';
				else
				echo $rr['element'].' <font color=red>не указан цвет фона или текста</font>'; 
				}
			else
			echo $rr['element']; 
			}
		}  
	echo '</TABLE>';
	echo '<br>
	<input type="submit" class="submit" name="paket_del" value="Отмеченные удалить">';
	echo '</form>';
	}
echo '</TD></TR></TABLE>';
require ('bottom.php'); 
?>