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.sql.ResultSet;
import java.sql.SQLException;

public class Remover extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JLabel idLable;
    private JTextField idField;
    private String table;
    private String Searchsql;
    private String searchSQL;
    private String deleteSQL;
    private String searchErrMsg;
    private int id;
    private Connector searchConnector;
    private Connector deleteConnector;

    public Remover(String table) {
        try {
            searchConnector = new Connector();
            deleteConnector = new Connector();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
        this.table = table;
        if (table.equals("books")) {
            setTitle("删除图书信息");
            idLable.setText("请输入书号：");
            searchErrMsg = "未找到该图书信息";
        } else if (table.equals("students")) {
            setTitle("删除学生信息");
            idLable.setText("请输入学号：");
            searchErrMsg = "未找到该学生信息";
        } else if (table.equals("rents")) {
            setTitle("删除借阅信息");
            idLable.setText("请输入书号：");
            searchErrMsg = "未找到该借阅信息";
        }
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        pack();
        setResizable(false);
        setLocation();
        setVisible(true);
    }

    private void onOK() {
        // 在此处添加您的代码
        try {
            this.id = Integer.valueOf(idField.getText());
            searchSQLMaker();
            deleteSQLMaker();
            ResultSet rs = searchConnector.executeQuery(searchSQL);
            if (rs.next()) {
                deleteConnector.executeUpdate(deleteSQL);
            } else {
                JOptionPane.showMessageDialog(this, searchErrMsg, "错误", JOptionPane.WARNING_MESSAGE);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        dispose();
    }

    private void searchSQLMaker() {
        if (table.equals("books")) {
            searchSQL = "select * from " + table + " where book_id=" + id + ";";
        } else if (table.equals("students")) {
            searchSQL = "select * from " + table + " where student_id=" + id + ";";
        } else if (table.equals("rents")) {
            searchSQL = "select * from " + table + " where book_id=" + id + ";";
        }
    }

    private void deleteSQLMaker() {
        if (table.equals("books")) {
            deleteSQL = "delete from " + table + " where book_id=" + id + ";";
        } else if (table.equals("students")) {
            deleteSQL = "delete from " + table + " where student_id=" + id + ";";
        } else if (table.equals("rents")) {
            deleteSQL = "UPDATE books SET borrowed_by = null WHERE book_id=" + id + ";";
        }
    }

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