class ImportController {

	import() {
		let params = {csrf: document.querySelector('[name=csrf]')?.value};
		let body = new URLSearchParams(params);
		console.log(newsletter);
		for (const key in newsletter) {
			body.append('newsletter[' + key + ']', newsletter[key]);
		}

		
		let request = function () {
			return fetch(nextUrl, {method: "POST",   headers: {
				"X-Requested-With": "XMLHttpRequest",
				"Content-Type": "application/x-www-form-urlencoded",
			}, body})
			.then((response) => {
				nextUrl = false;
				if (!response.ok) {
					return Promise.resolve(response.text()).then((responseInText) => {
						return Promise.reject([response, responseInText]);
					});
				}										
				if (!response.ok) { 
					let message = response.statusText + " " + response.body();
					throw new Error(message); 
				}
				return response.json()
			})
			
			.then((json) => {
					document.getElementById('progress-status').innerHTML = '';
					
					if (json['error']) {
						document.getElementById('progress-bar').classList.add('bg-danger');
						document.getElementById('progress-status').innerHTML = '<div class="text-danger">' + json['error'] + '</div>';

						//enableBtn();
					}

					let percent = (json['position']) * 100 / json['count'];				
					
					if (json['success']) {
						document.getElementById('progress-bar').style.width = "100%";//.classList.add('bg-success');
						document.getElementById('progress-status').innerHTML = '<div class="text-success">' + json['success'] + '</div>';

						//enableBtn();
					}

					if (json['table']) {
						document.getElementById('progress-status').innerHTML = json['table'];
					}					
					
					if (json['file']) {
						document.getElementById('progress-file').innerHTML = json['file'];
					}					
					
					if (json['position']) {
						document.getElementById('progress-position').innerHTML = json['position'];
					}			
					
					if (json['count']) {
						document.getElementById('progress-count').innerHTML = json['count'];
					}			
					
					if (json['page']) {
						document.getElementById('progress-page').innerHTML = json['page'];
					}

					console.log(json['url'] && (!json['count'] || !json['last'] ));
					if (json['url'] && (!json['count'] || !json['last'] )) {
						nextUrl = json['url'];

						window.ajaxStack.add(request);
					} else {
						percent = 100;
						document.getElementById('progress-bar').classList.add('bg-success');
						//enableBtn();
						document.getElementById('progress-bar').style.width = '100%';

						if (json['success']) {
							//window.location.href += '&success=' + json['success'];  
						} else {
							//window.navigation.reload();
						}
					}

					document.getElementById('progress-bar').style.width = percent + '%';					
				})/*.then((json) => {
					if (nextUrl) {
						ajaxStack.add(request);
					}
				})*/
				.catch(error => {
					console.log(error);
						let [response, responseInText] = error;
						let message = response.statusText ?? "Error!";
						let progessSuccess, progressStatus;

						if (typeof error.json === "function") {
							error.json().then(jsonError => {
								message = jsonError;
							}).catch(genericError => {
								message = error.statusText + " " + message;
							});
						}				
					
						displayToast("danger", "Error", message);
						if (progessSuccess = document.getElementById('progress-success')) progessSuccess.innerHTML = '';
						if (progressStatus = document.getElementById('progress-status')) progressStatus.innerHTML = '<div class="text-danger">' + message + '</div>';
						//console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
						document.getElementById('progress-bar').classList.add('bg-danger');
						//enableBtn();
				});
		};

		window.ajaxStack.add(request);		
	}	
}
