var app = {
	loading: function(is) {
		$('#loading').css('display', is ? 'block': 'none');
	},
	info: function(message, title, position) {
		toastr.info(message, title, {positionClass: 'toast-' + (position || 'top-center'), timeOut: 1000});
	},
	error: function(message, title, position) {
		toastr.error(message, title, {positionClass: 'toast-' + (position || 'top-center')});
	},
	//============CRUD about userpace===============
	editSpaceDetail : function(event, bussCode) {
		bussCode = event ? $(event.target).data("id") : bussCode;
		if (bussCode) {
			$.getJSON('changeAdmin/detail/' + bussCode, function(data){
				if(data.content && data.content.businessCode)
					app.showSpaceDetailModal(data.content);
				else
					app.error('没有找到企业资料');
			});
		} else {
			app.error('错误!');
		}
	},
	showSpaceDetailModal: function(content) {
		var status = app.getStatus(content);
		$('#changeAdminDetailModal').showModal({
			data: content,
			tplExp: '@{([^}]*)}',
			shown: function(){
				var scope = $(this);
				scope.find('.x-btn-image').click(app.showImageBox);
				if (status == 'UNAUDIT') {
					var btn = scope.find('.btn-submit');
					btn.show();
					btn.click({form: scope.find('form'), data: content}, app.auditSpaceDetail);
					scope.find('#auditWrap').show();
					scope.find('input[name=enable]').change(function(){
						if(0 == $(this).val()) {
							scope.find('#reasonChooseWrap').show();
							btn.addClass('x-btn-inverse');
						} else {
							scope.find('#reasonChooseWrap').hide();
							btn.removeClass('x-btn-inverse');
						}
					});
					scope.find('input[name=reason]').change(function(){
						if("other" == $(this).val()) {
							scope.find('#otherReason').show();
						} else {
							scope.find('#otherReason').hide();
						}
					});
				} else if(status == 'UNACTIVE') {
					scope.find('.x-btn-send').click(content, app.resendMail).show();
				} else if(status == 'UNAPPLY') {
					scope.find('#reasonWrap').show();
				}
			}
		});
	},
	auditSpaceDetail: function(event) {
		var form = event.data.form, content = form.serializeObject(), detail =  event.data.data, 
			data = {enable: content.enable, reason: content.reason, businessCode: detail.businessCode};
		if(0 == data.enable && "other" == data.reason) {
			if(!content.otherReason) {
				app.error('请简单描述下您的理由');
				return;
			} else {
				data.reason = content.otherReason;
			}
		}
		app.loading(true);
		$.post('changeAdmin/detail/audit', data, function(data){
			app.loading(false);
			if (data && data.errMsg) {
				app.error(data.errMsg, '审批失败', 'top-right');
			} else {
				window.location.reload();
			}
		}, 'json');
	},
	resendMail: function(event, email) {
		var data = event.data;
		email = email || data.adminEmail;
		app.loading(true);
		$.post('changeAdmin/detail/send', {
			businessCode: data.businessCode,
			email: email
		}, function(data){
			app.loading(false);
			if (data && data.errMsg) {
				app.error(data.errMsg, '发送失败', 'top-right');
			} else {
				app.info('发送成功');
			}
		}, 'json');
	},
	//============list view===============
	showSpaceDetailList: function(params) {
		$('.pagination').pagination({
			url: 'changeAdmin/detail/list',
			params: params,
			renderData: function(content){
				app.loading(false);
				var list = $('.x-data-list');
				if(content.length == 0) {
					list.empty();
					$('.x-data-list-wrap .x-empty').show();
				} else {
					$('.x-data-list-wrap .x-empty').hide();
					list.appendTpl('<div class="x-item"><div class="x-icon"><a class="x-btn-image" href="javascript:void(0};" data-src="#{businessCodeImage}"><img src="#{businessCodeImage}" width="50px" height="50px"></a></div><p><a class="x-title x-editor-trigger" href="javascript:void(0);" data-id="#{businessCode}">#{enName}</a><span class="pull-right x-text-muted">#{fn:parseDate}</span></p><div class="x-text-muted">#{businessCode}<span class="pull-right x-text-muted">#{adminName}</span></div></div>', content, {
						parseDate: function(data) {
							var d = data.enDate;
							if (d) {
								return new Date(d).format('yyyy-mm-dd');
							}
							return '';
						}
					});
					list.find('.x-editor-trigger').click(app.editSpaceDetail);
					list.find('.x-btn-image').click(app.showImageBox);
				}
			}
		});
	},
	searchSpaceDetail: function(event) {
		var target = $(event.target), form = $('.x-nav-tabs form'), data = form.serializeObject();
		if (!target.is('a')) {
			target = $('.x-nav-tabs .active a');
		}
		data.status = target.data('status');
		data.pageNumber = 1;
		app.showSpaceDetailList(data);
		$('.x-nav-tabs .active').removeClass('active');
		target.parent().addClass('active');
	},
	getStatus: function(data) {
		if (data) {
			return {310: "UNAPPLY", 311: "UNAUDIT", 312: "UNACT", 313: "ENABLED"}[data.auditStatus];
		}
		return $('.x-nav-tabs .active a').data('status');
	},
	showImageBox: function(event, bussCodeImage) {
		bussCodeImage = event.toElement.currentSrc;
		var src = $(this).data('src'), box = $('#image-box');
		if(src == null) {
			src = bussCodeImage;
		}
		if (src) {
			if (/^.*\.pdf$/i.test(src)) {
				window.open(src);
			} else {
				box.show();
				box.find('img').attr('src', src);
				box.find('a').click(function(){
					box.hide();
				});
			}
		} else {
			app.error('没有图片');
		}
	},
	//============document ready===============
	init : function() {
		app.showSpaceDetailList({
			pageNumber: 1,
			pageSize: 10, 
			status: app.getStatus()
		});
		$('.x-nav-tabs a').click(app.searchSpaceDetail);
		$('.btn-search').click(app.searchSpaceDetail);
		new Router({
			'#/(.+)': function(businessCode) {
				app.editSpaceDetail(null, businessCode);
			}
		}).start();
		$('.x-btn-search').click(function() {
			$('#searchlist').toggle();
		});
		// 点击空白处关闭下拉框
		$(document).click(function(e) {
			var target = $(e.target);
			if(target.closest('.x-btn-search').length==0 && target.closest('#searchlist').length==0) {
				$('#searchlist').hide();
			}
		});
		// 监听Enter键
		$(function() { 
			var $inp=$('input');
			$inp.keypress(function(e){
		    var key=e.which;
		    if(key==13) {
		    	$('.btn-search').click();
		    	$('#searchlist').hide();
		    }
		  }) 
		});
		app.loading(false);
	}
};
$(document).ready(function() {
	app.init();
});