<?php
require 'inc.php';
$obj_tab_area=new cls_tab_sys_area();

$tab_company=DB_PRE."hr_company";
$obj_tab_company=new cls_tab_hr_company();

$this_is_company_html=$obj_tab_company->ishtml;

if(!isset($int_pagesize)) $int_pagesize=10;
if(!isset($get_act)) $get_act="";
(!isset($get_page))? $get_page=0:$get_page=intval($get_page);

if(!isset($get_s_state)) $get_s_state=-101;
if(!isset($get_s_vip)) $get_s_vip=-1;
if(!isset($get_s_addtime1)) $get_s_addtime1="";
if(!isset($get_s_addtime2)) $get_s_addtime2="";
if(!isset($get_s_name)) $get_s_name="";
if(!isset($get_s_viptime1)) $get_s_viptime1="";
if(!isset($get_s_viptime2)) $get_s_viptime2="";

$page_footer_info=<<<HTML
HTML;

$arr_vip=array();
$arr_vipname=array();
$arr_vipdays=array();
$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."hr_company_vip");
while($obj_rs = $obj_db->fetch_array($obj_result))
{
	$arr_vipname["id_".$obj_rs["vip_id"]]=$obj_rs["vip_name"];
	$arr_vipdays["id_".$obj_rs["vip_id"]]=$obj_rs["vip_days"];
	$arr_vip[]=$obj_rs;
}

