/*
显示文本
<span  data-type='{ table:"{$table}",re:"text",ids:"0,1,2"}'></span>
显示option
<input type="hidden" name="reid" value="0" /><span  data-type='{ re:"option",field:"reid",table:"{$table}"}'></span>

*/
$(document).ready(function(){ 
	try{
		//初始载入
		$("[data-type]").each(function() {
			var thearea=$(this);
			var options=thearea.attr("data-type");
			options=eval("(" +options+ ")");
			var table=typeof(options.table)!="undefined"?options.table:""; //数据表
			var re=typeof(options.re)!="undefined"?options.re:"option";//应用类型option,id,ids,text
			var revalue=typeof(options.revalue)!="undefined"?options.revalue:"id";//id,ids
			var id=typeof(options.id)!="undefined"?options.id:""; //当前分类id
			var sid=typeof(options.sid)!="undefined"?options.sid:0; //起始分类id
			var where=typeof(options.where)!="undefined"?options.where:false;//条件
			var sign=typeof(options.sign)!="undefined"?options.sign:"-";//上下级分类名称间隔符
			var sort=typeof(options.sort)!="undefined"?options.sort:true;
			var url=typeof(options.url)!="undefined"?options.url:'/v.php?s=/public/type/get';
			
			thearea.load(
				url,
				{
					table:table,
					re:re,
					id:id,
					sid:sid,
					sign:sign,
					revalue:revalue,
					where:where
				}
			);
			
		});
		
		$(document).on("change",".select_type",function(){
			var thearea=$(this).closest("[data-type]");
			var options=thearea.attr("data-type");
			options=eval("(" +options+ ")");
			var table=typeof(options.table)!="undefined"?options.table:"";
			var re=typeof(options.re)!="undefined"?options.re:"option";//option,id,ids,text
			var revalue=typeof(options.revalue)!="undefined"?options.revalue:"ids";//id,ids
			var sid=typeof(options.sid)!="undefined"?options.sid:0;
			var field=typeof(options.field)!="undefined"?options.field:"";
			var url=typeof(options.url)!="undefined"?options.url:'/v.php?s=/public/type/get';
			var sort=typeof(options.sort)!="undefined"?options.sort:true;
			var the=$(this);
			the.nextAll().remove();
			//表单赋值
			if(field!=""){
				var fieldname=$("[name='"+field+"']");
				if(revalue=="id"){
					fieldname.val(the.val());	
				}else{
					//console.log(the.index());
					if(the.index()==0){
						if(sid!=0){
							if(the.val()==0){
								fieldname.val(sid);
							}else{
								fieldname.val(sid+","+the.val());	
							}	
						}else{
							if(the.val()==0){
								fieldname.val("0");
							}else{
								fieldname.val("0,"+the.val());	
							}	
						}
					}else{
						if(sid!=0){
							var sid_length=sid.split(',').length-1;
							var val_old=fieldname.val();
							var val_old_arr=val_old.split(",");
							val_old_arr[the.index()+1+sid_length]=the.val();
							if(the.val()==0){
								fieldname.val(val_old_arr.slice(0,the.index()+1+sid_length).join(","));	
							}else{
								fieldname.val(val_old_arr.slice(0,the.index()+2+sid_length).join(","));	
							}
						}else{
							var val_old=fieldname.val();
							var val_old_arr=val_old.split(",");
							val_old_arr[the.index()+1]=the.val();
							if(the.val()==0){
								fieldname.val(val_old_arr.slice(0,the.index()+1).join(","));	
							}else{
								fieldname.val(val_old_arr.slice(0,the.index()+2).join(","));	
							}
						}
					}
				}
			}
	
			//添加子集
			if(the.val()!=0){
				$.get(
					url,
					{
						sid:the.val(),
						table:table,
						re:"option"
					},
					function(data){
						if(data!=""){
							the.after(data);
						}
					}
				);
			}
			
			//排序
			if(the.val()!=0 && sort===true){
				$.get(
					url,
					{
						reid:the.val(),
						table:table,
						re:"sort"
					},
					function(data){
						if(data!=""){
							$('input.disorder').val(data);
							//console.log(data);
						}
					}
				);
			}
		});
	}catch(e){}
});
	