document.addEventListener("DOMContentLoaded", function() {
var progress_cat = 0;
var progress_file = 0;
var progress_button = 0;
var progress_upload = 0;
var progress_converting = 0;
var progress_handler = 0;
document.getElementById('upload').onclick = function() {
select = parseInt(document.getElementById('category_choose').value);
name_category = document.getElementById('category_choose').options[document.getElementById('category_choose').options.selectedIndex].text;
file_value = document.forms.upload_file.elements.file_excel.value;
if (progressRefresh() >= 60) {
UIkit.notification({
message: 'Процесс уже запущен!',
pos: 'bottom-right',
status: 'warning'
});
} else {
if (!select) {
UIkit.notification({
message: 'Укажите категорию!',
pos: 'bottom-right',
status: 'warning'
});
} else {
if (file_value) {
UIkit.notification({
message: 'Идет загрузка файла...',
pos: 'bottom-right',
status: 'primary'
});
progress_button = 20;
progressRefresh();
}
document.getElementById('spinner').classList.toggle('uk-hidden');
uploadFile();
}
}
return false;
}
document.getElementById('file_excel').onclick = function() {
select = parseInt(document.getElementById('category_choose').value);
if (!select) {
UIkit.notification({
message: 'Укажите категорию!',
pos: 'bottom-right',
status: 'warning'
});
return false;
}
}
function uploadFile() {
var form = document.forms.upload_file;
var formData = new FormData(form);
var req = new XMLHttpRequest();
req.open("POST", 'handler.php');
req.onreadystatechange = function() {
if (req.readyState == 4) {
request = JSON.parse(req.responseText);
if (request.result) {
UIkit.notification({
message: request.success,
pos: 'bottom-right',
status: 'success'
});
progress_converting = 10;
progressRefresh();
handlerParse(select, request.name);
} else {
UIkit.notification({
message: request.error,
pos: 'bottom-right',
status: 'danger'
});
progress_converting = 0;
progressRefresh();
}
}
}
req.upload.onprogress = function(event) {
progressSuccess = event.loaded * 100 / event.total;
progressBarUpload(progressSuccess);
}
req.send(formData);
}
document.getElementById('category_choose').onchange = function() {
if (this.value > 0) {
progress_cat = 20;
} else {
progress_cat = 0;
}
progressRefresh();
}
document.getElementById('file_excel').onchange = function() {
file = document.forms.upload_file.elements.file_excel.value;
if (file) {
progress_file = 20;
} else {
progress_file = 0;
}
progressRefresh();
}
function progressBarUpload(progress) {
UIkit.util.ready(function() {
var bar = document.getElementById('js-progressbar-uploadFile');
var divBar = document.getElementById('progress-upload');
if (!divBar.style.display) {
divBar.style.display = 'block';
}
bar.value = progress;
if (bar.value >= 100) {
divBar.style.display = "";
UIkit.notification({
message: 'Файл загружен на сервер...',
pos: 'bottom-right',
status: 'success'
});
progress_upload = 20;
progressRefresh();
}
});
}
function progressRefresh() {
progress = progress_cat +
progress_file +
progress_button +
progress_upload +
progress_converting +
progress_handler;
document.getElementById('js-progressbar').value = progress;
return progress;
}
function handlerParse(group, path) {
var req = new JsHttpRequest();
UIkit.notification({
message: 'Идет обработка файла...',
pos: 'bottom-right',
status: 'primary'
});
req.open(null, 'handler_update.php', true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
request = JSON.parse(req.responseText);
if (request.result) {
UIkit.notification({
message: request.success,
pos: 'bottom-right',
status: 'success'
});
progress_handler = 10;
progressRefresh();
setTimeout(success, 2000);
} else {
UIkit.notification({
message: "Произошла ошибка!",
pos: 'bottom-right',
status: 'danger'
});
progress_handler = 0;
progressRefresh();
}
}
}
req.send({
group: group,
path: path
});
}
function success() {
name_category = document.getElementById('category_choose').options[document.getElementById('category_choose').options.selectedIndex].text;
UIkit.modal.alert('Файл загружен и обработан: ' + name_category);
document.getElementById('category_choose').value = document.getElementById('category_choose').options[0].text;
document.getElementById('upload_file').reset();
progress_cat = 0;
progress_file = 0;
progress_button = 0;
progress_upload = 0;
progress_converting = 0;
progress_handler = 0;
document.getElementById('spinner').classList.toggle('uk-hidden');
progressRefresh();
}
});