<?php
include_once('../lib/allchar.php');
//=============================================
$cat_table=OCAT;
$pic=empty($_POST['pic'])?NULL:$_POST['pic'];
$isindex=isset($_POST['isindex'])?1:0;
$_POST['brand']=empty($_POST['brand'])?NULL:implode(",",$_POST['brand']);
$_POST['ext_field_cat']*=1;
if(!empty($_POST['ext_field_cat']))
	$ext_table=$config['table_pre'].'defind_'.$_POST['ext_field_cat'];
else
	$ext_table='';
	
if(!empty($_POST["cat"])&&empty($_GET["id"]))
{
	if($_POST["pid"]==0)
	{
		$sql="select max(catid) as catid from $cat_table where catid<9999";
		$db->query($sql);
		$re=$db->fetchRow();
		$id=$re["catid"];
		if(!$id)
			$id=1000;
		else
			$id=substr($id*1000,0,4)+1;
	}
	else
	{
		$s=$_POST["pid"]."00";
		$b=$_POST["pid"]."99";
		$sql="select max(catid) as catid from $cat_table where catid>$s and catid<$b";
		$db->query($sql);
		$re=$db->fetchRow();
		$id=$re["catid"];
		if(!$id)
			$id=$_POST["pid"]."01";
		else
			$id=$id+1;
	}
	foreach(explode("\r\n",$_POST['cat']) as $catv)
	{
		if(!empty($catv))
		{
			$sql="insert into $cat_table (`catid`,`cat`,`nums`,`isindex`,`pic`,`brand`,ext_table,ext_field_cat,template) values 
				('$id','$catv','0','".$isindex."','$pic','$_POST[brand]','$ext_table','$_POST[ext_field_cat]','$_POST[template]')";
			$db->query($sql);
			$id+=1;
		}
	}
	admin_msg("module.php?m=offer&s=add_cat.php&aid=".$_POST['pid'],'填加成功');
}
//==================================================
if(!empty($_POST["cat"])&&!empty($_GET["id"]))
{	
	$ext_table=$config['table_pre'].'defind_'.$_POST['ext_field_cat'];
	$_POST['ext_field_cat']*=1;
	
	if($_POST['pid']!=0)
	{
		if($_POST["pid"]!==substr($_GET['id'],0,strlen($_GET['id'])-2))
		{
			$s=$_POST["pid"]."00";
			$b=$_POST["pid"]."99";
			$sql="select max(catid) as catid from $cat_table where catid>$s and catid<$b";
			$db->query($sql);
			$re=$db->fetchRow();
			$id=$re["catid"];
			if(!$id)
				$id=$_POST["pid"]."01";
			else
				$id=$id+1;
		}
		else
			$id=$_GET['id'];
		//编辑当前类别信息
		$sql="update $cat_table set catid='$id', cat='".$_POST['cat']."',isindex='".$isindex."' 
			,pic='$_POST[pic]',brand='$_POST[brand]',ext_table='$ext_table',ext_field_cat='$_POST[ext_field_cat]',template='$_POST[template]' where catid='".$_GET['id']."'";
		$re=$db->query($sql);
		//如果当前类别下面带有子类别把子类别一起移过去
		$s=$_GET['id']."00";
		$b=$_GET['id']."99";
		$sql="update $cat_table set catid=replace(catid,$_GET[id],$id) where catid>=$s and catid<=$b";
		$re=$db->query($sql);
	}
	else
	{	
		//当前编辑的类别为一级类别，无需操作ＩＤ号。
		$sql="update $cat_table set 
		cat='$_POST[cat]',isindex='$isindex',pic='$_POST[pic]',brand='$_POST[brand]',ext_table='$ext_table',ext_field_cat='$_POST[ext_field_cat]',template='$_POST[template]' where catid='$_GET[id]'";
		$re=$db->query($sql);
	}
	if($re)
	{	
		msg("module.php?m=offer&s=cat_list.php");
	}
}
if(!empty($_GET['id']))
{
	$sql="select * from $cat_table where catid='$_GET[id]'";
	$db->query($sql);
	$de_cat=$db->fetchRow();
}

