/*
 |--------------------------------------------------------------------------
 | SkyCaiji (蓝天采集器)
 |--------------------------------------------------------------------------
 | Copyright (c) 2018 https://www.skycaiji.com All rights reserved.
 |--------------------------------------------------------------------------
 | 使用协议  https://www.skycaiji.com/licenses
 |--------------------------------------------------------------------------
 */
'use strict';function ProxyClass(){}
ProxyClass.prototype={constructor:ProxyClass,init_setting:function(proxyConfig){var $_o=this;$('#proxy_ip_table').attr('data-tpl',$('#proxy_ip_table .proxy-ip-tpl').html());$('#proxy_ip_table .proxy-ip-tpl').remove();$('[name="open"]').bind('click',function(){if($(this).val()==1){$('.content-wrapper').removeClass('wrapper-not-enable')}else{$('.content-wrapper').addClass('wrapper-not-enable')}});$('.edit-proxy-groups').bind('click',function(){windowModal('管理分组',ulink('proxy/groups'))});$('#batch_proxy_ip').bind('click',function(){windowModal('批量添加',ulink('proxy/batch'))});$('#add_proxy_ip').bind('click',function(){windowModal('添加代理IP',ulink('proxy/add'),{lg:1})});$('#invalid_proxy_ip').bind('click',function(){confirmRight('确定清理无效ip？',function(){windowModal('正在清理...',ulink('proxy/clearInvalid'),{ajax:{success:function(){$_o.reload_iframe('清理完成')}}})})});$('#proxy_ip_iframe').bind('load',function(){$('#panel_proxy_ip .loading').hide();$(this).show();var mainheight=$(this).contents().find('body').height()+1;$(this).height(mainheight)});$('#proxy_ip_table').on('click','.delete-proxy-ip',function(){$(this).parents('tr').eq(0).remove()});$('[name="use"]').bind('click',function(){$('[id^="proxy_use_"]').hide();$('#proxy_use_'+$(this).val()).show()});$('[name="api[open]"]').bind('click',function(){if($(this).val()==1){globalOp.showPanelCollapse('#panel_proxy_api')}});$('#proxy_api .p-api-add').bind('click',function(){$_o.add_api()});$('#proxy_api').on('click','.p-api-format a[data-val]',function(){var obj=$(this).parents('.p-api-panel').eq(0).find('[data-name="api_format"]');globalOp.insertAtCaret(obj,$(this).attr('data-val'))});globalOp.eleExchange('#proxy_api','.p-api-move','.p-api-panel');$('#proxy_api').on('click','.p-api-delete',function(){var obj=$(this);confirmRight('确定删除？',function(){obj.parents('.p-api-panel').eq(0).remove()})});$('#proxy_api').on('click','.btn-api-test',function(){var config={};$(this).parents('.p-api-panel').eq(0).find('[data-name]').each(function(){var name=$(this).attr('data-name');config[name]=$(this).val()});windowModal('测试接口',ulink('proxy/testApi'),{ajax:{type:'post',data:{config:config}}})});if(proxyConfig){$('[name="open"][value="'+toInt(proxyConfig.open)+'"]').trigger('click');$('[name="failed"]').val(toInt(proxyConfig.failed));$('[name="group_id"]').val(toInt(proxyConfig.group_id));$('[name="use"][value="'+proxyConfig.use+'"]').trigger('click');$('[name="use_num"]').val(toInt(proxyConfig.use_num));$('[name="use_time"]').val(toInt(proxyConfig.use_time));if(proxyConfig.api){$('[name="api[open]"][value="'+toInt(proxyConfig.api.open)+'"]').trigger('click');$('[name="api[insert]"]').val(proxyConfig.api.insert)}
if(proxyConfig.apis){for(var i in proxyConfig.apis){$_o.add_api(proxyConfig.apis[i])}}}},reload_iframe:function(msg){$('#myModal').modal('hide');toastr.success(msg);$('#panel_proxy_ip .loading').show();$('#proxy_ip_iframe').hide();$('#proxy_ip_iframe').attr('src',$('#proxy_ip_iframe').attr('src')).hide()},add_api:function(data){data=data?data:{};var tpl=$('#proxy_api_tpl').clone();tpl.removeAttr('id').css('display','block');var unique=generateUUID();var collapseId='api_collapse_'+unique;tpl.find('.p-api-title').attr('href','#'+collapseId);tpl.find('.p-api-collapse').attr('id',collapseId);tpl.find('[data-name]').each(function(){var name=$(this).attr('data-name');$(this).attr('name','apis[i_'+unique+']['+name+']');if(data[name]){$(this).val(data[name])}});if(data.api_url){tpl.find('.p-api-title small').text('：'+data.api_url)}
$('#proxy_api_box').append(tpl)},init_list:function(search){search=search?search:{};if(search){for(var i in search){$('#form_search').find('[name="'+i+'"]').val(search[i])}}
$('#form_list').on('change','[data-name="ip_list[]"],[data-name="user_list[]"],[data-name="pwd_list[]"],[data-name="type_list[]"],[data-name="gid_list[]"]',function(){$(this).parents('tr').eq(0).find('[data-name="ips[]"]').prop('checked',!0)});$('#form_list').on('click','.op-delete',function(){var tr=$(this).parents('tr').eq(0);var ip=tr.find('[data-name="ips[]"]').val();ajaxOpen({type:'get',dataType:'json',url:ulink('proxy/op?op=delete&ip=_ip_',{'_ip_':ip}),success:function(data){if(data.code==1){tr.fadeOut(100,function(){tr.remove()});toastr.success(data.msg)}else{toastr.error(data.msg)}}})});$('#form_list').on('click','.check-all-ip',function(){var checked=$(this).is(":checked")?true:!1;$('[data-name="ips[]"]').prop('checked',checked)});$('#form_list').on('click','.delete-all-ip',function(){confirmRight('确定删除选中的IP？',function(){$('#form_list').find('[name="op"]').val('delete_all');var ips=new Array();$('#form_list').find('[data-name="ips[]"]').each(function(){if($(this).is(':checked')){ips.push($(this).val())}});if(ips){$('[name="ips"]').val(JSON.stringify(ips))}
$('#form_list').submit()})});$('#form_list').on('click','.update-all-ip',function(){confirmRight('确定修改？',function(){$('#form_list').find('[name="op"]').val('update_all');var ips=new Array();var paramNames=['ip_list','user_list','pwd_list','type_list','gid_list'];var paramDatas={};for(var i in paramNames){paramDatas[paramNames[i]]=new Array()}
$('#form_list').find('[data-name="ips[]"]').each(function(){if($(this).is(':checked')){ips.push($(this).val());var tr=$(this).parents('tr').eq(0);for(var paramName in paramDatas){paramDatas[paramName].push(tr.find('[data-name="'+paramName+'[]"]').val())}}});if(ips){$('[name="ips"]').val(JSON.stringify(ips))}
for(var paramName in paramDatas){if(paramDatas[paramName]){$('[name="'+paramName+'"]').val(JSON.stringify(paramDatas[paramName]))}}
$('#form_list').submit()})})},init_add:function(){var $_o=this;var formid='#win_form_proxy_add';$(formid+' .proxy-ip-list').attr('data-tpl','<tr>'+$(formid+' .tpl-proxy-ip').html()+'</tr>');$(formid+' .tpl-proxy-ip').remove();$(formid+' .add-proxy-ip').bind('click',function(){$(formid+' .proxy-ip-list tbody').append($(formid+' .proxy-ip-list').attr('data-tpl'))});$(formid).on('click','.op-delete',function(){$(this).parents('tr').eq(0).remove()});$(formid).bind('submit',function(){ajaxOpen({type:'post',dataType:'json',url:$(this).attr('action'),data:$(this).serialize(),success:function(data){if(data.code==1){$_o.reload_iframe('添加成功')}else{toastr.error(data.msg)}}});return!1})},init_batch:function(){var $_o=this;var formid='#win_form_proxy_batch';$(formid+' .format a[data-val]').bind('click',function(){var obj=$('#win_form_proxy_batch input[name="format"]');globalOp.insertAtCaret(obj,$(this).attr('data-val'))});$(formid+' .btn-test').bind('click',function(){$(formid).find('[name="is_test"]').val(1);ajaxOpen({type:'POST',dataType:'json',url:$(formid).attr('action'),data:$(formid).serialize(),success:function(data){if(data.code==1){$(formid+' .test-result').show();$(formid+' .test-result').find('textarea').val(data.msg)}else{toastr.error(data.msg)}}})});$(formid).bind('submit',function(){$(formid).find('[name="is_test"]').val('');ajaxOpen({type:'POST',dataType:'json',url:$(this).attr('action'),data:$(this).serialize(),success:function(data){if(data.code==1){$_o.reload_iframe('添加成功')}else{toastr.error(data.msg)}},});return!1})},init_groups:function(groups){var $_o=this;var formid='#win_form_proxy_groups';$(formid+' .proxy-group-list').attr('data-tpl','<tr>'+$(formid+' .proxy-group-tpl').html()+'</tr>');$(formid+' .proxy-group-tpl').remove();$(formid+' .proxy-group-add').bind('click',function(){$(formid+' .proxy-group-list tbody').append($(formid+' .proxy-group-list').attr('data-tpl'))});$(formid).on('click','.proxy-group-delete',function(){var prtObj=$(this).parents('tr').eq(0);var groupId=prtObj.find('[name="group_id[]"]').val();confirmRight('确定删除该分组？',function(){ajaxOpen({type:'get',dataType:'json',url:ulink('proxy/delete_group?id='+groupId),success:function(data){if(data.code==1){prtObj.remove();if(data.msg){toastr.success(data.msg)}}}})})});if(isObject(groups)){for(var i in groups){var tpl=$(formid+' .proxy-group-list').attr('data-tpl');tpl=$(tpl);var groupData=groups[i];if(isObject(groupData)){tpl.find('[name="group_id[]"]').val(groupData.id);tpl.find('[name="group_sort[]"]').val(groupData.sort);tpl.find('[name="group_name[]"]').val(groupData.name);if(groupData._ip_num){tpl.find('.proxy-group-ip-num').html(groupData._ip_num+'个IP')}}
$(formid+' .proxy-group-list tbody').append(tpl)}}},}
var proxyClass=new ProxyClass()