// src/ui/views/ListView.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material
import "../components"
import "../dialogs"
import "../styles"

Page {
    id: root
    property var studentModel: StudentService.students

    background: Rectangle {
        color: "transparent"
    }

    Connections {
        target: StudentService
        function onStudentsChanged() {
            root.studentModel = StudentService.students
        }
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 20
        anchors.margins: 24

        // 标题和操作栏
        Rectangle {
            Layout.fillWidth: true
            height: 80
            radius: 12
            color: "#252541"
            opacity: 0.9
            border.color: "#4a4a7a"

            RowLayout {
                anchors.fill: parent
                anchors.margins: 16

                Label {
                    text: "📋 学生信息管理"
                    font {
                        bold: true
                        pixelSize: 24
                        family: "Microsoft YaHei"
                    }
                    color: "#e0e0ff"
                }

                Item { Layout.fillWidth: true }

                RowLayout {
                    spacing: 15

                    // 刷新按钮
                    Button {
                        id: refreshBtn
                        text: "刷新数据"
                        Layout.preferredWidth: 140
                        Layout.preferredHeight: 48
                        Material.background: "#6a5acd"
                        Material.foreground: "white"

                        contentItem: RowLayout {
                            spacing: 8
                            anchors.centerIn: parent

                            Text {
                                text: "🔄"
                                font.pixelSize: 16
                            }

                            Text {
                                text: parent.parent.text
                                font.bold: true
                                color: "white"
                            }
                        }

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

                        onClicked: StudentService.loadData()
                    }

                    // 排序选择器
                    ComboBox {
                        id: sortCombo
                        Layout.preferredWidth: 200
                        Layout.preferredHeight: 48
                        model: ["按学号排序", "按学院排序", "按宿舍排序"]

                        background: Rectangle {
                            color: "#2a2a4a"
                            radius: 8
                            border.color: sortCombo.activeFocus ? "#6a5acd" : "#4a4a7a"
                        }

                        contentItem: Text {
                            text: sortCombo.displayText
                            color: "#e0e0ff"
                            leftPadding: 16
                            verticalAlignment: Text.AlignVCenter
                        }

                        popup: Popup {
                            y: parent.height
                            width: parent.width
                            implicitHeight: contentItem.implicitHeight

                            background: Rectangle {
                                color: "#2a2a4a"
                                radius: 8
                                border.color: "#4a4a7a"
                            }

                            contentItem: ListView {
                                clip: true
                                implicitHeight: contentHeight
                                model: sortCombo.popup.visible ? sortCombo.delegateModel : null

                                delegate: ItemDelegate {
                                    width: sortCombo.width
                                    height: 48

                                    contentItem: Text {
                                        text: modelData
                                        color: "#e0e0ff"
                                        leftPadding: 16
                                        verticalAlignment: Text.AlignVCenter
                                    }

                                    background: Rectangle {
                                        color: parent.hovered ? "#3a3a5a" : "transparent"
                                    }
                                }
                            }
                        }

                        onActivated: StudentService.sortStudents(index)
                    }
                }
            }
        }

        // 学生表格卡片 - 移除DropShadow
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            radius: 12
            color: "#1a1a2e"
            opacity: 0.8
            border.color: "#4a4a7a"

            // 简单阴影效果替代
            Rectangle {
                anchors.fill: parent
                anchors.margins: 4
                radius: parent.radius
                color: "#40000000"
                z: -1
            }

            // 学生表格
            StudentTable {
                anchors {
                    fill: parent
                    margins: 16
                }
                model: root.studentModel

                onEditRequested: editDialog.open(student)
                onDeleteRequested: confirmDialog.open(student.id)
            }
        }
    }

    // 编辑对话框
    EditDialog { id: editDialog }

    // 删除确认对话框
    ConfirmDialog {
        id: confirmDialog
        onConfirmed: function(studentId) {
            StudentService.deleteStudent(studentId)
        }
    }
}