#include <fstream>
#include <cstring>
#include <graphics.h>
#include <conio.h>
using namespace std;
#define MAX_STUDENTS 100  // 最大学生数量
#define FILENAME "students.txt"  // 数据存储文件
//图片传输
int  TU(int x, int y)
{
    IMAGE neam;
    loadimage(&neam, "捕获.jpg", 400, 300);
    putimage(x, y, &neam);
    return 0;
}
// 按钮结构体
struct Button {
    int x, y;
    int width, height; 
    char text[20];
};
// 学生信息结构体
struct Student {
    char id[20];
    char name[20];
    char gender[4];
    char age[10];
    char major[30];
    char className[20];
};
Student students[MAX_STUDENTS];  // 学生数组
int studentCount = 0;            // 当前学生数量
//绘按键
void drawButton(Button btn) {
    setfillcolor(LIGHTGRAY); 
    bar(btn.x, btn.y, btn.x + btn.width, btn.y + btn.height);

    setlinecolor(BLACK); 
    rectangle(btn.x, btn.y, btn.x + btn.width, btn.y + btn.height);

    setbkmode(TRANSPARENT); 
    settextcolor(BLACK); 
    outtextxy(btn.x + 10, btn.y + 10, btn.text);
}
// 检查鼠标是否在按钮范围内
bool isMouseInButton(Button btn, int mouseX, int mouseY) {
    return (mouseX >= btn.x && mouseX <= btn.x + btn.width &&
        mouseY >= btn.y && mouseY <= btn.y + btn.height);
}
// 添加学生
void addStudent() {
    if (studentCount >= MAX_STUDENTS) {	
        fillrectangle(10, 175, 640, 190);
        outtextxy(10, 175, "学生数量已达上限！");
        return;
    }
    Student s;
    fillrectangle(10, 175, 640, 190);
    outtextxy(10, 175, "请输个人信息:");
	InputBox(s.id, 20, "学号");
// 检查学号是否已存在
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, s.id) == 0) {
            fillrectangle(10, 175, 640, 190);
            outtextxy(10, 175, "该学号已存在！");
            return;
        }
    }
    InputBox(s.name, 20, "姓名");
    InputBox(s.gender, 4, "性别");
    InputBox(s.age, 10, "年龄");
    InputBox(s.major, 30, "专业");
    InputBox(s.className, 20, "班级");
    students[studentCount++] = s;
    fillrectangle(10, 175, 640, 190);
    outtextxy(10, 175, "添加成功！");
}

// 查询学生
void searchStudent() {
    char targetId[20];
    fillrectangle(10, 175, 640, 190);
    outtextxy(10, 175, "请输入要查询的学号:");
    InputBox(targetId, 20, "学号");
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, targetId) == 0) {
            fillrectangle(10, 175, 640, 480);
            outtextxy(10, 175, students[i].id);
            outtextxy(10, 200, students[i].name);
            outtextxy(10, 225, students[i].gender);
            outtextxy(10, 250, students[i].age);
            outtextxy(10, 275, students[i].major);
            outtextxy(10, 300, students[i].className);
            outtextxy(10, 325, "按任意键返回主菜单");
            getchar();
            fillrectangle(10, 150, 640, 480);
			TU(120,175);
            return;
        }
        fillrectangle(10, 175, 640, 190);
        outtextxy(10, 175, "找不到该学生");
    }
}
// 删除学生
void deleteStudent() {
    char targetId[20];
    InputBox(targetId, 20, "学号");
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, targetId) == 0) {
            for (int j = i; j < studentCount - 1; j++) {
                students[j] = students[j + 1];
            }
            studentCount--;
            fillrectangle(10, 175, 640, 190);
            outtextxy(10, 175, "删除成功");
            return;
        }
    }
    fillrectangle(10, 175, 640, 190);
    outtextxy(10, 175, "未找到该学生");
}
//修改
void StudentFix(){
	char targetId[20];
    InputBox(targetId, 20, "学号");
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, targetId) == 0){
	InputBox(students[i].name, 20, "姓名");
    InputBox(students[i].gender, 4, "性别");
    InputBox(students[i].age, 10, "年龄");
    InputBox(students[i].major, 30, "专业");
    InputBox(students[i].className, 20, "班级");
    fillrectangle(10, 175, 640, 190);
    outtextxy(10, 175, "修改成功！");
		 return;	
		}
	}
}
// 保存数据到文件
void saveToTxt() {
    ofstream outFile(FILENAME);
    if (!outFile) {
        fillrectangle(10, 175, 640, 190);
        outtextxy(10, 175, "保存失败");
        return;
    }
    outFile << "学号\t\t姓名\t性别\t年龄\t专业\t班级" << endl;
    for (int i = 0; i < studentCount; i++) {
        outFile << students[i].id << "\t"
            << students[i].name << "\t"
            << students[i].gender << "\t"
            << students[i].age << "\t"
            << students[i].major << "\t"
            << students[i].className << endl;
    }
    outFile.close();
    fillrectangle(10, 175, 640, 190);
    outtextxy(10, 175, "保存成功");
}
// 数据加载函数
void loadFromTxt() {
    ifstream inFile(FILENAME);
    if (!inFile) return;

    char header[256];
    inFile.getline(header, 256);
// 读取数据
    studentCount = 0;
    while (!inFile.eof() && studentCount < MAX_STUDENTS) {
        Student s;
        inFile >> s.id >> s.name >> s.gender
            >> s.age >> s.major >> s.className;
        if (strlen(s.id) > 0) {
            students[studentCount++] = s;
        }
    }
    inFile.close();
}
int main() {
    loadFromTxt();   
    initgraph(640, 480);
    TU(120,175);
//定义菜单的按键
    Button btnAdd = { 50, 50, 150, 50, "添加学生" };
    Button btnQuery = { 250, 50, 150, 50, "查询学生" };
    Button btnExit = { 450, 50, 150, 50, "退出" };
    Button btnDelet = { 50,100,150, 50,"删除学生" };
    Button btnFix={250,100,150,50,"修改学生"};
    Button btnSave = { 450,100,150, 50,"保存数据" };
//绘制菜单按键
    drawButton(btnAdd);
    drawButton(btnQuery);
    drawButton(btnExit);
    drawButton(btnDelet);
    drawButton(btnSave);
	drawButton(btnFix);
    while (true) {
// 获取鼠标消息
        if (MouseHit()) {
            MOUSEMSG msg = GetMouseMsg();
// 检查鼠标左键点击
            if (msg.uMsg == WM_LBUTTONDOWN) {
                if (isMouseInButton(btnAdd, msg.x, msg.y)) {
                    addStudent();
                }
                if (isMouseInButton(btnQuery, msg.x, msg.y)) {
                    searchStudent();
                }
                if (isMouseInButton(btnExit, msg.x, msg.y)) {
                    break;
                }
                if (isMouseInButton(btnDelet, msg.x, msg.y)) {
                    deleteStudent();
                }
				if (isMouseInButton(btnFix, msg.x, msg.y)) {
                    StudentFix();
				}
                if (isMouseInButton(btnSave, msg.x, msg.y)) {
                    saveToTxt();
                }

            }
        }
    }
// 关闭图形窗口
    closegraph();
    return 0;
}