pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Controls
import Camper

ApplicationWindow {
    height: 480
    title: pageStack.title ? qsTr("%1 — Camper").arg(pageStack.title) : qsTr("Camper")
    visible: true
    width: 640

    StackView {
        id: pageStack

        property string title: (currentItem as Page)?.title ?? ""

        anchors.fill: parent
        focus: true
        initialItem: loginPage
    }

    Component {
        id: loginPage

        LoginPage {
        }
    }

    Component {
        id: reservationsPage

        ReservationsPage {
        }
    }

    ErrorNotification {
        id: errorNotification

        anchors {
            bottom: parent.bottom
            bottomMargin: 8
            left: parent.left
            margins: 18 * 4
            right: parent.right
        }
    }

    Connections {
        function onClosed() {
            pageStack.replace(null, loginPage);
        }

        function onErrorOcurred(errorMessage) {
            errorNotification.show(errorMessage);
        }

        function onOpened() {
            (pageStack.currentItem as LoginPage)?.saveSettings();
            pageStack.replace(reservationsPage);
        }

        target: Database
    }
}
