import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Item
{
    property alias companyConf: companyConf
    property alias miscConf: miscConf
    anchors.fill: parent
    TabBar
    {
        id: bar
        width: parent.width
        TabButton
        {
            text: qsTr("Benutzer")
        }
        TabButton
        {
            text: qsTr("Datenbank")
        }
        TabButton
        {
            text: qsTr("Das Unternehmen")
        }

        TabButton
        {
            text: qsTr("Sicherung")
        }

        TabButton
        {
            text: qsTr("Sonstiges")
        }
    }

    StackLayout
    {
        id: confContainer
        anchors.fill: parent
        currentIndex: bar.currentIndex
        Item
        {
            id: userTab
            UsersPage
            {
                id: usersPage
                anchors.fill: parent
            }
        }
        Item
        {
            id: dbTab
            DbConfiguration
            {
                id: dbConf
                anchors.fill: parent
            }
        }
        Item
        {
            id: companyTab
            CompanyConf
            {
                id: companyConf
                anchors.fill: parent
            }
        }

        Item
        {
            id: backup
            BackupSettings
            {
                id: backupSettings
                anchors.fill: parent
            }
        }

        Item
        {
            id: miscelanea
            MiscConf
            {
                id: miscConf
                anchors.fill: parent
            }
        }
    }

    RowLayout
    {
        width: parent.width
        anchors.bottom: parent.bottom
        Item
        {
            Layout.fillWidth: true
        }

        Button
        {
            text: qsTr("Ablehnen")
            onClicked: contentStack.pop()
        }

        Button
        {
            text: qsTr("Speichern")
            onClicked:
            {
                switch (confContainer.currentIndex)
                {
                    case 1:
                        updateDbConf()
                        break

                    case 2:
                        updateCompanyInfo()
                        break
                    case 4:
                        updateMiscConf()
                        break
                    default:
                        console.log("Need to handle users")
                }
            }
        }
    }

    function updateDbConf()
    {
        var db = {}
        db['database'] = {}
        db['database']['DB_HOST'] = dbConf.dbHost.text.trim()
        db['database']['DB_PORT'] = dbConf.dbPort.text.trim()
        db['database']['DB_NAME'] = dbConf.dbName.text.trim()
        db['database']['DB_USER'] = dbConf.dbUserName.text.trim()
        db['database']['DB_PASS'] = dbConf.dbPassword.text.trim()


        if (db['database']['DB_HOST'] === '' || db['database']['DB_PORT'] === '' ||
                db['database']['DB_NAME'] === '' || db['database']['DB_USER'] === '' ||
                db['database']['DB_PASS'] === '');
        else config.saveDbConf(db)
    }

    function updateCompanyInfo()
    {
        var company = {}
        company['company'] = {}
        company['company']['NAME'] = companyConf.name.text.trim()
        company['company']['STREET'] = companyConf.street.text.trim()
        company['company']['HOUSE'] = companyConf.house.text.trim()
        company['company']['ZIPCODE'] = companyConf.zipcode.editText? companyConf.zipcode.editText.trim(): companyConf.zipcode.currentText
        company['company']['CITY'] = companyConf.city.editText? companyConf.city.editText.trim(): companyConf.city.currentText

        if (company['company']['NAME'] === '' || company['company']['STREET'] === '' ||
                company['company']['HOUSE'] === '' || company['company']['ZIPCODE'] === '' ||
                company['company']['CITY'] === '');
        else config.saveCompanyInfo(company)
    }

    function updateMiscConf()
    {
        var misc = {}
        misc['misc'] = {}
        misc['misc']['SYSTRAY'] = miscConf.sysTray.checked
        config.saveMiscConf(misc)
    }
}
