<?php
 /*
 * 74cms 申请职位
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once(dirname(__FILE__).'/../include/common.inc.php');
$act = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'app';
require_once(QISHI_ROOT_PATH.'include/mysql.class.php');
$db = new mysql($dbhost,$dbuser,$dbpass,$dbname);
if ($_SESSION['uid']=='' || $_SESSION['username']=='')
{
	$captcha=get_cache('captcha');
	$smarty->assign('verify_userlogin',$captcha['verify_userlogin']);
	$smarty->display('plus/ajax_login.htm');
	exit();
}
if ($_SESSION['utype']!='2')
{
	exit('<div class="floatContent">
			<div class="float_common_box">
				<div class="float_tips">
					<div>必须是个人会员可以申请职位！</div>
				</div>
			</div>
		</div>');
}
require_once(QISHI_ROOT_PATH.'include/fun_personal.php');
require_once(QISHI_ROOT_PATH.'include/fun_user.php');
$user=get_user_info($_SESSION['uid']);
if ($user['status']=="2") 
{
	exit('<div class="floatContent">
			<div class="float_common_box">
				<div class="float_tips">
					<div>您的账号处于暂停状态，请先联系网站管理员设为正常后进行操作！</div>
				</div>
			</div>
		</div>');
}
if ($act=="app")
{		
		$id=isset($_GET['id'])?$_GET['id']:exit("id 丢失");
		$jobs=app_get_jobs($id);
		if (empty($jobs))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>申请职位失败！</div>
					</div>
				</div>
			</div>');
		} 
?>
<script type="text/javascript">
$(".but80").hover(function(){$(this).addClass("but80_hover")},function(){$(this).removeClass("but80_hover")});
//计算今天申请数量
var app_max="<?php echo $_CFG['apply_jobs_max'] ?>";
var app_today="<?php echo get_now_applyjobs_num($_SESSION['uid']) ?>";
$(".ajax_app_tip > span:eq(0)").html(app_max);
$(".ajax_app_tip > span:eq(1)").html(app_today);
$(".ajax_app_tip > span:eq(2)").html(app_max-app_today);
//验证
$("#ajax_app").die().live('click',function() {
	if (app_max-app_today==0 || app_max-app_today<0 )
	{
	alert("您今天申请的职位数量已经超出最大限制！");
	}
	else if ($("#app :checkbox[checked]").length>(app_max-app_today))
	{
	alert("您今天还可以申请"+(app_max-app_today)+"个职位，已选职位超出了最大限制！");
	}
	else if ($("#app :checkbox[checked]").length==0)
	{
	alert("请选择申请的职位！");
	}
	else
	{
		$("#app").hide();
		$("#waiting").show();
		var tsTimeStamp= new Date().getTime(); 
			 var jidArr=new Array();
			 $("#app :checkbox[checked]").each(function(index){jidArr[index]=$(this).val();});
		$.post("<?php echo $_CFG['site_dir'] ?>user/user_apply_jobs.php", { "resumeid": $("#app :radio[checked]").val(),"jobsid": jidArr.join("-"),"notes": $("#notes").val(),"time":tsTimeStamp,"act":"app_save"},
	 	function (data,textStatus)
	 	 {
			if (data=="ok")
			{
				$(".ajax_app_tip").hide();
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
			}
			else if(data=="repeat")
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("您已经申请过该职位，不能重复申请！");
			}
			else
			{
				$("#app").hide();
				$("#waiting").hide();
				$("#app_ok").show();
				$(".float_auth_success").html("申请失败！"+data);
			}
	 	 })
	}
});
function DialogClose()
{
	$("#FloatBg").hide();
	$("#FloatBox").hide();
}
</script> 
<?php
	$max_apply_jobs = $_CFG['apply_jobs_max'];
	$apply_job_num = get_now_applyjobs_num($_SESSION['uid']);
	
?>
<div class="floatContent" id="app">
	<div class="float_common_box">
		<div class="float_top_tip ajax_app_tip">您每天可以申请<span><?php echo $max_apply_jobs; ?></span>个职位，今天已经申请了<span><?php echo $apply_job_num;?></span>个，还可以申请<span><?php echo $max_apply_jobs - $apply_job_num;?></span>个</div>
		<table class="float_auth_table">
			<tbody>
				<tr height="40">
					<td align="right">要申请的职位：</td>
					<td>
						<div class="float_control">
							<?php
							foreach($jobs as $j){
							?>
							 <label><input name="jobsid" type="checkbox" value="<?php echo $j['id']?>" checked="checked" /><?php echo $j['jobs_name']?></label>
							<?php }?>
						</div>
					</td>
				</tr>
				<tr height="40">
					<td align="right">其他描述：</td>
					<td>
						<div class="float_control">
							<textarea name="notes" id="notes"  style="width:300px; height:60px; line-height:180%; font-size:12px;"></textarea>
						</div>
					</td>
				</tr>
			</tbody>
		</table>
		<div class="float_btn_guoup">
			<input type="submit" value="提交" name="Submit2" class="float_but55lan" id="ajax_app">&nbsp;&nbsp;&nbsp;
			<input type="button" value="取消" class="float_but55hui DialogClose">
		</div>
	</div>
</div>
<div class="floatContent" style="display:none" id="waiting" >
	<div class="float_common_box" >
		<img src="<?php echo  $_CFG['site_template']?>images/30.gif"  border="0"/>
	</div>
</div>
<div class="floatContent" style="display:none" id="app_ok" >
	<div class="float_common_box" >
		<div class="float_auth_success">职位申请成功！<a href="<?php echo get_member_url(2,true)?>personal_apply.php?act=apply_jobs" >查看已申请的职位</a></div>
	</div>
</div>
<?php
}
elseif ($act=="app_save")
{
	$jobsid=isset($_POST['jobsid'])?$_POST['jobsid']:exit("出错了");
	$resumeid = get_pid($_SESSION['uid']);
	$notes=isset($_POST['notes'])?trim($_POST['notes']):"";
	$jobsarr=app_get_jobs($jobsid);
	if (empty($jobsarr))
	{
		exit("职位丢失");
	}
	$resume_basic=get_resume_info($_SESSION['uid'],$resumeid);
	$resume_basic = array_map("addslashes", $resume_basic);
	if (empty($resume_basic))
	{
	exit("简历丢失");
	}
	$i=0;
	foreach($jobsarr as $jobs)
	{
		$jobs = array_map("addslashes",$jobs);
		if (check_jobs_apply($jobs['id'],$resumeid,$_SESSION['uid']))
		{
		 continue ;
		}
		$personal_fullname=$resume_basic['fullname'];
		$addarr['resume_id']=$resumeid;
		$addarr['resume_name']=$personal_fullname;
		$addarr['personal_uid']=intval($_SESSION['uid']);
		$addarr['jobs_id']=$jobs['id'];
		$addarr['jobs_name']=$jobs['jobs_name'];
		$addarr['belong_id']=$jobs['company_id'];
		$addarr['belong_name']=$jobs['companyname'];
		$addarr['belong_uid']=$jobs['uid'];
		$addarr['belong_parentuid']=$jobs['parentuid'];
		$addarr['notes']= $notes;
		$addarr['apply_addtime']=time();
		$addarr['is_look']=1;
		$insert_id = inserttable(table('personal_jobs_apply'),$addarr,1);
		if($insert_id){
			$mailconfig=get_cache('mailconfig');					
			$jobs['contact']=$db->getone("select * from ".table('jobs_contact')." where pid='{$jobs['id']}' LIMIT 1 ");
			$sms=get_cache('sms_config');	
			$comuser=get_user_info($jobs['uid']);	
			if ($mailconfig['set_applyjobs']=="1"  && $comuser['email_audit']=="1")
			{	
				dfopen("{$_CFG['site_domain']}{$_CFG['site_dir']}plus/asyn_mail.php?uid={$_SESSION['uid']}&key=".asyn_userkey($_SESSION['uid'])."&act=jobs_apply&jobs_id={$jobs['id']}&jobs_name={$jobs['jobs_name']}&personal_fullname={$personal_fullname}&email={$comuser['email']}");
			}
			//sms	
			if ($sms['open']=="1"  && $sms['set_applyjobs']=="1"  && $comuser['mobile_audit']=="1")
			{
			//修正bug,求职者申请职位不发送短信
				dfopen("{$_CFG['site_domain']}{$_CFG['site_dir']}plus/asyn_sms.php?uid={$_SESSION['uid']}&key=".asyn_userkey($_SESSION['uid'])."&act=jobs_apply&jobs_id=".$jobs['id']."&jobs_name=".$jobs['jobs_name'].'&jobs_uid='.$jobs['uid']."&personal_fullname=".$personal_fullname."&mobile=".$comuser['mobile']);
			}
		}
		$i=$i+1;
	 }
	 if ($i==0)
	 {
	 exit("repeat");
	 }
	 else
	 {
	 exit("ok");
	 }
}
?>
