<?php
include_once("../includes/global.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
//==================================================
if(!empty($_POST['delsel'])&&$_POST['delsel']==lang_show('delete'))
{
	if(!empty($_POST["checkhr"])&&is_array($_POST["checkhr"]))
	{
		foreach($_POST["checkhr"] as $id)
		{
			$table_name=$config['table_pre']."defind_".$id;
			$db->query("delete from ".EXTENDCAT." where id='$id'");//删除分类
			$db->query("delete from ".EXTENDFILE." where catid='$id'");//删除字段
			$db->query("DROP TABLE `$table_name`");//删除生成的表
		}	
		msg("add_field_cat.php");
		exit();
	}
}
if(!empty($_GET["action"])&&$_GET["action"]=="del"&&!empty($_GET["id"]))
{
	$table_name=$config['table_pre']."defind_".$_GET['id'];
	$db->query("delete from ".EXTENDCAT." where id='$_GET[id]'");//删除分类
	$db->query("delete from ".EXTENDFILE." where catid='$_GET[id]'");//删除字段
	$db->query("DROP TABLE `$table_name`");//删除生成的表
}
//===================================================
if(!empty($_POST['resort']))
{
    if(is_array($_POST["sort"]))
	{
	   foreach($_POST['sort'] as $key=>$v)
	   {
		  $msql="update ".EXTENDCAT." set sort='".$v."' where id=".$key;
		  $db->query($msql); 
	   }
	}
}
//===================================================
if(!empty($_POST["hraction"])&&!empty($_GET['id']))
{		
	$sql="update ".EXTENDCAT." set catname='$_POST[catname]' where id='$_POST[catid]'";
	$db->query($sql);
	msg("add_field_cat.php");
	exit();
}
if(!empty($_POST["hraction"]))
{
	$sql="insert into ".EXTENDCAT." (`catname`) values ('$_POST[catname]')";
	$db->query($sql);
	$id=$db->lastid();
	
	creat_table($id);
	
	msg("add_field_cat.php");
	exit();
}
//=================================================
function creat_table($id)
{
	global $db,$config;
	$table_name=$config['table_pre']."defind_".$id;
	$csql="
	CREATE TABLE `".$table_name."` (
	  `id` int(11) NOT NULL auto_increment,
	  `infoid` int(11) unsigned default NULL,
	  `infotype` varchar(30) default NULL,
	  `userid` int(11) unsigned default NULL,
	  `color` varchar(30) default NULL,
	  `size` varchar(30) default NULL,
	   PRIMARY KEY  (`id`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
	";
	$db->query($csql);
	$sql="ALTER TABLE `$table_name` ADD INDEX ( `infoid`,`infotype` )";
	$db->query($sql);
	//----------------------初始默认字段
	$sql="INSERT INTO `".EXTENDFILE."` VALUES ( 0,'color', '颜色', '请选择颜色', 0, '', 'varchar', 10, 1, 5, '1|red,2|blue', 0, $id, '', 1)";
	$db->query($sql);
	$sql="INSERT INTO `".EXTENDFILE."` VALUES ( 0,'size', '尺码', '请选择颜色', 0, '', 'varchar', 10, 1, 5, '1|sl,2|xl', 0,$id, '', 1)";
	$db->query($sql);
	//----------------------
}
//===============================================
$sql="select * from ".EXTENDCAT." order by sort";
$db->query($sql);
$hr=$db->getRows();

if(!empty($_GET['id']))
{
	$sql="select * from ".EXTENDCAT." where id='$_GET[id]'";
	$db->query($sql);
	$mn=$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" />
</HEAD>
<body>
<script type="text/javascript">
function checkall()
{
	 for(var j = 0 ; j < document.getElementsByName("checkhr[]").length; j++)
	 {
	  	if(document.getElementsByName("checkhr[]")[j].checked==true)
	  	  document.getElementsByName("checkhr[]")[j].checked = false;
		else
		  document.getElementsByName("checkhr[]")[j].checked = true;
	 }
}
</script>
<form name="form" method="post">
  <div class="bigbox">
    <div class="bigboxhead"><?php echo lang_show('new_attributes');?></div>
    <div class="bigboxbody">
      <table width="98%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="60"><?php echo lang_show('name');?></td>
          <td width="50"><input type="hidden" name="catid" value="<?php if (!empty($_GET["id"])) echo $_GET["id"];?>">
            <input name="catname" type="text" class="text" id="catnmae" value="<?php 
			 if(!empty($_GET["action"])&&$_GET["action"]=="modify"&&!empty($_GET["id"]))
			  {
				echo $mn['catname'];
			  }
			 ?>" size="30" />
          </td>
          <td align="left"><input class="btn" type="submit" name="hraction" value="<?php echo lang_show('Submit');?>" /></td>
        </tr>
      </table>
    </div>
  </div>
</form>
<div style="float:left; height:20px; width:50%;">&nbsp;</div>
<form action="" method="post">
  <div class="bigbox">
    <div class="bigboxhead"><?php echo lang_show('been_attributes');?></div>
    <div class="bigboxbody">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr class="theader">
          <td width="29" ><input type="checkbox" class="checkbox" name="checkbox" id="checkbox" onClick="checkall()"></td>
          <td width="124" ><?php echo lang_show('none');?></td>
          <td width="252" ><?php echo lang_show('name');?></td>
          <td width="487" ><?php echo lang_show('attributes_num');?></td>
          <td width="81" align="center" ><?php echo lang_show('actit');?></td>
        </tr>
        <?php
	      foreach ($hr as $v)
          {
		  $sql="select count(*) as num from ".EXTENDFILE." where catid='$v[id]'";
		  $db->query($sql);
		  $nums=$db->fetchField('num');
        ?>
        <tr>
          <td valign="top"><input type="checkbox" class="checkbox" name="checkhr[]" value="<?php echo $v['id'];?>"></td>
          <td valign="top"><input name="sort[<?php echo $v['id'];?>]" id="sort[<?php echo $v['id'];?>]" type="text" value="<?php echo $v['sort'];?>" size="5" maxlength="3"></td>
          <td><b><?php echo $v['catname'];?></b> </td>
          <td><?php echo $nums;?></td>
          <td align="center" valign="top">
		  <a href="add_field_cat.php?action=modify&id=<?php echo $v['id'];?>"><?php echo $editimg;?></a>
		  <a href="add_field_cat.php?action=del&id=<?php echo $v['id'];?>"> <?php echo $delimg;?></a>
		  <a href="edit_field.php?id=<?php echo $v['id'];?>"> <?php echo $setimg;?></a>
		  </td>
        </tr>
        <?php
		  }
			?>
        <tr>
          <td colspan="5" align="left" ><input class="btn" type="submit" name="delsel" id="delsel" value="<?php echo lang_show('delete');?>">
            <input class="btn" type="submit" name="resort" id="resort" value="<?php echo lang_show('none');?>"></td>
        </tr>
      </table>
    </div>
  </div>
</form>
</body>
</html>
