var total_price = 0;
var total_no = 1;
var prod_Arr = [];
//var category_id = getStorage("category_id");
//var pro_cate_id = getStorage("pro_cate_id");
var category_id = '';
var pro_cate_id = '';
var is_dsn = "";
var pro_cate_name = "";
var run_date = after(getDateTime(0));
//狮子王不能选日期
var this_year = getMonthAndDayByDate(getDateTime(0), 'year');
var lion_date = "";


var show_price_date_list = {};

var show_calendar_type = ''; //"date_type": ,  0 普通，1平日票，2 周末票

$(document).ready(function() {
	category_id = category_id ? category_id : getPar('category_id');
	pro_cate_id = pro_cate_id ? pro_cate_id : getPar('pro_cate_id');
	ifLogin(function(flag) {
		$('body').show();
		httpLoadInfo();
	},"scene_ticket.html?fx_uid="+getFxUid()+"&category_id="+category_id+"&pro_cate_id="+pro_cate_id);

});

function httpLoadInfo() {
	var year = getMonthAndDayByDate(run_date, 'year');
	var month = getMonthAndDayByDate(run_date, 'month');
	var day = getMonthAndDayByDate(run_date, 'day');
	//get_data();
	$("#run_date").text(month + "月" + day + "日");
	$("#sel_date").text(month + "月" + day + "日");
	get_data();
	$(".model").on("click", function(e) {
		$(".model_box").animate({
			height: "hide"
		}, 400, function() {
			$(".model").hide();
			$("#body_mod").removeClass("mod_fil");
		});
	});

	$(".model_box").on("click", function(e) {
		e.stopPropagation();
	});
}

function get_data() {
	$.ajax({
//		url: base_api + "prod/prod/get-prod-detail", //请求地址
		url: base_api + "prod/entrance-ticket/initialize", //请求地址
		data: { prod_cate_id: pro_cate_id, category_id: category_id },
		type: "post",
		dataType: "json",
		async: false,
		success: function(res_data) {
			ZZLog(res_data);
			var data = res_data['data'];
			if(res_data.flag) {
				show_calendar_type = data.date_type;
				is_dsn = data.is_dsn;
				var choose_date = data.date;
				run_date = choose_date;
				show_price_date_list = data.date_list;
				var cstr = getMonthAndDayByDate(choose_date,'month')+'月'+getMonthAndDayByDate(choose_date,'day')+"日";
				$('#run_date').text(cstr);
				$('#sel_date').text(cstr);
				list_html(data);
				$("#bg_img").css("background-image", "url(" + data.show_img + ")");
				$(".top_title").html(data.pro_cate_name);
				pro_cate_name = data.pro_cate_name;
			} else {
				if(codeNotLogin == res_data['code']) {
					alert(infoNotLogin);
					var URL = '/web/shop/scene_ticket.html?fx_uid='+getFxUid();
					URL = encodeURI(URL);
					location.href = res_data['url'] + URL;
				} else if(codeNotRegister == res_data['code']) {
					location.href = 'register.html?fx_uid='+getFxUid();
				} else {
					alert(res_data['msg']);
				}
			}
		},
		error: function(error) {
			alert(infoApiError);
		}
	});
}
//得到与当前日期相差几天的日期  +   -
	function getDatebyDayCount(addDayCount, curDate) {
		var d = new Date(curDate);
		d.setDate(d.getDate() + addDayCount); //获取AddDayCount天后的日期
		//格式化
		var year = ";" + d.getFullYear() + ";";
		var month = ";" + (d.getMonth() + 0 + 1) + ";";
		var day = ";" + d.getDate() + ";";
		if(year.length < 4) {
			year = "0" + year;
		}
		if(month.length < 4) {
			month = "0" + month;
		}
		if(day.length < 4) {
			day = "0" + day;
		}
		var datestr = year + "-" + month + "-" + day;
		datestr = datestr.replace(/;/g, "");
		return datestr;
	}
