<?php
include_once("../includes/global.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
//=======================================
if(!empty($_POST['senddata']) && $_POST['senddata']!="")
{
	$fieldname=$_POST['fieldname'];
	$fieldistop = $_POST['isdisplay'];
	$catid = !empty($_GET['catid'])?$_GET['catid']:$_POST['catid'];
	$table_name=$config['table_pre']."defind_".$catid;
	
	if(empty($_POST["PidValue"]))
	{
		$tsql = "select id from ".EXTENDFILE." where catid='$catid' and fieldName='".$fieldname."'";
		$db->query($tsql);
		$re = $db->fetchRow();
		if($re['id']>0)
		{
			echo "<div style=\"text-align:center; color:#F00;\">".lang_show('alert_chongfu')."<br>
			<a href='pro_info_add_field.php?cat_type=afield'>".lang_show('alert_fanhui')."</a></div>";
			exit();
		}
	}
	
	$posttemp=substr($fieldname,0,1);
	if(is_numeric($posttemp))
	{
		echo "<script>alert('".lang_show('alert_no_num')."');history.back();</script>";
		exit();
	}
	if($_POST['displayType']==0)
	{
		echo "<script>alert('".lang_show('alert_select_display_type')."');history.back();</script>";
		exit();
	}
	else
		$fielddisplaytype = $_POST['displayType'];
		
	$fielddisplay=$_POST["fieldDisplay"];
	$fieldisneed = $_POST['radio'];
	$fielddefaultvalue=$_POST['defaultvalue'];
	$fieldpro=$_POST['fieldpro'];
	$fieldlen=$_POST['fieldlen'];
	if(!is_numeric($fieldlen)&& $fieldlen!="")
	{
		echo "<script>alert('".lang_show('alert_input_num')."');history.back();</script>";
		exit();
	}
	$inputitem = "";
	if(!empty($_POST['inputselectvalue']))
	{
		if($_POST['inputselectvalue']=="")
			echo "<script>alert('".lang_show('alert_selectitem')."');history.back();</script>";
		else
			$inputitem=$_POST['inputselectvalue'];		
	}
	
	if(empty($_POST["PidValue"]))
	{
		$sql = "insert into ".EXTENDFILE." (fieldName,catid,fieldDisplay,fieldDes,IsNeed,defaultValue,fieldPro,fieldLen,IsDisplay,displayType,catItem,isSearch,module) values";
		$sql.=" ('".$fieldname."','$catid','".$fielddisplay."','$_POST[fieldDes]','".$fieldisneed."','".$fielddefaultvalue."','".$fieldpro."','".$fieldlen."','".$fieldistop."','".$fielddisplaytype."','".$inputitem."','$_POST[isSearch]','$_GET[module]')";
		
		//表字段调整
		$sql1="ALTER TABLE `".$table_name."` ADD `$fieldname` VARCHAR( $fieldlen ) NULL DEFAULT '$fielddefaultvalue';";
		$db->query($sql1);
		if(!empty($_POST['isSearch']))
		{	
			$sql2="ALTER TABLE `".$table_name."` ADD INDEX ( `$fieldname` )";
			$db->query($sql2);
		}
		if($db->query($sql))
			msg('add_field.php?id='.$catid.'&close=1');

	}
	else
	{
		$sql = "update ".EXTENDFILE." set fieldName='".$fieldname."',fieldDisplay='".$fielddisplay."',IsNeed='".$fieldisneed."',defaultValue='".$fielddefaultvalue."',fieldPro='".$fieldpro."',fieldLen='".$fieldlen."',IsDisplay='".$fieldistop."',displayType='".$fielddisplaytype."',catItem='".$inputitem."',isSearch='$_POST[isSearch]',fieldDes='$_POST[fieldDes]' where id=".$_POST["PidValue"];
		$db->query($sql);
	 	 
		 //表字段调整
		 if($_POST['oldFieldpro']!=$fieldpro)
		 {
			$sql="update $table_name set $_POST[oldName]='0'";
			$db->query($sql);
		 }
		 
		 $sql="ALTER TABLE `$table_name` CHANGE `$_POST[oldName]` `$fieldname` VARCHAR( $fieldlen ) NULL DEFAULT $fielddefaultvalue";
		 $db->query($sql);
		 
		 if(!empty($_POST['isSearch']))
		 {	
			$sql2="ALTER TABLE `$table_name` ADD INDEX ( `$fieldname` )";
			$db->query($sql2);
		 }
		 if($db->query($sql))
			msg('edit_field.php?id='.$_POST['catid'].'&module='.$_GET['module']);
	}
	
}


if(!empty($_GET['id']))
{
	$sql="select * from ".EXTENDFILE." where id='$_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 type="text/javascript" src="../script/Validator.js"></script>
</head>
<body onLoad="document.getElementById('fieldname').focus();">
<?php
if($_GET['close']==1)
{
?>
<script>
window.parent.closeWin('edit_field.php?id=<?php echo $_GET['id'];?>');
</script>
<?php } ?>
<div class="bigbox">
  <div class="bigboxbody">
    <form onSubmit="return Validator.Validate(this,3)" method="post" action="">
      <table width="100%" cellspacing="0" cellpadding="0" id="addfield" align="center">
        <tr>
          <td width="16%%" align="left"><?php echo lang_show('field_display_name');?></td>
          <td width="20%"><input dataType="Require" msg="<?php echo lang_show('beizhu_1');?>" class="text" name="fieldname" type="text" id="fieldname" value="<?php echo $de_cat['fieldName'];?>" ></td>
          <td class="bz"><?php echo lang_show('beizhu_1');?></td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_pro_');?></td>
          <td ><select class="select" name="fieldpro" id="fieldpro">
              <?php
			$tempPro = array ( 'varchar' => '字符(varchar)','int' => '整数(int)','text' => '大文本(text)','date' => '日期(date)','bool' => '布尔值(bool)',);
			foreach($tempPro as $pr=>$values){
				if($pr==$de_cat['fieldPro']){
					echo "<option value=\"".$pr."\" selected=selected >".$values."</option>";
				}else{
					echo "<option value=\"".$pr."\" >".$values."</option>";
				}
			}
		  ?>
            </select>
          </td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_len');?></td>
          <td><input dataType="Number" msg="<?php echo lang_show('field_len');?>" value="11" class="text" type="text" name="fieldlen" value="<?php if(!empty($de_cat['fieldLen'])) echo $de_cat['fieldLen'];?>"  id="fieldlen"></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_edit_display_name');?></td>
          <td><input dataType="Require" msg="<?php echo lang_show('display_page');?>" class="text" id="fieldDisplay" name="fieldDisplay" value="<?php echo $de_cat['fieldDisplay'];?>" type="text"></td>
          <td class="bz"><?php echo lang_show('display_page');?></td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_info_name');?></td>
          <td><input class="text" id="fieldDes" name="fieldDes" value="<?php echo $de_cat['fieldDes'];?>" type="text"></td>
          <td class="bz"><?php echo lang_show('inf_remarks');?></td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_display_type');?></td>
          <td><select class="select" name="displayType" id="displayType" onChange="InDefaultValue(this.value,'<?php echo lang_show('fieldselectvalue'); ?>','<?php echo lang_show('fieldeg');?>','<?php echo lang_show('egstr');?>');">
              <?php
			  $tempDisplay=lang_show('displayType');
              foreach($tempDisplay as $dt=>$values){
					if($dt==$de_cat['displayType'])
						echo "<option value=".$dt." selected=selected >".$values."</option>";
					else
						echo "<option value=".$dt." >".$values."</option>";
			  }
			   ?>
            </select>
          </td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_default_value');?></td>
          <td><input name="defaultvalue" type="text" class="text" id="defaultvalue" value="<?php if(!empty($de_cat['defaultValue'])) echo $de_cat['defaultValue'];?>" /></td>
          <td id="selectdisplay">&nbsp;</td>
        </tr>
        <tr id="adddefault">
          <td align="left"><?php echo lang_show('field_is_needfill');?></td>
          <td><?php
		$de_IsNeed=$de_IsNeed1=NULL;
		if($de_cat['IsNeed']!=""){
			if($de_cat['IsNeed']=="1"){
				$de_IsNeed="checked";
			}else{
				$de_IsNeed1="checked";
			}
		}else{
			$de_IsNeed="checked";
		}
		?>
            <input name="radio" type="radio" class="radio" id="Isneed" style="width: 20px;" value="1" <?php echo $de_IsNeed;?>>
            <?php echo lang_show('yes');?>
            <input type="radio" class="radio" name="radio" id="Isneed2" style="width: 20px;" value="0" <?php echo $de_IsNeed1;?>>
            <?php echo lang_show('no');?></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_is_seardisplay');?></td>
          <td colspan="2"><?php
		if($de_cat['isSearch']!=""){
			if($de_cat['isSearch']=="1"){
				$de_checked="checked";
			}else{
				$de_checked1="checked";
			}
		}else{
			$de_checked="checked";
		}
		?>
            <input name="isSearch" type="radio" class="radio" id="issearch" style="width: 20px;" value="1" <?php echo $de_checked;?>>
            <?php echo lang_show('yes');?>
            <input type="radio" class="radio" name="isSearch" id="issearch2" style="width: 20px;" value="0" <?php echo $de_checked1;?>>
            <?php echo lang_show('no');?></td>
        </tr>
        <tr>
          <td align="left"><?php echo lang_show('field_is_webdisplay');?></td>
          <td colspan="2"><?php
		$de_checked1=NULL;
		if($de_cat['IsDisplay']!=""){
			if($de_cat['IsDisplay']=="1"){
				$de_checked="checked";
			}else{
				$de_checked1="checked";
			}
		}else{
			$de_checked="checked";
		}
		?>
            <input name="isdisplay" type="radio" class="radio" id="isdisplay" style="width: 20px;" value="1" <?php echo $de_checked;?>>
            <?php echo lang_show('yes');?>
            <input type="radio" class="radio" name="isdisplay" id="isdisplay2" style="width: 20px;" value="0" <?php echo $de_checked1;?>>
            <?php echo lang_show('no');?></td>
        </tr>
        <tr>
          <td >&nbsp;</td>
          <td><input name="senddata" id="senddata" class="btn" type="submit" value="<?php echo lang_show('submit');?>">
            <input type="hidden" name="PidValue" id="PidValue" value="<?php if(!empty($_GET['id'])) echo $_GET['id'];?>">
			<input name="catid" type="hidden" value="<?php echo $de_cat['catid'];?>" />
			<input name="oldFieldpro" type="hidden" value="<?php echo $de_cat['fieldPro'];?>" />
			<input name="oldName" type="hidden" value="<?php echo $de_cat['fieldName'];?>" />
			</td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form>
  </div>
</div>
<script src="../script/jquery-1.4.4.min.js" language="javascript"></script>
<script src="../script/my_lightbox.js" language="javascript"></script>
<script language="javascript">
<?php 
if(!empty($_GET['id']))
{
?>
InDefaultValue('<?php echo $de_cat['displayType'];?>','<?php echo lang_show('fieldselectvalue'); ?>','<?php echo lang_show('fieldeg');?>','<?php echo lang_show('egstr');?>');
<?php
}
?>
//由lineid得到行号
function getlineNum(lineid)
{
	var j=0;
	for(i=0;i<document.getElementById('addfield').rows.length;i++)
	{
			if(document.getElementById('addfield').rows[i].id==lineid)
			{
				return j;
			}
			j++;
	}
	return -1;
}
//删除行
function delline(lineid)
{
	for(var i=addfield.rows.length-1;i>=0;i--)
	{
			if(addfield.rows[i].id.indexOf(lineid)!=-1)
			{
				addfield.deleteRow(i);
				break;
			}
	}
}
//values:选项值,info：生成行的数明文字
function InDefaultValue(values,info,display,egstr){
	var trobj = document.getElementById("selectdisplay");	
	if(values<=2){
		delline("InsertInto");
		trobj.innerHTML="";
	}else{
		if(getlineNum("InsertIntotemp")=="-1"){
			addListener();
			var tempob = new Object();
			tempob.catInfo=info+"：";
			tempob.catName="temp";
			tempob.infos = display;
			tempob.value = '<?php echo str_replace("\r\n","",$de_cat['catItem']);?>';
			var innum = getlineNum("adddefault");
			InsertRows("addfield",tempob,innum,null,"InsertInto");
			trobj.innerHTML=egstr;
		}
	}
}

//allsearchhead:Table名称,data：数据,addtype：在第几行插入新行,pro:属性,rowsnamepre:行Id前缀
function InsertRows(allsearchhead,data,addtype,pro,rowsnamepre){
	var newrows = document.getElementById("addfield").insertRow(addtype);
	newrows.id=rowsnamepre+""+data.catName;
	var cells = newrows.cells;
	for(var i=0;i<3;i++){
			var newcell = newrows.insertCell(cells.length);
			switch(i){
				case 0:
					newcell.height="24px";// 
					newcell.style.cssText="text-align:left;";
					newcell.innerHTML=data.catInfo;
				break;
				case 1:
					var tts = "<textarea class='text' name='inputselectvalue' cols='' rows='5'>"+data.value+"</textarea>";
						newcell.innerHTML=tts;
				break;
				case 2:
					newcell.innerHTML=data.infos+"<font color=#ff0000>其中“,|”为半角字符</font>";
				break;
			}
		}
}
function addListener(){
	var oodi = document.getElementById("senddata");
	if (window.attachEvent)
	{	//IE 的事件代
		oodi.attachEvent("onclick",function(){if(document.getElementById("inputselectvalue").value==""){alert("选项值不能为空");return false}return true;}); 
	}
	else
	{
		//其它浏览器的事件代码
		oodi.addEventListener("click",function(){if(document.getElementById("inputselectvalue").value==""){alert("选项值不能为空");return false}return true;},false);
	}
} 
</script>
</body>
</HTML>
