$(function(){

	
	fetchPicture = function(){
		var prodId = $("#prodId").val();
		var _path=HttpRest+"product/picture/fetch.shtml?prodId="+prodId;
		$.ajax({
			url: _path, 
			type: "get", 
			dataType: "json",
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					var vhtml = template('picHtml', data);
					$("#piclist").html(vhtml);
				}		
			}
		});
	};

	fetchPicture();
	
	chooseImg = function(picId,_path){
		$("#picId").val(picId);
		$("#_path").val(_path);
	};
	
	removeBind = function(picId){
		$("#pic_"+picId).remove();
	}


	$("#saveBtn").click(function(){ // 保存尺码
		var _path = $("#_path").val();
		var picId = $("#picId").val();
		if(_path==''){
			layer.msg('图片路径为空',{icon: 5});
			return;
		}
		var prodId = $("#prodId").val();
		var params={prodId:prodId,type:'2',picId:picId,fpath:_path};

		var _path=HttpRest+"product/picture/save.shtml";
		$.ajax({
			url: _path, 
			type: "post", 
			dataType: "json",
			data:$.toJSON(params),   
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					layer.msg('保存成功',{icon: 1});
					$("#picId").val('0');
					$("#_path").val("");
					var vhtml = template('addPicHtml',params);
					$("#piclist").append(vhtml);
				}
			}
		});
	});
	
	removeBind = function(picId){
		var _path=HttpRest+"product/picture/remove.shtml?picId="+picId;
		$.ajax({
			url: _path, 
			type: "get", 
			dataType: "json",
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					$("#pic_"+picId).remove();
					layer.msg('删除成功',{icon: 1});
				}		
			}
		});
	};
});