function list_html(data) {
	var temp = "";
	for(var i = 0, j = data.prod_arr.length; i < j; i++) {
		var str = '<span class="ulev3 count aticket" type="text" readonly="true" one_price="' + data.prod_arr[i].prod_price + '" prod_name="' + data.prod_arr[i].prod_name + '" prod_id="' + data.prod_arr[i].prod_id + '">0</span>';
		var img = 'images/jianhao.png';
		if (i == 0) {
			img = 'images/jianhao_sel.png';
			str = '<span class="ulev3 count aticket" type="text" readonly="true" one_price="' + data.prod_arr[i].prod_price + '" prod_name="' + data.prod_arr[i].prod_name + '" prod_id="' + data.prod_arr[i].prod_id + '">1</span>'
		}
		temp += '<div class="ub ub-ver disney_div">' +
			'<div style="padding-bottom: 0.12rem;">' + data.prod_arr[i].prod_name + '</div>' +
			'<div class="ub">' +
			'<div class="ub-f1 ub">' +
			'<img class="cut" style="width: 0.22rem;height: 0.22rem;vertical-align: middle;" src="'+img+'"/>' +
//			'<span class="ulev3 count aticket" type="text" readonly="true" one_price="' + data.prod_arr[i].prod_price + '" prod_name="' + data.prod_arr[i].prod_name + '" prod_id="' + data.prod_arr[i].prod_id + '">0</span>' +
			str+
			'<img class="add" style="width: 0.22rem;height: 0.22rem;vertical-align: middle;" src="images/jiahao_sel.png"/>' +
			'</div>' +
			'<div class="ub-f1 ub tx-r">' +
			'<div class="ub-f1 ub ub-pe ub-ae font_price" >￥</div>' +
			'<div class="ulev3 ub ub-ae font_price" style="line-height: 1;">' + data.prod_arr[i].prod_price + '<span style="font-size:0.12rem">/张</span></div>' +
			'</div>' +
			'</div>' +
			'</div>'
	}
	$("#tick_type").html(temp);
	//订票须知 
	if(data.booking_notice) {
		var booking_notice_arr = data.booking_notice.split('<br/>');
		var bnHTML = '';
		for(var i = 0, m = booking_notice_arr.length; i < m; i++) {
			bnHTML += '<div class="font_gray">'+booking_notice_arr[i]+'</div>';
		}
		$(".booking_notice").html(bnHTML);
	}
	//加
	$(".add").on("touchstart", function() {
		if(is_dsn == "1") {
//			var tot_no = 0;
			$(".aticket").each(function(index, domE) {
				total_no += parseInt($(this).text());
			});
			if(total_no == 5) {
				alert("迪士尼最多只能买五张票");
			} else {
				var current = $(this).parent().find(".aticket").text();
				var now_count = parseInt(current) + 1;
				$(this).parent().find(".aticket").text(now_count);
				$(this).parent().find(".cut").attr("src", "images/jianhao_sel.png");
//				countTotal();
			}
		} else {
			var current = $(this).parent().find(".aticket").text();
			var now_count = parseInt(current) + 1;
			$(this).parent().find(".aticket").text(now_count);
			$(this).parent().find(".cut").attr("src", "images/jianhao_sel.png");
//			countTotal();
		}

	});

	//减
	$(".cut").on("touchstart", function() {
		var current = $(this).parent().find(".aticket").text();
		var now_count = parseInt(current) - 1;
		if(now_count > 0) {
			$(this).parent().find(".aticket").text(now_count);
		} else {
			$(this).parent().find(".aticket").text('0');
			$(this).attr("src", "images/jianhao.png");
		}
//		countTotal();
	})
}

//总价
function countTotal() {
	total_price = 0;
	prod_Arr = [];
	prod_info = "";
	total_no = 0;
	var tick_no = '';
	$(".aticket").each(function(index, domE) {
		tick_no = parseInt($(this).text());
		var tick_price = parseFloat($(this).attr("one_price"));
        total_price = (total_price * 100 + tick_no * tick_price * 100) / 100;
		total_no += tick_no;
		prod_info = {
			"prod_id": $(this).attr("prod_id"),
			"prod_count": $(this).text(),
			"prod_name": $(this).attr("prod_name")
		}
		prod_Arr.push(prod_info);
	});

}

$("#date_select").on("click", function() {
	$('.model_box').removeClass('fadeInUp animated');
	$('.model_box').removeClass('fadeInDown animated');
	var caType = window.calendar.init.showTypeEnum.show_price_day;
	
	window.calendar.init({
		startDate: getDateTime(0),
		chooseDate: run_date,
		showType:caType,
		showPriceDays:show_price_date_list
	}, function(year, month, day, ymdString) {
		$('.model_box').addClass('fadeInDown animated');
		setTimeout(function() {
			$('.model').hide();
			$('#body_mod').removeClass('mod_fil');
		}, 400);
		run_date = ymdString;
		$("#run_date").text(month + "月" + day + "日");
		$("#sel_date").text(month + "月" + day + "日");

		$.ajax({
			type:"post",
			url:base_api+"prod/entrance-ticket/get-prod-arr",
			data:{prod_cate_id:pro_cate_id,date:ymdString},
			dataType: "json",
			success:function(res_data){
				ZZLog(res_data);
				if(false==res_data['flag']){
					if(codeNotLogin == res_data['code']) {
						alert(infoNotLogin);
						var URL = '/web/shop/scene_ticket.html?fx_uid='+getFxUid();
						URL = encodeURI(URL);
						location.href = res_data['url'] + URL;
					} else if(codeNotRegister == res_data['code']) {
						location.href = 'register.html?'+version+'&fx_uid='+getFxUid();
					} else {
						alert(res_data['msg']);
					}
				}else{
					list_html(res_data['data']);
				}
			},error:function(){
				alert(infoApiError);
			}
		});


	});
	$(".model").show();
	$('.model_box').show();
	$("#body_mod").addClass("mod_fil");
	$('.model_box').addClass('fadeInUp animated');
	//	$(".model_box").animate({
	//		height: "show"
	//	}, 400);

});

function reserve() {
	countTotal();
	if(total_no == "0") {
		alert("请选择票种数量");
	} else {
		var fill_order_unbus_20170306 = {
			"pro_cate_id": pro_cate_id,
			"pro_cate_name": pro_cate_name,
			"start_date": run_date,
			"prod_arr": prod_Arr,
			"total_money": total_price
		}
		setStorJson("fill_order_unbus_20170306", fill_order_unbus_20170306);
		console.log(fill_order_unbus_20170306);
		window.location.href = "fill_order_unbus.html?"+version+"&fx_uid="+getFxUid();
	}

}