import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material  // 确保导入Material模块
import "../components"
import "../dialogs"

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

    // 临时数据 - 用于测试界面显示
    property var searchResults: [
        {
            id: "2023001",
            name: "张三",
            gender: "男",
            college: "计算机学院",
            department: "软件工程系",
            className: "软工1班",
            dormitory: "A101",
            hometown: "北京",
            phone: "13800138000"
        },
        {
            id: "2023002",
            name: "李四",
            gender: "女",
            college: "经济管理学院",
            department: "金融系",
            className: "金融2班",
            dormitory: "B203",
            hometown: "上海",
            phone: "13900139000"
        }
    ]

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

        // 标题区域
        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 {
            Layout.fillWidth: true
            height: 120
            radius: 12
            color: "#252541"
            border.color: "#4a4a7a"
            opacity: 0.9

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

                // 搜索栏
                SearchBar {
                    id: searchBar
                    Layout.fillWidth: true
                    Layout.preferredHeight: 48
                    onSearchRequested: {
                        console.log("搜索请求:", searchType.currentValue, searchField.text)
                        // 实际应用中应调用服务层搜索方法
                        // StudentService.searchStudents({
                        //     type: searchType.currentValue,
                        //     keyword: searchField.text
                        // })
                    }
                }

                // 提示信息
                Label {
                    text: "提示:可按照学号、姓名、学院、班级等条件查询"
                    font.pixelSize: 12
                    color: "#a0a0c0"
                    Layout.alignment: Qt.AlignHCenter
                }
            }
        }

        // 查询结果卡片
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            radius: 12
            color: "#1e1e3a"
            border.color: "#4a4a7a"
            opacity: 0.9

            // 结果标题
            Label {
                text: "查询结果"
                font.bold: true
                font.pixelSize: 18
                color: Material.primary  // 使用Material.primary代替Material.accent
                padding: 16
                anchors.top: parent.top
                anchors.left: parent.left
            }

            // 结果统计信息
            RowLayout {
                anchors.top: parent.top
                anchors.right: parent.right
                anchors.margins: 16
                spacing: 16

                Label {
                    text: "共找到 <b>" + root.searchResults.length + "</b> 条记录"
                    font.pixelSize: 14
                    color: "#e0e0ff"
                }

                Button {
                    text: "导出结果"
                    background: Rectangle {  // 直接设置背景而不是使用Material.background
                        color: "#3a3a6a"
                        radius: 6
                    }
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 32

                    contentItem: Label {
                        text: "💾 " + parent.text
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        font.pixelSize: 12
                        color: "#e0e0ff"
                    }

                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered: parent.background.color = "#4a4a8a"
                        onExited: parent.background.color = "#3a3a6a"
                        onClicked: console.log("导出结果")
                    }
                }
            }

            // 学生表格
            StudentTable {
                anchors {
                    fill: parent
                    topMargin: 50
                    margins: 16
                }
                model: root.searchResults  // 使用临时数据
                onEditRequested: editDialog.open(student)
                onDeleteRequested: confirmDialog.open(student.id)
            }
        }
    }

    // 对话框
    EditDialog { id: editDialog }
    ConfirmDialog {
        id: confirmDialog
        onConfirmed: function(studentId) {
            console.log("确认删除学生:", studentId)
            // 实际应用中应调用服务层删除方法
            // StudentService.deleteStudent(studentId)
        }
    }
}