$(document).ready(function () {
	InitializeTableCost();
	InitializeTableComponents();
});

function trigerDocumentsPZ($this){
	$this = $($this);
	$this.closest('tr').next().find('div').toggle(400);
}
function trigerStockOperations($this){
	$this = $($this);
	$this.closest('tr').next().next().find('div').toggle(400);
}
function InitializeTableCost() {
	// Initialize appendGrid
	$('#tableTimeEdit').appendGrid({
		hideRowNumColumn: false,
		initRows: 1,
		initData: $.parseJSON($("input[name='action_list']").val()),
		columns: [
			{
				name: 'indeks',
				display: 'Indeks',
				type: 'text',
				ctrlAttr: {readonly: 'readonly'},
				displayCss: {
					'width': '80px',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				}
			}, {
				name: 'name',
				display: 'Nazwa',
				type: 'text',
				ctrlAttr: {readonly: 'readonly'},
				displayCss: {
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				}
			}, {
				name: 'category',
				display: 'Kategoria',
				type: 'text',
				ctrlAttr: {readonly: 'readonly'},
				displayCss: {
					'width': '20%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				},
			}, {
				name: 'unit_id',
				display: SUGAR.language.get("app_strings", "LBL_JM"),
				type: 'custom',
				displayCss: {
					'width': '5%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				customBuilder: function (parent, idPrefix, name, uniqueIndex) {
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					var ctrl = document.createElement('span');
					$(ctrl).attr({id: ctrlId, name: ctrlId}).appendTo(parent);
					$(parent).css({'text-align': 'right'});
				},
				customSetter: function (idPrefix, name, uniqueIndex, value) {
					var tmp = SUGAR.language.get("app_list_strings", "ecmproducts_unit_dom");
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					$('#' + ctrlId).html(tmp[value]);
				},
				customGetter: function (idPrefix, name, uniqueIndex) {
					return '';
				},
				ctrlAttr: {readonly: 'readonly'},
				ctrlCss: {
					'text-align': 'left'
				},
				ctrlClass: 'inputs',
			}, {
				name: 'quantity',
				display: 'Ilość',
				type: 'text',
				ctrlCss: {'text-align': 'right'},
				ctrlAttr: {readonly: 'readonly'},
				displayCss: {
					'width': '10%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				},
			}, {
				name: 'price',
				display: 'Koszt',
				type: 'custom',
				ctrlCss: {'text-align': 'right'},
				ctrlAttr: {readonly: 'readonly'},
				displayCss: {
					'width': '10%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black',
				},
				customBuilder: function (parent, idPrefix, name, uniqueIndex) {
					// Tworzenie Id dla inputow
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					//kontener
					var ctrl = document.createElement('span');
					//dodajemy kontener do komorki w tabeli
					$(ctrl).attr({id: ctrlId, name: ctrlId}).appendTo(parent);
					//Tworzymy pierwszy input dla ceny jednostkowej
					var Single = $('<input/>', {type: 'text', id: ctrlId + '_Single', name: ctrlId + '_Single', class: 'inputs', readonly: 'readonly'}).css({'width': '100%', 'text-align': 'right'});
					//Ustawiamy wartosc domyslna
					$(Single).val('0,00');
					//dodajemy do kontenera
					$(Single).appendTo(ctrl);
					//oddzielamy widoczna linia
					var hr = document.createElement('hr');
					$(hr).css('margin', '0em').appendTo(ctrl);
					//Tworzymy drugi input dla sumy
					var Summary = $('<input/>', {type: 'text', id: ctrlId + '_Summary', name: ctrlId + '_Summary', class: 'inputs', readonly: 'readonly'}).css({'width': '100%', 'text-align': 'right'});
					//Ustawiamy wartosc domyslna
					$(Summary).val('0,00');
					$(Summary).appendTo(ctrl);
					return ctrl;
				},
				customGetter: function (idPrefix, name, uniqueIndex) {
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					var Single = $('#' + ctrlId + '_Single').val();
					var Summary = $('#' + ctrlId + '_Summary').val();
					return {Single: Single, Summary: Summary};
				},
				customSetter: function (idPrefix, name, uniqueIndex, value) {
					if (value != null) {
						var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
						$('#' + ctrlId + '_Single').val(value['Single'] == null ? '0,00' : FormatNumber(value['Single']));
						$('#' + ctrlId + '_Summary').val(value['Summary'] == null ? '0,00' : FormatNumber(value['Summary'],6));
					}
				},
			}, ],
		i18n: {
			rowEmpty: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_ROWEMPTY"),
		},
		hideButtons: {
			removeLast: true,
			append: true,
			insert: true,
			remove: true,
			moveUp: true,
			moveDown: true
		}
	});
	$('#tableTimeEdit_price_td_head').html('Cena<br>Wartość');
	var count = $('#tableTimeEdit').appendGrid('getRowCount');
	for (var i = 0; i < count; i++) {
		var value = $('#tableTimeEdit').appendGrid('getCtrlValue', 'quantity', i);
		$('#tableTimeEdit').appendGrid('setCtrlValue', 'quantity', i, FormatNumber(value,6));
	}
	//Podsumowanie
	var data = $('#tableTimeEdit').appendGrid('getAllValue');
	var sumPriceSingle = 0;
	var sumPriceTotal = 0;
	$.each(data, function (index, value) {
		sumPriceTotal += parseFloat(UnformatNumber(value.price.Summary,6));
	});
	$('#tableTimeEdit').find('tfoot tr:last').after('<tr><td colspan="6" style="vertical-align: middle;border-top: 1px solid black">Suma:</td><td style="text-align: right;border-top: 1px solid black">' + FormatNumber(sumPriceTotal,6) + '</td></tr>');
}

function InitializeTableComponents() {
	// Initialize appendGrid
	$('#tableComponents').appendGrid({
		hideRowNumColumn: false,
		initRows: 1,
		initData: $.parseJSON($("input[name='component_list']").val()),
		columns: [
			{
				name: 'code',
				display: SUGAR.language.get("app_strings", "LBL_INDEX"),
				type: 'text',
				displayCss: {
					'width': '150px',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				ctrlCss: {
					'text-align': 'left',
					'vertical-align': 'middle',
				},
				ctrlAttr: {readonly: 'readonly'},
				ctrlClass: 'inputs'
			}, {
				name: 'name',
				display: SUGAR.language.get("app_strings", "LBL_NAME"),
				type: 'text',
				displayCss: {
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				ctrlAttr: {readonly: 'readonly'},
				ctrlClass: 'inputs',
				ctrlCss: {
					'text-align': 'left'
				},
			}, {
				name: 'product_category_name',
				display: SUGAR.language.get("app_strings", "LBL_CATEGORY"),
				type: 'text',
				displayCss: {
					'width': '10%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				ctrlCss: {
					'text-align': 'left'
				},
				ctrlClass: 'inputs',
				ctrlAttr: {
					readonly: 'readonly'
				},
			}, {
				name: 'unit_id',
				display: SUGAR.language.get("app_strings", "LBL_JM"),
				type: 'custom',
				displayCss: {
					'width': '3%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				customBuilder: function (parent, idPrefix, name, uniqueIndex) {
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					var ctrl = document.createElement('span');
					$(ctrl).attr({id: ctrlId, name: ctrlId}).appendTo(parent);
					$(parent).css({'text-align': 'right'});
				},
				customSetter: function (idPrefix, name, uniqueIndex, value) {
					var tmp = SUGAR.language.get("app_list_strings", "ecmproducts_unit_dom");
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					$('#' + ctrlId).html(tmp[value]);
				},
				customGetter: function (idPrefix, name, uniqueIndex) {
					return '';
				},
				ctrlAttr: {readonly: 'readonly'},
				ctrlCss: {
					'text-align': 'left'
				},
				ctrlClass: 'inputs',
			}, {
				name: 'quantity_recipe',
				display: SUGAR.language.get("app_strings", "LBL_QUANTITY_RECIPE"),
				type: 'text',
				ctrlClass: 'inputs',
				value: '0.00000000',
				displayCss: {
					'width': '7%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				ctrlCss: {
					'text-align': 'right'
				},
				ctrlAttr: {readonly: 'readonly'},
			}, {
				name: 'divider',
				display: SUGAR.language.get("app_strings", "LBL_DIVIDER"),
				type: 'text',
				ctrlClass: 'inputs',
				value: '0,00',
				displayCss: {
					'width': '7%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				ctrlCss: {
					'text-align': 'right'
				},
				ctrlAttr: {readonly: 'readonly'},
			}, {
				name: 'quantity',
				display: SUGAR.language.get("app_strings", "LBL_QUANTITY"),
				type: 'text',
				ctrlClass: 'inputs',
				value: '0.000000',
				displayCss: {
					'width': '7%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				ctrlCss: {
					'text-align': 'right'
				},
				ctrlAttr: {readonly: 'readonly'},
			}, {
				name: 'price',
				display: 'Cena / Wartość',
				type: 'custom',
				displayCss: {
					'width': '7%',
					'vertical-align': 'middle',
					'text-align': 'center',
					'background-color': 'rgb(224,240,255)',
					'border': '1px solid rgb(48,192,255)',
					'height': '0px',
					'font-family': 'Arial',
					'font-size': '12px',
					'color': 'black'
				},
				customBuilder: function (parent, idPrefix, name, uniqueIndex) {
					// Tworzenie Id dla inputow
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					//kontener
					var ctrl = document.createElement('span');
					//dodajemy kontener do komorki w tabeli
					$(ctrl).attr({id: ctrlId, name: ctrlId}).appendTo(parent);
					//Tworzymy pierwszy input dla ceny jednostkowej
					var Single = $('<input/>', {type: 'text', id: ctrlId + '_Single', name: ctrlId + '_Single', class: 'inputs', readonly: 'readonly'}).css({'width': '100%', 'text-align': 'right'});
					//Ustawiamy wartosc domyslna
					$(Single).val('0,00');

					//dodajemy do kontenera
					$(Single).appendTo(ctrl);
					//oddzielamy widoczna linia
					var hr = document.createElement('hr');
					$(hr).css('margin', '0em').appendTo(ctrl);
					//Tworzymy drugi input dla sumy
					var Summary = $('<input/>', {type: 'text', id: ctrlId + '_Summary', name: ctrlId + '_Summary', class: 'inputs', readonly: 'readonly'}).css({'width': '100%', 'text-align': 'right'});
					//Ustawiamy wartosc domyslna
					$(Summary).val('0,00');
					$(Summary).appendTo(ctrl);
					return ctrl;
				},
				customGetter: function (idPrefix, name, uniqueIndex) {
					var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
					var Single = $('#' + ctrlId + '_Single').val();
					var Summary = $('#' + ctrlId + '_Summary').val();
					return {Single: Single, Summary: Summary};
				},
				customSetter: function (idPrefix, name, uniqueIndex, value) {
					if (value != null) {
						var ctrlId = idPrefix + '_' + name + '_' + uniqueIndex;
						$('#' + ctrlId + '_Single').val(value['Single'] == null ? '0,00' : FormatNumber(value['Single']));
						$('#' + ctrlId + '_Summary').val(value['Summary'] == null ? '0,00' : FormatNumber(value['Summary'],6));
					}
				},
			}, {
				name: 'ecmcomponent_id',
				display: 'ecmcomponent_id',
				type: 'text',
				invisible: true,
			}, {
				name: 'product_category_id',
				display: 'product_category_id',
				type: 'text',
				invisible: true,
			},
		],
		i18n: {
			append: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_APPEND"),
			remove: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_REMOVE"),
			insert: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_INSERT"),
			moveUp: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_MOVEUP"),
			moveDown: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_MOVEDOWN"),
			removeLast: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_REMOVELAST"),
			rowEmpty: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_ROWEMPTY"),
			rowDrag: SUGAR.language.get("app_strings", "LBL_APPENDGRID_I18_ROWDRAG"),
		},
		customGridButtons: {
			insert: function () {
				var button = document.createElement('img');
				button.src = 'modules/EcmQuotes/images/add_position.gif';
				return button;
			},
			remove: function () {
				var button = document.createElement('img');
				button.src = 'modules/EcmQuotes/images/deleterow.gif';
				return button;
			},
			moveUp: function () {
				var button = document.createElement('img');
				button.src = 'modules/EcmQuotes/images/moverowup.gif';
				return button;
			},
			moveDown: function () {
				var button = document.createElement('img');
				button.src = 'modules/EcmQuotes/images/moverowdown.gif';
				return button;
			},
		},
		afterRowRemoved: function (caller, rowIndex) {
			var quantity = $('#tableComponents').appendGrid(
					'getRowCount');
			if (quantity == 0) {
				$('#tableComponents').appendGrid('appendRow', 1);
			}
		},
		hideButtons: {
			removeLast: true,
			append: true,
			insert: true,
			remove: true,
			moveUp: true,
			moveDown: true
		}
	});
	$('#tableComponents_price_td_head').html('Cena<br>Wartość');
	var count = $('#tableComponents').appendGrid('getRowCount');
	for (var i = 0; i < count; i++) {
		var value = $('#tableComponents').appendGrid('getCtrlValue', 'quantity', i);
		var divider = $('#tableComponents').appendGrid('getCtrlValue', 'divider', i);
		$('#tableComponents').appendGrid('setCtrlValue', 'quantity', i, QuantityFormat(value,6));
		$('#tableComponents').appendGrid('setCtrlValue', 'divider', i, FormatNumber(divider,2));
	}
	//Podsumowanie
	var html = '<tr><td colspan="8" style="vertical-align: middle;border-top: 1px solid black">Suma:</td><td style="text-align: right;border-top: 1px solid black"><span  id="sumComponentsPriceTotal">0,00</span></td></tr>';
	$('#tableComponents').find('tfoot tr:last').after(html);
	updateSumComponents();
}
function updateSumComponents() {
	var data = $('#tableComponents').appendGrid('getAllValue');
	var sumPriceSingle = 0;
	var sumPriceTotal = 0;
	$.each(data, function (index, value) {
		
		sumPriceTotal += parseFloat(UnformatNumber(value.price.Summary,6));
	});
	$('#sumComponentsPriceTotal').html(FormatNumber(sumPriceTotal,6));
}