//订单填写：非巴士

//接收参数:param
//详：
//pro_cate_id: '',     父级产品id
//pro_cate_name:'迪士尼乐园门票'   //父级产品名称
//start_date: '',		乘车日期(游玩日期)
//prod_arr:[{prod_id:1,prod_count:1,prod_name:"成人"},{prod_id:2,prod_count:2,prod_name:"儿童"}]  产品明细数组
//total_money: 385,     订单总金额
var param = null;
window.onload = function() {
	ifLogin(function(flag) {
		$('body').show();
		param = getStorJson('fill_order_unbus_20170306');
		//			param = {
		//				pro_cate_id: '1',
		//				pro_cate_name: "迪士尼乐园门票",
		//				start_date: "2017-03-08",
		//				prod_arr: [{ prod_id: '1', prod_count: '1', prod_name: "成人" }, { prod_id: '2', prod_count: '1', prod_name: "儿童" }],
		//				total_money: '200'
		//			}
		if(!param) {
			alert('缺少参数');
			$('body').hide();
			return;
		}
		$('body').show();
		var m = getMonthAndDayByDate(param['start_date'], 'month');
		var d = getMonthAndDayByDate(param['start_date'], 'day');
		$('#start_date').text('游玩日期：'+m+'月'+d+'日');
		var arr = param['prod_arr'];
		var HTML = '';
		for(var i = 0, m = arr.length; i < m; i++) {
			var dict = arr[i];
			if(dict['prod_count'] <= 0) {
				continue;
			}
			var cstr = '';
			if(i != 0 && dict['prod_count'] > 0) {
				cstr = '<br>';
			}
			var tempHTML = cstr+'<span class="text_c_2b ulev1">'+dict['prod_count']+'</span>'+
							'<span class="text_c_2b ulev1 ui_m_r05">'+dict['prod_name']+'</span>';
			HTML += tempHTML;
		}
		$('#person_count').html(HTML);

		$('#pro_cate_name').text(param['pro_cate_name']);
		$('#total_balance').text(param['total_money']);
	});
}

$(document).ready(function() {

//	$('#contact_cardId').on('keyup', function() {
//		var value = $(this).val().replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");
//		$(this).val(value);
//	})
	//去结算
	$('#go_settlement').on('click', function() {
		var prod_arr = [];
		for(var i = 0, m = param['prod_arr'].length; i < m; i++) {
			var dict = param['prod_arr'][i];
			var obj = {
				prod_id: dict['prod_id'],
				prod_count: dict['prod_count']
			}
			prod_arr.push(obj);
		}
		var _pro_cate_id = param['pro_cate_id'] ? param['pro_cate_id'] : "";
		var _start_date = param['start_date'] ? param['start_date'] : "";
		var _prod_arr = JSON.stringify(prod_arr) ? JSON.stringify(prod_arr) : [];
		var _contacts_name = $('#contact_person').val();
		var _contacts_phone = $('#contact_phone').val();
		var _contacts_ID = $('#contact_cardId').val();
		var _total_money = param['total_money'] ? param['total_money'] : "";
		if(_pro_cate_id.trim().length <= 0) return;
		if(_start_date.trim().length <= 0) return;
		if(_prod_arr.length <= 0) return;
		if(_contacts_name.trim().length <= 0) {
			alert('请输入预订人姓名');
			$('#contact_person').focus();
			return;
		}
		if(_contacts_phone.trim().length != 11) {
			alert('手机号格式不正确');
			$('#contact_phone').focus();
			return;
		}
//		if(_contacts_ID.trim().length != 18) {
//			alert('身份证号码格式不正确');
//			$('#contact_cardID').focus();
//			return;
//		}
		$('#loadingToast').show();
		var _data = {
			pro_cate_id: _pro_cate_id,
			start_date: _start_date,
			prod_arr: _prod_arr,
			contacts_name: _contacts_name.trim(),
			contacts_phone: _contacts_phone.trim(),
			contacts_ID: _contacts_ID.trim(),
			total_money: _total_money,
		}
		ZZLog(base_api + 'prod/prod/make-order', JSON.stringify(_data));
		submit();

		function submit() {
			$.ajax({
				type: "post",
				url: base_api + 'prod/prod/make-order',
				data: _data,
				dataType: 'json',
				success: function(res_data) {
					$('#loadingToast').hide();
					ZZLog(res_data);
					if(false == res_data['flag']) {
						if(codeNotLogin == res_data['code']) {
							location.href = res_data['url'] + '/web/shop/';
						} else {
							alert(res_data['msg']);
						}
					} else {
                        location.href = res_data['data']['pay_url'];
					}
				},
				error: function() {
					$('#loadingToast').hide();
					alert(infoApiError);
				}
			});
		}
	})

})