package com.msksbr.MainFrm.MenuItemDiaog;

import com.msksbr.LoginFrm.ScreenSize;
import com.msksbr.SQL.Connector;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SearchDIalog extends JDialog {
    public JLabel messageLabel;
    protected int idInt;
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField idFIeld;
    private String table;

    public SearchDIalog(String table) {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        if (table.equals("books")) {
            this.table = "books";
            this.setTitle("查询图书信息");
            messageLabel.setText("请输入书号：");
        } else if (table.equals("students")) {
            this.table = "students";
            this.setTitle("查询学生信息");
            messageLabel.setText("请输入学号：");
        } else if (table.equals("rents")) {
            this.table = "rents";
            this.setTitle("查询借阅信息");
            messageLabel.setText("请输入书号：");
        }
        setListener();
        pack();
        setLocation();
        setVisible(true);
    }

    private void setListener() {
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
        idFIeld.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    onOK();
                }
            }
        });
    }

    private void setLocation() {
        setLocation(new ScreenSize().width / 2 - getWidth() / 2, new ScreenSize().height / 2 - getHeight() / 2);
    }

    private void onOK() {
        ResultSet rs = SQLExe();
        String message = "";
        try {
            if (rs != null && rs.next()) {
                String title = "";
                if (table.equals("books")) {
                    message = "书名：\t" + rs.getString("book_name") + "\n类别：\t" + rs.getString("book_type") + "\n书号：\t" + rs.getInt("book_id") + "\nISBN：\t" + rs.getString("ISBN");
                    title = "图书信息";
                } else if (table.equals("students")) {
                    message = "学生姓名：\t" + rs.getString("student_name") + "\n学号：\t" + rs.getInt("student_id") + "\n班级：\t" + rs.getString("student_class") + "\n性别：\t" + rs.getString("student_gender");
                    title = "学生信息";
                } else if (table.equals("rents")) {
                    message = "学生姓名：\t" + rs.getString("student_name") + "\n学号：\t" + rs.getInt("student_id") + "\n班级：\t" + rs.getString("student_class") + "\n性别：\t" + rs.getString("student_gender") + "\n书名：\t" + rs.getString("book_name") + "\n书号：\t" + rs.getInt("book_id") + "\n类别：\t" + rs.getString("book_type") + "\nISBN：\t" + rs.getString("ISBN");
                    title = "借阅信息";
                }
                new showMessae(message, title);
            } else {
                JOptionPane.showMessageDialog(this, "未找到", "未找到", JOptionPane.WARNING_MESSAGE);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private ResultSet SQLExe() {
        ResultSet rs = null;
        try {
            idInt = Integer.valueOf(idFIeld.getText());
            Connector connector = new Connector();
            rs = connector.executeQuery(SQLExeMaker(table));
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return rs;
    }

    private String SQLExeMaker(String table) {
        if (table.equals("books")) {
            return "SELECT * FROM `books` WHERE `book_id` = " + idInt + ";";
        } else if (table.equals("students")) {
            return "SELECT * FROM `students` WHERE `student_id` = " + idInt + ";";
        } else if (table.equals("rents")) {
            return "SELECT * FROM `rents` WHERE `book_id` = " + idInt + ";";
        } else {
            return null;
        }
    }
}
