const newJobModal = document.getElementById('new-job-modal');
const updateJobModal = document.getElementById('update-job-modal');
const jobHistoryModal = document.getElementById('job-history-modal');
const updateJobForm = document.getElementById('update-job-form');
var minutesChoices = new Choices(document.getElementById('minutes'), {removeItems: true, removeItemButton: true, allowHTML: false});
var hoursChoices = new Choices(document.getElementById('hours'), {removeItems: true, removeItemButton: true, allowHTML: false});
var mdaysChoices = new Choices(document.getElementById('mdays'), {removeItems: true, removeItemButton: true, allowHTML: false});
var monthsChoices = new Choices(document.getElementById('months'), {removeItems: true, removeItemButton: true, allowHTML: false});
var wdaysChoices = new Choices(document.getElementById('wdays'), {removeItems: true, removeItemButton: true, allowHTML: false});

var upMinutesChoices = new Choices(document.getElementById('up-minutes'), {removeItems: true, removeItemButton: true, allowHTML: false});
var upHoursChoices = new Choices(document.getElementById('up-hours'), {removeItems: true, removeItemButton: true, allowHTML: false});
var upMdaysChoices = new Choices(document.getElementById('up-mdays'), {removeItems: true, removeItemButton: true, allowHTML: false});
var upMonthsChoices = new Choices(document.getElementById('up-months'), {removeItems: true, removeItemButton: true, allowHTML: false});
var upWdaysChoices = new Choices(document.getElementById('up-wdays'), {removeItems: true, removeItemButton: true, allowHTML: false});

updateJobModal.addEventListener('show.bs.modal', function (event) {
    var button = event.relatedTarget;
    var id = button.getAttribute('data-id');
    upMinutesChoices.destroy();
    upHoursChoices.destroy();
    upMdaysChoices.destroy();
    upMonthsChoices.destroy();
    upWdaysChoices.destroy();

    var formData = new FormData();
    formData.append("id", id);

    request('/admin/common/api/cron/job-details', formData, function (data) {
        try {
            data = JSON.parse(data);
            if (!(data instanceof Object)) {
                new Toast({
                    type: "danger",
                    title: 'Hata!',
                    message: 'Bir hata oluştu. Lütfen tüm alanları kontrol ediniz',
                    duration: 10000,
                });
            } else {
                if (data['status']) {
                    document.getElementById('job_id').value = id;
                    document.getElementById('up-url').value = data['job']['jobDetails']['url'];
                    document.getElementById('up-title').value = data['job']['jobDetails']['title'];

                    data['job']['jobDetails']['schedule']['minutes'].forEach(function(e) {
                        document.querySelector("#up-minutes option[value='"+e+"']").selected = true;
                    });
                    data['job']['jobDetails']['schedule']['hours'].forEach(function(e) {
                        document.querySelector("#up-hours option[value='"+e+"']").selected = true;
                    });
                    data['job']['jobDetails']['schedule']['mdays'].forEach(function(e) {
                        document.querySelector("#up-mdays option[value='"+e+"']").selected = true;
                    });
                    data['job']['jobDetails']['schedule']['months'].forEach(function(e) {
                        document.querySelector("#up-months option[value='"+e+"']").selected = true;
                    });
                    data['job']['jobDetails']['schedule']['wdays'].forEach(function(e) {
                        document.querySelector("#up-wdays option[value='"+e+"']").selected = true;
                    });
                    if(data['job']['jobDetails']['enabled']) {
                        document.getElementById('up-status').value = 1;
                    }else{
                        document.getElementById('up-status').value = 0;
                    }

                    upMinutesChoices = new Choices(document.getElementById('up-minutes'), {removeItems: true, removeItemButton: true, allowHTML: false});
                    upHoursChoices = new Choices(document.getElementById('up-hours'), {removeItems: true, removeItemButton: true, allowHTML: false});
                    upMdaysChoices = new Choices(document.getElementById('up-mdays'), {removeItems: true, removeItemButton: true, allowHTML: false});
                    upMonthsChoices = new Choices(document.getElementById('up-months'), {removeItems: true, removeItemButton: true, allowHTML: false});
                    upWdaysChoices = new Choices(document.getElementById('up-wdays'), {removeItems: true, removeItemButton: true, allowHTML: false});
                }else{
                    new Toast({
                        type: "danger",
                        title: 'Hata!',
                        message: data['message'],
                        duration: 10000,
                    });
                }
            }
        } catch (e) {
            new Toast({
                type: "danger",
                title: 'Hata!',
                message: 'Bir hata oluştu. Lütfen daha sonra tekrar deneyiniz',
                duration: 10000,
            });
        }
    });
});

