<?php
include_once "./config.php";
$type = @$_POST['type'];
if ($type == 'bind') {
    $name = @$_POST['name'];
    $quId=@$_POST['quId'];
    $qu=$quarr[$quId];
    $recommend = @$_POST['recommend'];
    $conn = new mysqli($db_ip, $db_name, $db_psd, $server);
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
        exit("异常！");
    }
    $roleID = getRoleIDByName($qu['url'], $name);
    if ($roleID == 0) {
        exit("该角色不存在！");
     }
    $userinfo = mysqli_query($conn, "SELECT * FROM user WHERE (uid='" . $name . "' or roleid='" . $roleID . "') and server='".$quId."' limit 1");
    if ($userinfo && mysqli_num_rows($userinfo) > 0) {
        exit("您已经绑定过了，不可再次绑定！");
    } else {
        $dl = mysqli_query($conn, "SELECT * FROM zhucema WHERE ma='" . $recommend . "' limit 1");
        if ($dl && mysqli_num_rows($dl) > 0) {
        } else {
            exit("此福利码不存在，请返回上级页面刷新后输入！");
        }
        //赠送操作
        $params = array("ids" => $roleID, "title" => "管理员的邮件-入群福利", "content" => "入群福利", "items" => $qu['zs'],);
        if (mailByRoleName($qu['url'], $params)) {
            mysqli_query($conn, "insert INTO `user`(`uid`, `roleid`, `recommend`, `bindtime`,`welfare`,`server`) VALUES ('" . $name . "','" . $roleID . "', '" . $recommend . "', '" . date("Y-m-d H:i:s", time()) . "',1,".$quId.")");
            exit("绑定并领取成功，请返回游戏在邮件内领取！");
        }
    }
}
?>



<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>入群福利</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<link rel="stylesheet" href="style/weui.css">
<link rel="stylesheet" href="style/weuix.css">
<script src='./static/js/jquery-2.1.4.min.js' type='text/javascript'></script>
<style>
.page-bg {
	max-width: 750px;
	margin: 0 auto;
}
.weui-cells {
	margin-top:0;
	border: 1px solid #eee;
	border-radius: 1em;
}
.bgc-b{
	background-color:#fff;
}

</style>
</head>
<body  class="page-bg">
    <div class="weui-cell none1 bgc-b tc-div">
            <div class="weui-cell__hd"><label class="weui-label">选择区服:</label></div>
            <div class="weui-cell__bd">
                 <select id="qu" style="color: #777373;border: 1px solid #78bf77;width: 40%;border-radius: 6px;padding: 5px;">
					<?php 
					foreach ($quarr  as $key => $value) {
                       if($key == @$_GET['qu']){
					        echo "<option value='".$key."' selected='selected'>".$value["name"]." </option>";
					    }else{
					        echo "<option value='".$key."'>".$value["name"]." </option>";
					    }
					}
	                ?>
				</select>
            </div>
        </div>
		 <div class="weui-cell none1 bgc-b">
            <div class="weui-cell__hd"><label class="weui-label">角色名称 :</label></div>
            <div class="weui-cell__bd" >
                <input class="weui-input" style="height:120px" name="user_name" value="<?php echo @$_GET['rolename']?>" id="user_name" type="text" placeholder="请确保输入正确">
            </div>
        </div>
		 <div class="weui-cell none1 bgc-b">
            <div class="weui-cell__hd"><label class="weui-label">福利码 :</label></div>
            <div class="weui-cell__bd">
                <input  style="height:120px" class="weui-input" name="recommend1" id="recommend1" type="text" placeholder="请向群主索取福利码">
				<p style="color:red;font-size:13px;position: absolute;bottom:0;"></p>
            </div>
        </div>
			
        <div class="weui-pay-btn">
            <button type="submit" class="weui-btn weui-btn_primary" onclick="pay()" style="color: #84d187;
    border: 1px solid #84d187;background-color:initial;">绑定并领取福利</button>
        </div>
    </div>
</div>
<div class="weui-footer weui-footer_fixed-bottom">
</div>
    <script type="text/javascript">
	<?php
	if(@$_GET['rolename']){
	    echo 'alert("您的角色未绑定福利码，请先点击确定后进行绑定")';
	}
	
	?>
	

// 这个脚本是 ie6和ie7 通用的脚本
function custom_close() {
	if(confirm("您确定要关闭本页吗？")) {
		window.opener=null;
		window.open('','_self');
		window.close();
	} 
}
function welfare(){
	var name=$("input[name=user_name]").val();
	var recommend=$("#recommend1").val();
	if(!name) {
		alert("请输入角色UID！");
		return;
	}else{
		$.ajax({
			url:'./welfare.php?uid='+name+'&recommend='+recommend,
			type:'get',
			success:function(res){
				alert(res);
			}
		})
	}
}


function pay() {
	var name=$("input[name=user_name]").val();
	var recommend=$("#recommend1").val();
	var qu=$("#qu").val();
	
	if(!qu) {
		alert("请选择区服！");
		return;
	}
	if(!name){
	    alert("请输入角色名")
	    return
	}
	if(!recommend){
	    alert("请输入福利码")
	    return
	}
	$.ajax({
	    type:"post",
	    url:"./bind.php",
	    data:{
	        name:name,
	        quId:qu,
	        recommend:recommend,
	        type:"bind",
	    },
	    success:function(data){
	        alert(data)
	    }
	})
}
</script>
</body>
</html>