var param = getStorJson('fill_order_20170307');
window.onload = function() {
	ifLogin(function(flag) {
		$('body').show();
		httpLoadInfo();
	})

}

function httpLoadInfo() {

	if(!param) {
		alert('缺少参数');
		$('body').hide();
		return;
	}
	$('body').show();
	//去程信息
	var go_dict = param.go_info;
	var go_month = getMonthAndDayByDate(go_dict.go_start_date, 'month');
	var go_day = getMonthAndDayByDate(go_dict.go_start_date, 'day');
	$('.go_time').text('去程: ' + go_month + '月' + go_day + '日（' + go_dict.go_start_time + '）出发');
	$('.go_pro').text(go_dict.go_pro_cate_name);
	//返程信息
	if(param.if_back == '1') {
		var back_dict = param.back_info;
		var back_month = getMonthAndDayByDate(back_dict.back_start_date, 'month');
		var back_day = getMonthAndDayByDate(back_dict.back_start_date, 'day');
		$('.back_time').text('返程: ' + back_month + '月' + back_day + '日（' + back_dict.back_start_time + '）出发');
		$('.back_pro').text(back_dict.back_pro_cate_name);
		$('.back_info').show();
	} else {
		$('.back_info').hide();
	}

	$('#total_price').text(param.total_money);

	var arr = param.prod_arr;
	var HTML = '';
	for(var i = 0, m = arr.length; i < m; i++) {
		var dict = arr[i];
		var str = '';
		if(dict['prod_count'] == 0) {
			continue;
		}
		if(i != 0) {
			str = '</br>';
		}
		var tempHTML = str+'<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);
}
$(document).ready(function() {
	var _pay_type = '1';
	//去结算
	$('#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 if_back = param.if_back ? param.if_back : "0";
		var prod_arr = JSON.stringify(prod_arr) ? JSON.stringify(prod_arr) : [];
		var total_money = param.total_money;
		var contacts_name = $('#contacts_name').val();
		var contacts_phone = $('#contacts_phone').val();
		var pay_type = _pay_type ? _pay_type : '1';
		var start_date = param.go_info.go_start_date ? param.go_info.go_start_date : '';
		var start_time = param.go_info.go_start_time ? param.go_info.go_start_time : '';
		var run_id = param.go_info.go_run_id ? param.go_info.go_run_id : '';

		//add date:2017-04-05 09:51:07
		var back_run_id = "";
		var back_prod_arr = [];
		var back_start_date = "";
		var back_start_time = "";
		var back_pro_cate_id = "";
		var is_line = "";
		if(param['if_back'] != "0") {
			is_line = param['is_line'] ? param['is_line'] : "";
//			if(param['is_line'] == '1') {
				for(var i = 0, m = param['back_prod_arr'].length; i < m; i++) {
					var dict = param['back_prod_arr'][i];
					var obj = {
						prod_id: dict['prod_id'],
						prod_count: dict['prod_count']
					}
					back_prod_arr.push(obj);
				}
//			}
			back_run_id = param.back_info.back_run_id ? param.back_info.back_run_id : '';
			back_start_time = param.back_info.back_start_time ? param.back_info.back_start_time : '';
			back_start_date = param.back_info.back_start_date ? param.back_info.back_start_date : '';
			back_pro_cate_id = param['back_pro_cate_id'] ? param['back_pro_cate_id'] : '';
			back_prod_arr = JSON.stringify(back_prod_arr) ? JSON.stringify(back_prod_arr) : "";
			is_line = param['is_line'] ? param['is_line'] : "";
		}
		

		
		if(pro_cate_id.trim().length <= 0) return;
		if(prod_arr.length <= 0) return;
		if(contacts_name.trim().length <= 0) {
			$('#contacts_name').focus();
			alert('请输入联系人姓名');
			return;
		}
		if(contacts_phone.trim().length != 11) {
			$('#contacts_phone').focus();
			alert('手机号格式不正确');
			return;
		}
//		if(total_money.trim().length <= 0) return;
		$('#loadingToast').show();
		var data = {
			pro_cate_id: pro_cate_id,
			back_pro_cate_id: back_pro_cate_id,
			if_back: if_back,
			is_line: is_line,
			prod_arr: prod_arr,
			back_prod_arr: back_prod_arr,
			contacts_name: contacts_name,
			contacts_phone: contacts_phone,
			pay_type: _pay_type,
			start_date: start_date,
			back_start_date: back_start_date,
			start_time: start_time,
			back_start_time: back_start_time,
			run_id: run_id,
			back_run_id: back_run_id
		}
		ZZLog(base_api + 'prod/prod/make-order', data);

		submit();

		function submit() {
			$.ajax({
				type: "post",
				url: base_api + 'prod/prod/make-order',
				data: data,
				dataType: 'json',
				//				traditional: true, //这里设置为true
				success: function(res_data) {
					ZZLog(res_data);
					$('#loadingToast').hide();
					if(false == res_data['flag']) {
						ZZLog('调用接口失败');
						if(codeNotLogin == res_data['code']) {
							location.href = res_data['url']+'/web/shop/';
							//							alert(infoNotLogin);
						} else {
							alert(res_data['msg']);
						}
					} else {
						ZZLog('调用接口成功');
                        location.href = res_data['data']['pay_url'];
					}
				},
				error: function() {
					$('#loadingToast').hide();
					alert(infoApiError);
				}
			});
		}
	})

})



