pragma ComponentBehavior: Bound
import QtCore
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Page {
    id: page

    function saveSettings() {
        settings.connectOptions = connectOptions.text;
        settings.databaseName = databaseName.text;
        settings.hostName = hostName.text;
        settings.port = port.value;
        settings.usePort = usePort.checked;
    }

    title: qsTr("Login")

    ColumnLayout {
        anchors.centerIn: parent

        GridLayout {
            columns: 2

            MnemonicLabel {
                Layout.alignment: Qt.AlignRight
                buddy: user
                mnemonic: qsTr("&User:")
            }

            TextField {
                id: user

                Layout.fillWidth: true
                focus: true

                onAccepted: loginAction.trigger()
            }

            MnemonicLabel {
                Layout.alignment: Qt.AlignRight
                buddy: password
                mnemonic: qsTr("&Password:")
            }

            TextField {
                id: password

                Layout.fillWidth: true
                echoMode: TextInput.Password

                onAccepted: loginAction.trigger()
            }
        }

        Expander {
            Layout.fillWidth: true
            Layout.topMargin: 8
            title: qsTr("&Advanced options")

            GridLayout {
                columns: 2

                MnemonicLabel {
                    Layout.alignment: Qt.AlignRight
                    buddy: hostName
                    mnemonic: qsTr("&Host name:")
                }

                TextField {
                    id: hostName

                    Layout.fillWidth: true
                    text: settings.hostName

                    onAccepted: loginAction.trigger()
                }

                CheckBox {
                    id: usePort

                    Layout.alignment: Qt.AlignRight
                    Mnemonic.label: qsTr("Por&t:")
                    checked: settings.usePort
                    text: Mnemonic.richTextLabel

                    Shortcut {
                        sequence: usePort.Mnemonic.sequence

                        onActivated: usePort.click()
                    }
                }

                SpinBox {
                    id: port

                    Accessible.name: qsTr("Port number")
                    Layout.fillWidth: true
                    editable: true
                    enabled: usePort.checked
                    from: 1
                    to: 65535
                    value: settings.port
                }

                MnemonicLabel {
                    Layout.alignment: Qt.AlignRight
                    buddy: databaseName
                    mnemonic: qsTr("&Database name:")
                }

                TextField {
                    id: databaseName

                    Layout.fillWidth: true
                    text: settings.databaseName

                    onAccepted: loginAction.trigger()
                }

                MnemonicLabel {
                    Layout.alignment: Qt.AlignRight
                    buddy: connectOptions
                    mnemonic: qsTr("&Options:")
                }

                TextField {
                    id: connectOptions

                    Layout.fillWidth: true
                    text: settings.connectOptions

                    onAccepted: loginAction.trigger()
                }
            }
        }

        Button {
            Layout.alignment: Qt.AlignRight
            action: loginAction
        }
    }

    Settings {
        id: settings

        property string connectOptions: ""
        property string databaseName: ""
        property string hostName: ""
        property int port: 5432
        property bool usePort: false

        category: "database"
    }

    MnemonicAction {
        id: loginAction

        mnemonic: qsTr("Log &in")

        onTriggered: function () {
            Database.open(user.text.trim(), password.text.trim(), hostName.text.trim(), usePort.checked, port.value, databaseName.text.trim(), connectOptions.text.trim());
        }
    }
}
