<?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";

	$boxesDB=new BiaoCommon($conn); 	

	$table_name		= $_REQUEST[table_name];
	$module_name	= $_REQUEST[module_name];
	$module_num		= $_REQUEST[module_num];
	
	$customer_auto_id=$_REQUEST[customer_auto_id];
	
	if($table_name=="boxes_customer"){
		$return_array[]	= "bc_auto_id";
		$return_array[]	= "bc_name";
		$return_array[]	= "bc_id";
	}
	else if($table_name=="boxes_customer_linkman"){
		$return_array[]	= "blm_name";
		$condition="customer_auto_id='$customer_auto_id' and ";
	}
	else if($table_name=="boxes_module"){
		$return_array[]	= "bm_name";
		$return_array[]	= "bm_auto_id";
		$condition="bm_level!=0 and ";
		$index="order by bm_topic asc";
	}
	else if($table_name=="boxes_role"){
		$return_array[]	= "br_name";
		$return_array[]	= "br_auto_id";
	}
	else{
		//默认设置
		//在找到prefix以后
	}
	
	if($_POST[input_id])
		$the_id=$_POST[input_id];
	else
		$the_id=$_REQUEST[the_id];
		
		
	//高级查询
	$boxesDB->setTableName("field_display_table");
	$boxesDB->setSelectField("*");
	$sql="where (table_name='$table_name' and search_is_display='1') order by field_display_auto_id asc";
	$boxesDB->setCondition($sql);
	$boxes_search_list=$boxesDB->selectResult();
	
	for($i=0;$i<count($boxes_search_list);$i++){
		$row=$boxes_search_list[$i];
		$field_name="search_".$row[field_name];
		if($_REQUEST[$field_name]!=""){
			$boxesDB->setPageParameter("$field_name",$_REQUEST[$field_name]);
			$condition.="$row[field_name] like '%".$_REQUEST[$field_name]."%' and ";//全部都是模糊查找
		}
	}
	if($condition){//去掉最后一个and	
		$condition=substr($condition,0,strlen($condition)-4);
		$condition="where ".$condition." ";
	}
	$condition.=$index;

	//显示
	$boxesDB->setTableName("field_display_table");
	$boxesDB->setSelectField("*");
	$sql="where (table_name='$table_name' and select_is_display=1) order by select_display_number asc";
	$boxesDB->setCondition($sql);
	$boxes_display=$boxesDB->selectResult();
	
	if(!count($boxes_display)){
		echo"<script>alert(\"“ $table_name ”里的“选择”设置出错！\");self.close();</script>";
		exit;
	}
	
	for($i=0;$i<count($boxes_display);$i++){
		$row=$boxes_display[$i];
		$display_list[]=array("width"=>$row[select_display_width],"display"=>$row[display_name],"field_name"=>$row[field_name]);
	}
	$auto_id		= $boxes_display[0][prefix]."auto_id";
	$id				= $boxes_display[0][prefix]."id";
	
	if(!$return_array){
		$return_array[]=$boxes_display[0][prefix]."name";
	}

	//置空
	if($_POST[clear_confirm]){
		echo"<script>";	
		echo"var value=new Array(";
		for($i=0;$i<count($return_array)-1;$i++){
			echo"'',";
		}		
		echo"'');";
		
		echo"window.returnValue = value;";
		echo"self.close();";
		echo"</script>";
	}
	//选择动作
	else if($_POST[select_confirm] or $_POST[select_id]){

		$boxesDB->setTableName($table_name);
		$sql="where $auto_id='$_POST[select_id]'";
		$boxesDB->setCondition($sql);
		$result=$boxesDB->selectResult();
		$row=$result[0];
		
		echo"<script>";	
		echo"var value=new Array(";
		for($i=0;$i<count($return_array)-1;$i++){
			echo"'".$row[$return_array[$i]]."',";
		}
		echo"'".$row[$return_array[count($return_array)-1]]."');";
		
		echo"window.returnValue = value;";
		echo"self.close();";
		echo"</script>";
	}
	//查询结果
	else{
		$boxesDB->setTableName($table_name);
		$boxesDB->setCondition($condition);	
		$boxesDB->setSelectField("*");
			
		$rows=$boxesDB->getPageRow();
		
		//如果数量超过200个就不显示
		if($rows[totalRow]>$ADVANCED_SEARCH_MAX and $ADVANCED_SEARCH_LIMIT){
			$select_field="<font color=red>提示：符合要求数据比较多，系统设置不显示，请缩小查找范围！</font><br>";
		}
		else{
			$result=$boxesDB->selectResult(); 
			
			for($i=0;$i<count($result);$i++){
				for($j=0;$j<count($display_list);$j++){
					$list_array[$i][]=array("width"=>$display_list[$j][width],"action"=>"","display"=>$result[$i][$display_list[$j][field_name]]);
				}
				$result_array[]=array("id"=>$result[$i][$auto_id],"list"=>$list_array[$i]);
			}
		}
		
		$select_field.="<input class='INPUT_BUTTON' type='button' name='search_button' value=' 高级查询 ' onClick=\"showSelectSearchWindow('select_common.php','$table_name','$module_name','2')\">";
		
	}
	
	$number=count($result_array);
	$smart->assign(array(
						"module_name"		=>	$module_name,
						"result_top_list"	=>	$display_list,
						"select_field"		=>	$select_field,
						"result_array"		=>	$result_array,
						"the_id"			=>	$the_id,
						"number"			=>	$number));
	
	$smart->display('select_common.tpl.htm');
	
?>
