<?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/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];
	$module_num		= $_REQUEST[module_num];
	$auto_id_value	= $_REQUEST[the_id];
	$operator		= $HTTP_SESSION_VARS["user_name"];
	$bu_auto_id		= $HTTP_SESSION_VARS["user_auto_id"];
	$update			= $_POST[update];
	

	//检查数据库设置
	$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(\"表设置出问题啦！请联系管理员!\");</script>";
		exit;
	}
	
	$prefix=$boxes_check[0][prefix];
	$auto_id=$prefix."auto_id";
	$id=$prefix."id";

/*#####################################################################*/	
	//隐藏字段
	$hidden_field_name=array("bf_module_num");
/*#####################################################################*/

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

		$error="";
		
		checkRole($table_name,$module_num,$bu_auto_id,"ADD");
		
		$boxesDB->setTableName("field_display_table");
		$boxesDB->setSelectField("*");
		$sql="where (table_name='$table_name' and admin_is_display='1')";
		$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]] or $_POST[$row[field_name]]=="addselect")){
				$error=$row[display_name]."不能为空！";
				echo"<script>alert(\"保存失败：".$row[display_name]."不能为空！\");</script>";
				break;
			}
			if($row[field_type]=="isAUTO"){
				$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];
				
				$lastID=$result_temp[$row[field_name]];
				$field_value=createInfoID($row[field_name],$lastID);
			}
			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);
		}
		for($k=0;$k<count($hidden_field_name);$k++){
			$add_array[]=array("".$hidden_field_name[$k]."",$_POST[$hidden_field_name[$k]]);
		}
		
		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][$auto_id]."','".$boxes_temp[0][$id]."','".$boxes_temp[0][$name]."');";
			echo"window.returnValue = value;";
