import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Dialogs
import QtCore

ApplicationWindow
{
    id: appWindow
    width: Screen.width * .6
    height: Screen.height * .75
    visible: true
    title: "PYQCRM"
    property string confile: ""

    TopBar
    {

        id:topBar
        anchors
        {
            rightMargin: 9
            leftMargin: 9
        }

        visible: bad_config? false: true
    }

    Item
    {
        id: mainView
    }

    Loader
    {
        id: appLoader
        anchors
        {
            left: parent.left
            right: parent.right
            top: topBar.bottom
            bottom: parent.bottom
            topMargin: 0
            rightMargin: 9
            leftMargin: 9
        }

        property alias window: appWindow
    }

    Component.onCompleted:
    {
        if(bad_config)
        {
            importDialog.open()
        }
        else appLoader.source= "LoginScreen.qml"
    }

    Dialog
    {
        id: importDialog
        modal: true
        anchors.centerIn: parent
        standardButtons: Dialog.Yes | Dialog.No
        onAccepted: settingsFiledialog.open()
        onRejected: appLoader.source= "firststart.qml"
        title: qsTr("Einstellungen importieren")
    }

    FileDialog
    {
        id: settingsFiledialog
        title: qsTr("PYQCRM Einstellungen")
        currentFolder: StandardPaths.standardLocations(StandardPaths.DocumentsLocation)[0]
        modality: "ApplicationModal"
        nameFilters: [qsTr("PYQCRM Einstellungen (*.pyqcrm)")]
        onAccepted:
        {
            exportFilePassword.open()
            confile = selectedFile
        }
    }

    Dialog
    {
        id: exportFilePassword
        modal: true
        title: qsTr("PYQCRM Einstellungen")
        anchors.centerIn: parent
        standardButtons: Dialog.Ok | Dialog.Cancel
        onAccepted: config.importConfig(confile, exportPasswordInput.text)
        ColumnLayout
        {
            RowLayout
            {
                Label
                {
                    text: qsTr("Passwort eingeben:")
                }

                TextField
                {
                    id: exportPasswordInput
                    echoMode: TextInput.Password
                    implicitWidth: 300
                }
            }
        }
    }
}
