/**
 * [OElove] (C)2010-2099 oelove.com Inc. biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/01/13 by CL $
*/
//AJAX请求数据
function _getGiftPageList($page) {
	if (typeof($page) == "undefined") {
		$page = 1;
	}
	$touid = $("#gift_touid").val(); //接收会员ID
	$catid = $("#gift_catid").val(); //分类
	$obj_box = $("#gift_json_data");
	$.ajax({
		type: "GET",
		url:  _ROOT_PATH+"index.php?m=user&c=gift&a=list&datatype=json",
		cache: false,
		data: {touid:$touid, s_catid:$catid, page:$page, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$prepage = $json.prepage; //上一页
			$nextpage = $json.nextpage; //下一页
			$result = $json.result;
			if ($result.length > 0) {
				$obj_box.html($result);
				if ($prepage > 0) { //有上一页
					$("#gift_pre").attr("data-page", $prepage);
					$("#gift_pre").show();
				}
				else { 
					$("#gift_pre").hide();
				}

				if ($nextpage > 0) { //还有下一页
					$("#gift_next").attr("data-page", $nextpage);
					$("#gift_next").show();
				}
				else { 
					$("#gift_next").hide();
				}
			}
		},
		error: function() {
		
		}
	});
}


$(function(){
	//选择礼物分类
	$(document).on("change", "#gift_catid", function(){
		_getGiftPageList(1);
	});

	//加载上一页、下一页数据
	$(document).on("click", "[f='but_get_giftpage']", function(){
		$_page = $(this).attr("data-page");
		_getGiftPageList($_page);
	});

	//选中某个礼物
	$(document).on("click", "[f='but_select_gift']", function(){
		$_giftid = $(this).attr("data-id");
		$_touid = $(this).attr("data-touid");

		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?m=user&c=gift",
			cache: false,
			data: {a:"select", id:$_giftid, touid:$_touid, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$("#send_gift_select_data").html($result);
					$("#send_gift_select_box").show();
					$("#send_gift_select_data").fadeIn(JS_FADEIN_TIME);
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("获取数据失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});
	
	//关闭某个礼物
	$(document).on("click", "[f='but_send_gift_select_close']", function(){
		$("#send_gift_select_data").fadeOut(JS_FADEOUT_TIME, function(){
			$("#send_gift_select_box").hide();
		});
	});

	//确定赠送礼物
	$(document).on("click", "[f='but_confirm_send_gift']", function(){
		$_giftid = $(this).attr("data-id");
		$_touid = $(this).attr("data-touid");
		$_content = $("#gift_content").val();

		$("#gift_id").val($_giftid);
		$("#gift_touid").val($_touid);
		
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=user&c=gift",
			cache: false,
			data: {a:"send", giftid:$_giftid, touid:$_touid, content:$_content, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("赠送成功");
					//关闭选择礼物窗
					$("#send_gift_select_data").fadeOut(JS_FADEOUT_TIME, function(){
						$("#send_gift_select_box").hide();
					});
				}
				else {
					//选择支付方式		
					$("#send_gift_pay_box").show();
					//varPopMarginAuto("send_gift_pay_data"); //居中
					$_height = parseInt($("#send_gift_pay_data").height());
					$_css_height = (550-$_height)/2;
					$("#send_gift_pay_data").css("top", $_css_height+"px");
					$("#send_gift_pay_data").fadeIn(JS_FADEIN_TIME);
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});

	//关闭支付方式
	$(document).on("click", "[f='but_send_gift_pay_close']", function(){
		$("#send_gift_pay_data").fadeOut(JS_FADEIN_TIME, function(){
			$("#send_gift_pay_box").hide();
		});
	});

	//使用支付方式赠送礼物
	$(document).on("click", "[f='but_send_gift_confirm_pay']", function(){
		$sdkid = $(this).attr("data-sdkid");
		$_giftid = $("#gift_id").val();
		$_touid = $("#gift_touid").val();
		$_content = $("#gift_content").val();
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=user&c=gift",
			cache: false,
			data: {a:"send", giftid:$_giftid, touid:$_touid, content:$_content, sdkid:$sdkid, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					if ($result.length > 0) { //在线支付
						window.location.href = _ROOT_PATH+"index.php?c=pay&a=submit&paynum="+$result;
					}
					else {
						ToastShow("赠送成功");
						//关闭选择礼物窗
						$("#send_gift_select_data").fadeOut(JS_FADEOUT_TIME, function(){
							$("#send_gift_select_box").hide();
						});
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("提交失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});


	});	

});
