
function load_groups() {
	var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_GROUP_STATUS', 2);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'ACC_GROUP_DESCRIPTION');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	JSCUS_SQL._ACCOUNT._GROUP.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
		if(IS_SUCCESS === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}

		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
			return;
		}

		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
			return; 
		}

		$('select[name="select-product-branch"]').empty();
		$('select[name="select-product-branch"]').append($('<option>', { 
		        value: 0,
		        text : 'Select Branch'
		    }));

		$.each(JSON_OBJECT.ACC_GROUP_ID, function(i, item) {
			$('select[name="select-product-branch"]').append($('<option>', { 
		        value: item,
		        text : JSON_OBJECT.ACC_GROUP_DESCRIPTION[i]
		    }));
		});

		$('select[name="select-product-branch"]').val(0).trigger('change');

		JSOC_DIALOG.VAR_LOADING_SPINNER(false);
	}); 
}

function load_branch_products(branch_id) {
	branch_id = parseInt(branch_id);

	if(branch_id > 0) {
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);

		var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_ACC_GROUP_ID', parseInt(branch_id));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_STATUS', 2);

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRODUCT_DESCRIPTION');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

		JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
			if(IS_SUCCESS === false) {
				JSOC_DIALOG.VAR_LOADING_SPINNER(false);
				alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
				return;
			}

			var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
				JSOC_DIALOG.VAR_LOADING_SPINNER(false);
				alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
				return;
			}

			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
				html = '<tr><td colspan="5" class="text-center">Add products to this branch</td></tr>';
				$('#tbl-product-branch tbody').empty().html(html);
				$('#tbl-product-branch tfoot').addClass('d-none');
				JSOC_DIALOG.VAR_LOADING_SPINNER(false);
				JSOC_DIALOG.VAR_MESSAGE_BOX('No products found.', 'Information', 'OK', 'ERROR', null);
				return; 
			}
			else {
				var html = '';

				$('#tbl-product-branch tbody').empty();

				$.each(JSON_OBJECT.CAT_PRODBRANCH_ID, function(i, item) {
					html += '<tr>';
						html += '<th id="PRODUCT_BRANCH_ID_'+item+'"><input type="hidden" name="PRODUCT_BRANCH_ID_'+item+'_PRODUCT_ID" id="PRODUCT_BRANCH_ID_'+item+'_PRODUCT_ID" value="'+JSON_OBJECT.CAT_PRODUCT_ID[i]+'">'+JSON_OBJECT.CAT_PRODUCT_DESCRIPTION[i]+'</th>';
						html += '<td class="text-right"><input type="number" class="form-control text-right" name="PRODUCT_BRANCH_ID_'+item+'_PRICING" id="PRODUCT_BRANCH_ID_'+item+'_PRICING" value="'+JSON_OBJECT.CAT_PRODUCT_AMOUNT_PRICE[i]+'"></td>';
						html += '<td class="text-right"><input type="number" class="form-control text-right" name="PRODUCT_BRANCH_ID_'+item+'_REORDER_POINT" id="PRODUCT_BRANCH_ID_'+item+'_REORDER_POINT" value="'+JSON_OBJECT.CAT_PRODUCT_REORDER_POINT[i]+'"></td>';
						html += '<td class="text-right"><input type="number" class="form-control text-right" name="PRODUCT_BRANCH_ID_'+item+'_REORDER_QTY" id="PRODUCT_BRANCH_ID_'+item+'_REORDER_QTY" value="'+JSON_OBJECT.CAT_PRODUCT_REORDER_QUANTITY[i]+'"></td>';
						html += '<td class="text-center"><button type="button" class="btn btn-danger" title="Delete Item" onclick="remove_product_branch('+item+')"><i class="fas fa-trash-alt"></i></button></td>'
					html += '</tr>';
				});

				$('#tbl-product-branch tbody').html(html);
				$('#tbl-product-branch tfoot').removeClass('d-none');

				JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			}
		});
	}
	else
		$('#tbl-product-branch tfoot').addClass('d-none');
}

function load_product_types() {
	JSOC_DIALOG.VAR_LOADING_SPINNER(true);

	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODTYPE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODTYPE_STATUS', 2);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRODTYPE_DESCRIPTION');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	JSCUS_SQL._CATEGORY._PRODTYPE.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
		if(IS_SUCCESS === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}

		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
			return;
		}

		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			return; 
		}

		$('#select-product-types').empty();
		$('#select-product-types').append($('<option>', { 
		    value: 0,
		    text : 'Any Category'
		}));

		$.each(JSON_OBJECT.CAT_PRODTYPE_ID, function(i, item) {
			$('#select-product-types').append($('<option>', { 
		        value: item,
		        text : JSON_OBJECT.CAT_PRODTYPE_DESCRIPTION[i]
		    }));
		});

		$('#select-product-types').val(0).trigger('change');

		JSOC_DIALOG.VAR_LOADING_SPINNER(false);
	}); 
}

function load_products() {
	JSOC_DIALOG.VAR_LOADING_SPINNER(true);

	var branch_id = parseInt($('#select-product-branch').val());
	var category_id = parseInt($('#select-product-types').val());

	var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODUCT_CAT_PRODTYPE_ID_LV1', category_id);
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODUCT_STATUS', 2);

	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRODUCT_DESCRIPTION');
	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

	JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET(GET_MAP_ARRAY, function(IS_SUCCESS, RESPONSE_STRING) {
		if(IS_SUCCESS === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}

		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);

		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
			return;
		}
		
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			return; 
		}
		else {
			$('#select-products').empty().append($('<option>', { 
			    value: 0,
				text : 'Select Product'
			}));

			$.each(JSON_OBJECT.CAT_PRODUCT_ID, function(i, item) {
				if($('#tbl-product-branch tr:contains("'+JSON_OBJECT.CAT_PRODUCT_DESCRIPTION[i]+'")').length <= 0) {
					$('#select-products').append($('<option>', { 
					    value: item,
					    text : JSON_OBJECT.CAT_PRODUCT_DESCRIPTION[i]
					}));
				}
			});

			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
		}
	});
}

