/*
 |--------------------------------------------------------------------------
 | SkyCaiji (蓝天采集器)
 |--------------------------------------------------------------------------
 | Copyright (c) 2018 https://www.skycaiji.com All rights reserved.
 |--------------------------------------------------------------------------
 | 使用协议  https://www.skycaiji.com/licenses
 |--------------------------------------------------------------------------
 */
'use strict';function DataapiClass(){this.formid='#form_dataapi'}
DataapiClass.prototype={constructor:DataapiClass,init_list:function(search){$('#dataapi_list .delete').bind('click',function(){var curTr=$(this).parents('tr[data-da-id]').eq(0);confirmRight('删除后将无法请求该接口！确定删除？',function(){ajaxOpen({type:'post',url:ulink('dataapi/op?op=delete'),data:{'id':curTr.attr('data-da-id')},success:function(data){ajaxDataMsg(data);if(data.code==1){curTr.remove()}}})})});$('#dataapi_list .status').bind('click',function(){var curTr=$(this).parents('tr[data-da-id]').eq(0);var curObj=$(this);ajaxOpen({type:'post',url:ulink('dataapi/op?op=status'),data:{'id':curTr.attr('data-da-id'),'status':(curObj.hasClass('status-close')?0:1)},success:function(data){ajaxDataMsg(data);data=data.data;if(data.status){curObj.removeClass('status-close').html('开启')}else{curObj.addClass('status-close').html('关闭')}}})});if(isObject(search)){for(var i in search){$('#form_search').find('[name="'+i+'"]').val(search[i])}}},init:function(dataapi){var $_o=this;$('#btn_dataset').bind('click',function(){windowModal('数据集',ulink('dataset/select?from=dataapi'))});$('#tips_config_cond').bind('click',function(){confirmRight({msg:'<b>传入参数名：</b>请求接口时url中传入或post提交数据中的参数名称',yes:'确定',textAlign:'left'})});$('#add_config_cond').bind('click',function(){$_o.add_cond(null)});$('#config_conds').on('click','.config-cond-add',function(){$_o.add_cond({sub:1},this)});$('#config_conds').on('change','[name="conds[field][]"]',function(){var curTr=$(this).parents('tr[id^="cond_"]').eq(0);var fname=$(this).val();fname=fname?('默认：'+fname):'';curTr.find('[name="conds[name][]"]').attr('placeholder',fname)});$('#config_conds').on('click','.config-cond-dlt',function(){var curObj=$(this);confirmRight('确定删除？',function(){curObj.parents('tr[id^="cond_"]').remove();$_o.first_cond()})});globalOp.eleExchange('#config_conds','.config-cond-move','tr[id^="cond_"]',{stop:function(event,ui){$_o.first_cond()}});if(isObject(dataapi)){$($_o.formid).find('[name="name"]').val(dataapi.name?dataapi.name:'');$($_o.formid).find('[name="route"]').val(dataapi.route?dataapi.route:'');$($_o.formid).find('[name="desc"]').val(dataapi.desc?dataapi.desc:'');$($_o.formid).find('[name="sort"]').val(dataapi.sort?toInt(dataapi.sort):0);$($_o.formid).find('[name="status"][value="'+toInt(dataapi.status)+'"]').prop('checked',!0);var config=dataapi.config;$_o.dataset_load({'dataset_id':dataapi.ds_id},dataapi.config)}},first_cond:function(){var firstObj=$('#config_conds').find('tr[id^="cond_"]:first').eq(0);if(firstObj.hasClass('config-cond-sub')){firstObj.removeClass('config-cond-sub');firstObj.find('[name="conds[sub][]"]').val('')}},add_cond:function(cond,curObj){cond=isObject(cond)?cond:null;var trId='cond_'+generateUUID();var tr=$('#tpl_config_cond').clone();tr.attr('id',trId);trId='#'+trId;var isSub=!1;if(cond){for(var i in cond){tr.find('[name="conds['+i+'][]"]').val(cond[i]?cond[i]:'')}
if(cond.sub){isSub=!0}}
if(isSub&&curObj){var endTr=null;var curTr=$(curObj).parents('tr[id^="cond_"]').eq(0);curTr.nextAll().each(function(){if(!$(this).hasClass('config-cond-sub')){return!1}
endTr=$(this)});if(!endTr){endTr=curTr}
endTr.after(tr)}else{$('#config_conds tbody').append(tr)}
if(cond){$(trId).find('[name="conds[field][]"]').trigger('change')}
if(isSub){$(trId).addClass('config-cond-sub')}},dataset_load:function(dsConfig,daConfig){var $_o=this;dsConfig=isObject(dsConfig)?dsConfig:{};daConfig=isObject(daConfig)?daConfig:null;var dsId=toInt(dsConfig.dataset_id);if(dsId>0){ajaxOpen({url:ulink('dataapi/dataset?ds_id='+dsId),success:function(data){var dsData=data.data;if(isObject(dsData)){var fields=isObject(dsData.fields)?dsData.fields:{};$('#config_conds_box').show();$($_o.formid).find('[name="ds_id"]').val(dsData.id);$('#btn_dataset').html('数据集：'+dsData.name);var opts='<option value="">无</option>';var chks='';for(var i in fields){opts+='<option value="'+i+'">'+htmlspecialchars(fields[i])+'</option>';chks+='<label class="checkbox-inline"><input type="checkbox" name="config[hide_fields][]" value="'+i+'"> '+htmlspecialchars(fields[i])+'</label>'}
$('#tpl_config_cond').find('[name="conds[field][]"]').html(opts);$($_o.formid+' [name="config[order_field]"]').html(opts);$('#config_hide_fields').html(chks);$('#config_conds').find('[name="conds[field][]"]').each(function(){var curVal=$(this).val();$(this).html(opts).val(curVal)});if(daConfig){var conds=daConfig.conds;if(isObject(conds)){for(var i in conds.logic){$_o.add_cond({sub:conds.sub[i],logic:conds.logic[i],field:conds.field[i],op:conds.op[i],name:conds.name[i]})}}
delete daConfig.conds}}
$('#myModal').modal('hide');if(daConfig){for(var i in daConfig){var curObj=$($_o.formid).find('[name="config['+i+']"]').eq(0);if(curObj.length>0&&!curObj.is('input:radio')&&!curObj.is('input:checkbox')){curObj.val(daConfig[i])}}
$($_o.formid).find('[name="config[order_sort]"][value="'+daConfig.order_sort+'"]').prop('checked',!0);if(isObject(daConfig.hide_fields)){for(var i in daConfig.hide_fields){$($_o.formid).find('[name="config[hide_fields][]"][value="'+daConfig.hide_fields[i]+'"]').prop('checked',!0)}}}}})}},};var dataapiClass=new DataapiClass()