<?php
/*
 * 74cms 企业会员中心
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once(dirname(__FILE__).'/company_common.php');
$smarty->assign('nav','info');
$act=trim($_GET["act"])?trim($_GET["act"]):"company_profile";
// 第一步 创建企业基本资料
if($act=="reg_baseinfo")
{
	if(!empty($company_info)){
		if($company_info['audit']=="1"){
			header("location:company_index.php");
		}elseif($company_info['certificate_img']!=""){
			header("location:?act=company_auth_complete");
		}else{
			header("location:?act=company_auth");
		}
	}
	$smarty->assign('title','企业基本信息 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->display('member_company/company_reg_baseinfo.htm');
}
elseif ($act=='reg_baseinfo_save')
{
	if(!empty($company_info)){
		if($company_info['audit']=="1"){
			header("location:company_index.php");
		}elseif($company_info['certificate_img']!=""){
			header("location:?act=company_auth_complete");
		}else{
			header("location:?act=company_auth");
		}
	}
	$setsqlarr['uid']=$parentuid;
	$setsqlarr['companyname']=trim($_POST['companyname'])?trim($_POST['companyname']):showmsg('您没有输入企业名称！',1);
	$setsqlarr['district']=intval($_POST['district'])>0?intval($_POST['district']):showmsg('您选择所在地区！',1);
	$setsqlarr['sdistrict']=intval($_POST['sdistrict']);
	$setsqlarr['district_cn']=trim($_POST['district_cn']);
	$setsqlarr['contact']=trim($_POST['contact'])?trim($_POST['contact']):showmsg('您没有输入联系人！',1);
	$setsqlarr['sex']=trim($_POST['sex'])?intval($_POST['sex']):showmsg('您没有选择性别！',1);;
	$setsqlarr['sex_cn']=trim($_POST['sex_cn']);
	$setsqlarr['telephone']=trim($_POST['telephone'])?trim($_POST['telephone']):showmsg('您没有填写联系电话！',1);
	$company_profile=$db->getone("select * from ".table('company_profile')." where companyname='".$setsqlarr['companyname']."' LIMIT 1 ");
	if(empty($company_profile))
	{	
		if(inserttable(table('company_profile'),$setsqlarr)){
			$mem['fullname'] = $setsqlarr['contact'];
			$mem['sex'] = $setsqlarr['sex'];
			$mem['sex_cn'] = $setsqlarr['sex_cn'];
			$mem['mobile'] = $setsqlarr['telephone'];
			updatetable(table("members"),$mem,array('uid'=>$uid));
			header("Location: ?act=company_auth");
		}else{
			showmsg('完善企业基本信息失败,请重新完善！',1);
		}
	}else{
		showmsg('该公司已经注册！',1);
	}
}
// 第二步 营业执照
elseif($act=="company_auth")
{	
	if(empty($company_info)){
		header("location:?act=reg_baseinfo");
	}elseif($company_info['audit']=="1"){
		header("location:company_index.php");
	}elseif($company_info['certificate_img']!=""){
		header("location:?act=company_auth_complete");
	}
	$smarty->assign('title','企业营业执照 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('nav','info');
	$smarty->display('member_company/company_auth.htm');
}
elseif ($act=='company_auth_save')
{
	if(empty($company_info)){
		header("location:?act=reg_baseinfo");
	}elseif($company_info['audit']=="1"){
		header("location:company_index.php");
	}elseif($company_info['certificate_img']!=""){
		header("location:?act=company_auth_complete");
	}
	require_once(QISHI_ROOT_PATH.'include/upload.php');
	$setsqlarr['audit']=2;//添加默认审核中..
	!$_FILES['certificate_img']['name']?showmsg('请上传图片！',1):"";
	$certificate_dir="../../data/".$_CFG['updir_certificate']."/".date("Y/m/d/");
	make_dir($certificate_dir);
	$setsqlarr['certificate_img']=_asUpFiles($certificate_dir, "certificate_img",$_CFG['certificate_max_size'],'gif/jpg/bmp/png',true);
	if ($setsqlarr['certificate_img'])
	{
	$setsqlarr['certificate_img']=date("Y/m/d/").$setsqlarr['certificate_img'];
	$auth=get_company($_SESSION['parentuid']);//获取原始图片
	@unlink("../../data/".$_CFG['updir_certificate']."/".$auth['certificate_img']);
	$wheresql="uid=$uid ";
	write_memberslog($uid,1,8002,$_SESSION['username'],"/",$parentuid,"上传了营业执照",0);

	updatetable(table('jobs'),array('company_audit'=>2),$wheresql);
	!updatetable(table('company_profile'),$setsqlarr,$wheresql)?showmsg('保存失败！',0):header("location:?act=company_auth_complete");
	
	}
	else
	{
	showmsg('保存失败！',0);
	}
}
elseif($act=='company_auth_complete'){
	$smarty->assign('title','企业营业执照 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->display('member_company/company_auth_complete.htm');
}
elseif ($act=='company_profile')
{
	if($uid==$parentuid){
		$super = 1;
	}else{
		$super = 0;
	}
	if ($company_info['tag'] && !$super)
	{
		$tag=explode('|',$company_info['tag']);
		$taglist=array();
		if (!empty($tag) && is_array($tag))
		{
			foreach($tag as $t)
			{
			$tli=explode(',',$t);
			$taglist[]=array($tli[0],$tli[1]);
			}
		}
		$company_info['tag']=$taglist;
	}
	if ($company_info['logo'])
	{
		$company_info['logo']=$_CFG['site_dir']."data/logo/".$company_info['logo'];
	}
	else
	{
		$company_info['logo']=$_CFG['site_dir']."data/logo/no_logo.gif";
	}
	$smarty->assign('super',$super);
	$smarty->assign('consultant',$consultant);
	$company_url=url_rewrite("QS_companyshow",array('id'=>$company_info['id']));
	$smarty->assign('title','企业资料管理 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('company_profile',$company_info);
	$smarty->assign('company_url',$company_url);
	$smarty->assign('company_nature',intval($company_info['nature']));
	$smarty->display('member_company/company_profile.htm');
}
//猎头 企业logo预加载
elseif ($act=='company_logo_preloading')
{
	$uid=intval($_SESSION['uid']);
	//LOGO图片的预处理
	require_once(QISHI_ROOT_PATH.'include/upload.php');
	if($_FILES['upload_file']['name']){
		$uplogopreload_dir="../../data/logopreload/".date("Y/m/d/");
		make_dir($uplogopreload_dir);
		$setsqlarr['logopreload']=_asUpFiles($uplogopreload_dir, "upload_file",$_CFG['logo_max_size'],'jpg/jpeg/gif/bmp/png',$uid);
		if ($setsqlarr['logopreload'])
		{
			$setsqlarr['logopreload']=date("Y/m/d/").$setsqlarr['logopreload'];
			$logo_src="../../data/logopreload/".$setsqlarr['logopreload'];
			exit($logo_src);
		}
		else
		{
			exit("上传失败!");
		}
		
	}
}
//猎头 企业logo保存
elseif ($act=='company_logo_save')
{	
	$uid=intval($_SESSION['uid']);
	//LOGO图片的处理
	require_once(QISHI_ROOT_PATH.'include/upload.php');
	require_once(QISHI_ROOT_PATH.'include/imageresize.class.php');
	$company_info=get_company($uid);	//获取原始logo信息
	if(!empty($company_info['logo'])){
		$uplogo_dir="../../data/logo/";
		@unlink($uplogo_dir.$company_info['logo']); //先删除原始图片
	}
	$uplogo_dir="../../data/logo/".date("Y/m/d/");
	make_dir($uplogo_dir);
	$logopre_src=$_POST['url'];
	$thumb_dir=$uplogo_dir;
	$mtsrc = makethumb($logopre_src,$thumb_dir,300,260); //生成缩略图,在此基础上进行裁剪
	$imgresize = new ImageResize();
	$imgresize->load($mtsrc);
	$w = intval($_POST['x2']) - intval($_POST['x1']);
	$h = intval($_POST['y2']) - intval($_POST['y1']);
	$imgresize->cut($w,$h, intval($_POST['x1']), intval($_POST['y1']));
	$imgresize->save($mtsrc);
	$setsqlarr['logo']=substr($mtsrc,16);// 去除logo路径前面的 ../../data/logo/
	$wheresql="uid='".$uid."'";
	if (updatetable(table('company_profile'),$setsqlarr,$wheresql)) {
		write_memberslog($uid,1,8003,$_SESSION['username'],"上传了企业LOGO");
		echo $mtsrc;
	} else {
		echo '保存失败！';
	}
}
//猎头   	企业资料保存
elseif ($act=='company_profile_save')
{
	//其他非上传提交
	$setsqlarr['uid']=intval($_SESSION['parentuid']);
	// $setsqlarr['nature']=trim($_POST['nature'])?intval($_POST['nature']):showmsg('您选择企业性质！',1);
	// $setsqlarr['nature_cn']=trim($_POST['nature_cn']);
	$setsqlarr['trade']=trim($_POST['trade'])?trim($_POST['trade']):showmsg('您选择所属行业！',1);
	$setsqlarr['trade_cn']=trim($_POST['trade_cn']);
	$setsqlarr['district']=intval($_POST['district'])>0?intval($_POST['district']):showmsg('您选择所属地区！',1);
	$setsqlarr['sdistrict']=intval($_POST['sdistrict']);
	$setsqlarr['district_cn']=trim($_POST['district_cn']);
	$setsqlarr['scale']=trim($_POST['scale'])?trim($_POST['scale']):showmsg('您选择公司规模！',1);
	$setsqlarr['scale_cn']=trim($_POST['scale_cn']);  
	$setsqlarr['address']=trim($_POST['address'])?trim($_POST['address']):showmsg('请填写通讯地址！',1);   
	$setsqlarr['website']=trim($_POST['website']);
	$setsqlarr['contents']=trim($_POST['contents'])?trim($_POST['contents']):showmsg('请填写公司简介！',1);
	$setsqlarr['tag']=trim($_POST['tag']);
	//map
	$setsqlarr['map_x']=trim($_POST['x'])?trim($_POST['x']):showmsg('请先在地图上标记我的位置！',1);
	$setsqlarr['map_y']=trim($_POST['y'])?trim($_POST['y']):showmsg('请先在地图上标记我的位置！',1);
	$setsqlarr['map_zoom']=trim($_POST['zoom']); 
	//map end
	$link[0]['text'] = "查看修改结果";
	$link[0]['href'] = '?act=company_profile';
	$link[1]['text'] = "发布招聘信息";
	$link[1]['href'] = "company_jobs.php?act=choose_addjobs";
	$link[2]['text'] = "会员中心首页";
	$link[2]['href'] = "company_index.php?";
	if ($_CFG['company_repeat']=="0")
	{
		$info=$db->getone("SELECT uid FROM ".table('company_profile')." WHERE companyname ='{$setsqlarr['companyname']}' AND uid<>'{$parentuid}' LIMIT 1");
		if(!empty($info))
		{
			showmsg("{$setsqlarr['companyname']}已经存在，同公司信息不能重复注册",1);
		}
	}
	
	$_CFG['audit_edit_com']<>"-1"?$setsqlarr['audit']=intval($_CFG['audit_edit_com']):'';
	if (updatetable(table('company_profile'), $setsqlarr," uid='{$parentuid}'"))
	{
		$jobarr['scale']=$setsqlarr['scale'];
		$jobarr['scale_cn']=$setsqlarr['scale_cn'];	
		$jobarr['trade']=$setsqlarr['trade'];
		$jobarr['trade_cn']=$setsqlarr['trade_cn'];		
		if (!updatetable(table('jobs'),$jobarr," parentuid=".$setsqlarr['uid']."")) showmsg('修改公司基本信息出错！',0);
		$soarray['scale']=$jobarr['scale'];
		updatetable(table('jobs_search_scale'),$soarray," parentuid=".$setsqlarr['uid']."");
		updatetable(table('jobs_search_rtime'),$soarray," parentuid=".$setsqlarr['uid']."");
		updatetable(table('jobs_search_recommend'),$soarray," parentuid=".$setsqlarr['uid']."");
		updatetable(table('jobs_search_hot'),$soarray," parentuid=".$setsqlarr['uid']."");
		updatetable(table('jobs_search_key'),$soarray," parentuid=".$setsqlarr['uid']."");
		company_trade_save($parentuid,$company_info['id'],$jobarr['trade']);
		$jobs = $db->getall("select id from ".table('jobs')." where parentuid=".$parentuid);
		foreach ($jobs as $key => $value) {
			jobs_trade_save($_SESSION['parentuid'],$value['id'],$setsqlarr['trade']);
		}
		
		write_memberslog($uid,$_SESSION['utype'],8001,$_SESSION['username'],"/",$_SESSION['parentuid'],"修改企业资料",0);
		showmsg("保存成功！",2,$link);
	}
	else
	{
		showmsg("保存失败！",0);
	}
}
//账号管理
elseif ($act=='account_manage')
{
	global $db;
	if($parentuid==$uid){
		$super = 1;
	}else{
		$super = 0;
	}
	
	if($super){
		//合计
		$total[0]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE parentuid='{$main_user[uid]}' and jobs_type=0");
		$total[1]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE parentuid='{$main_user[uid]}' and jobs_type=1");
		$total[2]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE parentuid='{$main_user[uid]}' and jobs_type=2");
		$total[3]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_parentuid='{$main_user[uid]}' and r.talent=1");
		$total[4]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_parentuid='{$main_user[uid]}' and r.talent=2");
		$smarty->assign('total',$total);
		//得到超级会员发布的职位数目
		$sup_job_count[0]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$main_user[uid]}' and jobs_type=0");
		$sup_job_count[1]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$main_user[uid]}' and jobs_type=1");
		$sup_job_count[2]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$main_user[uid]}' and jobs_type=2");
		$sup_job_count[3]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_uid='{$main_user[uid]}' and r.talent=1");
		$sup_job_count[4]=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_uid='{$main_user[uid]}' and r.talent=2");
		$smarty->assign('sup_job_count',$sup_job_count);
	}
	
	//循环得到普通会员的各项数目
	if($super){
		$com_member=get_com_user_info($parentuid);
		foreach ($com_member as $key=>$value) 
		{
			$com_member[$key]['add_junior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$value[uid]}' and jobs_type=0");
			$com_member[$key]['add_senior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$value[uid]}' and jobs_type=1");
			$com_member[$key]['add_entrust']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$value[uid]}' and jobs_type=2");
			$com_member[$key]['down_junior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_uid='{$value[uid]}' and r.talent=1");
			$com_member[$key]['down_senior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_uid='{$value[uid]}' and r.talent=2");
		}
	}else{
		$com_member[0]=get_user_info($uid);
		$com_member[0]['add_junior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$uid}' and jobs_type=0");
		$com_member[0]['add_senior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$uid}' and jobs_type=1");
		$com_member[0]['add_entrust']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('jobs')." WHERE uid='{$uid}' and jobs_type=2");
		$com_member[0]['down_junior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_uid='{$uid}' and r.talent=1");
		$com_member[0]['down_senior']=$db->get_total("SELECT COUNT(*) AS num FROM ".table('company_down_resume')." as d left join ".table('resume')." as r on d.resume_id=r.id WHERE d.company_uid='{$uid}' and r.talent=2");
	}
	
	
	$smarty->assign('title','企业账号管理 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('company_info',$company_info);
	$smarty->assign('main_user',$main_user);
	$smarty->assign('com_member',$com_member);
	$smarty->assign('super',$super);
	$smarty->display('member_company/company_account_manage.htm');
}
//新增账号
elseif($act == "account_add")
{
	if($uid!=$parentuid){
		showmsg("您当前登录的是子帐号，没有创建账号权限！",1);
	}elseif($setmeal['endtime']!="0" && $setmeal['endtime']<=time()){
		showmsg('套餐已过期，请联系客服续费！',1);
	}elseif($setmeal['account']=="0"){
		showmsg('您当前的套餐权限不足！',1);
	}

	$smarty->assign('title','企业添加账号 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->display('member_company/company_add_account.htm'); 
}
//新增账号  保存
elseif ($act=='account_add_save')
{
	global $db,$QS_pwdhash,$online_ip,$timestamp;
	if($uid!=$parentuid){
		showmsg("您当前登录的是子帐号，没有创建账号权限！",1);
	}elseif($setmeal['endtime']!="0" && $setmeal['endtime']<=time()){
		showmsg('套餐已过期，请联系客服续费！',1);
	}elseif($setmeal['account']=="0"){
		showmsg('您当前的套餐权限不足！',1);
	}else{
		$total_sql="select COUNT(*) AS num from  ".table("members")." where parentuid = ".$uid;
		$total_val=$db->get_total($total_sql);
		if($total_val>=($setmeal['account']+1)){
			showmsg('您当前的套餐最大用户数为:'.$setmeal['account'].'!',1);
		}
	}
	$username = trim($_POST['username'])?trim($_POST['username']):showmsg('请填写账号！',1);
	$email=trim($_POST['email'])?trim($_POST['email']):showmsg('请填写邮箱！',1);	
	$ck_username=get_user_inusername($username);
	$ck_email=get_user_inemail($email);
	if(!empty($ck_username)) 
	{
		showmsg('用户名已经存在，请重填写用户名！',1);
	}
	elseif (!empty($ck_email))
	{
		showmsg('邮箱已经存在，请重填写邮箱！',1);
	}
	$password = trim($_POST['password'])?trim($_POST['password']):showmsg('请填写密码！',1);
	$password2 = trim($_POST['password2'])?trim($_POST['password2']):showmsg('请填写密码！',1);
	if($password !== $password2){
		showmsg('两次密码输入不一致，请重填写！',1);
	}else{
		if(strlen($password) < 6){
			showmsg('密码长度最少为6位，请重填写！',1);	
		}
	}
	$pwd_hash=randstr();
	$password_hash=md5(md5($password).$pwd_hash.$QS_pwdhash);
	
	$setsqlarr['parentuid']=intval($_SESSION['uid']);
	$setsqlarr['utype']='1';
	$setsqlarr['username']= $username;
	$setsqlarr['password']= $password_hash;
	$setsqlarr['pwd_hash']= $pwd_hash; 
	$setsqlarr['fullname']=trim($_POST['fullname'])?trim($_POST['fullname']):showmsg('请填写姓名！',1);
	$setsqlarr['sex']=trim($_POST['sex'])?intval($_POST['sex']):showmsg('请选择性别！',1);
	$setsqlarr['sex_cn'] = trim($_POST['sex']) == '1'?'男':'女';
	$setsqlarr['mobile']=trim($_POST['mobile']);
	if (empty($setsqlarr['mobile']) || !preg_match("/^(13|15|18)\d{9}$/",$setsqlarr['mobile']))
	{
		showmsg("手机号错误！",1);
	}
	$sql = "select * from ".table('members')." where mobile = '{$setsqlarr['mobile']}' LIMIT 1";
	$userinfo_bymobile=$db->getone($sql);
	if ($userinfo_bymobile && $userinfo_bymobile['uid']<>$uid)
	{
		showmsg("手机号已经存在！请填写其他手机号码",1);
	}
	$setsqlarr['email']=$email;
	$setsqlarr['reg_time']= $timestamp;
	$setsqlarr['reg_ip']= $online_ip;
	$setsqlarr['consultant'] = $main_user['consultant'];//分配客服

	$link[0]['text'] = "查看添加的结果";
	$link[0]['href'] = '?act=account_manage'; 
	$link[2]['text'] = "会员中心首页";
	$link[2]['href'] = "company_index.php?";
	 
	$query_insert = inserttable(table('members'),$setsqlarr);
	if($query_insert){
		showmsg("保存成功！",2,$link);
	}
}
elseif($act == "account_edit")
{
	$uid = intval($_GET['uid'])>0?intval($_GET['uid']):showmsg("参数错误！",1);
	$smarty->assign('title','企业修改账号 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('userinfo',get_user_info($uid));
	$smarty->display('member_company/company_edit_account.htm'); 
}	
//修改账号  保存
elseif ($act=='account_edit_save')
{
	global $QS_pwdhash;
	$uid=intval($_POST['uid'])>0?intval($_POST['uid']):showmsg('参数错误！',1);
	if($uid!=intval($_SESSION['uid']) && intval($_SESSION['uid'])!=intval($_SESSION['parentuid'])){
		showmsg('权限验证错误！',1);
	}
	$userinfo = get_user_info($uid);
	$username = trim($_POST['username'])?trim($_POST['username']):showmsg('请填写账号！',1);
	$email=trim($_POST['email'])?trim($_POST['email']):showmsg('请填写邮箱！',1);	
	if($userinfo['username']!=$username){
		$ck_username=get_user_inusername($username);
		if(!empty($ck_username)) 
		{
			showmsg('用户名已经存在，请重填写用户名！',1);
		}
	}
	if($userinfo['email']!=$email){
		$ck_email=get_user_inemail($email);
		if (!empty($ck_email))
		{
			showmsg('邮箱已经存在，请重填写邮箱！',1);
		}
	}
	
	
	$setsqlarr['username']= $username;
	$setsqlarr['fullname']=trim($_POST['fullname'])?trim($_POST['fullname']):showmsg('请填写姓名！',1);
	$setsqlarr['sex']=trim($_POST['sex'])?intval($_POST['sex']):showmsg('请选择性别！',1);
	$setsqlarr['sex_cn'] = trim($_POST['sex']) == '1'?'男':'女';
	$setsqlarr['mobile']=trim($_POST['mobile']);
	if (empty($setsqlarr['mobile']) || !preg_match("/^(13|15|18)\d{9}$/",$setsqlarr['mobile']))
	{
		showmsg("手机号错误！",1);
	}
	if($userinfo['mobile']!=$setsqlarr['mobile']){
		$sql = "select * from ".table('members')." where mobile = '{$setsqlarr['mobile']}' LIMIT 1";
		$userinfo_bymobile=$db->getone($sql);
		if ($userinfo_bymobile && $userinfo_bymobile['uid']<>$uid)
		{
			showmsg("手机号已经存在！请填写其他手机号码",1);
		}
	}
	
	$setsqlarr['email']=$email;
	 
	$link[0]['text'] = "查看修改的结果";
	$link[0]['href'] = '?act=account_edit&uid='.$uid; 
	$link[2]['text'] = "会员中心首页";
	$link[2]['href'] = "company_index.php?";
	 
	if(updatetable(table('members'),$setsqlarr,array("uid"=>$uid))){
		showmsg("保存成功！",2,$link);
	}
}
// 单独修改密码
elseif ($act=='account_password_edit_save')
{
	global $QS_pwdhash;
	$uid=intval($_POST['uid'])>0?intval($_POST['uid']):showmsg('参数错误！',1);
	if($uid!=intval($_SESSION['uid']) && intval($_SESSION['uid'])!=intval($_SESSION['parentuid'])){
		showmsg('权限验证错误！',1);
	}
	$password = trim($_POST['npassword']);
	if(md5(md5($password).$userinfo['pwd_hash'].$QS_pwdhash)!=$userinfo['password']){
		$pwd_hash=randstr();
		$password_hash=md5(md5($password).$pwd_hash.$QS_pwdhash);
		$setsqlarr['password']= $password_hash;
		$setsqlarr['pwd_hash']= $pwd_hash; 
	}
	if(updatetable(table('members'),$setsqlarr,array("uid"=>$uid))){
		showmsg("保存成功！",2,$link);
	}
}
//删除子帐号
elseif ($act=='account_del')
{
	$uid =intval($_GET['uid'])>0?intval($_GET['uid']):showmsg("参数错误！",1);
	if(intval($_SESSION['uid'])!=intval($_SESSION['parentuid']) || $uid==intval($_SESSION['uid'])){
		showmsg('权限验证错误！',1);
	}
	if(del_account($uid))
	{
	showmsg("删除成功！",2);
	}
	else
	{
	showmsg("删除失败！",0);
	}
}
//使用信息
elseif($act == 'company_log')
{	
	if($parentuid==$uid){
		$wheresql = " where log_mode=1 and log_parentuid=".$parentuid." and log_utype=1 ";
	}else{
		$wheresql = " where log_mode=1 and log_uid=".$uid." and log_utype=1 ";
	}
	$log_type = $_POST['log_type'];
	$starttime = strtotime($_POST['starttime']);
	$endtime = strtotime($_POST['endtime']);
	if($starttime){
		$wheresql .= " and log_addtime>".$starttime." ";
	}
	if($endtime){
		$wheresql .= " and log_addtime<".($endtime+3600*24)." ";
	}
	$type_sql = " ";
	$type_str = "";
	foreach($log_type as $v){
		$type_sql .= 'or log_type='.$v.' ';
		$type_str .= ",".$v;
	}
	$type_str = ltrim($type_str,",");
	if(!empty($type_arr)){
		$type_json = json_encode($type_arr);
	}
	$type_sql = trim($type_sql)!=""?" and (".ltrim(trim($type_sql),"or").") ":"";
	$wheresql .= $type_sql;
	require_once(QISHI_ROOT_PATH.'include/page.class.php'); 
	$perpage=10;
	$total_sql="select COUNT(*) AS num from qs_members_log ".$wheresql." ";
	$total_val=$db->get_total($total_sql);
	$page = new page(array('total'=>$total_val, 'perpage'=>$perpage));
	$offset=($page->nowindex-1)*$perpage;
	$sql = "select * from qs_members_log ".$wheresql." order by log_id desc limit ".$offset.",".$perpage;
	$log = $db->getall($sql);
	$smarty->assign('type_str',$type_str);
	$smarty->assign('starttime',$_POST['starttime']);
	$smarty->assign('endtime',$_POST['endtime']);
	$smarty->assign('log',$log);
	$smarty->assign('title','企业使用信息 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('page',$page->show(5)); 
	$smarty->display('member_company/company_log.htm');
}
//同步绑定
elseif($act == 'company_binding')
{	
	$smarty->assign('title',"同步绑定 - 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('user',get_user_info(intval($_SESSION['uid'])));
	$smarty->display('member_company/company_binding.htm');
}
// 账户金额
elseif($act== 'company_accounts')
{
	$smarty->assign('act',$act);
	$smarty->assign('title',"账户金额 - 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('my_setmeal',get_user_setmeal($_SESSION['parentuid']));
	// 账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->assign('userinfo',get_user_info(intval($_SESSION['uid'])));
	$smarty->assign('ip_address',$ip_address);
	// 账户资金日志
	$wheresql=" where puid=".intval($_SESSION['parentuid']);
	require_once(QISHI_ROOT_PATH.'include/page.class.php'); 
	$perpage=5;
	$total_sql="select COUNT(*) AS num from ".table("account_log").$wheresql." ";
	$total_val=$db->get_total($total_sql);
	$page = new page(array('total'=>$total_val, 'perpage'=>$perpage));
	$offset=($page->nowindex-1)*$perpage;
	$smarty->assign('account_log_list',get_account_log($offset,$perpage,$wheresql));
	if($total_val>$perpage)
	{
		$smarty->assign('page',$page->show(5));
	}
	$smarty->display('member_company/company_account_money.htm');
}
// 我的套餐
elseif($act== 'company_setmeal')
{
	$smarty->assign('act',$act);
	$smarty->assign('title',"我的套餐- 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('my_setmeal',get_user_setmeal($_SESSION['parentuid']));
	// 各种套餐详情
	$sql="select * from ".table("setmeal")." where apply=1 and expense>0 order by id asc ";
	$row=$db->getall($sql);
	$smarty->assign('setmeal_list',$row);
	// 账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->display('member_company/company_setmeal.htm');
}
// 购买套餐选择支付方式支付
elseif($act=="company_setmeal_pay")
{
	$setmeal_id=$_GET['setmeal_id']?intval($_GET['setmeal_id']):showmsg("请选择套餐！",1);
	$smarty->assign('act',$act);
	$smarty->assign('title',"选择支付方式- 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('userinfo',get_user_info(intval($_SESSION['uid'])));
	$smarty->assign('ip_address',$ip_address);
	// 选择套餐的信息
	$setmeal_info=get_setmeal_one($setmeal_id);
	if($setmeal_info['discount']>0)
	{
		$setmeal_info['expense']=$setmeal_info['expense']*$setmeal_info['discount']*0.1;
	}
	$smarty->assign('setmeal_info',$setmeal_info);
	// 可抵现的最大积分
	if($points>$_CFG['points_to_money_max'])
	{
		$smarty->assign('points_to_money_max',$_CFG['points_to_money_max']);
	}
	else
	{
		$smarty->assign('points_to_money_max',$points);
	}
	// 企业当前的套餐信息
	$smarty->assign('my_setmeal',get_user_setmeal(intval($_SESSION['parentuid'])));
	// 支付方式
	$smarty->assign("pay_type",get_payment());
	// 账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->display('member_company/company_setmeal_pay.htm');
}
// 生成购买套餐订单
elseif($act == 'order_add')
{	
	$myorder=get_user_order(intval($_SESSION['parentuid']),1);
	$order_num=count($myorder);
	if ($order_num>=5)
	{
	$link[0]['text'] = "立即查看";
	$link[0]['href'] = '?act=company_recharge&is_paid=1';
	showmsg("未处理的订单不能超过 5 条，请先处理后再次申请！",1,$link,true,8);
	}
	$payment_name=empty($_POST['pay_type'])?showmsg("请选择付款方式！",1):$_POST['pay_type'];
	$paymenttpye=get_payment_info($payment_name);
	if (empty($paymenttpye)) showmsg("支付方式错误！",0);
	$amount=intval($_POST['pay_money']);
	$fee=number_format(($amount/100)*$paymenttpye['fee'],1,'.','');//手续费
	$order['oid']= strtoupper(substr($paymenttpye['typename'],0,1))."-".date('ymd',time())."-".date('His',time());//订单号
	$order['v_url']=$_CFG['main_domain']."include/payment/respond_".$paymenttpye['typename'].".php";
	$order['v_amount']=$amount+$fee; 
	$setmeal_id=intval($_POST['setmeal_id']);
	$setmeal_info=get_setmeal_one($setmeal_id);
	$amount_points=intval($_POST['porint_to_money']);
	$order_id=add_order(intval($_SESSION['parentuid']),$order['oid'],$order['v_amount'],$amount_points,$payment_name,"购买套餐-".$setmeal_info['setmeal_name'],$timestamp,0,$setmeal_id,$utype='1');
	if ($order_id)
		{
			$myorder=get_order_one($_SESSION['uid'],$order_id);
			if($myorder['payment_name']=='balance')
			{
				// 账户余额付款
				action_user_account($_SESSION['uid'],1,$myorder['amount'],2);
				write_account_log($_SESSION['uid'],$_SESSION['parentuid'],1,$_SESSION['username'],$myorder['description']."账户余额付款--".$myorder['amount']."元","购买成功");
				order_paid($myorder['oid']);
				header("location:?act=company_setmeal");
			}
			else
			{
				header("location:?act=payment&order_id=".$order_id);
			}
		}
		else
		{
		showmsg("添加订单失败！",0);
		}
}
elseif($act=="order_del")
{
	$id=intval($_GET["id"]);
	$uid=intval($_SESSION['uid']);
	del_order($uid,$id)?showmsg("取消订单成功！",2):showmsg("取消订单失败！",0);
}
//企业购买套餐确认订单
elseif ($act=='payment')
{
	$smarty->assign('payment',get_payment());
	$order_id=intval($_GET['order_id']);
	$myorder=get_order_one($_SESSION['uid'],$order_id);
	$payment=get_payment_info($myorder['payment_name']);
	if (empty($payment)) showmsg("支付方式错误！",0);
	$fee=number_format(($myorder['amount']/100)*$payment['fee'],1,'.','');//手续费
	$order['oid']=$myorder['oid'];//订单号
	$order['v_url']=$_CFG['main_domain']."include/payment/respond_".$payment['typename'].".php";
	$order['v_amount']=$myorder['amount']+$fee;
	if ($myorder['payment_name']!='remittance' && $myorder['payment_name']!='balance')//假如是非线下支付，
	{
		require_once(QISHI_ROOT_PATH."include/payment/".$payment['typename'].".php");
		$payment_form=get_code($order,$payment);
		if (empty($payment_form)) showmsg("在线支付参数错误！",0);
	}
	$smarty->assign('points',get_user_points($_SESSION['uid']));
	$smarty->assign('title','确认订单 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('fee',$fee);
	$smarty->assign('amount',$myorder['amount']);
	$setmeal_info=get_setmeal_one($myorder['setmeal']);
	$smarty->assign('setmeal_name',$setmeal_info['setmeal_name']);
	$smarty->assign('oid',$order['oid']);
	$smarty->assign('byname',$payment);
	$smarty->assign('payment_form',$payment_form);
	$smarty->display('member_company/company_setmeal_order_pay.htm');
}
elseif($act== "company_recharge")
{
	$smarty->assign('act',$act);
	$smarty->assign('title',"账户充值-订单列表- 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('userinfo',get_user_info(intval($_SESSION['uid'])));
	$smarty->assign('ip_address',$ip_address);
	$getsql=" select * from ".table("order")." where ";
	$wheresql=" uid=".intval($_SESSION['parentuid'])." and utype=1 ";
	$is_paid=intval($_GET['is_paid']);
	if($is_paid>0)
	{
		$wheresql.=" and is_paid={$is_paid} ";
	}
	require_once(QISHI_ROOT_PATH.'include/page.class.php'); 
	$perpage=5;
	$total_sql="select COUNT(*) AS num from ".table("order")." where ".$wheresql." ";
	$total_val=$db->get_total($total_sql);
	$page = new page(array('total'=>$total_val, 'perpage'=>$perpage));
	$offset=($page->nowindex-1)*$perpage;
	$smarty->assign('order_list',get_order_list($offset,$perpage,$getsql.$wheresql));
	if($total_val>$perpage)
	{
		$smarty->assign('page',$page->show(7));
	}
	// 账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->display('member_company/company_order_list.htm');
}
// 账户充值选择付款方式
elseif($act=="company_recharge_paytype")
{
	$account_funds=$_REQUEST['account_funds']?$_REQUEST['account_funds']:showmsg("请输入充值金额！",1);
	$smarty->assign('act',$act);
	$smarty->assign('title',"账户充值-选择付款方式- 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('userinfo',get_user_info(intval($_SESSION['uid'])));
	$smarty->assign('ip_address',$ip_address);
	$smarty->assign('my_setmeal',get_user_setmeal(intval($_SESSION['parentuid'])));
	$smarty->assign("account_funds",$account_funds);
	$smarty->assign("pay_type",get_payment());
	//当前账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->display('member_company/company_recharge_pay.htm');
}
// 账户充值添加订单
elseif($act=="account_order_add")
{
	$myorder=get_user_order(intval($_SESSION['parentuid']),1);
	$order_num=count($myorder);
	if ($order_num>=5)
	{
	$link[0]['text'] = "立即查看";
	$link[0]['href'] = '?act=company_recharge&is_paid=1';
	showmsg("未处理的订单不能超过 5 条，请先处理后再次申请！",1,$link,true,8);
	}
	$payment_name=empty($_POST['pay_type'])?showmsg("请选择付款方式！",1):$_POST['pay_type'];
	$paymenttpye=get_payment_info($payment_name);
	if(empty($paymenttpye))showmsg("支付方式错误！",0);
	$amount=intval($_POST['pay_money']);
	$fee=number_format(($amount/100)*$paymenttpye['fee'],1,'.','');//手续费
	$order['oid']= strtoupper(substr($paymenttpye['typename'],0,1))."-".date('ymd',time())."-".date('His',time());//订单号
	$order['v_url']=$_CFG['main_domain']."include/payment/respond_".$paymenttpye['typename'].".php";
	$order['v_amount']=$amount+$fee; 
	$order_id=add_order(intval($_SESSION['parentuid']),$order['oid'],$amount,0,$payment_name,"账户充值-".$amount,$timestamp,0,0,$utype='1');
	if ($order_id)
		{
		header("location:?act=payment&order_id=".$order_id);
		}
		else
		{
		showmsg("添加订单失败！",0);
		}
}
//企业提现表单
elseif($act=="company_withdrawals")
{
	$smarty->assign('act',$act);
	$smarty->assign('title',"账户金额 - 申请提现 - 企业会员中心 - ".$_CFG['site_name']);
	$smarty->assign('my_setmeal',get_user_setmeal($_SESSION['parentuid']));
	// 账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->assign('userinfo',get_user_info(intval($_SESSION['uid'])));
	$smarty->assign('ip_address',$ip_address);
	// 支付方式
	$pay_type=get_payment();
	foreach ($pay_type as $key => $value)
	{
		if($value['typename']=="alipay" || $value['typename']=="tenpay")
		{
			$arr[]=$value;
		}
	}
	$smarty->assign('pay_type',$arr);
	$smarty->display('member_company/company_withdrawals.htm');
}
elseif($act=="company_withdrawals_order_add")
{
	$myorder=get_account_withdrawal(intval($_SESSION['parentuid']),1);
	if (!empty($myorder))
	{
	$link[0]['text'] = "立即查看";
	$link[0]['href'] = "?act=company_withdrawals_order_show&order_id=".$myorder['id'];
	showmsg("您的账户有未处理的申请提现记录,不能申请提现！",1,$link);
	}
	$payment_name=empty($_POST['pay_type'])?showmsg("请选择付款方式！",1):$_POST['pay_type'];
	$paymenttpye=get_payment_info($payment_name);
	if (empty($paymenttpye)) showmsg("支付方式错误！",0);
	$extract_money=floatval($_POST['extract_money']);
	$fee=number_format(($extract_money/100)*$paymenttpye['fee'],1,'.','');//手续费
	$setarr['oid']= "TX".date('ymd',time())."-".date('His',time());//订单号
	$setarr['uid']=intval($_SESSION['parentuid']);
	$setarr['utype']=intval($_SESSION['utype']);
	$setarr['username']=$_SESSION['username'];
	$setarr['extract_money']=$extract_money-$fee;
	$setarr['extract_pay_type']=$_POST['pay_type'];
	$setarr['extract_pay_account']=empty($_POST['extract_pay_account'])?showmsg("请输入提现帐号！",1):$_POST['extract_pay_account'];
	$setarr['extract_pay_uname']=empty($_POST['extract_pay_uname'])?showmsg("请输入姓名！",1):$_POST['extract_pay_uname'];
	$setarr['addtime']=time();
	$order_id=inserttable(table("account_withdrawal"),$setarr,1);
	if ($order_id)
		{
		header("location:?act=company_withdrawals_order_show&order_id=".$order_id);
		}
		else
		{
		showmsg("添加订单失败！",0);
		}
}
elseif($act=="company_withdrawals_order_show")
{
	$smarty->assign('payment',get_payment());
	$order_id=intval($_GET['order_id']);
	$order_one=get_account_withdrawa_one($_SESSION['parentuid'],$order_id);
	$payment=get_payment_info($order_one['extract_pay_type']);
	if (empty($payment)) showmsg("支付方式错误！",0);
	$fee=number_format(($order_one['extract_money']/100)*$payment['fee'],1,'.','');//手续费
	$smarty->assign('title','申请提现 - 企业会员中心 - '.$_CFG['site_name']);
	$smarty->assign('fee',$fee);
	$smarty->assign('order_one',$order_one);
	$smarty->assign('byname',$payment['byname']);
	$smarty->assign('userinfo',get_user_info(intval($_SESSION['uid'])));
	$smarty->assign('ip_address',$ip_address);
	$smarty->assign('my_setmeal',get_user_setmeal(intval($_SESSION['parentuid'])));
	// 账户情况
	$account=get_user_account(intval($_SESSION['parentuid']));
	if(empty($account))
	{
		$account['available_funds']=0;
		$account['blocked_funds']=0;
		$smarty->assign('account',$account);
	}
	else
	{
		$smarty->assign('account',get_user_account(intval($_SESSION['parentuid'])));
	}
	$smarty->display('member_company/company_withdrawals_order_show.htm');

}
unset($smarty);
?>