jQuery(function($) {
	layui.use('element', function(){
		var element = layui.element;
	});
	layui.use(['layer', 'form','table'], function(){
		var form = layui.form;
		$('.ct-tab-content button').on('click',function() {
			if ( typeof($(this).attr('lay-submit')) == 'string' && typeof($(this).attr('lay-filter')) == 'string' ) {
				var filter = $(this).attr('lay-filter');
				if ($(this).attr('lay-filter').length > 0) {
					var table = $(this).parent().attr('table');
					$('#public_table').attr('table',table);
					tool.checkbox();
					if (table == 'post') {
                        if (tool.num == 1){
                            ctspider_submit(filter);
                        } else {
                            layer.msg('请选择分类',{anim: 6});
                        }
					} else {
						ctspider_submit(filter);
					}
				} else {
					layer.msg('未知错误，请刷新重试...', {icon: 2,time: 3000,anim: 6});
				}
			}
		});

        //注入开关字段
		var switch_arr = ['hide_ip','multi','list_thumb','dynamic','add_source_url','page_open','test_url','auto_tags','wp_tags','auto_excerpt','auto_thumb','translate_state','translate_title'];
		//注入开关隐藏域 开关字段和隐藏域class对应
		var switch_arr_group = {
			list_thumb : 'ct-list-thumb-group',
			add_source_url : 'ct-source-custom-field-group',
            page_open : 'ct-page-rule-group',
			test_url : 'ct-test-url-link-group',
			auto_tags : 'wp_tags',
			auto_excerpt : 'auto_excerpt_index',
			auto_thumb : 'auto_thumb_index',
		};
		for (var i = 0; i < switch_arr.length; i++) {
			switch_on(switch_arr[i],switch_arr_group);
		}


		form.on('select(rule_select)', function(data){
			if (data.value == 6) {
				$(data.elem).parent().next().show();
			}else{
				$(data.elem).parent().next().hide();
			}

		}); 


		$('body').on('click','.add-rule',function() {
			var html = '<div class="layui-form-item content-rule-list"><hr><label class="layui-form-label">匹配规则</label><div class="layui-input-block" style="width:42%"><input type="text" ct-filter="content_select" name="content_select[a][]" lay-verify="required" lay-vertype="tips" autocomplete="off" class="layui-input"><div class="layui-form-mid layui-word-aux"><div class="layui-inline"><input type="button" class="layui-btn layui-bg-cyan layui-btn-disabled" value="规则选项"></div><div class="layui-inline"><select ct-filter="rule_select" name="rule_select[c][]" lay-verify="required" lay-filter="rule_select" lay-verType="tips" class="rule_select"><option value="0">请选择一个</option><option value="1">文章内容</option><option value="2">文章摘要</option><option value="3">文章标签</option><option value="4">特色图片</option><option value="5">发布时间</option><option value="6">自定义栏目</option></select></div><div class="layui-inline" style="display:none"><input type="text" ct-filter="content_custom" name="content_custom[d][]" autocomplete="off" class="layui-input"></div></div></div><div class="layui-form-mid ct-index-select" id="ct-index-select">&nbsp;&nbsp;<span>索引</span><span style="display:none1">: <a ct-filter="index-help" ct-title=" 0 : [ 提取所有匹配的内容 ], 1 : [ 提取第1个匹配的内容 ], -1 : [ 提取最后1个匹配的内容 ]">[?]</a></span></div><div class="layui-input-inline ct-index-input" style="width:3%;display:none1"><input type="text" value="0" ct-filter="content_index" name="content_index[b][]" lay-verify="required|number" lay-vertype="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width:3%"><button type="button" class="layui-btn layui-btn-danger rule-delete">删除</button></div></div>';
			$('.content-rule-list').last().after(html);
			//tool.filter();
			reset.content_rule();
			form.render('select');
		});	

		$('body').on('click','.add-content-filter',function() {
			var html = '<div class="layui-form-item content-filter-list"><label class="layui-form-label">CSS选择器</label><div class="layui-input-block" style="width: 42%;"><input type="text" lay-verify="required" lay-verType="tips" ct-filter="content_filter" value="" name="" autocomplete="off" class="layui-input"></div><div class="layui-form-mid ct-index-select">&nbsp;&nbsp;<span>索引</span><span> : <a ct-filter="index-help" ct-title=" 0 : [ 提取所有匹配的内容 ], 1 : [ 提取第1个匹配的内容 ], -1 : [ 提取最后1个匹配的内容 ]">[?]</a></span></div><div class="layui-input-inline ct-index-input" style="width: 3%;"><input type="text" value="0" ct-filter="content_filter_index" value="0" lay-verify="required|number" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width: 3%;"><button type="button" ct-reset="content_filter" class="layui-btn layui-btn-danger rule-delete">删除</button></div></div>';
			$('.content-filter-list').last().after(html);
			reset.content_filter();
		});

        $('body').on('click','.add-html-filter',function() {
            var html = '<div class="layui-form-item html-filter-list"><label class="layui-form-label" style="width:auto">HTML 标签</label><div class="layui-input-inline" style="width:200px"><input type="text" name="html_tag_filter[a][0]" ct-filter="html-tag-filter-a" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-form-mid">&nbsp;&nbsp;&nbsp;&nbsp;是否删除标签内容</div><div class="layui-input-inline" style="width:120px"><select ct-filter="html-tag-filter-b" name="html_tag_filter_select[b][0]" class="rule_select"><option value="0">否</option><option value="1">是</option></select></div><div class="layui-form-mid">&nbsp;&nbsp;&nbsp;&nbsp;索引</div><div class="layui-input-inline" style="width:50px"><input type="text" name="html_tag_filter_index[c][0]" value="0" ct-filter="html-tag-filter-c" lay-verify="required|number" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline"><button type="button" ct-reset="html_filter" class="layui-btn layui-btn-danger rule-delete">删除</button></div></div>';
            $('.html-filter-list').last().after(html);
            form.render('select');
            reset.html_filter();
        });

        $('body').on('click','.add-title-replace',function() {
            var html = '<div class="layui-form-item title-replace-list" style="position:relative"><div class="layui-input-inline" style="width:45%;margin-left:15px"><div class="layui-form-mid layui-word-aux">关键字：</div><input type="text" ct-filter="title-replace-keyword" name="title_replace[a][keyword][0]" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width:45%"><div class="layui-form-mid layui-word-aux">替换为：</div><input type="text" ct-filter="title-replace-value" name="title_replace[a][value][0]" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="position:absolute;bottom:0"><button type="button" ct-filter="title_replace" class="layui-btn layui-btn-danger rule-delete">删除</button></div></div>';
            $('.title-replace-list').last().after(html);
            reset.title_replace();
        });

        $('body').on('click','.add-content-replace',function() {
            var html = '<div class="layui-form-item content-replace-list" style="position:relative"><div class="layui-input-inline" style="width:30%;margin-left:15px"><div class="layui-form-mid layui-word-aux">关键字：</div><input type="text" value="" ct-filter="content-replace-keyword" name="content_replace[a][keyword][0]" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width:60%"><div class="layui-form-mid layui-word-aux">替换为：</div><input type="text" value="" ct-filter="content-replace-value" name="content_replace[a][value][0]" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="position:absolute;bottom:0"><button type="button" ct-reset="content_replace" class="layui-btn layui-btn-danger rule-delete">删除</button></div></div>';
            $('.content-replace-list').last().after(html);
            reset.content_replace();
        });

        $('body').on('click','.add-custom-style',function() {
            var html = '<div class="layui-form-item customstyle-list" style="position:relative"><div class="layui-input-inline" style="width:20%;margin-left:15px"><div class="layui-form-mid layui-word-aux">HTML标签 或 CSS 选择器</div><input type="text" value="" name="style[a][selector][0]" ct-filter="customstyle-selector" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width:5%"><div class="layui-form-mid layui-word-aux">索引</div><input type="text" value="0" name="style[a][index][0]" ct-filter="customstyle-index" lay-verify="required|number" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width:15%"><div class="layui-form-mid layui-word-aux">属性</div><input type="text" value="" name="style[a][attr][0]" ct-filter="customstyle-attr" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="width:50%"><div class="layui-form-mid layui-word-aux">值</div><input type="text" value="" name="style[a][value][0]" ct-filter="customstyle-value" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input"></div><div class="layui-input-inline" style="position:absolute;bottom:0"><button type="button" ct-reset="custom_style" class="layui-btn layui-btn-danger rule-delete">删除</button></div></div>';
            $('.customstyle-list').last().after(html);
            reset.custom_style();
        });

        $('.ct-tab-content').on('click','.rule-delete',function() {
            $(this).parent().parent().remove();
            if ($(this).attr('ct-reset')) {
                reset.action($(this).attr('ct-reset'));
			}
        });

		$('.ct-tab-content').on('click','.ct-index-select span:first-child',function() {
			$(this).next('span').toggle();
			$(this).parent().next('.ct-index-input').toggle();
		});					


		var reset = {
			action : function(actions) {
                var  str='reset.'+actions+'();';
                eval(str);
			},
			content_rule : function() {
                $('.content-rule-list').each(function(index,element) {
                    $(this).find("[ct-filter='content_select']").attr('name','content_select[a]['+index+']');
                    $(this).find("[ct-filter='content_index']").attr('name','content_index[b]['+index+']');
                    $(this).find("[ct-filter='rule_select']").attr('name','rule_select[c]['+index+']');
                    $(this).find("[ct-filter='content_custom']").attr('name','content_custom[d]['+index+']');
                });
			},
			content_filter : function () {
                $('.content-filter-list').each(function(index,element) {
                    $(this).find("[ct-filter='content_filter']").attr('name','content_filter[a][selector]['+(index -1)+']');
                    $(this).find("[ct-filter='content_filter_index']").attr('name','content_filter[a][index]['+(index -1)+']');
                });
            },
			html_filter : function () {
                $('.html-filter-list').each(function(index,element) {
                	console.log(index);
                    $(this).find("[ct-filter='html-tag-filter-a']").attr('name','html_tag_filter[a]['+(index -1)+']');
                    $(this).find("[ct-filter='html-tag-filter-b']").attr('name','html_tag_filter_select[b]['+(index -1)+']');
                    $(this).find("[ct-filter='html-tag-filter-c']").attr('name','html_tag_filter_index[c]['+(index -1)+']');
                });
            },
			title_replace : function () {
                $('.title-replace-list').each(function(index,element) {
                    $(this).find("[ct-filter='title-replace-keyword']").attr('name','title_replace[a][keyword]['+(index -1)+']');
                    $(this).find("[ct-filter='title-replace-value']").attr('name','title_replace[a][value]['+(index -1)+']');
                });
            },
			content_replace : function () {
                $('.content-replace-list').each(function(index,element) {
                    $(this).find("[ct-filter='content-replace-keyword']").attr('name','content_replace[a][keyword]['+(index -1)+']');
                    $(this).find("[ct-filter='content-replace-value']").attr('name','content_replace[a][value]['+(index -1)+']');
                });
            },
			custom_style : function () {
                $('.customstyle-list').each(function(index,element) {
                    $(this).find("[ct-filter='customstyle-selector']").attr('name','style[a][selector]['+(index -1)+']');
                    $(this).find("[ct-filter='customstyle-index']").attr('name','style[a][index]['+(index -1)+']');
                    $(this).find("[ct-filter='customstyle-attr']").attr('name','style[a][attr]['+(index -1)+']');
                    $(this).find("[ct-filter='customstyle-value']").attr('name','style[a][value]['+(index -1)+']');
                });
            },
		};


		var tool = {
			num : 0,
			hover : function (range,filter) {
				$(range).on('mouseover mouseout','[ct-filter="'+filter+'"]',function(event) {
					 if(event.type == "mouseover"){
						var title = $(this).attr('ct-title');
						layer.tips(title, this,{
							tips : [1,'#78BA32'],
							time : 50000,
						});				 	
					}else if(event.type == "mouseout"){
						layer.closeAll('tips');
					}				
				});

			},
			/*
			* id Int 任务ID
			* type String 任务类型
			* success Callback 成功后的回调
			* complete Callback 任务结束后的回调
			*/
			ctgrab : function (id, type, beforesend_callback, success_callback, complete_callback) {
				$.ajax({
					url: ajaxurl,
					type: 'POST',
					dataType: 'json',	
					timeout : 0,	
					data: {
							action : 'ctspider_ajax',
							type : type,
							id : id, 
						},
				    beforeSend:function(xhr){ 
						var loading = layer.load(0, {time: 299000,shade:[0.6, '#000'], scrollbar: false});
						// 执行前的回调
						if (type == 'grab') {
							if(typeof beforesend_callback == "function") {
								beforesend_callback();
							}
						}						
				    },						
					success: function(data) {
						var title = '';
						if (type == 'contenttest' || type == 'listtest' ) {
							if (type == 'contenttest' ) title = '文章详细内容';
							if (type == 'listtest' ) title = '列表数据显示';
				    		layer.open({
				    			type: 2,
				        		title: title,
				        		maxmin: true,
				        		resize : false,
				        		move: false,
				        		//closeBtn: 0,
				        		shadeClose: false, //点击遮罩关闭层
								scrollbar: false,
				        		area : ['70%' , '98%'],
				        		content: '../wp-content/plugins/ctspider/view/wp-ctspider-htmlshow.php',
	                            zIndex: layer.zIndex, //重点1
	                            success: function(layero){
	                                layer.setTop(layero); //重点2
	                            },
				    		});	
						}

						// 成功后的回调
						if (type == 'grab') {
							if(typeof success_callback == "function") {
								success_callback(data);
							}
						}

					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						layer.msg('出现错误', {icon: 5, time: 1000, scrollbar: true});
                    },
				    complete:function(){
                        layer.closeAll('loading');
						// 执行结束后的回调
						if (type == 'grab') {
							if(typeof complete_callback == "function") {
								complete_callback();
							}
						}                        
					}
				});				
			},
			post_category : function () {
                $('#categorychecklist').find('input').each(function(index,element) {
                    $(this).attr('name','post_category[a]['+$(this).val()+']');
				});
			},
			checkbox : function () {
                $('#categorychecklist').find('input').each(function(index,element) {
                	if ($(this).prop('checked') == true) {
                		tool.num = 1;
                		return false
					}else{
                		tool.num = 0;
                		return true;
					}
                });
			},
			init : function () {
				tool.hover('.ct-tab-content','index-help');
				$('button[lay-filter="listtest"]').click(function() {
					var ID =  $('#data-basic').attr('basicID');
					tool.ctgrab(ID, 'listtest');
				});
				$('button[lay-filter="contenttest"]').click(function() {
					var ID =  $('#data-basic').attr('basicID');
					tool.ctgrab(ID, 'contenttest');
				});
				tool.post_category();
			},
		};
		tool.init();

		function ctspider_submit(string) {
			var ID =  $('#data-basic').attr('basicID');
			var update_id;
			if( !isNaN(ID) && (/^(\+|-)?\d+$/.test(ID)) && ID > 0 ) {
				update_id = ID;
			}else{
				update_id = '';
			}
			form.on('submit('+string+')', function(data){
				ctspider_save(data,update_id);
			});	
		}


		// 任务数据保存ajax
		function ctspider_save(object,id) {
			var table = $('#public_table').attr('table');
			$.ajax({
				url: ajaxurl,
				type: 'POST',
				dataType: 'json',		
				data: {
						action : 'ctspider_ajax',
						id : id,
						field : object.field,
						table : table,
					},
			    beforeSend:function(xhr){ 
					layer.load(0, {time: 2000, shade:[0.6, '#000'], scrollbar: false});
			    },						
				success: function(data) {
					if (data == 0) {
						layer.msg('没有数据被修改', {icon: 5, time: 2000, scrollbar: true});
					}else if (data > 0) {
						$('#data-basic').attr('basicID',data);
						layer.msg('保存成功', {icon: 6, time: 2000, scrollbar: true});
					} else {
						layer.msg('保存失败', {icon: 5, time: 2000, scrollbar: true});
					}
				},
			    complete:function(){
                    layer.closeAll({scrollbar: true});
				}
			});				
		}

		function switch_on(string,object) {
			form.on('switch('+string+')', function(data){
				//console.log(data);
				if (data.elem.checked == true) {
					$('input[name="'+string+'"]').attr('value',1);
					if (object[string]) {
						$('.'+object[string]).show();
						$('.'+object[string] +' input[type="text"]').attr('lay-verify', 'required').attr('lay-vertype','tips');
					}
				}else{
					$('input[name="'+string+'"]').attr('value',0);
					if (object[string]) $('.'+object[string]).hide();	
					$('.'+object[string] +' input').removeAttr('lay-verify');				
				}
			}); 
		}

		// 列表删除功能
		$('.ctspider-list .row-actions .delete').on('click',function(){
			var id = $(this).attr('task-id');
			layer.confirm('确定要删除吗？', {icon: 3, title:'提示'}, function(index){
				//ctspider_list(id, 'delete', '删除');
				ctspider_list({id: id, type: 'delete'}, {success: '任务删除成功', error: '任务删除失败'});
				layer.close(index);
			});
		});

		// 列表任务状态修改
		$('.ctspider-list .row-actions .state').on('click',function(){
			var _this = this;
			var id = $(this).attr('task-id');
			var state = $(this).attr('task-state');
			var info = '';
			if (state == 1) {
				info = '停止任务?'
			}else{
				info = '启动任务?'
			}
			layer.confirm(info, {icon: 3, title:'提示'}, function(index){

				ctspider_list(
					{id: id, type: 'state', state: state}, 
					{success: $(_this).html() + '成功', error: $(_this).html() + '失败'}, 
					function(){
					if (state == 0) {
						$(_this).attr('task-state',1);
						$('.task-'+id).addClass('active').find('.ct-update button').removeClass('layui-btn-disabled').find('span').html('更新');
						$(_this).html('暂停');					
					} else {
						$(_this).attr('task-state',0);
						$('.task-'+id).removeClass('active').find('.ct-update button').addClass('layui-btn-disabled').find('span').html('已暂停');
						$(_this).html('启动');						
					}
				});

				layer.close(index);
			});
		});	


		// 任务更新
		$('.ctspider-list .ct-update button').on('click',function(){
			if (!$(this).hasClass('layui-btn-disabled')) {
				var id = $(this).parent().find('button').attr('task-id');
				var _this = this;
				tool.ctgrab(id, 'grab', function(){
					$(_this).find('i').removeClass('layui-hide');
					$(_this).find('span').html('正在更新');
				}, function(data){
					$(_this).find('i').addClass('layui-hide');
					var task = '.task-'+id;

					if (data.num > 0) {
						$message = '更新成功';
						$(task).find('.ct-title').html(data.title);
						$(task).find('.ct-title').attr('href',data.url);
						$(task).find('.ct-time').html(data.time);
						$(task).find('.update_num').html(data.num);
						var count = $(task).find('.ct-count').html();
						$(task).find('.ct-count').html(parseInt(count) + data.num);
					} else if(data.num == 0) {
						$(task).find('.ct-time').html(data.time);
						$message = '已经是最新状态';
					}

					layer.msg($message, {time: 2000, scrollbar: true});
				}, function(){
					// 延迟执行
					setTimeout(function(){
						$(_this).find('span').html('更新');
						$(_this).find('i').addClass('layui-hide');
					},2000);
					
				});
			}
		});


		/*
		* object Object 参数
		* message Object 提示信息 默认两个参数 message.success; message.error; 成功信息和失败信息
		* callback Function 成功后执行
		*/
		function ctspider_list(object, message, callback) {
			var data = {
				action : 'ctspider_ajax'
			};
			var info = {
				success: '成功',
				error: '失败',
			};
			for (var key in object) {
				data[key] = object[key];
			}
			for (var key in message) {
				info[key] = message[key];
			}
			$.ajax({
				url: ajaxurl,
				type: 'POST',
				dataType: 'json',		
				data: data,
			    beforeSend:function(xhr){ 
					layer.load(0, {time: 2000, shade:[0.6, '#000'], scrollbar: false});
			    },						
				success: function(data) {
					if (data == 1) {
						layer.msg(info.success, {icon: 6, time: 1000, scrollbar: true});
						if (object.type == 'delete') $('.task-' + object.id).remove();
						if (object.type == 'state') {
							if(typeof callback == "function") {
								callback();
							}
						}

					} else if (data == 0){
						layer.msg(info.error, {icon: 5, time: 1000, scrollbar: true});
					} else {
						layer.msg('未知错误', {icon: 5, time: 1000, scrollbar: true});
					}
				},
			    complete:function(){
		            layer.closeAll({scrollbar: true});
				}
			});
		} // End ctspider_list()



		/** 日志记录 ajax 通用函数封装 */ 
		function ctspiderAjax_log(object,success_callback) {
			// 初始化数据发送
			var data = {
				action : 'ctspider_ajax',
			};
			// 传参数据组合
			for (key in object) {
				data[key] = object[key];
			}

			$.ajax({
				url : ajaxurl,
				type : 'POST',
				dataType : 'json',
				data : data,
				beforeSend : function(xhr) {
					var index = layer.load(0, {time: 999992000, shade:[0.6, '#000'], scrollbar: false});
				},
				success : function(res) {
					if(typeof success_callback == "function") {
						success_callback(res);
					}
				},
				complete : function() {
					layer.closeAll('loading');
				}
			});
		}


		/** 日志记录 区域 */ 
		if ($('#ctspider-log').length > 0) {
			var table = layui.table;
	        //var loading = layer.msg('<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d; </i>  数据加载中...');		
			
			// table操作
			function tablelog($whereobj = {}) {
				var index = layer.msg('<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d; </i>  数据加载中...',{time: 999999});
				var logData = {};
				logData['data'] = table.render({
					elem: '#ctspider-log',
					url:  ajaxurl + '?ctspider=log',
					cellMinWidth: 80, 
					where: $whereobj,					
					page: {
						curr: 1
					},
					request: {
							pageName: 'log_page',
					}, 
					limit: 30,
					loading: true,
					cols: [[
						{type:'checkbox'},
						//{field:'id', width:80, title: '编号', sort: true,},
						{field:'task_name', width:120, title: '任务名称', sort: true},
						{field:'state', width:100, title: '任务状态', sort: true},
						{field:'title', title: '标题', sort: true},
						{field:'url', title: '地址源'},
						{field:'time', title: '时间', width:180, sort: true},
						//{field:'sign', title: '签名', width: '30%', minWidth: 100}, //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
					]],
					done: function (res, curr, count) {
							logData['count'] = count;
							$('.logCount').find('em').html(count);
		                    layer.close(layer.index);
		                    $('#ctspider-log-reset').find('i').removeClass('layui-anim layui-anim-rotate layui-anim-loop');
		                }
				});	
				return logData;		
			}
			tablelog(); // 执行

			// 日志模块 各功能初始化
			var logActive = {
				select : {}, // (应用button)动作监听赋值初始化
				filter : {}, // (筛选button)动作监听赋值初始化
				remove : {}, // (删除button)动作监听赋值初始化
				removeaction : {}, // (删除button)操作监听赋值初始化
				// 多选框动作返回值
				getCheckData : function() {
					var checkStatus = table.checkStatus('ctspider-log');
						dataAll = checkStatus.data;
					return dataAll;
				},
			};

			// log选项事件监听
			form.on('select(ctspider-log-seletc)', function(data){
			  logActive.select = data;
			});

			// 应用点击
		  	$('#ctspider-appation').on('click', function(){
		  		var log_select = logActive.select;
		  		var data = logActive.getCheckData();
		  		if(JSON.stringify(log_select) == "{}" || log_select == "") {
		  			layer.msg('<i class="layui-icon layui-icon-about"></i> 请选择应用选项1',{time: 1500,anim: 6},function(){});
		  			return false;
		  		}				  		

		  		// 如果没有选中任何一条记录返回
		  		if (data.length == '') {
		  			layer.msg('<i class="layui-icon">&#xe60b;</i>  最少选择一条记录',function(){});
		  			return false;
		  		}	
		  		// 开始执行		  		
		  		posts = {};
		  		logs = {};
		  		results = {};
		  		for (var i = 0; i < data.length; i++) {
			  		if (data[i].post_id > 0 ) {
			  			posts[i] = data[i].post_id;
			  		} else {
			  			logs[i] = data[i].id;
			  		}
		  		}
		  		results['posts'] = posts;
		  		results['logs'] = logs;		  		
                layer.confirm('确定要删除这<strong style="color:red;font-size:22px;"> '+ data.length +' </strong>条记录吗?',{icon: 3, title:log_select.elem[log_select.value].innerHTML}, function(index){
                	layer.close(index);

                    // ajax 执行
				  	var action = {
				  		type : 'ctspider_log',
				  		removeaction : logActive.select.value,
				  		results : results,
				  	};
				  	ctspiderAjax_log(action,function(res) {
				  		//console.log(res);
				  		if (res > 0) {
				  			layer.msg('删除成功', {icon: 6, time: 2000, scrollbar: true});
							//loading = layer.msg('<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d; </i>  数据加载中...');
							tablelog({
		  							task_id : logActive.filter,
		  						});		  			
				  		} else {
				  			layer.msg('删除失败', {icon: 5, time: 2000, scrollbar: true});
				  		}
				  	});

                });			  				  					  		
		  	});			


		  	// 项目筛选事件监听
			form.on('select(ctspider-log-filter)', function(data){
			  	logActive.filter = data.value;
			});

		  	// 项目筛选点击
		  	$('#ctspider-filter').on('click',function(){
			  	var action = {
			  		type : 'ctspider_log_task_filter',
			  	};
			  	ctspiderAjax_log(action,function(res) {
			  		//loading = layer.msg('<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d; </i>  数据加载中...');
			  		tablelog({
			  			task_id : logActive.filter,
			  		});
			  	});
		  	});


		  	// 删除数据时间筛选事件监听
			form.on('select(ctspider-log-timefind)', function(data){
			  	logActive.remove = data.value;
			});

		  	// 删除操作事件监听
			form.on('select(ctspider-log-deleteaction)', function(data){
			  	logActive.removeaction = data.value;
			});

		  	// 项目删除点击
		  	$('#ctspider-log-remove').on('click', function(){
		  		if(JSON.stringify(logActive.remove) == "{}" || logActive.remove == "") {
		  			layer.msg('<i class="layui-icon layui-icon-date">&#xe637;</i> 请选择时间选项',{time: 1500,anim: 6},function(){});
		  			return false;
		  		}
		  		if(JSON.stringify(logActive.removeaction) == "{}" || logActive.removeaction == "") {
		  			layer.msg('<i class="layui-icon layui-icon-about"></i> 请选择删除操作选项',{time: 1500,anim: 6},function(){});
		  			return false;
		  		}		
		  		layer.confirm('确定只保留<strong style="color:red;font-size:22px;"> '+ logActive.remove +' </strong>天的数据吗?',{icon: 3, title:'提示 ： ' + $('select[name="ctspider-log-deleteaction"]').find("option:selected").text()}, function(index){
			  		layer.close(index);
			  		if(logActive.remove > 0) {
					  	var action = {
					  		type : 'ctspider_log_remove',
					  		removetime : logActive.remove,
					  		removeaction : logActive.removeaction,
					  	};		  			
					  	ctspiderAjax_log(action,function(res) {
					  		if (res > 0) {				  			
						  		loading = layer.msg('<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63e; </i>  删除成功');
						  		tablelog({
						  			task_id : logActive.filter,
						  		});				  				  			
					  		} else {
					  			layer.msg('数据均在<span style="font-size:22px;"> '+ logActive.remove +' </span>天范围内', {time: 2000});
					  		}

						});
			  		}		  			
		  		});
		  	}); // End


		  	// 刷新
		  	$('#ctspider-log-reset').on('click',function() {
		  		// loading = layer.msg('<i class="layui-icon layui-anim layui-anim-rotate layui-anim-loop">&#xe63d; </i>  数据加载中...');
		  		$('#ctspider-log-reset').find('i').addClass('layui-anim layui-anim-rotate layui-anim-loop');
		  		logActive.filter  = "all";
		  		tablelog();
		  	});
		} //End 日志记录 区域


		/** 系统设置JS */
		form.on('select(update_mode)', function(data){
			if (data.value == 1) {
				$(this).parent().parent().parent().find('.layui-word-aux').removeClass('layui-hide');
			}else{
				$(this).parent().parent().parent().find('.layui-word-aux').addClass('layui-hide');
			}
		});

		// 授权验证
		$('button[lay-filter="authority"]').click(function () {
			var _this = this;
            $.ajax({
                url : ajaxurl,
                type : 'POST',
                dataType : 'json',
                data : {
                    action : 'ctspider_ajax',
                	type : 'authority',
				},
                beforeSend : function(xhr) {
                    $(_this).find('i').removeClass('layui-hide');
                    layer.load(0, {time: 999992000, shade:[0.6, '#000'], scrollbar: false});
                },
                success : function(data) {
					if (data == 1) {
						$('button[lay-filter="authority-text"]').find('span').html('已授权');
                        $('button[lay-filter="authority-text"]').removeClass('layui-btn-danger');
                        loading = layer.msg('<i class="layui-icon layui-icon-ok">&#xe605; </i> 授权成功');

                        $('.authority-error').hide();
					} else {
                        $('button[lay-filter="authority-text"]').find('span').html('未授权');
                        $('button[lay-filter="authority-text"]').addClass('layui-btn-danger');
                        loading = layer.msg('<i class="layui-icon layui-icon-close">&#x1006; </i> 授权失败');
					}
                    $(_this).find('i').addClass('layui-hide');
                },
                complete : function() {
                    layer.closeAll('loading');
                }
            });
        });



	});//end use(form)

});