function add_product() {
	var product = parseInt($('#select-products').val());
	var html = '';

	$('#select-products option:selected').each(function(i, item) {
		html += '<tr id="product_'+item.value+'"><td><button class="btn btn-danger" type="button" title="Delete" onclick="remove_product(\'product_'+product+'\')"><i class="fas fa-times"></i></button></td><td>'+$(this).text()+'</td></tr>';
	});

	

	if($('#product-list #default-row').length == 1) 
		$('#product-list tbody').empty().append(html);
	else
		$('#product-list tbody').append(html);
}

function remove_product(product) {
	$('#'+product).remove();

	if($('#product-list tbody > tr').length <= 0)
		$('#product-list tbody').empty().append('<tr id="default-row"><td colspan="2" class="text-center">No products selected</td></tr>');
}

function save_products() {
	var SAVE_MAP_ARRAY = [];

	var product = 0;
	var branch = parseInt($('#select-product-branch').val());

	$('#product-list tbody > tr[id^="product_"]').each(function() {
		product = parseInt($(this).attr('id').replace('product_', ''));

		SAVE_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_ACC_GROUP_ID", branch);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_CAT_PRODUCT_ID", product);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_STATUS", 2);

		JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_SAVE(SAVE_MAP_ARRAY, function(IS_SUCCESS, HTTP_RESPONSE) {
			if(IS_SUCCESS === false) {
				console.error(HTTP_RESPONSE);
				return;
			}
			else if(JSRC_JSON.REF_IS_JSTRING(HTTP_RESPONSE) == false) {
				console.error(HTTP_RESPONSE);
				return;			
			}
			else {
				var JSON_OBJECT = JSRC_JSON.REF_CREATE(HTTP_RESPONSE);
				console.log(JSON_OBJECT);
			}
		});

		$('#product-list tbody').empty().append('<tr><td colspan="2" class="text-center">No products selected</td></tr>');
		load_product_types();
		$('#NEW_PRODUCT_BRANCH_MODAL').modal('hide');
		load_branch_products(branch);
	});
}

function remove_product_branch(product_branch_id) {
	var branch_id = parseInt($('#select-product-branch').val());

	var SAVE_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_ID", product_branch_id);
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_STATUS", 1); //deactivate product

	JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_SAVE(SAVE_MAP_ARRAY, function(IS_SUCCESS, HTTP_RESPONSE) {
		load_branch_products(branch_id);
	});
}

function save_branch_products() {
	JSOC_DIALOG.VAR_LOADING_SPINNER(true);

	var rows = $('#tbl-product-branch tbody > tr');

	var product_branch_id = 0;
	var product_pricing = 0;
	var product_reorder_point = 0;
	var product_reorder_qty = 0;
	
	var BUNDLE_MAP_ARRAY = [];
	var SAVE_MAP_ARRAY = [];

	$.each(rows, function(i, item) {
	    product_branch_id = parseInt($(this).find('th:first').attr('id').replace('PRODUCT_BRANCH_ID_',''));
	    product_pricing = parseFloat($('#PRODUCT_BRANCH_ID_'+product_branch_id+'_PRICING').val());
	    product_reorder_point = parseFloat($('#PRODUCT_BRANCH_ID_'+product_branch_id+'_REORDER_POINT').val());
	    product_reorder_qty = parseFloat($('#PRODUCT_BRANCH_ID_'+product_branch_id+'_REORDER_QTY').val());

	    SAVE_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_ID", product_branch_id);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_AMOUNT_PRICE", product_pricing);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_REORDER_POINT", product_reorder_point);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_REORDER_QUANTITY", product_reorder_qty);
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_CAT_PRODBRANCH_STATUS", 2); //activate product

		BUNDLE_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE(BUNDLE_MAP_ARRAY, SAVE_MAP_ARRAY);	
	});

	JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_SAVE(BUNDLE_MAP_ARRAY, function(IS_SUCCESS, HTTP_RESPONSE) {

		if(IS_SUCCESS === false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			console.error(HTTP_RESPONSE);
			return;
		}
		else if(JSRC_JSON.REF_IS_JSTRING(HTTP_RESPONSE) == false) {
			JSOC_DIALOG.VAR_LOADING_SPINNER(false);
			console.error(HTTP_RESPONSE);
			return;			
		}
		else {
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(HTTP_RESPONSE);
			console.log(JSON_OBJECT);

			JSOC_DIALOG.VAR_MESSAGE_BOX('Transaction succesful!', 'Information', 'OK', 'SUCCESS', null);
		}

		$('#NEW_PRODUCT_BRANCH_MODAL').modal('hide');
		JSOC_DIALOG.VAR_LOADING_SPINNER(false);
	});
}


function show_add_modal() {
	if(parseInt($('#select-product-branch').val()) == 0) 
		JSOC_DIALOG.VAR_MESSAGE_BOX('Select branch first', 'Information', 'OK', 'ERROR', null);
	else {
		load_product_types();
		$('#NEW_PRODUCT_BRANCH_MODAL').modal('show');
	}
}


$(document).ready(function() {
	JSOC_DIALOG.VAR_LOADING_SPINNER(true);
	load_groups();
});