import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material
import "../components"
import "../dialogs"
import "../styles"

Page {
    id: root
    background: Rectangle {
        color: "#1a1a2e"
    }

    // 主布局 - 现在包含在ScrollView中
    ScrollView {
        id: mainScroll
        anchors.fill: parent
        clip: true
        contentWidth: parent.width
        contentHeight: contentLayout.implicitHeight + 100

        ColumnLayout {
            id: contentLayout
            width: parent.width
            spacing: 20
            anchors.margins: 24

            // 标题区域
            Rectangle {
                Layout.fillWidth: true
                height: 80
                radius: 12
                color: "#252541"
                border.color: "#4a4a7a"

                Label {
                    anchors.centerIn: parent
                    text: "➕ 添加新学生"
                    font {
                        bold: true
                        pixelSize: 28
                        family: "Microsoft YaHei"
                    }
                    color: "#e0e0ff"
                }
            }

            // 表单卡片 - 使用固定高度确保可滚动
            Rectangle {
                id: formCard
                Layout.fillWidth: true
                Layout.preferredHeight: 600 // 固定高度确保可滚动
                radius: 12
                color: "#1e1e3a"
                border.color: "#4a4a7a"

                // 表单内容 - 内嵌ScrollView
                ScrollView {
                    id: formScroll
                    anchors.fill: parent
                    anchors.margins: 16
                    clip: true
                    contentWidth: parent.width
                    contentHeight: formContainer.height + 50

                    ColumnLayout {
                        id: formContainer
                        width: parent.width
                        spacing: 10

                        // 表单标题
                        Label {
                            text: "学生基本信息"
                            font.bold: true
                            font.pixelSize: 20
                            color: Material.accent
                            Layout.bottomMargin: 15
                            Layout.alignment: Qt.AlignHCenter
                        }

                        // 表单内容
                        StudentForm {
                            id: studentForm
                            Layout.fillWidth: true
                        }
                    }
                }
            }

            // 按钮区域 - 保持在底部
            Item {
                Layout.fillWidth: true
                Layout.preferredHeight: 70

                RowLayout {
                    anchors.right: parent.right
                    spacing: 20

                    // 清空按钮
                    Button {
                        text: "清空"
                        Layout.preferredWidth: 140
                        Layout.preferredHeight: 48
                        Material.background: "#3a3a6a"
                        Material.foreground: "#e0e0ff"

                        contentItem: Label {
                            text: "🗑🗑️ " + parent.text
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 16
                        }

                        background: Rectangle {
                            radius: 8
                            color: parent.hovered ? "#4a4a8a" : "#3a3a6a"
                            border.color: parent.hovered ? "#6a5acd" : "transparent"
                        }

                        onClicked: studentForm.clear()
                    }

                    // 提交按钮
                    Button {
                        text: "提交"
                        Layout.preferredWidth: 140
                        Layout.preferredHeight: 48
                        Material.background: "#6a5acd"
                        Material.foreground: "white"

                        contentItem: Label {
                            text: "✅ " + parent.text
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 16
                            font.bold: true
                        }

                        background: Rectangle {
                            radius: 8
                            color: parent.hovered ? Qt.darker("#6a5acd", 1.2) : "#6a5acd"
                        }

                        onClicked: {
                            StudentService.addStudent(studentForm.getStudentData())
                            studentForm.clear()
                        }
                    }
                }
            }
        }
    }
}