<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: install.php 34718 2014-07-14 08:56:39Z nemohou $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(!$_G['uid']) {
	showmessage('not_loggedin', NULL, array(), array('login' => 1));
}
$uid = intval($_G['uid']);
$siteid = $_G['wechat']['setting']['wsq_siteid'];
$applyid = intval($_GET['applyid']);

if($_GET['action'] == 'full'){
	$result = array();
	if($_GET['formhash'] != $_G['formhash']){
		$result['showmessage'] = $_G['charset']=='gbk'?iconv('GBK','UTF-8',lang('message','submit_invalid')):lang('message','submit_invalid');
		$result['full'] = 2;
		echo json_encode($result); 
		exit;
	}
	$apply = DB::fetch_first("SELECT eid,applynumber FROM ".DB::table('xj_eventapply')." WHERE applyid=$applyid");
	$items = DB::fetch_first("SELECT * FROM ".DB::table('xj_event')." A,".DB::table('forum_thread')." B WHERE A.tid=B.tid AND A.eid=".$apply['eid']);
	//活动已成功报名人总数
	$ybnumber = DB::result_first("SELECT sum(applynumber) FROM ".DB::table('xj_eventapply')." WHERE eid=".$apply['eid']." AND verify=1");
	if($items['event_number']>0 && intval($_GET['applynumber'])>($items['event_number']-($ybnumber-$apply['applynumber']))){
		$result['showmessage'] = $_G['charset']=='gbk'?iconv('GBK','UTF-8',lang('plugin/xj_event','baomrsym')):lang('plugin/xj_event','baomrsym');
		$result['full'] = 2;
		echo json_encode($result); 
		exit;
	}
	$applys = array();
	$applys['realname'] =  addslashes($_GET['realname']);
	$applys['mobile'] =  addslashes($_GET['mobile']);
	$applys['qq'] =  addslashes($_GET['qq']);
	$applys['bmmessage'] =  addslashes($_GET['message']);
	$applys['applynumber'] = intval($_GET['applynumber']);
	$applys['session'] = intval($_GET['session']);
	$ufielddata = array();
	$selectuserfield = unserialize($items['userfield']);
	$sysuserfield = unserialize($_G['setting']['activityfield']);
	foreach($sysuserfield as $key => $value){
		if(in_array($key,$selectuserfield)){
			if(is_array($_GET[$key])){
				$ufielddata[$key] = implode(',',$_GET[$key]);
			}elseif($key=='birthday'){
				$ufielddata[$key] = $_GET['birthyear'].'-'.$_GET['birthmonth'].'-'.$_GET['birthday'];
			}elseif($key=='residecity'){
				$ufielddata[$key] = $_GET['resideprovince'].$_GET['residecity'].$_GET['residedist'].$_GET['residecommunity'];
			}else{
				$ufielddata[$key] = $_GET[$key];
			}
		}
	}
	$applys['ufielddata'] = serialize($ufielddata);
	DB::update('xj_eventapply',$applys,"applyid=$applyid");
	//发提醒
	notification_add($items['authorid'], 'system', $_G['username'].' '.lang('plugin/xj_event','xglhd').' '.$items['subject'].' '.lang('plugin/xj_event','baominzl').'，<a href="forum.php?mod=viewthread&tid='.$items['tid'].'" target="_blank">'.lang('plugin/xj_event','view').'</a>', array());

	$result['showmessage'] = $_G['charset']=='gbk'?iconv('GBK','UTF-8',lang('plugin/xj_event', 'bmzlxgcg')):lang('plugin/xj_event', 'bmzlxgcg');
	$result['full'] = 1;
	echo json_encode($result); 
	exit;

}



$apply = DB::fetch_first("SELECT * FROM ".DB::table('xj_eventapply')." WHERE applyid=$applyid");
$items = DB::fetch_first("SELECT * FROM ".DB::table('xj_event')." WHERE eid=".$apply['eid']);
$items['setting'] = unserialize($items['setting']);
$setting = $items['setting'];
if($apply['verify']==1 && !$items['setting']['noverify']){
	showmessage('quickclear_noperm');
}




