
var feedback_admin = {
	
	maxImages: 5,
	images: [],
	id: 0,
	
	init: function(id){
		var self = this;
		this.id = id;
		self.initImages();
		$('#f_images2').on('click','.f-image-remove', function(){
			self.removeImage(this);
		});
		$('.f_activate').click(function(){
			self.activate(this);
		});
		$('.f_remove').click(function(){
			self.remove(this);
		});
		
		$('#f_form2').submit(function(){ self.submit(); return false; });
		$('#f_submit_btn2').attr('disabled', false);
	},
	
	submit: function(){
		var self = this;
		$('#f_form2 .f-error').hide();
		var post = this.getFormData(); 

		if(post.name.length < 3) return $('#f_name_error2').show();
		if(post.text.length < 10) return $('#f_text_error2').show();

		$('#f_submit_btn2').html('<i class="fa fa-spin fa-refresh"></i> Отправка...').attr('disabled', true);
		
		post.images = self.images.length;
		$.post('/feedback/'+post.url+'.php', post, function(id){
			self.id = id;
			self.saveImages().done(function(){
				document.location.href = '/otzyvy/';
			/*	$('#f_form2').remove();
				$('#f_success2').show();
				$('body,html').animate({
					scrollTop: 0
				}, 800);*/
			});
		});
	},
	
	
	
	getFormData: function(){
		var i, data, post = {};
		data = $('#f_form2').serializeArray();
		for(i in data) post[data[i].name] = data[i].value;
		return post;
	},
	
	saveImages: function(dfd){
		var i, flag = true, self = this;
		dfd = dfd || $.Deferred();
		for(i in self.images){ //console.log(self.images[i])
			if(self.images[i].uploaded) continue;
			flag = false;
			self.images[i].uploaded = true;
			self.images[i].formData.id = self.id;
				self.images[i].submit().done(function(){			
				self.saveImages(dfd);
			});
			break;
		}
		if(flag) dfd.resolve();
		return dfd.promise();
	},
	
	initImages: function(){
		var self = this, div;
		$('#f_images2 .f-preview').each(function(k){
			div = $(this);
			self.images[k] = {div:div, uploaded: true}; 
		}); //console.log(self.images)
		var self = this;
		$('#f_form2 .fileinput-btn').fileupload({
			url: '/feedback/uploader.php',
			autoUpload: false,
			acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
			maxFileSize: 10000000,
	        previewMaxWidth: 112,
	        previewMaxHeight: 112,
	        disableImageResize: false,
	        imageMaxWidth: 1024,
    		imageMaxHeight: 1024,
	        previewCrop: true,
	        formData: {}
		}).on('fileuploadadd', function (e, data) {

		}).on('fileuploadprocessalways', function (e, data) {
			self.imgReady(data);
		});
		$('.fileinput-btn').bind('fileuploaddone',function(e, data){ 
			//self.uploaded(data.result);
		});
	},
	
	imgReady: function(data){
		if(this.images.length >= this.maxImages) return this.disableImages();
		var id = this.images.length;
		var div = $('<div class="f-preview" style="display:none" data-id="'+id+'"/>');
		div.append(data.files[0].preview);
		div.append('<div><button type="button" class="btn btn-danger btn-xs f-image-remove" data-id="'+id+'"><b>X</b></button></div>');
		$('#f_images2').append(div);
		div.fadeIn('fast');
		data.div = div;
		data.uploaded = false;
		this.images.push(data);
		if(this.images.length >= this.maxImages) this.disableImages();
	},
	
	disableImages: function(){
		$('.fileinput-btn').attr('disabled', true);
	},
	
	removeImage: function(btn){
		var i, imgs = [], id = $(btn).attr('data-id'), self = this;
		for(i in this.images){ 
			if(i == id) continue;
				imgs.push(this.images[i]);
				this.images[i].div.attr('data-id', imgs.length-1);
				this.images[i].div.find('.f-image-remove').attr('data-id', imgs.length-1);
		}
		this.images[id].div.fadeOut('fast', function(){
			self.images[id].div.remove();
			self.images = imgs;
		});
		
		$('#f_form2 .fileinput-btn').attr('disabled', false);
		var bid = $(btn).attr('data-bid');
		if(typeof bid != 'undefined') $.post('/feedback/remove_image.php',{id:bid});
	},
		
	activate: function(obj){
		var id = $(obj).attr('data-id');
		$('.feed_'+id).removeClass('feedback_not');
		$(obj).remove();
		$.post('/feedback/activate.php',{id:id});
	},
	
	remove: function(obj){
		var id = $(obj).attr('data-id');
			if(!confirm('Удалить запись?')) return;
		$('.feed_'+id).fadeOut('fast');
		$('.feed_parent_'+id).fadeOut('fast');
		$.post('/feedback/remove.php',{id:id});
	}
}


$(function(){ feedback_admin.init(); });