//			echo"self.close();";
			echo"</script>";
			
		}
		else{
			$submit=1;
		}
	}
	
	
	/***********************************************/
	/*                  修改动作                   */
	/**********************************************/
	if($_POST[update_confirm]){

		$error="";
		
		checkRole($table_name,$module_num,$bu_auto_id,"UPDATE");
		
		$boxesDB->setTableName("field_display_table");
		$boxesDB->setSelectField("*");
		$sql="where (table_name='$table_name' and admin_is_display='1')";
		$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]] or $_POST[$row[field_name]]=="addselect")){
				$error=$row[display_name]."不能为空！";
				echo"<script>alert(\"保存失败：".$row[display_name]."不能为空！\");</script>";
				break;
			}
			if(!$row[can_be_same] and $_POST[$row[field_name]]){//判断是否可以相同
				$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],$_POST[$row[field_name]]);
		}
		for($k=0;$k<count($hidden_field_name);$k++){
			$update_array[]=array("".$hidden_field_name[$k]."",$_POST[$hidden_field_name[$k]]);
		}
		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;
			$update=1;
		}
	}
	
	/***********************************************/
	/*                  删除动作                   */
	/**********************************************/
	if($_POST[delete_confirm]){
		$error="";
		
		checkRole($table_name,$module_num,$bu_auto_id,"DELETE");

		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;
	}
	
	//查看
	if($auto_id_value and !$update){
		
		checkRole($table_name,$module_num,$bu_auto_id,"READ");
		
		$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' and admin_is_display='1') order by admin_display_number asc";
		$boxesDB->setCondition($sql);
		$boxes_tab1=$boxesDB->selectResult();
		for($i=0;$i<count($boxes_tab1);$i++){
			$row=$boxes_tab1[$i];
			
			
			$field_value=$result_row[$row[field_name]];
			
			if($row[admin_position]=="tab1")
				$tab1_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]);
			elseif($row[admin_position]=="tab2")
				$tab2_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]);		
			elseif($row[admin_position]=="tab3")
				$tab3_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]);	
			elseif($row[admin_position]=="tab4")
				$tab4_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]);									
		}	
	}
	//修改
	elseif($auto_id_value and $update){
		
		checkRole($table_name,$module_num,$bu_auto_id,"UPDATE");
		
		$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];
		
		for($i=0;$i<count($hidden_field_name);$i++){
			$$hidden_field_name[$i]=$result_row[$hidden_field_name[$i]];
		}
		
		$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_tab1=$boxesDB->selectResult();
		for($i=0;$i<count($boxes_tab1);$i++){
			$row=$boxes_tab1[$i];
			
			//下拉菜单
			if($row[admin_display_type]=="select"){
				if($submit)
					$$row[field_name]=getSelectContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				else
					$$row[field_name]=getSelectContent($row[field_name],$row[admin_display_width],$result_row[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//当下拉框数据比较多的时候使用
			elseif($row[admin_display_type]=="onselect"){
				$$row[field_name]=getOnSelectContent($row[field_name],$row[display_name]);
				
				$field_name=$row[field_name];
				$display_type="text";
			}
			//复选框
			else if($row[admin_display_type]=="checkbox"){
				if($submit)
					$$row[field_name]=getCheckBoxContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				else
					$$row[field_name]=getCheckBoxContent($row[field_name],$row[admin_display_width],$result_row[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//性别
			else if($row[admin_display_type]=="sex"){
				if($submit)
					$$row[field_name]=getSexContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				else 
					$$row[field_name]=getSexContent($row[field_name],$row[admin_display_width],$result_row[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//时间
			elseif($row[admin_display_type]=="time"){
				
				if($submit)
					$$row[field_name]=getTimeContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				else
					$$row[field_name]=getTimeContent($row[field_name],$row[admin_display_width],$result_row[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//当文本区域
			elseif($row[admin_display_type]=="textarea"){
				
				if($submit)
					$$row[field_name]=getTextAreaContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				else 
					$$row[field_name]=getTextAreaContent($row[field_name],$row[admin_display_width],format_string($result_row[$row[field_name]]));
					
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//一般TEXT
			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";
			}
			
			//检查是否单独成行
			$single_row=checkSingleRow($row[field_name],$row[admin_display_width]);
			
			if($submit)
				$field_value=$_POST[$row[field_name]];
			else 
				$field_value=$result_row[$row[field_name]];	
				
			if($row[admin_position]=="tab1")
				$tab1_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);
			elseif($row[admin_position]=="tab2")
				$tab2_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);
			elseif($row[admin_position]=="tab3")
				$tab3_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);
			elseif($row[admin_position]=="tab4")
				$tab4_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);		
		}
		
		$add_disabled="disabled";
	}
	//增加
	else{
		
		checkRole($table_name,$module_num,$bu_auto_id,"ADD");
		
		$title="增加".$module_name;
		if($copy)
			$title="增加复制".$module_name;
						
		for($i=0;$i<count($hidden_field_name);$i++){
			$$hidden_field_name[$i]=$_POST[$hidden_field_name[$i]];
		}
		
		$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_tab=$boxesDB->selectResult();
		for($i=0;$i<count($boxes_tab);$i++){
			$row=$boxes_tab[$i];
			
			//下拉菜单
			if($row[admin_display_type]=="select"){
				$$row[field_name]=getSelectContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//当下拉框数据比较多的时候使用
			elseif($row[admin_display_type]=="onselect"){
				
				$$row[field_name]=getOnSelectContent($row[field_name],$row[display_name]);
				
				$field_name=$row[field_name];
				$display_type="text";
			}
			//复选框
			else if($row[admin_display_type]=="checkbox"){
				
				$$row[field_name]=getCheckBoxContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//性别
			else if($row[admin_display_type]=="sex"){
				
				$$row[field_name]=getSexContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//时间
			elseif($row[admin_display_type]=="time"){
				
				if($submit)
					$$row[field_name]=getTimeContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				else
					$$row[field_name]=getTimeContent($row[field_name],$row[admin_display_width],date("Y-m-d",time()));
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//当文本区域
			elseif($row[admin_display_type]=="textarea"){
				
				$$row[field_name]=getTextAreaContent($row[field_name],$row[admin_display_width],$_POST[$row[field_name]]);
				
				if($$row[field_name]){
					$field_name="";
					$display_type="hidden";
				}
				else{
					$field_name=$row[field_name];
					$display_type="text";
				}
			}
			//一般TEXT
			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="[系统自动生成]";
			}
			//操作员
			elseif($row[admin_display_type]=="operator"){
				$readonly="readonly";
				$field_value=$operator;
			}
			else{
				$field_value=$_POST[$row[field_name]];
			}
			
			//检查是否单独成行
			$single_row=checkSingleRow($row[field_name],$row[admin_display_width]);
			
			if($row[admin_position]=="tab1")
				$tab1_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);
			elseif($row[admin_position]=="tab2")
				$tab2_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);
			elseif($row[admin_position]=="tab3")
				$tab3_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,
						"single_row"	=>	$single_row,
						"select_array"	=>	$$row[field_name]);
			elseif($row[admin_position]=="tab4")
				$tab4_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,
						"single_row"	=>	$single_row,
						"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&&module_num=$module_num&&the_id=$auto_id_value";
	if(count($tab1_array))
		$tab1_display=1;
	if(count($tab2_array))
		$tab2_display=1;
	if(count($tab3_array))
		$tab3_display=1;
	if(count($tab4_array))
		$tab4_display=1;	
		
	for($i=0;$i<count($hidden_field_name);$i++){
		$hidden_field[]=array("field_name"=>$hidden_field_name[$i],
							  "field_value"=>$$hidden_field_name[$i]);
	}
	$smart->assign(array(
					"module_name"			=>	$module_name,
					"table_name"			=>	$table_name,
					"title"					=>	$title,
					"state"					=>	$state,
					"action_file"			=>	$action_file,
					"tab1_array"			=>	$tab1_array,
					"tab1_display"			=>	$tab1_display,
					"tab2_array"			=>	$tab2_array,
					"tab2_display"			=>	$tab2_display,
					"tab3_array"			=>	$tab3_array,
					"tab3_display"			=>	$tab3_display,
					"tab4_array"			=>	$tab4_array,
					"tab4_display"			=>	$tab4_display,
					"add_disabled"			=>	$add_disabled,
					"copy_disabled"			=>	$copy_disabled,
					"update_disabled"		=>	$update_disabled,
					"delete_disabled"		=>	$delete_disabled,
					"hidden_field"			=>	$hidden_field,
					"judgement"				=>	$judgement));


	//输出结果
	if($auto_id_value and !$update)
		$smart->display('system/display_system.tpl.htm');
	else
		$smart->display('system/admin_system_common.tpl.htm');
	/**********************************************************************************/
	/*                                   套用模板显示                                  */
	/**********************************************************************************/

	/**********************************************************************************/
	/*                                   隐藏等待框                                    */
	/**********************************************************************************/
	//隐藏层
?>
	<script>
		top.voucherMain.rows="0,*";
	</script>
<?php
	/**********************************************************************************/
	/*                                   隐藏等待框                                    */
	/**********************************************************************************/
?>