$(document).ready(function() {
	$(".form_date_start").datetimepicker({
		format: 'yyyy-mm-dd',
		autoclose: true,
		minView: 2,
		pickerPosition: "bottom-left"
	});
	$(".form_date_end").datetimepicker({
		format: 'yyyy-mm-dd',
		autoclose: true,
		minView: 2,
		pickerPosition: "bottom-left"
	});
	$("#div_right").click(function() {
		var jsonHtml = $('.div_center').attr('dict');
		var dict = getJsonHtml(jsonHtml);
		var data = {apply_id:dict.apply_id};
		ZZLog(data);
		$.ajax({
			type: "post",
			url: base_api + 'order/order/through-apply',
			data:data,
			dataType: 'json',
			success: function(res_data) {
				ZZLog(res_data);
				if(res_data.flag == false) {
					ZZAlertInfo(res_data.msg);
				} else {
					httpData();
					$('#div_class').modal('hide');
				}
			},
			error: function() {
				ZZAlertInfo(infoApiError);
			}
		});

	})

});

var apply_user_name = '';
var apply_user_phone = '';
var start_time = '';
var end_time = '';
var apply_status_id = '';

var pagesize = '10';
var _currpage = '1';
//vue对象
var vueTbody = null;
var vue_apply_status_list = null;

window.onload = function() {
	//创建Vue对象
	createVue();
	//配置检索参数
	httpSearchInfo();

}

//创建Vue对象
function createVue() {
	//表单数据
	vueTbody = new Vue({
		el: "#body_list",
		data: {
			apply_listinfo: [],
			no_info:"无查询结果",
			ajax:false
		},
		methods: {
			adoptDidClick(dict) {
				ZZLog(dict);
				$('#div_username').text(dict.apply_user_name);
				$('#div_price').text(dict.apply_money);
				$('#div_open_id').text(dict.open_id);
				var html = setJsonHtml(dict);
				$('.div_center').attr('dict', html);
			}
		}
	})

	//重置按钮
	var resetBtn = new Vue({
		el: "#btns",
		methods: {
			//重置按钮
			resetDidClick: function() {
				$('#apply_user_name').val('');
				$('#apply_user_phone').val('');
				$('#start_time').val('');
				$('#end_time').val('');
				$('#apply_status option:first').prop("selected", "selected");
				httpFirstData();
			},
			//查询按钮
			searchDidClick: function() {
				httpFirstData();
			}
		}
	})

}

//检索数组的配置数据
function httpSearchInfo() {
	$.ajax({
		type: "post",
		url: base_api + 'order/order/apply-base-type',
		dataType: 'json',
		success: function(res_data) {
			ZZLog(res_data);
			if(res_data.flag == false) {
				ZZAlertInfo(res_data.msg);
			} else {
				var data = res_data.data;
				//				vue_apply_status_list.apply_status_list = data.apply_status_list;
				//状态筛选条件数组
				var apply_statusHTML = {
					props: ['item'],
					template: '<option :value="item.apply_status_id">{{item.apply_status_des}}</option>'
				}
				vue_apply_status_list = new Vue({
						el: "#apply_status",
						components: {
							'apply_status_item': apply_statusHTML,
						},
						data: function() {
							return {
								apply_status_list: data.apply_status_list
							}
						}
					})
					//第一次请求数据
				httpFirstData();
			}
		},
		error: function() {
			ZZAlertInfo(infoApiError);
		}
	});
}

//第一次请求数据
function httpFirstData() {
	_currpage = 1;
	apply_user_name = $('#apply_user_name').val();
	apply_user_phone = $('#apply_user_phone').val();
	start_time = $('#start_time').val();
	end_time = $('#end_time').val();
	apply_status_id = $('#apply_status').val();

	var urlstr = base_api + 'order/order/get-apply-list';
	var data = {
		apply_user_name: apply_user_name,
		apply_user_phone: apply_user_phone,
		start_time: start_time,
		emd_time: end_time,
		apply_status_id: apply_status_id,
		current_page: _currpage,
		page_size: pagesize
	};
	ZZLog('第一页查询参数', data);
	$.ajax({
		url: urlstr, //请求地址
		data: data,
		type: "post",
		dataType: "json",
		async: false,
		success: function(res_data) {
			ZZLog('第一次查询结果', res_data);
			if(res_data.flag == false) {
				ZZAlertInfo(res_data['msg'],function(){
					ifLogin(res_data);
				});
			} else {
				var data = res_data.data;
				totalpage = data.page.total_pages;
				if(vueTbody) {
					vueTbody.apply_listinfo = data.prod_list;
					vueTbody.ajax=true;
				}
				if(totalpage == 0) {
					$('.pageDiv').hide();
				} else {
					createPage(_currpage, totalpage);
					$('.pageDiv').show();
				}
				$('#body_list').show();
			}
		},
		error: function(error) {
			ZZAlertInfo(infoApiError);
		}
	});
}

//分页数据
function httpData() {
	var urlstr = base_api + 'order/order/get-apply-list';
	var data = {
		apply_user_name: apply_user_name,
		apply_user_phone: apply_user_phone,
		start_time: start_time,
		emd_time: end_time,
		apply_status_id: apply_status_id,
		current_page: _currpage,
		page_size: pagesize
	};
	ZZLog('分页查询参数', data);
	$.ajax({
		url: urlstr, //请求地址
		data: data,
		type: "post",
		dataType: "json",
		async: false,
		success: function(res_data) {
			if(res_data.flag == false) {
				ZZAlertInfo(res_data['msg'],function(){
					ifLogin(res_data);
				});
			} else {
				ZZLog(res_data.data)
				var data = res_data.data;
				totalpage = data.page.total_pages;
				if(vueTbody) {
					vueTbody.apply_listinfo = data.prod_list;
					vueTbody.ajax=true;
				}
				$('#body_list').show();
			}
		},
		error: function(error) {
			ZZAlertInfo(infoApiError);
		}
	});
}

//创建分页控件
function createPage(currpage, totalpage) {
	$(".pageDiv").createPage({
		pageCount: totalpage, //总页数
		current: currpage, //当前页
		turndown: 'true', //是否显示跳转框，显示为true，不现实为false,一定记得加上引号...
		backFn: function(p) {
			_currpage = p;
			httpData();
		}
	})
}