$apply['ufielddata'] = unserialize($apply['ufielddata']);
$userfield = unserialize($items['userfield']);
$tid = intval($items['tid']);
$selectuserfield = unserialize($items['userfield']);


if($selectuserfield) {
	if($selectuserfield) {
		$htmls = $settings = array();
		require_once libfile('function/profile');
		foreach($selectuserfield as $fieldid) {
			if(empty($ufielddata['userfield'])) {
				$memberprofile = C::t('common_member_profile')->fetch($_G['uid']);
				foreach($selectuserfield as $val) {
					if($val == 'birthday'){
						$tmp = explode('-',$apply['ufielddata']['birthday']);
						$ufielddata['userfield']['birthyear'] =  $tmp[0];
						$ufielddata['userfield']['birthmonth'] =  $tmp[1];
						$ufielddata['userfield']['birthday'] =  $tmp[2];	
					}elseif($val == 'realname'){
						$ufielddata['userfield']['realname'] = $apply['realname'];
					}elseif($val == 'mobile'){
						$ufielddata['userfield']['mobile'] = $apply['mobile'];
					}else{
						$ufielddata['userfield'][$val] = $apply['ufielddata'][$val];//$memberprofile[$val];
					}
				}
				unset($memberprofile);
			}



			
			$html = profile_setting($fieldid, $ufielddata['userfield'], false, true);
			if($html) {
				$settings[$fieldid] = $_G['cache']['profilesetting'][$fieldid];
				$htmls[$fieldid] = $html;
			}

		}

		
	}
} else {
	$selectuserfield = '';
}


//报名时可能选择的人数
$items['event_number_max'] = $items['event_number_max']>0?$items['event_number_max']:1;
$applynumber = array();
for($i=1;$i<=$items['event_number_max'];$i++){
	$applynumber[] = $i;
}
$thread =  DB::fetch(DB::query("SELECT subject FROM ".DB::table('forum_thread')." WHERE tid = '$tid'"));


?>
<script type="text/javascript" src="http://wsq.discuz.com/cdn/discuz/js/openjs.js"></script>
<script>
	var menu = new Array();
	WSQ.initBtmBar(menu);
	WSQ.showBtmBar();
	WSQ.initPlugin({name:'<?php echo $thread['subject'].lang('plugin/xj_event', 'baomin'); ?>'});

	var initWx = {
		'img': '<?php echo $_G['siteurl']; ?>/static/image/common/logo.png',
		'desc': '<?php echo $_G['bbsname'].lang('plugin/xj_event', 'huodzx'); ?>',
		'title': '<?php echo $thread['subject']; ?>',
	'pluginid':'xj_event:wsq_join_test',
		'param': 'a=1&b=2'
	};
	WSQ.initShareWx(initWx);