?>
<!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>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
<script src="../script/my_lightbox.js" language="javascript"></script>
<script type="text/javascript" src="../script/jquery-1.4.4.min.js"></script>
</head>
<body onLoad="document.getElementById('cat').focus();">
<div class="bigbox">
  <div class="bigboxhead tab" style=" width:90%"> <span class="cbox on"><a href="#"><?php echo lang_show('pro_info_add_cat');?></a></span> <span class="cbox"><a href="module.php?m=offer&s=cat_list.php"><?php echo lang_show('pro_info_cat');?></a></span> </div>
  <div class="bigboxbody" style="margin-top:-1px;">
    <form method="post" action=""  enctype="multipart/form-data"  style="margin-top:0px;">
      <table width="100%" cellspacing="0" cellpadding="0" align="center">
        <tr>
          <td class="body_left" align="left" ><?php echo lang_show('cat_level');?></td>
          <td width="21%"><select class="select" name="pid" >
              <option value="0" style='font-weight:bold; font-size:14px;'><?php echo lang_show('type_level_1');?></option>
              <?php
				if(!empty($_GET["aid"]))
					$slectid=$_GET["aid"];
				else
				{
					if($_GET["id"]>999)
						$slectid=substr($_GET["id"],0,strlen($_GET["id"])-2);
				}
				$db->query("select catid,cat from $cat_table where catid<=9999 order by nums asc");
				$catList=$db->getRows();
				foreach($catList as $v)
				{
					if($v['catid']==$slectid)
						echo "<option style='color:red;font-weight:bold;padding-left:20px;' value=".$v['catid']." selected=selected >|_".$v['cat']."</option>";
					else
						echo "<option style='color:red;font-weight:bold;padding-left:20px;' value=".$v['catid']." >|_".$v['cat']."</option>";
					$s=$v['catid'].'00';
					$b=$v['catid'].'99';
					$sql="select catid,cat from $cat_table where catid<=$b and catid>=$s order by nums asc";
					$db->query($sql);
					$scat=$db->getRows();
					foreach($scat as $sv)
					{
						if($sv['catid']==$slectid)
							echo "<option style='font-weight:bold;padding-left:40px;' value=".$sv['catid']." selected=selected >|____".$sv['cat']."</option>";
						else
							echo "<option style='font-weight:bold;padding-left:40px;' value=".$sv['catid']." >|____".$sv['cat']."</option>";
						$s=$sv['catid'].'00';
						$b=$sv['catid'].'99';
						$sql="select catid,cat from $cat_table where catid<=$b and catid>=$s order by nums asc";
						$db->query($sql);
						$tcat=$db->getRows();
						foreach($tcat as $tv)
						{
							if($tv['catid']==$slectid)
								echo "<option style='padding-left:60px;' value=".$tv['catid']." selected=selected >|________".$tv['cat']."</option>";
							else
								echo "<option style='padding-left:60px;' value=".$tv['catid']." >|________".$tv['cat']."</option>";
						}

					}
				}
				?>
            </select></td>
          <td width="64%" style="color:#666666"><?php echo lang_show('check_cats');?></td>
        </tr>
        <?php if(!empty($_GET['id'])){ ?>
        <tr>
          <td  align="left"><?php echo lang_show('pro_type_name');?></td>
          <td><input class="text" name="cat" type="text" value="<?php if(!empty($de_cat["cat"])) echo $de_cat['cat'];?>">          </td>
          <td style="color:#666666"><?php echo lang_show('pro_type_name');?></td>
        </tr>
        <tr>
          <td align="left">Logo</td>
          <td><input id="pic" type="text" name="pic" class="text" value="<?php echo isset($de_cat['pic'])?$de_cat['pic']:NULL;?>">          </td>
          <td> [<a href="javascript:uploadfile('<?php echo lang_show('upload');?>LOGO','pic',22,22)"><?php echo lang_show('upload');?></a>] 
            [<a href="javascript:preview('pic');"><?php echo lang_show('preview');?></a>]
            [<a onclick="javascript:$('#pic').val('');" href="#"><?php echo lang_show('del');?></a>]</td>
        </tr>
		<tr>
          <td align="left"><?php echo lang_show('binding_template');?></td>
          <td><input id="template" type="text" name="template" class="text" value="<?php echo $de_cat['template'];?>"></td>
          <td></td>
        </tr>
		
        <?php } else{?>
        <tr>
          <td align="left"><?php echo lang_show('pro_type_name');?></td>
          <td><textarea name="cat" rows="8" class="text" id="cat"><?php if(!empty($de_cat["cat"])) echo $de_cat['cat'];?>
</textarea>          </td>
          <td style="color:#666666"><?php echo lang_show('pro_type_des');?></td>
        </tr>
        <?php } ?>
        <tr>
          <td  align="left"><?php echo lang_show('indexsh');?></td>
          <td><input style="width:20px;" <?php if(!empty($de_cat['isindex'])){?>checked="checked"<?php } ?> name="isindex" type="checkbox" class="checkbox" id="isindex" value="1"></td>
          <td style="color:#666666"><?php echo lang_show('checkisshow');?></td>
        </tr>
		<tr>
          <td  align="left"><?php echo lang_show('con_property');?></td>
          <td colspan="2">
		  <select class="select" name="ext_field_cat">
		  	<option value=""><?php echo lang_show('nocon_property');?></option>
			　<?php
			  $sql="select * from ".EXTENDCAT;
			  $db->query($sql);
			  $extre=$db->getRows();
			  foreach($extre as $v)
			  {
				if($de_cat['ext_field_cat']==$v['id'])
					$str=" selected";
				else
					$str="";
			  ?>
			  <option <?php echo $str;?> value="<?php echo $v['id'];?>" /><?php echo $v['catname'];?></option>
			  <?php } ?>
		  </select>
		  </td>
        </tr>
       
        <tr>
          <td align="left">&nbsp;</td>
          <td><input name="" class="btn" type="submit" value="<?php echo lang_show('submit');?>"></td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form>
  </div>
</div>
</body>
</HTML>
