var odbicia = (function($, undefined) {
	"use strict"

	var modal = $('#generuj-odbicia-modal');

	var elementModalHtml = {
		Data: function (value, editable) {
			var label = $('<label class="col-xs-12 col-md-4 control-label">Data</label>');
			var column = $('<div class="col-xs-12 col-md-8"></div>');
			console.log(editable);
			var input = '';
			if(editable == 'true') {
				input = $('<input name="data[bGenerujOdbicie][Data]" id="bGenerujOdbicieData" type="text"' +
					' autocomplete="off" class="form-control .datepicker input-125"  required="required" value="' + value + '">');
			} else {
				input = $('<input name="data[bGenerujOdbicie][Data]" id="bGenerujOdbicieData" type="text"' +
					' autocomplete="off" class="form-control .datepicker input-125" disabled required="required" value="' + value + '">');
			}
			input.datetimepicker({
				format: "YYYY-MM-DD",
				locale: 'pl',
				keepOpen: false,
				showClose: true,
				useCurrent: false
			}).on('dp.change', function (ev) {
				$('.datepicker').trigger('blur');
			});
			column.append(input);
			return $('<div class="form-group"></div>').append(label).append(column);
		},
		Czas: function (value) {
			value = value !== undefined ? value : '00:00';
			var label = $('<label class="col-xs-12 col-md-4 control-label">Czas</label>');
			var column = $('<div class="col-xs-12 col-md-8"></div>');
			var input = $('<input name="data[bGenerujOdbicie][Czas]" id="bGenerujOdbicieCzas" type="text"' +
				' autocomplete="off" class="form-control timepicker input-125 valid" required="required" value="' + value + '">');
			/*
			input.datetimepicker({
				format: 'HH:mm',
				locale: 'pl',
				keepOpen: false,
				showClose: true,
				date: null
			}).on('dp.change', function (ev) {
				$('.timepicker').trigger('blur');
			});
			 */
			column.append(input);
			return $('<div class="form-group"></div>').append(label).append(column);
		},
		Rodzaj: function (value, options) {
			var label = $('<label class="col-xs-12 col-md-4 control-label">Rodzaj</label>');
			var column = $('<div class="col-xs-12 col-md-8"></div>');
			var select = $('<select name="data[bGenerujOdbicie][Rodzaj]" id="bGenerujOdbicieRodzaj" class="form-control" style="width: 100%"></select>');
			if (options) {
				var row;
				options.forEach(function(item){
					row = $('<option value="' + item + '">' + item + '</div>');
					select.append(row);
				});
			}
			column.append(select);
			return $('<div class="form-group"></div>').append(label).append(column);
		},
		Typ: function (value, options) {
			var label = $('<label class="col-xs-12 col-md-4 control-label">Typ</label>');
			var column = $('<div class="col-xs-12 col-md-8"></div>');
			var select = $('<select name="data[bGenerujOdbicie][bGenerujOdbicieTyp]" id="bGenerujOdbicieTyp" class="form-control" style="width: 100%"></select>');
			if (options) {
				var row;
				options.forEach(function(item){
					row = $('<option value="' + item + '">' + item + '</div>');
					select.append(row);
				});
			}
			column.append(select);
			return $('<div class="form-group"></div>').append(label).append(column);
		},
		RodzajPracy: function (value, options, bSpecyfika) {
			var label = $('<label class="col-xs-12 col-md-4 control-label">Rodzaj Pracy</label>');
			var column = $('<div class="col-xs-12 col-md-8"></div>');
			var fieldset = $('<fieldset id="bGenerujOdbicieRodzajPracy">');
			if (fieldset) {
				var input;
				options.forEach(function(item) {
					var checked = '';
					if(value === item) {
						checked = 'checked';
					}
					var inputLabel = item;
					inputLabel = inputLabel.replace(/([A-Z]+)/g, " $1").trim();
					if (bSpecyfika == 'SGB') {
						if('Praca' == inputLabel) {
							inputLabel = 'Praca Biuro';
						}
					}
					input = $('<div class="form-check"><input class="form-check-input" type="radio" name="data[bGenerujOdbicie][bRodzajPracy]" value="' + item + '" ' + checked +'><label class="form-check-label" for="data[bGenerujOdbicie][bRodzajPracy]"> ' + inputLabel + '</label></div>');
					fieldset.append(input);
				})
			}
			column.append(fieldset);
			return $('<div class="form-group"></div>').append(label).append(column);
		}
	}

	var app = {

		buildModal: function (czyUproszczone, values, bSpecyfika) {
			console.log('build');
			var form = modal.find('.form-horizontal');
			if (form.length > 0) {
				form.empty();
				var row = $('<div class="col-xs-12"></div>');
				if (czyUproszczone == 'true') {
					row.append(elementModalHtml.Data(values.Data, values.DataEditable));
				} else if (bSpecyfika == 'SGB') {
					row.append(elementModalHtml.Data(values.Data, values.DataEditable))
						.append(elementModalHtml.RodzajPracy(values.RodzajPracy, values.RodzajPracyOptions, bSpecyfika));
				} else {
					row.append(elementModalHtml.Data(values.Data, values.DataEditable))
						.append(elementModalHtml.Czas(values.Czas))
						.append(elementModalHtml.Rodzaj(values.Rodzaj, values.RodzajOptions))
						.append(elementModalHtml.Typ(values.Typ, values.TypOptions));
				}
				form.append(row);
				timepickerValid();
			}
		}

	}

	return app;

})(jQuery);