newJobModal.addEventListener('show.bs.modal', function (event) {
    var button = event.relatedTarget;
    if(button.hasAttribute('data-id')) {
        var id = button.getAttribute('data-id');
        minutesChoices.destroy();
        hoursChoices.destroy();
        mdaysChoices.destroy();
        monthsChoices.destroy();
        wdaysChoices.destroy();
    
        var formData = new FormData();
        formData.append("id", id);
    
        request('/admin/common/api/cron/job-details', formData, function (data) {
            try {
                data = JSON.parse(data);
                if (!(data instanceof Object)) {
                    new Toast({
                        type: "danger",
                        title: 'Hata!',
                        message: 'Bir hata oluştu. Lütfen tüm alanları kontrol ediniz',
                        duration: 10000,
                    });
                } else {
                    if (data['status']) {
                        document.getElementById('url').value = data['job']['jobDetails']['url'];
                        document.getElementById('title').value = data['job']['jobDetails']['title'];
    
                        data['job']['jobDetails']['schedule']['minutes'].forEach(function(e) {
                            document.querySelector("#minutes option[value='"+e+"']").selected = true;
                        });
                        data['job']['jobDetails']['schedule']['hours'].forEach(function(e) {
                            document.querySelector("#hours option[value='"+e+"']").selected = true;
                        });
                        data['job']['jobDetails']['schedule']['mdays'].forEach(function(e) {
                            document.querySelector("#mdays option[value='"+e+"']").selected = true;
                        });
                        data['job']['jobDetails']['schedule']['months'].forEach(function(e) {
                            document.querySelector("#months option[value='"+e+"']").selected = true;
                        });
                        data['job']['jobDetails']['schedule']['wdays'].forEach(function(e) {
                            document.querySelector("#wdays option[value='"+e+"']").selected = true;
                        });
    
                        minutesChoices = new Choices(document.getElementById('minutes'), {removeItems: true, removeItemButton: true, allowHTML: false});
                        hoursChoices = new Choices(document.getElementById('hours'), {removeItems: true, removeItemButton: true, allowHTML: false});
                        mdaysChoices = new Choices(document.getElementById('mdays'), {removeItems: true, removeItemButton: true, allowHTML: false});
                        monthsChoices = new Choices(document.getElementById('months'), {removeItems: true, removeItemButton: true, allowHTML: false});
                        wdaysChoices = new Choices(document.getElementById('wdays'), {removeItems: true, removeItemButton: true, allowHTML: false});
                    }else{
                        new Toast({
                            type: "danger",
                            title: 'Hata!',
                            message: data['message'],
                            duration: 10000,
                        });
                    }
                }
            } catch (e) {
                new Toast({
                    type: "danger",
                    title: 'Hata!',
                    message: 'Bir hata oluştu. Lütfen daha sonra tekrar deneyiniz',
                    duration: 10000,
                });
            }
        });
    }
});

jobHistoryModal.addEventListener('show.bs.modal', function (event) {
    document.querySelector('#job-history-modal tbody').innerHTML = "";
    
    var button = event.relatedTarget;
    var id = button.getAttribute('data-id');

    var formData = new FormData();
    formData.append("id", id);

    request('/admin/common/api/cron/job-history', formData, function (data) {
        try {
            data = JSON.parse(data);
            if (!(data instanceof Object)) {
                new Toast({
                    type: "danger",
                    title: 'Hata!',
                    message: 'Bir hata oluştu. Lütfen tüm alanları kontrol ediniz',
                    duration: 10000,
                });
            } else {
                if (data['status']) {
                    var row = "";
                    data['history']['history'].forEach(function(e) {
                        var date = new Date(e.date*1000);
                        var datePlanned = new Date(e.datePlanned*1000);
                        row += "<tr><td>"+e.statusText+"</td><td>"+date.toLocaleString("tr-TR")+"</td><td>"+datePlanned.toLocaleString("tr-TR")+"</td><td>"+e.duration+" ms</td></tr>";
                    });
                    document.querySelector('#job-history-modal tbody').innerHTML = row;
                }else{
                    new Toast({
                        type: "danger",
                        title: 'Hata!',
                        message: data['message'],
                        duration: 10000,
                    });
                }
            }
        } catch (e) {
            new Toast({
                type: "danger",
                title: 'Hata!',
                message: 'Bir hata oluştu. Lütfen daha sonra tekrar deneyiniz'+e,
                duration: 10000,
            });
        }
    });
});
