layui.define([ 'layer', 'table', 'form', 'element', 'notice' ], function(exports) {
	// 操作对象
	var layer = layui.layer;
	var table = layui.table;
	var form = layui.form;
	var toast = layui.notice;
	var $ = layui.jquery;

	// 表格ID
	var identifier = 'table';

	// 列表对象
	var tobj;

	var topt = {
		id : identifier,
		elem : '#' + identifier,
		defaultToolbar : [ 'filter', 'print'], //'exports'
		url : 'url',
		cols : 'cols',
		page : true,
		skin : 'row',
		even : true,
		size : 'sm'
	};

	// page
	// submit
	// remove
	// post
	// dialog
	// open
	// refresh

	// 封装方法
	var mod = {
		// 分页【分页参数】
		page : function(options) {
			// 参数重设
			for ( var index in options) {
				topt[index] = options[index];
			}

			// 初始化列表
			tobj = table.render(topt);
			// 监听复选框
			table.on('checkbox(' + identifier + ')', function(obj) {
				console.log(obj)
			});
			// 监听复选框
			table.on('t1(' + identifier + ')', function(obj) {
				console.log(obj)
			});
			// 监听事件
			table.on('toolbar(' + identifier + ')', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				var data = obj.data;
				if (obj.event === 'search' && topt.search) {
					topt.search(data);
				} else if (obj.event === 'add' && topt.add) {
					topt.add(data);
				} else if (obj.event === 'del' && topt.del) {
					topt.del(data);
				} else if (obj.event === 'b1' && topt.b1) {
					topt.b1(data);
				} else if (obj.event === 'b2' && topt.b2) {
					topt.b2(data);
				} else if (obj.event === 'b3' && topt.b3) {
					topt.b3(data);
				}
			});
			// 监听工具条
			table.on('tool(' + identifier + ')', function(obj) {
				var data = obj.data;
				if (obj.event === 'info' && topt.info) {
					topt.info(data);
				} else if (obj.event === 't1' && topt.t1) {
					topt.t1(data);
				} else if (obj.event === 't2' && topt.t2) {
					topt.t2(data);
				} else if (obj.event === 't3' && topt.t3) {
					topt.t3(data);
				}
				console.log('ID：' + data.id);
			});
			tobj.table = table;

			return tobj;
		},

		// 提交【地址、提示、参数、回调】
		submit : function(url, parames, callback) {
			parent.layer.confirm('Please confirm to submit', {
				icon : 1,
				title : 'Tips',
				btn : [ 'confirm', 'cancel' ]
			}, function() {
				var load;
				$.ajax({
					type : 'POST',
					url : url,
					data : parames,
					dataType : 'json',
					beforeSend : function(xhr, textStatus) {
						load = parent.layer.msg('loading', {
							icon : 16
						});
					},
					success : function(data, textStatus) {
						// parent.layer.msg(data.msg);
						if (data.code == 0) {
							toast.success(data.msg);
						} else {
							toast.error(data.msg);
						}
						if (callback) {
							callback(data);
						}
					},
					complete : function(xhr, textStatus) {
						parent.layer.close(load);
					}
				});
			});
		},

		// 提交【地址，回调】
		remove : function(url) {
			var checkStatus = table.checkStatus(identifier);
			var data = checkStatus.data;
			if (data.length == 0) {
				layer.msg("Please select Delete Data");
				toast.error("Please select Delete Data");
				return;
			}
			parent.layer.confirm('confirm deletion？', {
				icon : 3,
				title : 'tips',
				btn : [ 'confirm', 'cancel' ]
			}, function() {
				//
				var deleteParames = '';
				for (var i = 0; i < data.length; i++) {
					deleteParames = deleteParames + 'ids=' + data[i].id + "&";
				}
				//console.log(deleteParames); // 记得删
				//
				$.ajax({
					type : 'POST',
					url : url,
					data : deleteParames,
					dataType : 'json',
					beforeSend : function(xhr, textStatus) {
						parent.layer.msg('loading', {
							icon : 16
						});
					},
					success : function(data, textStatus) {
						// parent.layer.msg(data.msg);
						if (data.code == 0) {
							toast.success(data.msg);
						} else {
							toast.error(data.msg);
						}
					},
					complete : function(xhr, textStatus) {
						parent.layer.closeAll();
						tobj.reload();
					}
				});
			});
		},
		// 提交【地址，回调】
		idsSubmit : function(url, callback, actionText) {
			if(!actionText){
				actionText = 'operation'
			}
			var checkStatus = table.checkStatus(identifier);
			var data = checkStatus.data;
			if (data.length == 0) {
				layer.msg("Please select" + actionText + " data");
				toast.error("Please select" + actionText + "data");
				return;
			}
			parent.layer.confirm('confirm' + actionText + '？', {
				icon : 3,
				title : 'tips',
				btn : [ 'confirm', 'cancel' ]
			}, function() {
				//
				var params = '';
				for (var i = 0; i < data.length; i++) {
					params = params + 'ids=' + data[i].id + "&";
				}
				console.log(params); // 记得删
				//
				$.ajax({
					type : 'POST',
					url : url,
					data : params,
					dataType : 'json',
					beforeSend : function(xhr, textStatus) {
						parent.layer.msg('loading', {
							icon : 16
						});
					},
					success : function(data, textStatus) {
						if (data.code == 0) {
							toast.success(data.msg);
							if(callback) {
								callback(data, parent.layer.msg)
							}
						} else {
							toast.error(data.msg);
						}
					},
					complete : function(xhr, textStatus) {
						tobj.reload();
					}
				});
			});
		},

		// 数据【地址、参数、回调】
		post : function(url, parames, callback) {
			$.ajax({
				type : 'POST',
				url : url,
				data : parames,
				dataType : 'json',
				success : function(data, textStatus) {
					// parent.layer.msg(data.msg);
					if (data.code == 0) {
						toast.success(data.msg);
					} else {
						toast.error(data.msg);
					}
					if (callback) {
						callback(data);
					}
				},
				complete : function(xhr, textStatus) {
					form.render();
				}
			});
		},
		// 数据【地址、参数、回调】
		get : function(url, success,complete) {
			$.ajax({
				type : 'GET',
				url : url,
				success : success,
				complete : complete
			});
		},

		// 打开【地址、高度】回调
		dialog : function(url, h, callback) {
			parent.layer.open({
				type : 2,
				title : false,
				content : [ url ],
				shade : 0.6,
				resize : true,
				area : [ '64%', h ],
				closeBtn : 2,
				anim : 0,
				isOutAnim : false,
				scrollbar : false,
				btnAlign : 'r',
				btn : [ 'submit', 'back' ],
				yes : function(index, layero) {
					// 得到iframe页的窗口对象
					// var iframeWin = window[layero.find('iframe')[0]['name']];
					// iframeWin.reset();
					var iframeBody = parent.layer.getChildFrame('body', index);
					iframeBody.find('#submit').click();
				},
				btn2 : function(index, layero) {
					tobj.reload();
				},
				end: function(){
					if(callback){
						callback()
					}
				}
			});
		},

		// 打开【地址、高度】无回调
		open : function(url, h, title) {
			parent.layer.open({
				type : 2,
				title : title ? title : false,
				content : [ url ],
				shade : 0.6,
				resize : true,
				area : [ '64%', h ],
				closeBtn : 2,
				anim : 0,
				isOutAnim : false,
				scrollbar : false,
			});
		},
		// 刷新
		refresh : function() {
			// 关闭所有
			parent.layer.closeAll();
			// 刷新当前页
			setTimeout(function() {
				document.location = document.location;
			}, 1000)
		},
		//form 表单 转成 json对象
		formToJson(formData){
			var result = {};
			for(var i=0;i< formData.length;i++){
				let obj = formData[i]
				result[obj.name] = obj.value;
			}
			return result;
		}
	};

	// 输出
	exports('tool', mod);
});
