/*
 |--------------------------------------------------------------------------
 | SkyCaiji (蓝天采集器)
 |--------------------------------------------------------------------------
 | Copyright (c) 2018 https://www.skycaiji.com All rights reserved.
 |--------------------------------------------------------------------------
 | 使用协议  https://www.skycaiji.com/licenses
 |--------------------------------------------------------------------------
 */
'use strict';function DatasetClass(){this.formid='#form_dataset'}
DatasetClass.prototype={constructor:DatasetClass,init_list:function(search){$('#dataset_list .delete').bind('click',function(){var curTr=$(this).parents('tr[data-ds-id]').eq(0);confirmRight('将清空该数据集的所有数据，确定删除？',function(){ajax_check_userpwd({type:'post',url:ulink('dataset/op?op=delete'),data:{'id':curTr.attr('data-ds-id')},success:function(data){ajaxDataMsg(data);if(data.code==1){curTr.remove()}}})})});var dsIds=[];$('tr[data-ds-id]').each(function(){dsIds.push($(this).attr('data-ds-id'))});ajaxOpen({type:'post',url:ulink('dataset/dbCount'),data:{ids:dsIds},success:function(data){if(data.code==1){var ids=data.data;if(isObject(ids)){if(isObject(ids.db)){for(var i in ids.db){$('tr[data-ds-id="'+i+'"]').find('.db-count').text(ids.db[i])}}
if(isObject(ids.da)){for(var i in ids.da){$('tr[data-ds-id="'+i+'"]').find('.da-count').text(ids.da[i])}}}}}});if(isObject(search)){for(var i in search){$('#form_search').find('[name="'+i+'"]').val(search[i])}}},init:function(dataset){var $_o=this;$('#form_dataset').bind('submit',function(){var formObj=$(this);var settings=getFormAjaxSettings(formObj);settings.complete=function(){formObj.find('button[type="submit"]').removeAttr('disabled')};ajax_check_userpwd(settings);return!1});$('#add_dataset_field').bind('click',function(){windowModal('添加字段',ulink('dataset/field'))});$('#set_dataset_indexes').bind('click',function(){windowModal('合理设置索引可提升数据查询速度',ulink('dataset/indexes?ds_id='+$('#form_dataset').find('[name="id"]').val()),{lg:1})});$('#dataset_fields').on('click','.field-name',function(){var tr=$_o.field_tr(this);var objid=tr.attr('id');var field=tr.find('[name="fields[]"]').val();var options={};options.ajax={type:'post',data:{objid:objid,field:field}};windowModal('编辑字段',ulink('dataset/field'),options)});$('#dataset_fields').on('click','.field-dlt',function(){var tr=$_o.field_tr(this);confirmRight('会清空该字段的所有数据，确定删除？',function(){tr.remove()})});globalOp.eleExchange('#dataset_fields','.field-move','[id^="field_"]');if(isObject(dataset)){$($_o.formid).find('[name="name"]').val(dataset.name?dataset.name:'');$($_o.formid).find('[name="desc"]').val(dataset.desc?dataset.desc:'');var config=dataset.config;if(isObject(config)){var fields=config.fields;if(isObject(fields)){for(var i in fields){var field=fields[i];if(isObject(field)){field.name_original=field.name;$_o.field_add(field,null)}}}}}},db_init:function(list,search){var $_o=this;$('#dataset_db_list').on('change','input[type="text"],input[type="number"],textarea',function(){$(this).parents('tr').eq(0).find('input[name="ids[]"]').prop('checked',!0)});$('#dataset_db_list').on('focus','textarea',function(){$(this).addClass('txt-big')});$('#dataset_db_list').on('blur','textarea',function(){$(this).removeClass('txt-big')});$.datetimepicker.setLocale('zh');$('#addnew').bind('click',function(){var minId=0;$('[name="ids[]"]').each(function(){var val=toInt($(this).val());if(minId>val){minId=val}});minId=minId-1;$_o.db_add([{id:minId}])});$('#deleteall').bind('click',function(){confirmRight('确定删除选中的数据？',function(){var ids=[];$('input[name="ids[]"]:checked').each(function(){ids.push($(this).val())});$_o.db_delete(ids)})});$('#dataset_db_list').on('click','.db-delete',function(){var tr=$(this).parents('tr').eq(0);var id=tr.find('[name="ids[]"]').val();confirmRight('确定删除数据？',function(){$_o.db_delete(id)})});$_o.db_add(list);if(isObject(search)){for(var i in search){$('#form_search').find('[name="'+i+'"]').val(search[i])}}},db_add:function(list){if(!isObject(list)){list=[]}
var tr=$('#tpl_db').clone();tr.removeAttr('id');var trHtmls='';for(var i in list){var item=list[i];if(!isObject(item)){item={}}
var id=item.id?item.id:'';trHtmls+='<tr data-db-id="'+id+'">'+tr.html().replace(/\[_id_\]/g,'['+id+']')+'</tr>'}
$('#dataset_db_list tbody').append(trHtmls);$('#dataset_db_list').find('.db-time').datetimepicker({format:'Y-m-d H:i:s'});for(var i in list){var item=list[i];if(!isObject(item)){item={}}
var id=item.id?item.id:'';var trObj=$('#dataset_db_list').find('[data-db-id="'+id+'"]');trObj.find('[data-field="id"]').html(id>0?id:'&nbsp;&nbsp;');trObj.find('[name="ids[]"]').val(id);trObj.find('input[data-field],textarea[data-field]').each(function(){var name=$(this).attr('data-field');$(this).val(item[name]?item[name]:'')})}
$('#form_list').find('.overlay').hide()},db_delete:function(ids){if(!isObject(ids)){ids=isNull(ids)?[]:[ids]}
if(ids.length>0){var ids1=[];for(var i in ids){if(ids[i]<0){$('tr[data-db-id="'+ids[i]+'"]').remove()}else{ids1.push(ids[i])}}
ids=ids1}
if(ids.length>0){ajaxOpen({type:'post',url:ulink('dataset/dbDelete?ds_id='+$('#form_list').find('[name="ds_id"]').val()),data:{ids:ids},success:function(data){ajaxDataMsg(data);if(data.code==1){var ids=data.data;if(isObject(ids)){for(var i in ids){$('tr[data-db-id="'+ids[i]+'"]').remove()}}}}})}},field_add:function(field,objid){var tr=null;if(objid){tr=$('#'+objid)}else{tr=$('#tpl_field').clone();tr.attr('id','field_'+generateUUID())}
var fname=field.name;if(field.type=='varchar'&&field.len>0){fname+=' ('+field.len+')'}
tr.find('.field-name').text(fname).attr('data-dbname',field.name_dbname);tr.find('.field-type').text(window.tpl_lang['ds_f_type_'+field.type]);tr.find('.field-desc').text(field.desc);tr.find('[name="fields[]"]').val(encode_json2urlbase(field));if(!objid){$('#dataset_fields tbody').append(tr)}},field_init:function(objid,fieldData){var $_o=this;globalOp.inputSelectCustom('#form_field [name="type"]','len',null,'varchar');$('#form_field').bind('submit',function(){ajaxOpen({type:'POST',dataType:'json',url:$('#form_field').attr('action'),data:$('#form_field').serialize(),success:function(data){if(data.code==1){data=data.data;if(isObject(data)){var objid=data.objid;var field=data.field;if(isObject(field)){var hasField=!1;var totalLen=0;if(field.type=='varchar'){field.len=toInt(field.len);field.len=field.len>0?field.len:500;totalLen+=field.len}
$('#dataset_fields tr[id^="field_"]').each(function(){var curId=$(this).attr('id');var dbname=$(this).find('.field-name').attr('data-dbname');if(dbname==field.name_dbname){if(objid&&objid==curId){hasField=!1}else{hasField=!0}}
if(!objid||objid!=curId){var fData=$(this).find('[name="fields[]"]').val();fData=decode_urlbase2json(fData);if(fData.type=='varchar'){fData.len=toInt(fData.len);fData.len=fData.len>0?fData.len:500;totalLen+=fData.len}}});if(hasField){toastr.error('字段名称已存在！')}else if(totalLen>16383){toastr.error('所有字符串字段的总长度不能超过16383')}else{$_o.field_add(field,objid);$('#myModal').modal('hide')}}}}else{toastr.error(data.msg)}}});return!1});if(objid&&isObject(fieldData)){$('#form_field [name="objid"]').val(objid);$('#form_field [name="name"]').val(fieldData.name?fieldData.name:'');$('#form_field [name="name_original"]').val(fieldData.name_original?fieldData.name_original:'');$('#form_field [name="desc"]').val(fieldData.desc?fieldData.desc:'');$('#form_field [name="type"]').val(fieldData.type?fieldData.type:'').trigger('change');fieldData.len=toInt(fieldData.len);$('#form_field [name="len"]').val(fieldData.len>0?fieldData.len:'')}},field_tr:function(curObj){return $(curObj).parents('tr[id^="field_"]').eq(0)},indexes_init:function(indexes){var $_o=this;var tplIndex='#win_dataset_indexes .index-tpl [data-tpl="index"]';var tplFields='#win_dataset_indexes .index-tpl [data-tpl="fields"]';var tableEle='#win_dataset_indexes form table.index-list';$('#win_dataset_indexes .index-add-index').bind('click',function(){$_o.indexes_add(tplIndex,tplFields,tableEle,null)});$('#win_dataset_indexes').on('change','.index-field',function(){var curTr=$(this).parents('tr[id^="index_"]').eq(0);if($(this).val()=='-1'){if(curTr.find('.index-field').length<=1){toastr.error('最少保留一个字段！');$(this).val('')}else{$(this).parents('td').eq(0).remove()}}
var ixSlt=curTr.find('.index-type');var ixVal=ixSlt.val();ixSlt.find('option').prop('disabled',!1).show();var allowIxs={};curTr.find('.index-field').each(function(){var type=$(this).find('option[value="'+$(this).val()+'"]').attr('data-type');var allowTypes={bigint:['index','unique'],double:['index','unique'],mediumtext:['fulltext'],datetime:['index','unique']};if(isObject(allowTypes[type])){ixSlt.find('option').each(function(){if(!$(this).is(':disabled')){var optVal=$(this).attr('value');if(optVal&&allowTypes[type].indexOf(optVal)<0){$(this).prop('disabled',!0).hide()}}})}});if(ixSlt.find('option[value="'+ixVal+'"]').is(':disabled')){var showVal='';ixSlt.find('option').each(function(){var optVal=$(this).attr('value');if(optVal&&!$(this).is(':disabled')){showVal=optVal;return!1}});ixSlt.val(showVal)}});$(tableEle).on('click','.index-add-field',function(){var curTr=$(this).parents('tr[id^="index_"]').eq(0);var indexId=curTr.attr('id');var fieldsObj=$(tplFields).clone();fieldsObj.find('.index-field').attr('name','indexes['+indexId+'][fields][]');curTr.find('.index-fields').append(fieldsObj)});$(tableEle).on('click','.index-delete-index',function(){var curObj=$(this);confirmRight('确定删除？',function(){curObj.parents('tr[id^="index_"]').eq(0).remove()})});if(!isObject(indexes)){indexes={}}
for(var i in indexes){var index=indexes[i];if(isObject(index)){$_o.indexes_add(tplIndex,tplFields,tableEle,index)}}},indexes_add:function(tplIndex,tplFields,tableEle,indexData){if(!isObject(indexData)){indexData={}}
var trObj=$(tplIndex).clone();var indexId='index_'+generateUUID();trObj.attr('id',indexId);trObj.find('.index-type').attr('name','indexes['+indexId+'][type]').val(indexData.type?indexData.type:'');if(isObject(indexData.fields)){for(var fi in indexData.fields){var fieldsObj=$(tplFields).clone();fieldsObj.find('.index-field').attr('name','indexes['+indexId+'][fields][]').val(fi);trObj.find('.index-fields').append(fieldsObj)}}else{var fieldsObj=$(tplFields).clone();fieldsObj.find('.index-field').attr('name','indexes['+indexId+'][fields][]');trObj.find('.index-fields').append(fieldsObj)}
$(tableEle).append(trObj)}};var datasetClass=new DatasetClass()