import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import SddmComponents 2.0

Item {
    id: root
    width: 1920
    height: 1080

    property string backgroundPath: config.String("Theme", "Background", "")
    property string logoPath: config.String("Theme", "Logo", "")

    Image {
        id: bg
        anchors.fill: parent
        source: root.backgroundPath
        fillMode: Image.PreserveAspectCrop
        smooth: true
        cache: true
    }

    Rectangle {
        anchors.fill: parent
        color: "#050a14"
        opacity: 0.55
    }

    ColumnLayout {
        id: panel
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        spacing: 18
        width: Math.min(parent.width * 0.42, 560)

        Item {
            Layout.alignment: Qt.AlignHCenter
            Layout.preferredHeight: 180
            Layout.preferredWidth: 180

            Image {
                anchors.fill: parent
                source: root.logoPath
                fillMode: Image.PreserveAspectFit
                smooth: true
                cache: true
            }
        }

        Label {
            Layout.alignment: Qt.AlignHCenter
            text: "SAIKYO OS"
            font.pixelSize: 38
            font.weight: Font.DemiBold
            color: "#e8f1ff"
        }

        ComboBox {
            id: userBox
            Layout.fillWidth: true
            model: userModel
            textRole: "name"
            currentIndex: userModel.lastIndex
            focus: true
            background: Rectangle {
                radius: 10
                color: "#0b1d44"
                border.color: "#153a85"
                border.width: 1
            }
            contentItem: Text {
                leftPadding: 14
                rightPadding: 14
                text: userBox.displayText
                color: "#e8f1ff"
                verticalAlignment: Text.AlignVCenter
                elide: Text.ElideRight
            }
        }

        TextField {
            id: password
            Layout.fillWidth: true
            echoMode: TextInput.Password
            placeholderText: "Пароль"
            inputMethodHints: Qt.ImhNoPredictiveText
            background: Rectangle {
                radius: 10
                color: "#0b1d44"
                border.color: "#153a85"
                border.width: 1
            }
            color: "#e8f1ff"
            padding: 14
            onAccepted: loginButton.clicked()
        }

        Button {
            id: loginButton
            Layout.fillWidth: true
            text: "Войти"
            enabled: password.text.length > 0
            background: Rectangle {
                radius: 10
                color: loginButton.enabled ? "#1e55b8" : "#163b7a"
            }
            contentItem: Text {
                text: loginButton.text
                color: "#ffffff"
                font.pixelSize: 18
                font.weight: Font.DemiBold
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }
            onClicked: {
                sddm.login(userModel.get(userBox.currentIndex).name, password.text, sessionModel.lastIndex)
            }
        }

        Label {
            id: msg
            Layout.fillWidth: true
            text: (sddm.loginFailed ? "Неверный логин или пароль" : "")
            color: "#ff8a8a"
            wrapMode: Text.WordWrap
            horizontalAlignment: Text.AlignHCenter
        }

        RowLayout {
            Layout.fillWidth: true
            spacing: 12

            ComboBox {
                id: sessionBox
                Layout.fillWidth: true
                model: sessionModel
                textRole: "name"
                currentIndex: sessionModel.lastIndex
                background: Rectangle {
                    radius: 10
                    color: "#0b1d44"
                    border.color: "#153a85"
                    border.width: 1
                }
                contentItem: Text {
                    leftPadding: 14
                    rightPadding: 14
                    text: sessionBox.displayText
                    color: "#e8f1ff"
                    verticalAlignment: Text.AlignVCenter
                    elide: Text.ElideRight
                }
            }

            Button {
                text: "Выключить"
                Layout.preferredWidth: 160
                background: Rectangle {
                    radius: 10
                    color: "#0b1d44"
                    border.color: "#153a85"
                    border.width: 1
                }
                contentItem: Text {
                    text: parent.text
                    color: "#e8f1ff"
                    font.pixelSize: 16
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: sddm.powerOff()
            }
        }
    }

    Label {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        text: "SAIKYO"
        color: "#b7d0ff"
        opacity: 0.55
        font.pixelSize: 14
        letterSpacing: 4
    }
}
