layui.define(['jquery', 'element', 'dropdown'], function (exports) {
	"use strict";
	var MOD_NAME = 'tabPage',
		$ = layui.jquery,
		dropdown = layui.dropdown,
		element = layui.element;
	var tabPage = function (opt) {
		this.option = opt;
	};
	var tabData = new Array();
	var tabDataCurrent = 0;
	var contextTabDOM;
	tabPage.prototype.render = function (opt) {
		var option = {
			elem: opt.elem,
			data: opt.data,
			index: opt.index,
			tool: opt.tool || true,
			roll: opt.roll || true,
			success: opt.success ? opt.success : function (id) { },
			session: opt.session ? opt.session : false,
			preload: opt.preload ? opt.preload : false,
			height: opt.height || "100%",
			width: opt.width || "100%",
			closeEvent: opt.closeEvent,
			tabMax: opt.tabMax,
		}
		if (option.session) {
			if (sessionStorage.getItem(option.elem + "-pear-tab-page-data") != null) {
				tabData = JSON.parse(sessionStorage.getItem(option.elem + "-pear-tab-page-data"));
				option.data = JSON.parse(sessionStorage.getItem(option.elem + "-pear-tab-page-data"));
				tabDataCurrent = sessionStorage.getItem(option.elem + "-pear-tab-page-data-current");
				tabData.forEach(function (item, index) {
					if (item.id == tabDataCurrent) {
						option.index = index;
					}
				})
			} else {
				tabData = opt.data;
			}
		}
		var lastIndex;
		var tab = createTab(option);
		$("#" + option.elem).html(tab);
		$(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-prev").click(function () {
			rollPage("left", option);
		})
		$(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-next").click(function () {
			rollPage("right", option);
		})
		element.init();
		$("#" + option.elem).width(opt.width);
		$("#" + option.elem).height(opt.height);
		$("#" + option.elem).css({
			position: "relative"
		});
		closeEvent(option);
		option.success(sessionStorage.getItem(option.elem + "-pear-tab-page-data-current"));
		dropdown.render({
			elem: `#${option.elem} .layui-tab-control > .layui-icon-down`,
			trigger: 'hover',
			data: [{
				title: '关 闭 当 前',
				id: 1
			}, {
				title: '关 闭 其 他',
				id: 2
			}, {
				title: '关 闭 全 部',
				id: 3
			}],
			click: function (obj) {
				const id = obj.id;
				if (id === 1) {
					var currentTab = $(".layui-tab[lay-filter='" + option.elem +
						"'] .layui-tab-title .layui-this");
					if (currentTab.find("span").is(".able-close")) {
						var currentId = currentTab.attr("lay-id");
						tabDelete(option.elem, currentId, option.closeEvent, option);
					} else {
						layer.msg("当前页面不允许关闭", {
							icon: 3,
							time: 1000
						})
					}
				} else if (id === 2) {
					var currentId = $(".layui-tab[lay-filter='" + option.elem +
						"'] .layui-tab-title .layui-this").attr("lay-id");
					var tabtitle = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li");
					$.each(tabtitle, function (i) {
						if ($(this).attr("lay-id") != currentId) {
							if ($(this).find("span").is(".able-close")) {
								tabDelete(option.elem, $(this).attr("lay-id"), option.closeEvent,
									option);
							}
						}
					})
				} else {
					var currentId = $(".layui-tab[lay-filter='" + option.elem +
						"'] .layui-tab-title .layui-this").attr("lay-id");
					var tabtitle = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li");
					$.each(tabtitle, function (i) {
						if ($(this).find("span").is(".able-close")) {
							tabDelete(option.elem, $(this).attr("lay-id"), option.closeEvent, option);
						}
					})
				}
			}
		})
		$("body .layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title").on("contextmenu", "li",
			function (e) {
				var top = e.clientY;
				var left = e.clientX;
				var menuWidth = 100;
				var menu = `
`;
				contextTabDOM = $(this);
				var isOutsideBounds = (left + menuWidth) > $(window).width();
				if (isOutsideBounds) {
					left = $(window).width() - menuWidth;
				}
				layer.open({
					type: 1,
					title: false,
					shade: false,
					skin: 'pear-tab-page-menu',
					closeBtn: false,
					area: [menuWidth + 'px', '108px'],
					fixed: true,
					anim: false,
					isOutAnim: false,
					offset: [top, left],
					content: menu,
					success: function (layero, index) {
						layer.close(lastIndex);
						lastIndex = index;
						menuEvent(option, index);
						var timer;
						$(layero).on('mouseout', function () {
							timer = setTimeout(function () {
								layer.close(index);
							}, 30)
						});
						$(layero).on('mouseover', function () {
							clearTimeout(timer);
						});
						$(layero).on('contextmenu', function () {
							return false;
						})
					}
				});
				return false;
			})
		mousewheelAndTouchmoveHandler(option)
		return new tabPage(option);
	}
	tabPage.prototype.click = function (callback) {
		var option = this.option;
		var elem = this.option.elem;
		element.on('tab(' + this.option.elem + ')', function (data) {
			var id = $("#" + elem + " .layui-tab-title .layui-this").attr("lay-id");
			sessionStorage.setItem(option.elem + "-pear-tab-page-data-current", id);
			callback(id);
		});
	}
	tabPage.prototype.positionTab = function () {
		var $tabTitle = $('.layui-tab[lay-filter=' + this.option.elem + ']  .layui-tab-title');
		var autoLeft = 0;
		$tabTitle.children("li").each(function () {
			if ($(this).hasClass('layui-this')) {
				return false;
			} else {
				autoLeft += $(this).outerWidth();
			}
		});
		$tabTitle.animate({
			scrollLeft: autoLeft - $tabTitle.width() / 3
		}, 200);
	}
	tabPage.prototype.clear = function () {
		sessionStorage.removeItem(this.option.elem + "-pear-tab-page-data");
		sessionStorage.removeItem(this.option.elem + "-pear-tab-page-data-current");
	}
	tabPage.prototype.changeTabTitleById = function (id, title) {
		var currentTab = $(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title [lay-id='" + id + "'] .title");
		currentTab.html(title);
	}
	/**
	 * @since Pear Admin 4.0
	 * 
	 * 删除指定选项卡
	 * 
	 * @param id 编号
	 */
	tabPage.prototype.removeTab = function (id) {
		var elem = this.option.elem;
		if (id != undefined) {
			var currentTab = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title [lay-id='" + id + "']");
			if (currentTab.find("span").is(".able-close")) {
				tabDelete(elem, id, () => { });
			}
		} else {
			var tabtitle = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title li");
			$.each(tabtitle, function () {
				if ($(this).find("span").is(".able-close")) {
					tabDelete(elem, $(this).attr("lay-id"), () => { });
				}
			})
		}
	}
	/**
	 * @since Pear Admin 4.0
	 * 
	 * 删除其他选项卡
	 */
	tabPage.prototype.removeOtherTab = function () {
		var elem = this.option.elem;
		var currentId = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title .layui-this").attr("lay-id");
		var tabtitle = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title li");
		$.each(tabtitle, function () {
			if ($(this).attr("lay-id") != currentId) {
				if ($(this).find("span").is(".able-close")) {
					tabDelete(elem, $(this).attr("lay-id"), () => { });
				}
			}
		})
	}
	/**
	 * @since Pear Admin 4.0
	 * 
	 * 删除选中选项卡
	 */
	tabPage.prototype.removeCurrentTab = function () {
		var currentTab = $(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title .layui-this");
		if (currentTab.find("span").is(".able-close")) {
			var currentId = currentTab.attr("lay-id");
			tabDelete(this.option.elem, currentId, () => { });
		}
	}
	/**
	 * @since Pear Admin 4.0
	 * 
	 * 切换选项卡
	 * 
	 * @param opt 内容
	 */
	tabPage.prototype.changePage = function (opt) {
		var title = `
					 ${opt.title}
					 ဆ`;
		if ($(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title li[lay-id]").length <=
			0) {
			var that = this;
			if (opt.type === "_iframe") {
				element.tabAdd(this.option.elem, {
					id: opt.id,
					title: title,
					content: ``
				});
			} else {
				$.ajax({
					url: opt.url,
					type: 'get',
					dataType: 'html',
					async: false,
					success: function (data) {
						element.tabAdd(that.option.elem, {
							id: opt.id,
							title: title,
							content: `${data}
`,
						});
					},
					error: function (xhr, textstatus, thrown) {
						return layer.msg('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!');
					}
				});
			}
			tabData.push(opt);
			sessionStorage.setItem(that.option.elem + "-pear-tab-page-data", JSON.stringify(tabData));
			sessionStorage.setItem(that.option.elem + "-pear-tab-page-data-current", opt.id);
		} else {
			var isData = false;
			$.each($(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title li[lay-id]"),
				function () {
					if ($(this).attr("lay-id") == opt.id) {
						isData = true;
					}
				})
			if (isData == false) {
				if (this.option.tabMax != false) {
					if ($(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title li[lay-id]")
						.length >= this.option.tabMax) {
						layer.msg("最多打开" + this.option.tabMax + "个标签页", {
							icon: 2,
							time: 1000,
							shift: 6
						});
						return false;
					}
				}
				var that = this;
				if (opt.type === "_iframe") {
					element.tabAdd(this.option.elem, {
						id: opt.id,
						title: title,
						content: ``
					});
				} else {
					$.ajax({
						url: opt.url,
						type: 'get',
						dataType: 'html',
						async: false,
						success: function (data) {
							element.tabAdd(that.option.elem, {
								id: opt.id,
								title: title,
								content: `${data}
`,
							});
						},
						error: function (xhr, textstatus, thrown) {
							return layer.msg('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!');
						}
					});
				}
				tabData.push(opt);
				sessionStorage.setItem(that.option.elem + "-pear-tab-page-data", JSON.stringify(tabData));
				sessionStorage.setItem(that.option.elem + "-pear-tab-page-data-current", opt.id);
			}
		}
		element.tabChange(this.option.elem, opt.id);
		sessionStorage.setItem(this.option.elem + "-pear-tab-page-data-current", opt.id);
	}
	/**
	 * 刷新当前选型卡
	 * 
	 * @param time 动画时长
	 */
	tabPage.prototype.refresh = function (time) {
		var $iframe = $(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-content .layui-show > *");
		var $iframeLoad;
		if (time != false && time != 0) {
			$iframeLoad = $("#" + this.option.elem).find(".pear-tab-page-loading");
			$iframeLoad.css({
				display: "block"
			});
		}
		if ($iframe.attr("type") === "_iframe") {
			$iframe.attr("src", $iframe.attr("src"));
			$iframe.on("load", function () {
				$iframeLoad.fadeOut(1000, function () {
					$iframeLoad.css({
						display: "none"
					});
				});
			})
		} else {
			$.ajax({
				url: $iframe.attr("src"),
				type: 'get',
				dataType: 'html',
				success: function (data) {
					$iframe.html(data);
					if ($iframeLoad != undefined) {
						$iframeLoad.fadeOut(1000, function () {
							$iframeLoad.css({
								display: "none"
							});
						});
					}
				},
				error: function (xhr) {
					return layer.msg('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!');
				}
			});
		}
	}
	function tabDelete(elem, id, callback) {
		var tabTitle = $(".layui-tab[lay-filter='" + elem + "']").find(".layui-tab-title");
		var removeTab = tabTitle.find("li[lay-id='" + id + "']");
		var nextNode = removeTab.next("li");
		if (!removeTab.hasClass("layui-this")) {
			removeTab.remove();
			var tabContent = $(".layui-tab[lay-filter='" + elem + "']").find("*[id='" + id + "']")
				.parent();
			tabContent.remove();
			tabData = JSON.parse(sessionStorage.getItem(elem + "-pear-tab-page-data"));
			tabDataCurrent = sessionStorage.getItem(elem + "-pear-tab-page-data-current");
			tabData = tabData.filter(function (item) {
				return item.id != id;
			})
			sessionStorage.setItem(elem + "-pear-tab-page-data", JSON.stringify(tabData));
			return false;
		}
		var currId;
		if (nextNode.length) {
			nextNode.addClass("layui-this");
			currId = nextNode.attr("lay-id");
			$("#" + elem + " [id='" + currId + "']").parent().addClass("layui-show");
		} else {
			var prevNode = removeTab.prev("li");
			prevNode.addClass("layui-this");
			currId = prevNode.attr("lay-id");
			$("#" + elem + " [id='" + currId + "']").parent().addClass("layui-show");
		}
		callback(currId);
		tabData = JSON.parse(sessionStorage.getItem(elem + "-pear-tab-page-data"));
		tabDataCurrent = sessionStorage.getItem(elem + "-pear-tab-page-data-current");
		tabData = tabData.filter(function (item) {
			return item.id != id;
		})
		sessionStorage.setItem(elem + "-pear-tab-page-data", JSON.stringify(tabData));
		sessionStorage.setItem(elem + "-pear-tab-page-data-current", currId);
		removeTab.remove();
		var tabContent = $(".layui-tab[lay-filter='" + elem + "']").find("*[id='" + id + "']").parent();
		tabContent.remove();
	}
	/**
	 * @since Pear Admin 4.0
	 */
	function createTab(option) {
		var type = "";
		if (option.roll == true) {
			type = "layui-tab-roll";
		}
		if (option.tool != false) {
			type = "layui-tab-tool";
		}
		if (option.roll == true && option.tool != false) {
			type = "layui-tab-rollTool";
		}
		var tab = '';
		var headers = '
';
		var content = '';
		var loading = '
'
		var control = `
							
							
							
						`;
		// 处 理 选 项 卡 头 部
		var index = 0;
		$.each(option.data, function (i, item) {
			var titleItem = `
- 
								
								
									${item.title}
								
                                ဆ
							`;
			headers += titleItem;
			if (item.type === "_iframe") {
				content += `
`
			} else {
				$.ajax({
					url: item.url,
					type: 'get',
					dataType: 'html',
					async: false,
					success: function (data) {
						content += `
`;
					},
					error: function (xhr) {
						return layer.msg('Status:' + xhr.status + ',' + xhr.statusText + ',请稍后再试!');
					}
				});
			}
			index++;
		});
		headers += '';
		content += '