$.widget("ui.coveringnotelist", $.ui.dialog, {
	options: {
		filter: {},
		title: 'Lähetehaku'
	},
	_dialogCreate: $.ui.dialog.prototype._create,
	_create: function() {
		var widget = this;
		widget._dialogCreate();
		widget.option({
			height: $(window).height()-10,
			width: 600
		});
		widget.covCollection = new ec.webi.Coveringnotes;
		widget.covDateStart = $('<input id="fe-covdatestart-datepicker" type="text" />').datepicker();
		widget.covDateEnd = $('<input id="fe-covdatestend-datepicker" type="text" />').datepicker();
		widget.ecomId = $('<input type="text" size="5" />');
		widget.location = $('<input type="text" size="10" />');
		widget.mark = $('<input type="text" size="10" />');
		widget.customerName = $('<input type="text" size="10" />');
		widget._setFilterValues();
		widget.filterSubmit = $('<button>Päivitä</button>').button().click(function() {
			if (!widget.options.filter.covdate) {
				widget.options.filter.covdate = {};
			}
			widget.options.filter.covdate.min = ec.formatDate(widget.covDateStart.datepicker('getDate'));
			widget.options.filter.covdate.max = ec.formatDate(widget.covDateEnd.datepicker('getDate'));
			widget.options.filter.covecomid = widget.ecomId.val();
			widget.options.filter.location = widget.location.val();
			widget.options.filter.mark = widget.mark.val();
			widget.options.filter.customername = widget.customerName.val();
			widget.fetchCovnotes();
			return false;
		});
		widget.element
			.append($('<p />').append('Nro ').append(widget.ecomId).append(' Pvm ').append(widget.covDateStart).append(' - ').append(widget.covDateEnd).append(' ').append(widget.filterSubmit))
			.append($('<p />').append('Asiakas ').append(widget.customerName).append(' Kohde ').append(widget.location).append(' Merkki ').append(widget.mark));
		widget.covTable = $('<table class="selectabletable" cellpadding="0" cellspacing="0"><tr><th>Nro</th><th>Pvm</th><th>Asiakas</th><th>Kohde</th><th>Merkki</th></tr></table>').appendTo(widget.element);
		widget.covTableBody = $('<tbody></tbody>').appendTo(widget.covTable);
		widget.CoveringNoteView = Backbone.View.extend({
			tagName: 'tr',
			template: _.template('<td class="cov-ecomid"></td><td class="cov-covdate"></td><td class="cov-customer"></td><td class="cov-location"></td><td class="cov-mark"></td>'),
			initialize: function() {
				_.bindAll(this, 'render');
				this.model.bind('change', this.modelChanged);
				this.model.view = this;
			},
			events: {
				'click': 'covNoteClicked'
			},
			modelChanged: function() {
				this.view.render();
				widget._trigger('covNotesChanged');
			},
			covNoteClicked: function() {
				widget._trigger('covNoteClicked', null, {
					Id: this.model.get('Id'),
					EcomId: this.model.get('EcomId')
				});
				return false;
			},
			render: function() {
				$(this.el).html(this.template(this.model.toJSON.covnote));
				this.setContent();
				return this;
			},
			setContent: function() {
				var ecomid = this.model.get('EcomId') || '(numeroimaton lähete)';
				var custName = '';
				if (this.model.get('Customer')) {
					custName = this.model.get('Customer').Name;
				}
				var date = ec.parseDateTime(this.model.get('CovDate')), dateString = "";
				if (date != null) {
					var month = date.getMonth() + 1;
					dateString = date.getDate() + '.' + month + '.' + date.getFullYear();
				}
				var covDate = dateString;
				var location = this.model.get('Location');
				var mark = this.model.get('Mark');
				this.$('.cov-ecomid').text(ecomid);
				this.$('.cov-covdate').text(covDate);
				this.$('.cov-customer').text(custName);
				this.$('.cov-location').text(location);
				this.$('.cov-mark').text(mark);
			}

		});

		//haetaan lähetteet
		widget.fetchCovnotes();
	},
	getCollectionCount: function() {
		return this.covCollection.length;
	},
	fetchCovnotes: function() {
		var widget = this;
		widget.covCollection.url = 'lahetteet?' + $.param({filter: this.options.filter});
		widget.covCollection.fetch({
			success: function() {
				widget._trigger('covNotesChanged');
				widget.covTableBody.html('');
				widget.covCollection.each(function(covnote) {
					var view = new widget.CoveringNoteView({model: covnote});
					widget.covTableBody.append(view.render().el);
				});
			}
		});
	},
	_setFilterValues: function() {
		var widget = this;
		if (widget.options.filter.covdate) {
			widget.covDateStart.datepicker('setDate', ec.parseDateTime(widget.options.filter.covdate.min));
			widget.covDateEnd.datepicker('setDate', ec.parseDateTime(widget.options.filter.covdate.max));
		}
		if (widget.options.filter.covecomid) {
			widget.ecomId.val(widget.options.filter.covecomid);
		}
		if (widget.options.filter.customername) {
			widget.customerName.val(widget.options.filter.customername);
		}
		if (widget.options.filter.location) {
			widget.location.val(widget.options.filter.location);
		}
		if (widget.options.filter.mark) {
			widget.mark.val(widget.options.filter.mark);
		}
	}
});