if($get_act=='del_sel')
{
	$arr_msg=$obj_tab_company->on_del($get_companyids);
	if($arr_msg["err"]!="")
	{
		$obj_msg->alert($arr_msg["err"]);
	}

}
if($get_act=='vip')
{
	$arr_msg=$obj_tab_company->on_vip($get_companyids,$get_vip_id);
	if($arr_msg["err"]!="")
	{
		$obj_msg->alert($arr_msg["err"]);
	}

}
if($get_act=='state')
{
	$arr_msg=$obj_tab_company->on_state($get_companyids,$get_state_val);
	if($arr_msg["err"]!="")
	{
		$obj_msg->alert($arr_msg["err"]);
	}

}
switch($get_act)
{	
	case 'save':
	{
		$arr_fields=array();
		$arr_fields["company_id"]=$get_id;
		$arr_fields["company_name"]=$get_company_name;
		$arr_fields["company_idnum"]=$get_company_idnum;
		$arr_fields["company_industry"]=$get_company_industry;
		$arr_fields["company_nature"]=$get_company_nature;
		$arr_fields["company_builddate"]=$get_company_builddate;
		$arr_fields["company_regcapital"]=$get_company_regcapital;
		$arr_fields["company_currency"]=$get_company_currency;
		$arr_fields["company_scale"]=$get_company_scale;
		$arr_fields["company_area"]=$get_company_area;
		$arr_fields["company_intro"]=$get_company_intro;
		$arr_fields["company_linkname"]=$get_company_linkname;
		$arr_fields["company_tel"]=$get_company_tel;
		$arr_fields["company_fax"]=$get_company_fax;
		$arr_fields["company_email"]=$get_company_email;
		$arr_fields["company_website"]=$get_company_website;
		$arr_fields["company_zip"]=$get_company_zip;
		$arr_fields["company_address"]=$get_company_address;
		$arr_fields["company_vip_id"]=$get_company_vip_id;
		if(intval($get_company_vip_id)>0 && isset($get_company_vip_time)){
			if(is_date($get_company_vip_time)) $get_company_vip_time=strtotime($get_company_vip_time);
			$arr_fields["company_vip_time"]=$get_company_vip_time;
			$obj_rs=$obj_db->get_one("select vip_days from ".DB_PRE."hr_company_vip where vip_id=".$get_company_vip_id);
			$arr_fields["company_vip_endtime"]=$get_company_vip_time+$obj_rs["vip_days"]*86400;
		}
		if($get_id<1){
			$arr_fields["company_user_id"]=$get_company_user_id;
		}
		$arr_msg=$obj_tab_company->on_save($arr_fields);
		if($arr_msg["err"]=="") $get_id=$arr_msg["id"];
		include 'tpl/_msg.tpl.php';
	    break;
	}
	case 'add':
	{
		$get_id=0;
		$arr_company=array();
		$arr_company["company_area"]=$obj_tab_area->get_select_area("company_area");
		$arr_company["company_industry"]=get_select_html($obj_tab_company->industry);
		$arr_company["company_nature"]=get_select_html($obj_tab_company->nature);
		$arr_company["company_currency"]=get_select_html($obj_tab_company->currency);
		$arr_company["company_scale"]=get_select_html($obj_tab_company->scale);
		include 'tpl/company.add.tpl.php';
		break;
	}
	case 'edit':
	{
		$obj_result = $obj_db->query("SELECT * FROM ".$tab_company." where company_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["company_area"]=$obj_tab_area->get_select_area("company_area",$obj_rs["company_area"]);
			$obj_rs["company_industry"]=get_select_html($obj_tab_company->industry,$obj_rs["company_industry"]);
			$obj_rs["company_nature"]=get_select_html($obj_tab_company->nature,$obj_rs["company_nature"]);
			$obj_rs["company_currency"]=get_select_html($obj_tab_company->currency,$obj_rs["company_currency"]);
			$obj_rs["company_scale"]=get_select_html($obj_tab_company->scale,$obj_rs["company_scale"]);
			if($obj_rs["company_vip_time"]<1) $obj_rs["company_vip_time"]=TIME;
			if($obj_rs["company_builddate"]=="0000-00-00") $obj_rs["company_builddate"]="";
			$arr_company = $obj_rs;
		}
		include 'tpl/company.edit.tpl.php';
		break;
	}
	case "html_exe":
	{
		//分页
		$int_pagesize=100;
		$arr_company=array();
		$str_companyid="";
		$str_where=" where 1";
		if(isset($get_companyids)){
			if(is_array($get_companyids)){
				$str_companyid=get_arr_id($get_companyids);
			}else{
				$arr_x=explode(",",$get_companyids);
				$arr_y=array();
				foreach($arr_x as $item){
					if(!is_numeric($item)) continue;
					$arr_y[]=$item;
				}
				$str_companyid=implode(",",$arr_y);
			}
			if($str_companyid!=""){
				$str_where.=" and company_id in(".$str_companyid.")";
			}
		}
		$obj_rs   = $obj_db->get_one("SELECT count(*) as num FROM ".DB_PRE."hr_company".$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 company_id FROM ".DB_PRE."hr_company".$str_where." order by company_id desc".$str_limit);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_company[]=$obj_rs;
		}
		include 'tpl/company.html.tpl.php';
		break;
	}
	default:
	{
		$str_where=" where 1";
		//查询
		$get_issearch="";
		if($get_s_name!=""){
			$get_s_name=str_replace(" ","%",$get_s_name);
			$str_where.=" and (company_name like '%".$get_s_name."%' or company_linkname like '%".$get_s_name."%' or company_tel like '%".$get_s_name."%' or company_email like '%".$get_s_name."%'";
			$get_issearch="1";
			$arr_s_uid=array();
			$obj_result = $obj_db->query("SELECT user_id FROM ".DB_PRE."sys_user where user_name like '%".$get_s_name."%'");
			while($obj_rs = $obj_db->fetch_array($obj_result))
			{
				$arr_s_uid[]=$obj_rs["user_id"];
			}
			$str_x_where="";
			if(count($arr_s_uid)>0){
				$str_s_uid=implode(",",$arr_s_uid);
				$str_x_where=" or company_user_id in(".$str_s_uid.")";
			}
			$str_where.=$str_x_where.")";
		}
		if($get_s_state!=-101){
			$str_where.=" and company_state='".$get_s_state."'";
			$get_issearch="1";
		}
		if($get_s_vip>=0){
			$str_where.=" and company_vip_id='".$get_s_vip."'";
			$get_issearch="1";
		}
		if($get_s_vip==-2){
			$str_where.=" and company_vip_id>0";
			$get_issearch="1";
		}
		if(is_date($get_s_addtime1)){
			$str_where.=" and FROM_UNIXTIME(company_addtime)>='".$get_s_addtime1."'";
			$get_issearch="1";
		}
		if(is_date($get_s_addtime2)){
			$lng_x=format_date_int($get_s_addtime2)+86400;
			$str_s_regtime2=format_date($lng_x,"Y-m-d");
			$str_where.=" and FROM_UNIXTIME(company_addtime)<'".$str_s_regtime2."'";
			$get_issearch="1";
		}
		if(is_date($get_s_viptime1)){
			$str_where.=" and FROM_UNIXTIME(company_vip_time)>='".$get_s_viptime1."'";
			$get_issearch="1";
		}
		if(is_date($get_s_viptime2)){
			$lng_x=format_date_int($get_s_addtime2)+86400;
			$str_s_regtime2=format_date($lng_x,"Y-m-d");
			$str_where.=" and FROM_UNIXTIME(company_vip_endtime)<'".$str_s_regtime2."'";
			$get_issearch="1";
		}
		if(isset($get_fz_id) && intval($get_fz_id)>=0){
			$str_where.=" and company_fz_id='".$get_fz_id."'";
		}

		$arr_company = array();
		$arr_uid=array();
		$obj_rs  = $obj_db->get_one("SELECT count(*) as num FROM ".$tab_company.$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 ".$tab_company.$str_where." order by company_id desc".$str_limit);
		$lng_now_time=format_date_int(format_date(TIME,'Y-m-d'));
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["vipname"]="";
			if(isset($arr_vipname["id_".$obj_rs["company_vip_id"]])) $obj_rs["vipname"]=$arr_vipname["id_".$obj_rs["company_vip_id"]];
			$obj_rs["company_addtime"]=format_date($obj_rs["company_addtime"]);
			$arr_uid[]=$obj_rs["company_user_id"];
			if($obj_rs["company_state"]>0){
				$obj_rs["state"]="通过";
			}else{
				$obj_rs["state"]="<font color='#ff0000'>待审</font>";
			}
			$obj_rs["vipstate"]="";
			if($obj_rs["company_vip_id"]>0){
				if($obj_rs["company_vip_endtime"]>$lng_now_time){
					$lng_time=$obj_rs["company_vip_endtime"]-$lng_now_time;
					$lng_day=intval($lng_time/86400);
					if($lng_day<5){
						$obj_rs["vipstate"]="<font color='#ff0000'>(差".$lng_day."天)</font>";
					}else{
						$obj_rs["vipstate"]="(差".$lng_day."天)";
					}
				}else{
					$lng_time=$lng_now_time-$obj_rs["company_vip_endtime"];
					$lng_day=intval($lng_time/86400);
					if($lng_day==0) $lng_day="今天";
					$obj_rs["vipstate"]="<font color='#ff0000'>(已过期,".$lng_day.")</font>";
				}
			}
			$obj_rs["url"]=$obj_tab_company->get_url($obj_rs["company_id"]);
			$arr_company[] = $obj_rs;
		}
		$arr_uname=$obj_ctl_user->get_user_name($arr_uid);
		foreach($arr_uid as $item){
			if(!isset($arr_uname["id_".$item])) $arr_uname["id_".$item]="";
		}
		include 'tpl/company.tpl.php';
	}
}


?>