</script>
    
    
<style type="text/css">
    #container {margin:5px;}
    body {height:100%;}
	.input1{padding:14px; line-height:14px; width:100%;-moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius:4px; border:1px solid #d8d8d8;}
    select{height:42px;-webkit-appearance:none;appearance:none;border:none;font-size:14px;padding:0px 10px;display:block;width:100%;-webkit-box-sizing:border-box;box-sizing:border-box;background-color: #FFFFFF;color:#aaa;border-radius:4px;}
	.selectdiv{margin-top:0px;outline:none;-moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius:4px; border:1px solid #d8d8d8;position:relative;}
	.checkbox {
 width: 19px;
 height: 25px;
 padding: 0 5px 0 0;
 background: url(checkbox.png) no-repeat;
 display: block;
 float: left;
}
	label{float:left; font-size:14px; line-height:30px;}
</style>
<script type="text/javascript" src="source/plugin/xj_event/js/jquery.js"></script>
<script type="text/javascript">
$(function(){ 
     $("#testbtn").click(function(){
		  $.ajax({ 
			  type: 'POST', 
			  url: 'plugin.php?id=xj_event:wsq_join_modify&tid=<?php echo $tid; ?>&applyid=<?php echo $applyid; ?>&action=full', 
			  //data: {level:'fff', roomnumber:'bbb'},
			  data:$('#bmform').serialize(),
			  dataType: 'json', 
			  cache: false, 
			  error: function(){ 
				  alert('error'); 
				  return false; 
			  }, 
			  success:function(json){ 
			  	  if(json.full == 2){
					  alert(json.showmessage);
					  if(json.tourl != null){
					  	top.location.href=json.tourl;
					  }
					  return false;		  
				  }
				  if(json.full == 1){
					  alert('<?php echo lang('plugin/xj_event','bmzlxgcg'); ?>');
					  top.location.href="http://wsq.discuz.com/?c=index&a=viewthread&f=wx&tid=<?php echo $tid; ?>&siteid=<?php echo $siteid; ?>&_bpage=1";
					  return true;
				  }else{
					  alert('error'); 
					  return false; 
				  }
			  } 
		  });
    }); 
}); 
</script>
</head>
<body>
<div>
	<div style="height:20px;">
    </div>
    <form name="bmform" id="bmform">
    <input type="hidden" name="formhash" value="<?php echo $_G['formhash'];?>" />
    <input type="hidden" name="eid" value="<?php echo $items['eid'];?>" />

    
    <div style="padding:10px 30px;">
        <div class="selectdiv"> 
        <select name="session"> 
        <option><?php echo lang('plugin/xj_event','huodongcc'); ?></option>
        <?php foreach($setting['session'] as $key=>$value){ ?>
        <option value="<?php echo $key; ?>" <?php if($apply['session'] == $key){echo 'selected';} ?>><?php echo $value; ?></option>
        <?php } ?>
        </select> 
        </div> 
    </div>
    
    
	<?php 
	
	if(!empty($selectuserfield)){
    	foreach($selectuserfield as $fieldid){
			if($settings[$fieldid]['available']){
				if($settings[$fieldid]['formtype'] != 'file'){
					$htmls[$fieldid] = str_replace('class="px"','class="input1" placeholder="'.$settings[$fieldid]['title'].'"',$htmls[$fieldid]);
            		$htmls[$fieldid] = preg_replace("/<div.+<\/div>/is", "", $htmls[$fieldid]);
					if(strpos($htmls[$fieldid],'select')>0){
						$htmls[$fieldid] = str_replace('tabindex="1">','tabindex="1"><option>'.$settings[$fieldid]['title'].'</option>',$htmls[$fieldid]);
						echo '<div style="padding:10px 30px;">'.$htmls[$fieldid].'</div>';
					}elseif(strpos($htmls[$fieldid],'checkbox')>0){
						//$htmls[$fieldid] = str_replace('type="checkbox"','type="checkbox" class="checkbox"',$htmls[$fieldid]);
						echo '<div style="padding:10px 30px;">'.$htmls[$fieldid].'<div style="clear:both;"></div></div>';
					}else{
						echo '<div style="padding:10px 30px;">'.$htmls[$fieldid].'</div>';
					}
				}else{
				}
			}
		}
	}
    ?>
    
    
    
    
    <div style="padding:10px 30px;">
        <div class="selectdiv"> 
        <select name="applynumber"> 
        <option><?php echo lang('plugin/xj_event','baomingrs'); ?></option>
        <?php foreach($applynumber as $key=>$value){ ?>
        <option value="<?php echo $value; ?>" <?php if($apply['applynumber'] == $value){echo 'selected';} ?>><?php echo $value; ?></option>
        <?php } ?>
        </select> 
        </div> 
    </div>

	<div style="padding:10px 30px;">
    	<input name="message" type="text" placeholder="<?php echo lang('plugin/xj_event','liuyan'); ?>" class="input1">
    </div>
	</form>

	<a href="javascript:" id="testbtn" style="display:block; height:44px; line-height:44px; text-align:center; background-color:#ff4a00; color:#FFF;-moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius:4px; margin:10px 30px; font-size:16px; font-weight:bold;"><?php echo lang('plugin/xj_event','xgbmzl'); ?></a>
    
</div>  
</body>
</html>
