/*
 |--------------------------------------------------------------------------
 | SkyCaiji (蓝天采集器)
 |--------------------------------------------------------------------------
 | Copyright (c) 2018 https://www.skycaiji.com All rights reserved.
 |--------------------------------------------------------------------------
 | 使用协议  https://www.skycaiji.com/licenses
 |--------------------------------------------------------------------------
 */
'use strict';var tasksOpClass={get_task_id:function(obj){var taskId=$(obj).parents('tr[data-task-id]').eq(0).attr('data-task-id');taskId=taskId?taskId:0;return taskId},init_common:function(){$('table.datatable').on('click','.delete',function(){var obj=$(this);var url=ulink('task/op?op=delete&id=_id_',{'_id_':tasksOpClass.get_task_id(obj)});confirmRight(window.tpl_lang.confirm_delete,function(){ajaxOpen({type:"GET",url:url,dataType:"json",success:function(data){data.code==1?toastr.success(data.msg):toastr.error(data.msg);if(data.code==1){obj.parents('tr').eq(0).remove()}}})})});$('table.datatable').on('click','.auto',function(){var auto=1;var tips=[window.tpl_lang.task_auto_1,'green'];var val=$(this).attr('data-val');val=toInt(val);if(val!=0){auto=0;tips=[window.tpl_lang.task_auto_0,'red']}
var taskid=tasksOpClass.get_task_id(this);if(taskid>0){var curObj=$(this);var url=ulink('task/op?op=auto&auto=_auto_&id=_id_',{'_auto_':auto,'_id_':taskid});ajaxOpen({type:'GET',url:url,success:function(data){if(data.code==1){curObj.attr('data-val',auto);curObj.prop('title','');curObj.html(tips[0]);curObj.css('color',tips[1])}},dataType:'json'})}});var loadedFunc=function(){$('#myModal .modal-footer .close').html('关闭采集').prop('title','关闭并结束任务').addClass('btn btn-primary').removeClass('close');var btnBackstage=$('<button type="button" class="btn btn-default btn-backstage" data-dismiss="modal" title="关闭并在后台继续运行">后台运行</button>');btnBackstage.bind('click',function(){collectorEchoMsg.close_non_stop=!0});$('#myModal .modal-footer').prepend(btnBackstage)};$('table.datatable').on('click','.caiji',function(){var taskid=tasksOpClass.get_task_id(this);globalOp.collectorWindow('任务采集','admin/task/collect?id=_id_',{'_id_':taskid},{loaded_func:loadedFunc,lg:1})});$('table.datatable').on('click','.caiji-batch',function(){var taskids=[];$('input[name="batch[]"]:checked').each(function(){taskids.push($(this).val())});globalOp.collectorWindow('批量采集','admin/task/collectBatch?ids=_ids_',{'_ids_':taskids.join(',')},{loaded_func:loadedFunc,lg:1})});$('table.datatable').on('click','.houtai',function(){var taskid=tasksOpClass.get_task_id(this);tasksOpClass.backstage_collect(taskid)});$('table.datatable').on('click','.houtai-batch',function(){var taskids=[];$('input[name="batch[]"]:checked').each(function(){taskids.push($(this).val())});tasksOpClass.backstage_collect(taskids)});$('table.datatable thead th[data-order]').bind('click',function(){var order=$(this).attr('data-order');if(!order){return!1}
var className=$(this).prop('class');var sort='desc';if(className=='sorting_desc'){sort='asc'}
window.location.href=ulink('task/list?show=list&order='+order+'&sort='+sort);return!1});this.task_collected_num()},task_collected_num:function(){var taskIds=[];$('tr[data-task-id]').each(function(){taskIds.push($(this).attr('data-task-id'))});if(taskIds.length>0){ajaxOpen({type:'POST',url:ulink('task/taskCollectedNum'),data:{'task_ids':taskIds},success:function(data){if(data.code==1){data=data.data;if(isObject(data)){for(var i in data){var num=data[i];if(isObject(num)){var trObj=$('tr[data-task-id="'+i+'"]');trObj.find('.today-num').text(num.today);trObj.find('.total-num').text(num.total)}}}}}})}},init_folder:function(openedTgIds){tasksOpClass.init_common();$('#task_list_folder').on('click','.taskgroup',function(){tasksOpClass.open_folder($(this).parents('tr').eq(0).attr('data-tgid'))});tasksOpClass.open_folder('0',function(){if(isObject(openedTgIds)){for(var i in openedTgIds){tasksOpClass.open_folder(openedTgIds[i])}}})},open_folder:function(tgid,successFunc){var $_o=this;var trBox=$('#task_list_folder').find('tr[data-tgid="'+tgid+'"]').eq(0);var tgObj=trBox.find('.taskgroup');var tgLevel=trBox.attr('data-level');var loadingImg='<div class="'+(tgLevel>0?'loading-sm':'loading')+'"></div>';var opened=tgObj.hasClass('taskgroup-opened')?true:!1;var childs=$('#task_list_folder tr[data-parent-tgid="'+tgid+'"]');childs.css('display',(opened?'none':'table-row'));childs.each(function(){var cTgid=$(this).attr('data-tgid');var cOpened=$(this).hasClass('taskgroup-opened')?true:!1;if(cTgid){if(opened){$('#task_list_folder tr[data-parent-tgid="'+cTgid+'"]').css('display','none')}else{$('#task_list_folder tr[data-parent-tgid="'+cTgid+'"]').css('display',cOpened?'table-row':'none')}}});if(opened){tgObj.removeClass('taskgroup-opened');ajaxOpen({type:"GET",url:ulink('task/tgClose?tg_id=_tgid_',{'_tgid_':tgid}),dataType:"json",success:function(data){}})}else{tgObj.addClass('taskgroup-opened');if(trBox.attr('data-setted')!=1){tgObj.parents('td').eq(0).append(loadingImg);ajaxOpen({type:"GET",url:ulink('task/tgOpen?tg_id=_tgid_',{'_tgid_':tgid}),dataType:"json",success:function(data){if(tgLevel>0){tgObj.siblings('.loading-sm').remove()}else{trBox.hide()}
tgLevel=parseInt(tgLevel);var leftSpace=tgLevel>0?('padding-left:'+tgLevel*25+'px;'):'';var html='';if(data.code==1){if(isNull(data.data)){html=null}else{var html='';if(data.data.tgList){var datalist=data.data.tgList;for(var i in datalist){html+='<tr data-level="'+(tgLevel+1)+'" data-parent-tgid="'+tgid+'" data-tgid="'+datalist[i].id+'"><td style="'+leftSpace+'"><a href="javascript:;" class="taskgroup"> <span class="glyphicon icon-folder-pre"></span> <span class="glyphicon icon-folder"></span> '+datalist[i].name+' </a></td><td colspan="6"></td></tr>'}}
if(data.data.taskList){var todayDate=new Date();todayDate=todayDate.getFullYear()+'-'+(todayDate.getMonth()+1)+'-'+todayDate.getDate();var datalist=data.data.taskList;for(var i in datalist){var item=datalist[i];var auto=toInt(item.auto);html+='<tr data-parent-tgid="'+tgid+'" data-task-id="'+item.id+'"></td><td style="'+leftSpace+'">'+'<a href="'+ulink('task/set?id='+item.id)+'" class="edit">'+item.name+'</a></td><td class="sort"><input type="text" name="newsort['+item.id+']" class="form-control" value="'+item.sort+'" autocomplete="off" /></td>'+'<td><a href="javascript:;" class="auto" data-val="'+auto+'" style="color:'+(auto>0?'green':'red')+';" title="'+(item._timer_info?item._timer_info:'')+'">'+(window.tpl_lang['task_auto_'+auto])+'</a></td>'+'<td>'+item.addtime+'</td><td><label class="checkbox-inline chk-inline"><input type="checkbox" name="batch[]" value="'+item.id+'" /> '+item.caijitime+'</label>'+'&nbsp;<ul class="in-line"><li><a href="javascript:;" class="caiji">'+window.tpl_lang.caiji+'</a></li>'+' <li class="sep">|</li> <li><a href="javascript:;" class="houtai">后台</a></li></ul>'+'</td><td><ul class="in-line"><li>今日<a href="'+ulink('collected/list?task_id='+item.id+'&begin='+todayDate)+'" class="today-num" target="_blank">0</a>条</li>'+' <li class="sep">|</li> <li>总共<a href="'+ulink('collected/list?task_id='+item.id)+'" class="total-num" target="_blank">0</a>条</li></ul>'+'</td><td><ul class="in-line"><li><a href="'+ulink('collector/set?task_id='+item.id)+'">规则</a></li>'+' <li class="sep">|</li> <li><a href="'+ulink('release/set?task_id='+item.id)+'" title="'+(item._rele_module?item._rele_module:'')+'">发布</a></li>'+' <li class="sep">|</li> <li><a href="javascript:;" class="delete">'+window.tpl_lang.delete+'</a></li></ul></td></tr>'}}}}else{html=null}
html=isNull(html)?('<tr data-parent-tgid="'+tgid+'"><td style="'+leftSpace+'">'+window.tpl_lang.task_none_data+'</td><td colspan="6"></td></tr>'):html;trBox.attr('data-setted',1);trBox.after(html);if(successFunc&&typeof(successFunc)=='function'){successFunc()}
$_o.task_collected_num()}})}}},click_backstage_time:{},backstage_collect:function(ids){var isBatch=!1;if(isObject(ids)){isBatch=!0;ids=ids.join(',')}
if(!tasksOpClass.click_backstage_time[ids]){tasksOpClass.click_backstage_time[ids]=(new Date()).getTime()}else{if((new Date()).getTime()-tasksOpClass.click_backstage_time[ids]<=3000){return!1}else{tasksOpClass.click_backstage_time[ids]=(new Date()).getTime()}}
var url='';if(isBatch){url=ulink('admin/task/collectBatch?ids=_ids_&backstage_run=1',{'_ids_':ids})}else{url=ulink('admin/task/collect?id=_id_&backstage_run=1',{'_id_':ids})}
ajaxOpen({type:'GET',url:url,async:!0,timeout:3000,dataType:'html'});toastr.success('已添加到后台运行');winBackstageTask.count(1000)},}