<?php
require 'inc.php';
$obj_tab_site=new cls_tab_temp_site();
$obj_tab_temp=new cls_tab_temp();

$arr_state=$obj_tab_site->state;
$arr_fields_type=$obj_tab_site->fields_type;
$arr_industry=$obj_tab_site->industry;
if($get_act=="del"){
	$arr_return=$obj_tab_site->on_del($get_id);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="del_sel"){
	$arr_return=$obj_tab_site->on_del($get_siteid);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
switch($get_act)
{	
	case 'save':
	{
		(is_date($get_site_starttime))? $get_site_starttime=strtotime($get_site_starttime) : $get_site_starttime=0;
		(is_date($get_site_endtime))? $get_site_endtime=strtotime($get_site_endtime) : $get_site_endtime=0;
		$arr_fields=array();
		$arr_fields["site_id"]=$get_id;
		$arr_fields["site_temp_id"]=$get_site_temp_id;
		$arr_fields["site_user_id"]=$get_site_user_id;
		//$arr_fields["site_name"]=$get_site_name;
		$arr_fields["site_starttime"]=$get_site_starttime;
		$arr_fields["site_issel"]=intval($get_site_issel);
		$arr_fields["site_endtime"]=$get_site_endtime;
		$arr_msg=$obj_tab_site->on_save($arr_fields);
		if($arr_fields["site_issel"]>0){
			$obj_db->execute("update ".DB_PRE."temp_site set site_issel=0 where site_user_id='".$get_site_user_id."' and site_id!='".$get_id."'");
		}
		include 'tpl/_msg.tpl.php';
	    break;
	}
	case "cfg_save":
		$arr_site=array();
		$arr_val=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."temp_site where site_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_fields=$obj_tab_temp->get_fields($obj_rs["site_temp_id"]);
			$lng_count=count($arr_fields);
			for($i=0;$i<$lng_count;$i++){
				$var_get="get_N_temp_".$i;
				if(isset($$var_get)){
					$arr_val[$arr_fields[$i]["name"]]=$$var_get;
				}
			}
		}
		$str_cont=serialize($arr_val);
		$arr_fields=array();
		$arr_fields["site_id"]=$get_id;
		$arr_fields["site_content"]=$str_cont;
		$arr_msg=$obj_tab_site->on_save($arr_fields);
		include 'tpl/_msg.tpl.php';
	    break;
		break;
	case 'cfg':
		$arr_site=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."temp_site where site_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["company_name"]="";
			$obj_rs["temp_title"]="";
			$obj_result2 = $obj_db->query("SELECT temp_pic,temp_id,temp_title FROM ".DB_PRE."temp where temp_id=".$obj_rs["site_temp_id"]);
			if($obj_rs2 = $obj_db->fetch_array($obj_result2))
			{
				$obj_rs["temp_title"]=$obj_rs2["temp_title"];
			}
			$obj_result2 = $obj_db->query("SELECT company_name,company_user_id FROM ".DB_PRE."hr_company where company_user_id=".$obj_rs["site_user_id"]);
			if($obj_rs2 = $obj_db->fetch_array($obj_result2))
			{
				$obj_rs["company_name"]=$obj_rs2["company_name"];
			}
			$obj_rs["site_starttime"]=date("Y-m-d",$obj_rs["site_starttime"]);
			$obj_rs["site_endtime"]=date("Y-m-d",$obj_rs["site_endtime"]);
			$obj_rs["fields"]=$obj_tab_temp->get_fields($obj_rs["site_temp_id"],$obj_rs["site_content"]);
			$arr_site = $obj_rs;
		}
		include 'tpl/site.cfg.tpl.php';
		break;
	case 'edit':
	{
		$arr_site=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."temp_site where site_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["company_name"]="";
			$obj_rs["temp_title"]="";
			$obj_result2 = $obj_db->query("SELECT temp_pic,temp_id,temp_title FROM ".DB_PRE."temp where temp_id=".$obj_rs["site_temp_id"]);
			if($obj_rs2 = $obj_db->fetch_array($obj_result2))
			{
				$obj_rs["temp_title"]=$obj_rs2["temp_title"];
			}
			$obj_result2 = $obj_db->query("SELECT company_name,company_user_id FROM ".DB_PRE."hr_company where company_user_id=".$obj_rs["site_user_id"]);
			if($obj_rs2 = $obj_db->fetch_array($obj_result2))
			{
				$obj_rs["company_name"]=$obj_rs2["company_name"];
			}
			$obj_rs["site_starttime"]=date("Y-m-d",$obj_rs["site_starttime"]);
			$obj_rs["site_endtime"]=date("Y-m-d",$obj_rs["site_endtime"]);
			$obj_rs["fields"]=$obj_tab_temp->get_fields($obj_rs["site_temp_id"],$obj_rs["site_content"]);
			$arr_site = $obj_rs;
		}else{
			$arr_site = $obj_db->get_fields(DB_PRE."temp_site",true);
			$arr_site["company_name"]="";
			$arr_site["temp_title"]="";
		}
		include 'tpl/site.edit.tpl.php';
		break;
	}
	default:
	{
		//·ÖÒ³
		$str_where="";
		$arr_site=array();
		$arr_temp_id=array();
		$arr_user_id=array();
		$arr_temp=array();
		$arr_company=array();
		$obj_rs   = $obj_db->get_one("SELECT count(*) as num FROM ".DB_PRE."temp_site".$str_where);
		$int_total=$obj_rs["num"];
		$int_pages= ceil($int_total/$int_pagesize);
		$get_page = min($int_pages, $get_page);
		$get_page = max($get_page, 1);
		$page_split_page=get_pages($int_total, $get_page, $int_pagesize);
		$int_offset = $int_pagesize*($get_page-1);
		$str_limit = " limit ".$int_offset.",".$int_pagesize;
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."temp_site".$str_where.$str_limit);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["addtime"]=date("Y-m-d",$obj_rs["site_addtime"]);
			$obj_rs["state"]=array_search($obj_rs["site_state"],$arr_state);
			$arr_temp_id[]=$obj_rs["site_temp_id"];
			$arr_user_id[]=$obj_rs["site_user_id"];
			$arr_site[] = $obj_rs;
		}
		
		if(count($arr_temp_id)>0){
			$str_ids=implode(",",$arr_temp_id);
			$obj_result = $obj_db->query("SELECT temp_pic,temp_id,temp_title FROM ".DB_PRE."temp where temp_id in(".$str_ids.")");
			while($obj_rs = $obj_db->fetch_array($obj_result))
			{
				$arr_temp["id_".$obj_rs["temp_id"]]=$obj_rs;
			}
			$str_ids=implode(",",$arr_user_id);
			$obj_result = $obj_db->query("SELECT company_name,company_user_id FROM ".DB_PRE."hr_company where company_user_id in(".$str_ids.")");
			while($obj_rs = $obj_db->fetch_array($obj_result))
			{
				$arr_company["id_".$obj_rs["company_user_id"]]=$obj_rs;
			}
		}

		include "tpl/site.tpl.php";

	}
}

?>
