document.addEventListener('DOMContentLoaded', async function () {
const DateTime = luxon.DateTime
let curEinheit = sysParams.einheit
if (sysParams.doinit) {
await initSchema('2023-05-01')
}
let ret = await getData()
let schema = ret.data
if(curEinheit === 0) {
curEinheit = schema.einheit
}
fillSchema(schema)
document.querySelector('#sptab').addEventListener('click', markField);
document.querySelector('#einheiten').addEventListener('change', enterEinheit);
async function markField (e) {
let field = e.target;
let d = schema.data[34].day
field.setAttribute('aria-label','x');
field.setAttribute('disabled','disabled');
schema.data[parseInt(field.id.slice(2))-1].status = true
schema.data[parseInt(field.id.slice(2))-1].einheit = curEinheit
await storeData(schema)
fillSchema(schema)
if (e.srcElement.id === 'bt35') {
let ldt = DateTime.fromISO(d)
ldt = ldt.plus({day: 1})
await initSchema(ldt.toFormat('y-LL-dd'))
}
}
async function enterEinheit (e) {
let field = e.target;
curEinheit = parseInt(field.value)
schema.einheit = curEinheit
}
function buildCellHtml(day, einheit) {
let x = `
${day.toFormat('d')}
${day.setLocale('de').toFormat('ccc')}
${einheit !== 0 ? einheit : ''}
`
return x
}
function fillSchema(schema) {
const setArray = schema.data
let months = ''
for (let x of schema.months) {
months += x + ' - '
}
months = months.slice(0, -3)
months += ' '
for (let x of schema.years) {
months += x + '/'
}
months = months.slice(0, -1)
document.querySelector('#curmon').innerHTML = months
for(let i = 0; i < 35; i++) {
let sel = '#bt'+(i+1)
let day = DateTime.fromISO(setArray[i].day)
if (setArray[i].day !== '') {
document.querySelector(sel).innerHTML = buildCellHtml(day, setArray[i].einheit)
if (setArray[i].status) {
document.querySelector(sel).setAttribute('aria-label', 'x')
document.querySelector(sel).setAttribute('disabled', 'disabled')
} else {
document.querySelector(sel).setAttribute('aria-label', '')
document.querySelector(sel).removeAttribute('disabled')
}
} else {
document.querySelector(sel).setAttribute('disabled', 'disabled')
}
if(setArray[i].einheit !== 0) {
document.getElementById("einheiten").value = setArray[i].einheit
}
}
}
async function getData(testing) {
let erg = await fetch('data')
return erg.json()
}
async function storeData(data, testing) {
const response = await fetch('/data', {
method: "POST",
body: JSON.stringify(data),
headers: {
'Content-Type':"application/json"
}
})
return response.json()
}
async function initSchema(startdate) {
let setArray = []
let monthArray = []
let yearsArray = []
let ld0 = DateTime.fromISO(startdate)
let k = 0
for(let i = 0; i < 35; i++) {
let elem = {status: false}
elem.einheit = 0
if (i === 17) {
elem.day = ''
} else {
let ld = ld0.plus({day: k})
elem.day = ld.toFormat('y-LL-dd')
let month = ld.setLocale('de').toFormat('LLLL')
let year = ld.toFormat('y')
if (monthArray.indexOf(month) === -1) {
monthArray.push(month)
}
if (yearsArray.indexOf(year) === -1) {
yearsArray.push(year)
}
k++
}
setArray.push(elem)
}
let schema = {curdate: startdate, months: monthArray, years: yearsArray, data: setArray, einheit: curEinheit}
await storeData(schema)
}
});