const express = require('express');
const Database = require('better-sqlite3');
const auth = require('../middleware/authMiddleware');

const router = express.Router();
const db = new Database('plusfit.db');

/* Formular anzeigen */
router.get('/', auth, (req, res) => {
    const company = db.prepare(`
        SELECT * FROM company WHERE id = 1
    `).get();

    res.render('company', { company });
});

/* Speichern */
router.post('/', auth, (req, res) => {
    const c = req.body;

    db.prepare(`
        UPDATE company SET
            firmenname = ?,
            strasse = ?, hausnummer = ?, plz = ?, ort = ?, land = ?,
            telefon = ?, email = ?, web = ?,
            iban = ?, bic = ?, glaeubiger_id = ?
        WHERE id = 1
    `).run(
        c.firmenname,
        c.strasse, c.hausnummer, c.plz, c.ort, c.land,
        c.telefon, c.email, c.web,
        c.iban, c.bic, c.glaeubiger_id
    );

    res.redirect('/company');
});

module.exports = router;
