<?php
/***********************************************************************************/
/* MassCRM客户关系管理系统							   							   */
/* ========================      						  						   */
/* 欢迎使用 MassCRM ,在您开始使用之前,请务必完整阅读本软件附带的全部说明档	  		   */
/***********************************************************************************/
/* Welcome to http://www.massboxes.com & http://masscrm.massboxes.com		  	   */
/***********************************************************************************/
	require_once "../includes/check_login.inc.php";
	require_once "../includes/config.inc.php";
	require_once "../includes/common.inc.php";
	require_once "../includes/function.inc.php";
	require_once"../includes/tree.inc.php";
	require_once"../includes/BiaoCommon.class.php";
	
	global $boxesDB;
	global $table_name; 
	global $auto_id;
	global $auto_id_value;
	$boxesDB=new BiaoCommon($conn); 
	
	$table_name		= $_REQUEST[table_name];
	$module_name	= $_REQUEST[module_name];
	$auto_id_value=$_REQUEST[the_id];
	
	$boxesDB->setTableName("field_display_table");
	$sql="where table_name='".$table_name."'";
	$boxesDB->setCondition($sql);
	$boxes_check=$boxesDB->selectResult();
	if(!count($boxes_check)){
		echo"<script>alert(\"表field_display_table设置出问题啦！\");</script>";
		exit;
	}
	
	$prefix=$boxes_check[0][prefix];
	$auto_id=$prefix."auto_id";
	$id=$prefix."id";
	$name=$prefix."name";
	
	if($table_name=="boxes_discount"){
		$no1_return_value="bd_value";
		$no2_return_value="bd_name";
	}
	else{
		$no1_return_value=$prefix."name";
		$no2_return_value=$prefix."name";
	}

	/***********************************************/
	/*                  增加动作                   */
	/**********************************************/
	if($_POST[add_confirm]){

		$error="";
		
		$boxesDB->setTableName("field_display_table");
		$boxesDB->setSelectField("*");
		$sql="where (table_name='$table_name' and admin_is_display='1') order by admin_display_number asc";
		$boxesDB->setCondition($sql);
		$boxes_check_list=$boxesDB->selectResult();
		
		for($j=0;$j<count($boxes_check_list);$j++){
			$row=$boxes_check_list[$j];
			
			if(!$row[can_be_empty] and !$_POST[$row[field_name]]){
				$error=$row[display_name]."不能为空！";
				echo"<script>alert(\"保存失败：".$row[display_name]."不能为空！\");</script>";
				break;
			}
			
			if($row[field_type]=="isAUTO"){
				if($_POST[parent_id]){
					$boxesDB->setTableName($table_name);
					$boxesDB->setSelectField("*");
					$sql="where parent_id=".$_POST[parent_id]." order by $row[field_name] desc";
					$boxesDB->setCondition($sql);
					$boxes_temp=$boxesDB->selectResult();
					$number=count($boxes_temp);
					if(!$number){
						$sql="where $auto_id=".$_POST[parent_id]." order by $row[field_name] desc";
						$boxesDB->setCondition($sql);
						$boxes_temp=$boxesDB->selectResult();
						$field_value=$boxes_temp[0][$id]."-01";
					}
					else{
						$field_value=substr($boxes_temp[0][$id],strrpos($boxes_temp[0][$id],"-")+1,2)+1;
						if($field_value<10)
							$field_value="0".$field_value;

							
						$sql="where $auto_id=".$_POST[parent_id]." order by $row[field_name] desc";
						$boxesDB->setCondition($sql);
						$boxes_temp=$boxesDB->selectResult();
						$field_value=$boxes_temp[0][$id]."-".$field_value;
					}
				}
				else{
					$boxesDB->setTableName($table_name);
					$boxesDB->setSelectField("*");
					$sql="order by $row[field_name] desc";
					$boxesDB->setCondition($sql);
					$boxes_temp=$boxesDB->selectResult();
					$result_temp=$boxes_temp[0];
					
					$field_value=substr($boxes_temp[0][$id],strrpos($boxes_temp[0][$id],"-")+1,2)+1;
					if($field_value<10)
						$field_value="AUTO-0".$field_value;
					else
						$field_value="AUTO-".$field_value;
				}
			}
			else{
				$field_value=$_POST[$row[field_name]];
			}
			
			if(!$row[can_be_same] and $field_value){//判断是否可以相同
				$boxesDB->setTableName($table_name);
				$boxesDB->setSelectField("Count(*) AS total");
				$sql="where ".$row[field_name]."='".$field_value."'";
				$boxesDB->setCondition($sql);
				$boxes_temp=$boxesDB->selectResult();

				if($boxes_temp[0][total]){
					$error=$row[display_name]."字段不能重复！";
					echo"<script>";
					echo"alert(\"增加失败：已经存在重复".$row[display_name]."资料！\");";
					echo"</script>";
					break;
				}
				
			}
			$add_array[] = array($row[field_name],$field_value);
		}
		if(!$error){
			add_row($table_name,$add_array);
			
			//找到最新加入的数据,显示
			$boxesDB->setTableName($table_name);
			$boxesDB->setSelectField("*");
			$sql="order by $auto_id desc";
			$boxesDB->setCondition($sql);
			$boxes_temp=$boxesDB->selectResult();
			$auto_id_value=$boxes_temp[0][$auto_id];
			
			
			//关闭窗口
			echo"<script>";
			echo"alert('增加成功，按确定返回！');";
			echo"var value=new Array('".$boxes_temp[0][$no1_return_value]."','".$boxes_temp[0][$no2_return_value]."');";
			echo"window.returnValue = value;";
			echo"self.close();";
			echo"</script>";

		}
		else{
			$submit=1;
		}
	}
	
	
	/***********************************************/
	/*                  修改动作                   */
	/**********************************************/
	if($_POST[update_confirm]){

		$error="";
		
		$boxesDB->setTableName("field_display_table");
		$boxesDB->setSelectField("*");
		$sql="where (table_name='$table_name' and admin_is_display='1') order by admin_display_number asc";
		$boxesDB->setCondition($sql);
		$boxes_check_list=$boxesDB->selectResult();
		
		for($j=0;$j<count($boxes_check_list);$j++){
			$row=$boxes_check_list[$j];
			
			if(!$row[can_be_empty] and !$_POST[$row[field_name]]){
				$error=$row[display_name]."不能为空！";
				echo"<script>alert(\"保存失败：".$row[display_name]."不能为空！\");</script>";
				break;
			}
			
			if($row[field_type]=="isAUTO"){
				if($_POST[parent_id]){
					$boxesDB->setTableName($table_name);
					$boxesDB->setSelectField("*");
					$sql="where parent_id=".$_POST[parent_id]." order by $row[field_name] desc";
					$boxesDB->setCondition($sql);
					$boxes_temp=$boxesDB->selectResult();
					$number=count($boxes_temp);
					if(!$number){
						$sql="where $auto_id=".$_POST[parent_id]." order by $row[field_name] desc";
						$boxesDB->setCondition($sql);
						$boxes_temp=$boxesDB->selectResult();
						$field_value=$boxes_temp[0][$id]."-01";
					}
					else{
						$field_value=substr($boxes_temp[0][$id],strrpos($boxes_temp[0][$id],"-")+1,2)+1;
						if($field_value<10)
							$field_value="0".$field_value;
							
						$sql="where $auto_id=".$_POST[parent_id]." order by $row[field_name] desc";
						$boxesDB->setCondition($sql);
						$boxes_temp=$boxesDB->selectResult();
						$field_value=$boxes_temp[0][$id]."-".$field_value;
					}
				}
				else{
					$boxesDB->setTableName($table_name);
					$boxesDB->setSelectField("*");
					$sql="order by $row[field_name] desc";
					$boxesDB->setCondition($sql);
					$boxes_temp=$boxesDB->selectResult();
					
					$field_value=substr($boxes_temp[0][$id],strrpos($boxes_temp[0][$id],"-")+1,2)+1;
					if($field_value<10)
						$field_value="AUTO-0".$field_value;
					else
						$field_value="AUTO-".$field_value;
				}
			}
			else{
				$field_value=$_POST[$row[field_name]];
			}
			
			if(!$row[can_be_same] and $field_value){//判断是否可以相同
				$boxesDB->setTableName($table_name);
				$boxesDB->setSelectField("Count(*) AS total");
				$sql="where (".$row[field_name]."='".$_POST[$row[field_name]]."' and ".$auto_id."!='".$auto_id_value."')";
				$boxesDB->setCondition($sql);
				$boxes_temp=$boxesDB->selectResult();

				if($boxes_temp[0][total]){
					$error=$row[display_name]."字段不能重复！";
					echo"<script>";
					echo"alert(\"修改失败：已经存在重复".$row[display_name]."资料！\");";
					echo"</script>";
					break;
				}
				
			}
			$update_array[] = array($row[field_name],$field_value);
		}
		if(!$error){
			$condition="where $auto_id=$auto_id_value";
			update_row($table_name,$update_array,$condition);
			
			//关闭窗口
			echo"<script>";
			echo"alert('修改成功，按确定返回！');";
			echo"window.returnValue = 'update_complete';";
			echo"self.close();";
			echo"</script>";
		}
		else{
			$submit=1;
		}
	}
	
	/***********************************************/
	/*                  删除动作                   */
	/**********************************************/
	if($_POST[delete_confirm]){
		$error="";
		
		$boxesDB->setTableName($table_name);
		$sql="where parent_id='".$auto_id_value."'";
		$boxesDB->setCondition($sql);
		$boxesDB->setSelectField("*");
		$boxes_temp=$boxesDB->selectResult();

		if(count($boxes_temp)){
			$error="该节点拥有下一层节点，不能删除！";
			echo"<script>";
			echo"alert('$error');";
			echo"</script>";
		}

		if(!$error){
			$condition="where $auto_id=$auto_id_value";
			delete_row($table_name,$condition);
			
			//关闭窗口
			echo"<script>";
			echo"alert('删除成功，按确定返回！');";
			echo"window.returnValue = 'delete_complete';";
			echo"self.close();";
			echo"</script>";
		}
	}
	
	/***********************************************/
	/*                  复制动作                   */
	/**********************************************/
	if($_POST[copy_confirm]){
		$auto_id_value="";
		$submit=1;
		$copy=1;
	}
	
	
	/*
	 *区分修改还是增加，对应变量$title
	 *
	 */
	if($auto_id_value){
		$title="修改".$module_name."资料";
		$boxesDB->setTableName($table_name);
		$boxesDB->setSelectField("*");
		$sql="where ".$auto_id."='".$auto_id_value."'";
		$boxesDB->setCondition($sql);
		$boxes_result=$boxesDB->selectResult();
		$result_row=$boxes_result[0];
		
		
		$boxesDB->setTableName("field_display_table");
		$boxesDB->setSelectField("*");
		$sql="where table_name='$table_name' order by admin_display_number asc";
		$boxesDB->setCondition($sql);
		$boxes_list=$boxesDB->selectResult();
		for($i=0;$i<count($boxes_list);$i++){
			$row=$boxes_list[$i];
			
			if($row[admin_is_display]){
				if($row[admin_display_type]=="select" and $row[field_name]=="parent_id"){
					
					global $selectMenu;
					$boxesDB->setTableName($table_name);
					$sql="where parent_id=0";
					$boxesDB->setCondition($sql);
					$result=$boxesDB->selectResult(); 
					if(count($result)>0) ShowSelect($boxesDB,$result,$layer,$prefix,$result_row[parent_id]); 
				
					$parent_id="<select name='parent_id' onChange=\"check_parent('".$result_row[$auto_id]."',this.value);\">";
					$parent_id.="<option value='0'>";
					for($j=$row[admin_display_width]-3;$j>0;$j--)
					{
						$parent_id.="&nbsp;";
					}
					$parent_id.="</option>";
					$parent_id.=$selectMenu."</select>";
					
					$field_name="";
					$display_type="hidden";

					$judgement.="if(document.form1.parent_id.value==".$result_row[$auto_id]."){alert('错误提示：".$row[display_name]."不能选择自己！');return false;}";
				}
				else if($row[admin_display_type]=="checkbox"){
					$$row[field_name]="<input type='checkbox' name='".$row[field_name]."' value='1'";
					if($result_row[$row[field_name]]==1)$$row[field_name].=" checked";
					$$row[field_name].=">";
					for($j=$row[admin_display_width]-2;$j>0;$j--)
					{
						$$row[field_name].="&nbsp;";
					}
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			
				//检查是否只读
				$readonly=checkFieldReadOnly($row[is_readonly],"");
				
				//判断是否能为空
				$judgement.=checkFieldCanBeEmpty($row[field_name],$row[display_name],$row[can_be_empty]);
				
				//判断输入是否符合要求格式
				$field_type=checkFieldValidity($row[field_type]);
				
				//判断是否自动生成
				if($row[field_type]=="isAUTO"){
					$readonly="readonly";
					$field_value="[系统自动生成]";
				}
				else{
					if($submit)
						$field_value=$_POST[$row[field_name]];
					else
						$field_value=$result_row[$row[field_name]];
				}
				
		
				if($submit)
					$admin_array[]=array(
							"display_name"	=>	$row[display_name],
							"field_name"	=>	$field_name,
							"display_type"	=>	$display_type,
							"display_width"	=>	$row[admin_display_width],
							"field_value"	=>	$field_value,
							"is_readonly"	=>	$readonly,
							"field_type"	=>	$field_type,
							"select_array"	=>	$$row[field_name]);
				else	
					$admin_array[]=array(
							"display_name"	=>	$row[display_name],
							"field_name"	=>	$field_name,
							"display_type"	=>	$display_type,
							"display_width"	=>	$row[admin_display_width],
							"field_value"	=>	$field_value,
							"is_readonly"	=>	$readonly,
							"field_type"	=>	$field_type,
							"select_array"	=>	$$row[field_name]);
			}
			
		}
		
		$add_disabled="disabled";
		
		if($result_row[is_used])$delete_disabled="disabled";
	
	}
	else{
		$title="增加".$module_name."资料";
		if($copy)
			$title="增加复制".$module_name."资料";
		
		$boxesDB->setTableName("field_display_table");
		$boxesDB->setSelectField("*");
		$sql="where table_name='$table_name' order by admin_display_number asc";
		$boxesDB->setCondition($sql);
		$boxes_list=$boxesDB->selectResult();
		for($i=0;$i<count($boxes_list);$i++){
			$row=$boxes_list[$i];
			
			if($row[admin_is_display]){
				//选择框
				if($row[admin_display_type]=="select" and $row[field_name]=="parent_id"){
					
					global $selectMenu;
					$boxesDB->setTableName($table_name);
					$sql="where parent_id=0";
					$boxesDB->setCondition($sql);
					$result=$boxesDB->selectResult(); 
					$selected_value=$_POST[parent_id];
					if(count($result)>0) ShowSelect($boxesDB,$result,$layer,$prefix,$selected_value); 
				
					$parent_id="<select name='parent_id'>";
					$parent_id.="<option value='0'>";
					for($j=$row[admin_display_width]-3;$j>0;$j--)
					{
						$parent_id.="&nbsp;";
					}
					$parent_id.="</option>";
					$parent_id.=$selectMenu."</select>";
					$field_name="";
					$display_type="hidden";
				}
				//复选框
				else if($row[admin_display_type]=="checkbox"){
					$$row[field_name]="<input type='checkbox' name='".$row[field_name]."' value='1'>";
					for($j=$row[admin_display_width]-2;$j>0;$j--)
					{
						$$row[field_name].="&nbsp;";
					}
					$field_name="";
					$display_type="hidden";
				}
				//文本框
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
				
				//检查是否只读
				$readonly=checkFieldReadOnly($row[is_readonly],"");
				
				//判断是否能为空
				$judgement.=checkFieldCanBeEmpty($row[field_name],$row[display_name],$row[can_be_empty]);
				
				//判断输入是否符合要求格式
				$field_type=checkFieldValidity($row[field_type]);
				
				//判断是否自动生成
				if($row[field_type]=="isAUTO"){
					$readonly="readonly";
					$field_value="[系统自动生成]";
				}
				else{
					$field_value="";
				}
				
		
				if($submit)
					$admin_array[]=array(
							"display_name"	=>	$row[display_name],
							"field_name"	=>	$field_name,
							"display_type"	=>	$display_type,
							"display_width"	=>	$row[admin_display_width],
							"field_value"	=>	$_POST[$row[field_name]],
							"is_readonly"	=>	$readonly,
							"field_type"	=>	$field_type,
							"select_array"	=>	$$row[field_name]);
				else	
					$admin_array[]=array(
							"display_name"	=>	$row[display_name],
							"field_name"	=>	$row[field_name],
							"display_type"	=>	$display_type,
							"display_width"	=>	$row[admin_display_width],
							"field_value"	=>	$field_value,
							"is_readonly"	=>	$readonly,
							"field_type"	=>	$field_type,
							"select_array"	=>	$$row[field_name]);
			}
			
		}
		
		$update_disabled="disabled";
		$delete_disabled="disabled";
		$copy_disabled="disabled";
	}

	/**********************************************************************************/
	/*                                   套用模板显示                                  */
	/**********************************************************************************/
	$action_file=$_SERVER['PHP_SELF']."?table_name=$table_name&&module_name=$module_name&&the_id=$auto_id_value";
	$smart->assign(array(
					"module_name"			=>	$module_name,
					"table_name"			=>	$table_name,
					"title"					=>	$title,
					"state"					=>	$state,
					"action_file"			=>	$action_file,
					"admin_array"			=>	$admin_array,
					"add_disabled"			=>	$add_disabled,
					"copy_disabled"			=>	$copy_disabled,
					"update_disabled"		=>	$update_disabled,
					"delete_disabled"		=>	$delete_disabled,
					"judgement"				=>	$judgement));

	//输出结果
	$smart->display('basic/admin_basic_common.tpl.htm');
	/**********************************************************************************/
	/*                                   套用模板显示                                  */
	/**********************************************************************************/
?>