$(function(){
	var iptKeyUp = function(){
		if($(this).val()==""){
			$(this).addClass("ipt-value-error");
		}else{
			$(this).removeClass("ipt-value-error");		
		}
	};

	$(".btn-add-type").click(function(){
		var addTypeDialog = $('<div id="add-type-dialog"><input type="text" class="typename" id="typename" name="typename" value="" /></div>').dialog({resizable:false,modal:true,title:'添加商品类别',width:300,height:130,buttons:[{text:'确认添加',click:function(){
			var typename=$('#typename').val();if(typename==''){$('#typename').addClass('error').focus();return false;}else{$('#typename').removeClass('error');}
				$.ajax({
					type:"POST",url:"/qkadmin/attr",data:{inajax:1,act:'addType',name:typename},dataType:"json",success:function(json){
						if(json.success){
							location.reload();
						}else{
							qlib.tips({ icon:'error',msg:json.message });
						}
					}
				});			
				$('#add-type-dialog').dialog("destroy");
			}
			},{text:'取消',click:function(){addTypeDialog.dialog("destroy");}}]});
			$('#typename').unbind().blur(function(){var typename=$('#typename').val();if(typename==''){$('#typename').addClass('error').focus();}else{$('#typename').removeClass('error');}});
	});
	$('.btn-type-delete').click(function(){
		if(!confirm('删除操作不可恢复，是否确认？')){ return false; }
		var sid=$(this).attr("sid");
		var ext = $(this);
		$.ajax({ type:"POST",url:"/qkadmin/attr",data:{inajax:1,act:'delType',id:sid},dataType:"json",success:function(json){
				if(json.success){ ext.parent("td").parent("tr").remove(); }else{ qlib.tips(json.message); }
			}
		});

	});
	var addValue = function(){
		var attrId = $(this).attr("attrId");
		if(!attrId || isNaN(parseInt(attrId,10))){ alert("属性错误");return false; }
		var ipt = $(this).prev("input.ipt-value");
		var attrValue = ipt.val();
		if(attrValue==""){ ipt.addClass("ipt-value-error");ipt.focus(); }
		var sdata = { inajax:1,act:"addExtValue",attrId:attrId,attrValue:attrValue };
		$.ajax({
			type:"POST",url:"/qkadmin/attr",data:sdata,dataType:"json",success:function(json){
				if(json.success){
					var del = $('<a href="javascript:void(0)" title="删除" class="inner-value-close" sid="'+json.data.id+'">×</a>');
					del.bind('click',delExt);
					var html = $('<li><span class="inner-value-name">'+attrValue+'</span></li>');
					html.append(del);
					ipt.parent("li").before(html);
					ipt.val('');
				}else{
					qlib.tips({ icon:'error',msg:json.message });
				}
			}
		})
	};
	
	var delExt = function(){
		if(!confirm('删除操作不可恢复，是否确认？')){ return false; }
		var ext = $(this);
		var sid = $(this).attr("sid");
		if(!sid || isNaN(parseInt(sid,10))){ qlib.tips('属性可选值ID不存在'); return false; }
		var sdata = { inajax:1,act:"delExt",id:sid };
		$.ajax({ type:"POST",url:"/qkadmin/attr",data:sdata,dataType:"json",success:function(json){
				if(json.success){ ext.parent("li").remove(); }else{ qlib.tips(json.message); }
			}
		});
	};

	var delAttr  = function(){
		if(!confirm('删除后不可恢复，请确认是否继续？')){ return false; }
		var btn = $(this);
		var sid = $(this).attr("sid");
		if(!sid || isNaN(parseInt(sid,10))){  qlib.tips('属性ID不存在');return false; }
		var sdata = { inajax:1,act:"delAttr",id:sid };
		$.ajax({ type:"POST",url:"/qkadmin/attr",data:sdata,dataType:"json",success:function(json){ 
				if(json.success){ btn.parent("td").parent("tr").remove();  }else{ qlib.tips({ icon:'error',msg:json.message }); };
			}
		});
	}

	var addAttr = function(){
		var typeId = $(this).attr("sid");
		if(!typeId || isNaN(parseInt(typeId,10))){ qlib.tips('类型ID不存在');return false; }
		var addBtn = $('<a href="javascript:void(0)" class="btn-attr-add">添加</a>');
		addBtn.click(function(){
			var ipt = $(this).prev("input");
			var attrName = ipt.val(); if(attrName==""){ qlib.tips('属性名不能为空'); return false;}
			var sdata = { inajax:1,act:"addAttr",typeId:typeId,attrName:attrName };
			$.ajax({ type:"POST",url:"/qkadmin/attr",data:sdata,dataType:"json",success:function(json){ 
					if(json.success){
						var tableAttr  = $("table.table-attrs");
						var ipt = $('<input type="text" class="ipt-value" value="" />');ipt.keyup(iptKeyUp);
						var btn = $('<a href="javascript:void(0)" class="btn-value-add" attrId="'+json.data.id+'">添加</a>');btn.click(addValue);
						var del = $('<a href="javascript:void(0)" class="btn-attr-del" sid="'+json.data.id+'">删除属性</a>');del.click(delAttr);
						var tr = $('<tr><td>'+attrName+'<br/></td><td class="tleft ext-values"><ul class="exts"><li class="ext-value-add"></li></ul></td></tr>');
						tr.find("li.ext-value-add").append(ipt).append(btn);
						tr.find("td").eq(0).append(del);
						tableAttr.append(tr);
						addAttrDialog.dialog("destroy");
					}else{ qlib.tips({ icon:'error',msg:json.message }); }
				}
			});
		});
		var addIpt = $('<input type="text" class="ipt-value" value="">');addIpt.keyup(iptKeyUp);
		var addAttrDialog = $('<div></div>');
		addAttrDialog.dialog({ width:350,height:150,title:'添加商品属性',resizable:false,dialogClass:'layer_global_main' });
		addAttrDialog.append(addIpt).append(addBtn);
	};

	$(".btn-attr-add-dialog").unbind().click(addAttr);
	$("a.btn-value-add").unbind().click(addValue);
	$(".inner-value-close").unbind().bind("click",delExt);
	$("input.ipt-value").keyup(iptKeyUp);

	$(".btn-attr-del").unbind().click(delAttr);
});