/* 页面交互效果 */

(function(win, $) {

	// 导航栏切换
	$('.nav-li').on('click', function() {
		$('.nav-li').removeClass('active');
		$(this).addClass('active');
	})

	// 点击切换下拉框
	$("#type").chosen({
		disable_search : true, // 是否显示搜索框 true为不显示搜索框
		inherit_select_classes : false
	// 是否继承 select 元素的 class，如果设为
	// true，Chosen 将把 select 的 class 添加到容器上
	})

	// 定义dom对象
	var $pager = $(".pager");

	// 渲染-列表页码
	var renderPeoplePager = function(pageIndex, pageSize, total) {
		if ($pager.pagination()) {
			$pager.pagination('destroy');
		}
		/*
		 * if (!total) { return; }
		 */
		var showtotal = 30;
		if (total<30) {
			showtotal = total;
		}
		var pagerConfig = {
			pageIndex : pageIndex,
			pageSize : pageSize,
			total : showtotal,
			showJump : false,
			jumpBtnText : '跳转',
			pageBtnCount : 12,
			showFirstLastBtn : true,
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showPageSizes : false,
			infoFormat : '记录数：'+total,
		}
		if (FUtil.isMobile()) {
			$pager.pagination($.extend(pagerConfig, {
				pageBtnCount : 3,
				showFirstLastBtn : false,
				showInfo : false,
				showPageSizes : false
			}));
		} else {
			$pager.pagination(pagerConfig);
		}
		$pager.on("pageClicked", function(event, pagerdata) {
			// console.log(event);
			// console.log(pagerdata);
			Getpagepeoplelistinfo(pagerdata.pageIndex, pagerdata.pageSize);
		}).on("jumpClicked", function(event, pagerdata) {
			// console.log(event);
			// console.log(pagerdata);
			Getpagepeoplelistinfo(pagerdata.pageIndex, pagerdata.pageSize);
		}).on("pageSizeChanged", function(event, pagerdata) {
			// console.log(event);
			// console.log(pagerdata);
			Getpagepeoplelistinfo(pagerdata.pageIndex, pagerdata.pageSize);
		});
	};

	// 获取url参数值
	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return decodeURI(pair[1]);
			}
		}
		return ("");
	}

	var pagesize = 10;
	$('#searchButton').on('click', function() {
		epoint.openDialog('查询验证', './identifying_code.html', '', {
            width: 500,
            height: 200
        });
	})
	// ///////////////////////////////////////////////////////////////////////////////
	// 页面list初次渲染
	var firstLoad = true;
	if (getQueryVariable("name")) {
		$("#name").val(getQueryVariable("name"));
		firstLoad = false;
		$('#searchButton').click();
	}else {
		Getpagepeoplelistinfo(0, pagesize);
	}	
	// 获取指定页面数据
	function Getpagepeoplelistinfo(pageindex, pagesize) {
		var sfswqy = getQueryVariable("sfswqy");
		if (sfswqy == '2') {
			$(".publicenddate").removeClass("hidden");
		} else {
			$(".publicenddate").addClass("hidden");
		}
		layer.load('0', {
			offset : [ '450px', '1050px' ]
		});
		if (getQueryVariable("name") && firstLoad) {
			$("#name").val(getQueryVariable("name"));
			firstLoad = false;
		}
		/*
		 * if (!sfswqy) { sfswqy = '0'; }
		 */
		$.ajax({
			url : 'djguserinfotempaction_yth.action?cmd=getUserInfoListLS',
			type : 'post',
			data : {
				pageindex : pageindex,
				pagesize : pagesize,
				name : $("#name").val(),
				danweiname : $("#danweiname").val(),
				sfswqy : sfswqy
			},
			dataType : 'json',
			success : function(data) {
				// console.log(data);
				// 渲染列表页码控件
				renderPeoplePager(pageindex, pagesize, data.custom.count);
				renderPagerPeopleListcontent(data);
			},
			error : function(error) {
				console.log('接口不通' + error);
			}
		});
	}
	function Getpagepeoplelistinfo(pageindex, pagesize,inputcode) {
		var sfswqy = getQueryVariable("sfswqy");
		if (sfswqy == '2') {
			$(".publicenddate").removeClass("hidden");
		} else {
			$(".publicenddate").addClass("hidden");
		}
		layer.load('0', {
			offset : [ '450px', '1050px' ]
		});
		if (getQueryVariable("name") && firstLoad) {
			$("#name").val(getQueryVariable("name"));
			firstLoad = false;
		}
		/*
		 * if (!sfswqy) { sfswqy = '0'; }
		 */
		$.ajax({
			url : 'djguserinfotempaction_yth.action?cmd=getUserInfoListLS',
			type : 'post',
			data : {
				pageindex : pageindex,
				pagesize : pagesize,
				inputcode: inputcode,
				name : $("#name").val(),
				danweiname : $("#danweiname").val(),
				sfswqy : sfswqy
			},
			dataType : 'json',
			success : function(data) {
				// console.log(data);
				// 渲染列表页码控件
				renderPeoplePager(pageindex, pagesize, data.custom.count);
				renderPagerPeopleListcontent(data);
			},
			error : function(error) {
				console.log('接口不通' + error);
			}
		});
	}
	window.Getpagelistinfo=Getpagepeoplelistinfo;
	// 渲染页面
	function renderPagerPeopleListcontent(data) {
		var list = data.custom.list;
		var M = Mustache;
		var content = $("#persontemp").html();
		M.parse(content);
		$("#personlist").html(M.render(content, {
			datalist : list
		}).replace(/@/g, "<br>"));
		setTimeout(function() {
			layer.closeAll('loading');
		}, 0);
		if (getQueryVariable("sfswqy") == '2') {
			$(".publicenddate").removeClass("hidden");
		} else {
			$(".publicenddate").addClass("hidden");
		}
	}

	$('.wb-tree-items').each(function() {
		var _this = $(this);
		if (_this.find(".wb-tree-subs").length > 0) {
			_this.addClass('hasChild');
			if (_this.hasClass("current")) {
				_this.data("navtag", "1");
			} else {
				_this.data("navtag", "0");
			}

		}
	});
	$('.wb-tree-node').on('click', function(e) {
		e.preventDefault();
		var _this = $(this);
		if (_this.next().length > 0) {
			var curTag = _this.parent().data('navtag');
			if (curTag == "1") {
				_this.parent().toggleClass('current');
			} else {
				$('.wb-tree-items.hasChild').removeClass('current');
				_this.parent().addClass('current');
				$('.wb-tree-items.hasChild').data('navtag', '0');
				_this.parent().data('navtag', '1');
			}
		} else {
			window.location.href = _this.children("a").attr("href");
		}

	});

	$(".wb-tree-item").on("click", function() {
		$(".wb-tree-item").removeClass("active");
		$(this).addClass("active")
	})

	var getUrlParams = function(prop) {
		var params = {}, query = location.search.substring(1), arr = query.split('&'), rt;

		$.each(arr, function(i, item) {
			var tmp = item.split('='), key = tmp[0], val = tmp[1];

			if (typeof params[key] == 'undefined') {
				params[key] = val;
			} else if (typeof params[key] == 'string') {
				params[key] = [ params[key], val ];
			} else {
				params[key].push(val);
			}
		});
		rt = prop ? params[prop] : params;
		return rt;
	}

	var $geturl = getUrlParams();
	// console.log($geturl)
	var sfswqy = $geturl.sfswqy;

	$('.wb-tree-items[sfswqy="' + sfswqy + '"]').addClass("current")
}(this, jQuery));