<?php
require '../conn/conn.php';
require '../conn/function.php';
$action=$_GET["action"];
if($action=="ad"){
	$A_type=intval($_POST["A_type"]);
	$A_long=intval($_POST["A_long"]);
	$A_text=$_POST["A_text"];
	$A_url=$_POST["A_url"];
	$A_contact=$_POST["A_contact"];
	if($A_url==""){
		die("{\"code\":\"error\",\"msg\":\"请输入广告链接\"}");
	}
	switch($A_type){
		case 0:
		$A_price=$C_adprice1;
		if($A_text==""){
			die("{\"code\":\"error\",\"msg\":\"请输入广告文字\"}");
		}
		break;
		case 1:
		$A_price=$C_adprice2;
		$A_pic=$_POST["A_pic"];
		if($A_pic==""){
			die("{\"code\":\"error\",\"msg\":\"请上传广告图片\"}");
		}
		break;
		case 2:
		$A_price=$C_adprice3;
		$A_pic=$_POST["A_pic2"];
		if($A_pic==""){
			die("{\"code\":\"error\",\"msg\":\"请上传广告图片\"}");
		}
		break;
	}
	$time=date('Y-m-d H:i:s');
	mysqli_query($conn,"insert into sl_orders(O_title,O_attach,O_money,O_time,O_contact) values('广告申请','$A_type|$A_long|$A_text|$A_pic|$A_url',".($A_price*$A_long).",'$time','$A_contact')");
	$notify_url=gethttp().$_SERVER["HTTP_HOST"]."/pay/notify_url.php";
	$return_url=gethttp().$_SERVER["HTTP_HOST"]."/pay/return_url.php";
	$no=getrs("select O_id from sl_orders where O_title='广告申请' and O_time='$time' order by O_id desc","O_id");
	$sign=strtolower(md5("body=广告申请&fee=".($A_price*$A_long)."&no=".date('YmdHis').$no."&notify_url=".$notify_url."&pid=".$C_pid."&remark=".$no."&return_url=".$return_url."&key=".$C_pkey));
	die("{\"code\":\"success\",\"msg\":\"https://7-pay.cn/pay.php?body=广告申请&fee=".($A_price*$A_long)."&no=".date('YmdHis').$no."&notify_url=".$notify_url."&pid=".$C_pid."&remark=".$no."&return_url=".urlencode($return_url)."&sign=".$sign."\"}");
}
$A_pic="nopic.png";
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"> 
	<title>申请广告</title>
	<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">  
	<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
	<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="../upload/upload.js"></script>
	<style type="text/css">
		.showpic{height: 60px;border: solid 1px #DDDDDD;padding: 5px;}
	</style>
</head>
<body>
<div class="container">
	<a href="../" ><img src="../media/<?php echo $C_logo?>" style="margin: 10px;height: 60px;"></a>
<div class="panel panel-info">
	<div class="panel-heading">
		申请广告
	</div>
	<div class="panel-body">

		<form class="form-horizontal" id="form" role="form" onsubmit="ad();return false;">
			<div class="form-group">
				<label  class="col-sm-2 control-label">广告类型</label>
				<div class="col-sm-10" style="padding-top: 7px">
					<label onclick="change(0)"><input type="radio" value="0" name="A_type" checked="checked"> 首页文字广告（<?php echo $C_adprice1?>元/月）</label>
					<label onclick="change(1)"><input type="radio" value="1" name="A_type"> 首页图片广告（<?php echo $C_adprice2?>元/月）</label>
					<label onclick="change(2)"><input type="radio" value="2" name="A_type"> 跳转页广告（<?php echo $C_adprice3?>元/月）</label>
				</div>
			</div>


			<div class="form-group" id="A_textx">
				<label  class="col-sm-2 control-label">广告文字</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" name="A_text" placeholder="最多输入18个文字，多余字符会被省略">
				</div>
			</div>
			<div class="form-group" id="A_picx">
				<label  class="col-sm-2 control-label">广告图片</label>
				<div class="col-sm-10">
					<div class="row">
                      <div class="col-xs-3">
                      <img src="../media/<?php echo $A_pic?>" id="A_picx" class="showpic" onClick="showUpload('A_pic','A_pic','../media',1,null,'','');">
                    </div>
                    <div class="col-xs-9">
                            <div class="input-group">
                              <input type="text" id="A_pic" name="A_pic" class="form-control" value="" placeholder="图片像素220*120，超出的部分会被裁切">
                              <span class="input-group-btn">
                                      <button class="btn btn-primary m-b-5 m-t-5" type="button" onClick="showUpload('A_pic','A_pic','../media',1,null,'','');">上传</button>
                              </span>
                      </div>
                    </div>
                  </div>
				</div>
			</div>

			<div class="form-group" id="A_pic2x">
				<label  class="col-sm-2 control-label">广告图片</label>
				<div class="col-sm-10">
					<div class="row">
                      <div class="col-xs-3">
                      <img src="../media/<?php echo $A_pic?>" id="A_pic2x" class="showpic" onClick="showUpload('A_pic2','A_pic2','../media',1,null,'','');">
                    </div>
                    <div class="col-xs-9">
                            <div class="input-group">
                              <input type="text" id="A_pic2" name="A_pic2" class="form-control" value="" placeholder="图片像素500*120，超出的部分会被裁切">
                              <span class="input-group-btn">
                                      <button class="btn btn-primary m-b-5 m-t-5" type="button" onClick="showUpload('A_pic2','A_pic2','../media',1,null,'','');">上传</button>
                              </span>
                      </div>
                    </div>
                  </div>
				</div>
			</div>

			<div class="form-group">
				<label  class="col-sm-2 control-label">广告链接</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" name="A_url" placeholder="输入点击广告链接到的网址">
				</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-2 control-label">投放时长</label>
					<div class="col-sm-10">
						<select class="form-control" name="A_long">
							<option value="1">1个月</option>
							<option value="2">2个月</option>
							<option value="3">3个月</option>
							<option value="6">6个月</option>
							<option value="12">12个月</option>
						</select>
					</div>
			</div>
			<div class="form-group">
				<label  class="col-sm-2 control-label">联系方式</label>
				<div class="col-sm-10">
					<input type="text" class="form-control" name="A_contact" placeholder="可以输入QQ/手机/微信/邮箱，方便确认细节">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">说明：</label>
					<div class="col-sm-10" style="padding-top: 7px">
						请勿上传违法违规广告，否则会被强制下线
			        </div>
			</div>

			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-info">申请广告</button> <span id="msg" style="color: #ff0000"></span>
				</div>
			</div>
		</form>
	</div>
</div>
</div>
<script type="text/javascript">
function ad(){
	$.ajax({
	        type: "post",
	        url: "?action=ad",
	        data: $("#form").serialize(),
	        success: function(data) {
				data=JSON.parse(data);
				if(data.code=="success"){
					window.location.href=data.msg;
				}else{
					$("#msg").html(data.msg);
				}
	        }
	    })
}
function change(id){
	if(id==0){
		$("#A_textx").show();
		$("#A_picx").hide();
		$("#A_pic2x").hide();
	}
	if(id==1){
		$("#A_textx").hide();
		$("#A_picx").show();
		$("#A_pic2x").hide();
	}
	if(id==2){
		$("#A_textx").hide();
		$("#A_picx").hide();
		$("#A_pic2x").show();
	}
}
change(0);
</script>
</body>
</html>