document.getElementById('loginForm').onsubmit = function(event) {
    event.preventDefault();
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;
    if(username === 'admin' && password === 'admin') {
        showDashboard();
    } else {
        document.getElementById('errorMessage').innerText = 'Nesprávné jméno nebo heslo!';
    }
};
function showDashboard() {
    document.getElementById('loginPage').classList.add('hidden');
    document.getElementById('dashboardPage').classList.remove('hidden');
    renderCharts();
}
function logout() {
    document.getElementById('loginPage').classList.remove('hidden');
    document.getElementById('dashboardPage').classList.add('hidden');
}
function renderCharts() {
    var solarCtx = document.getElementById('solarPowerChart').getContext('2d');
    var solarPowerChart = new Chart(solarCtx, {
        type: 'line',
        data: {
            labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'],
            datasets: [{
                label: 'Výkon fotovoltaické elektrárny',
                data: [120, 190, 300, 250, 200, 300, 250],
                borderColor: 'rgb(255, 159, 64)',
                tension: 0.1
            }]
        }
    });
    var boilerCtx = document.getElementById('boilerPowerChart').getContext('2d');
    var boilerPowerChart = new Chart(boilerCtx, {
        type: 'line',
        data: {
            labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'],
            datasets: [{
                label: 'Výkon boileru',
                data: [100, 180, 290, 230, 210, 290, 240],
                borderColor: 'rgb(54, 162, 235)',
                tension: 0.1
            }]
        }
    });
}
var timeSlider = document.getElementById('timeSlider');
noUiSlider.create(timeSlider, {
    start: [8, 18],
    connect: true,
    range: {
        'min': 0,
        'max': 24
    },
    step: 1,
    tooltips: [true, true],
    format: {
      to: function(value) {
        return Math.round(value) + ":00";
      },
      from: function(value) {
        return Number(value.replace(':00', ''));
      }
    }
});
timeSlider.noUiSlider.on('update', function (values, handle) {
    console.log('Noční proud je od ' + values[0] + ' do ' + values[1]);
});
var ctx = document.getElementById('batteryCapacityGauge').getContext('2d');
var batteryCapacityGauge = new Chart(ctx, {
    type: 'doughnut',
    data: {
        datasets: [{
            data: [42, 58],
            backgroundColor: [
                'rgb(255, 205, 86)', 
                'rgb(201, 203, 207)' 
            ],
            borderColor: [
                'rgb(255, 205, 86)',
                'rgb(201, 203, 207)'
            ],
            borderWidth: 1,
            circumference: 180,
            rotation: 270
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        cutoutPercentage: 80, 
        legend: {
            display: false
        },
        tooltips: {
            enabled: false
        },
        animation: {
            animateRotate: true,
            animateScale: false,
            onComplete: function () {
            var width = ctx.canvas.clientWidth;
            var height = ctx.canvas.clientHeight;
            var fontSize = (height / 80).toFixed(2);
            ctx.font = fontSize + "Roboto";
            ctx.textBaseline = "middle";
            ctx.fillStyle = "rgb(11, 43, 129)";
            var text = batteryCapacityGauge.data.datasets[0].data[0] + "%";
            var textX = Math.round((width - ctx.measureText(text).width) / 2);
            var textY = height / 1.3;
            ctx.fillText(text, textX, textY);
        }
    }
}
});