package com.msksbr.MainFrm.MenuItemDiaog.Adder;

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 RentAdder extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField bIDField;
    private JTextField sIDField;
    private int bID;
    private int sID;
    private Connector bConnector;
    private Connector sConnector;
    private String bookSearchSQL;
    private String studentSearchSQL;
    private String updateSQL;
    private ResultSet bIDResult;
    private ResultSet sIDResult;

    public RentAdder() {
        try {
            bConnector = new Connector();
            sConnector = new Connector();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setTitle("添加借阅信息");

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
        pack();
        setResizable(false);
        setLocation();
        setVisible(true);
    }

    private void onOK() {
        // 在此处添加您的代码
        try {
            this.bID = Integer.valueOf(bIDField.getText());
            this.sID = Integer.valueOf(sIDField.getText());
            searchSQLMaker();
            updateSQLMaker();
            bIDResult = bConnector.executeQuery(bookSearchSQL);
            sIDResult = sConnector.executeQuery(studentSearchSQL);
            if (!bIDResult.next()) {
                JOptionPane.showMessageDialog(this, "未找到该书信息", "错误", JOptionPane.WARNING_MESSAGE);
            } else if (!sIDResult.next()) {
                JOptionPane.showMessageDialog(this, "未找到学生信息", "错误", JOptionPane.WARNING_MESSAGE);
            } else {
                if (bIDResult.getInt("borrowed_by") == 0) {
                    bConnector.executeUpdate(updateSQL);
                } else {
                    JOptionPane.showMessageDialog(this, "该书已借出", "错误", JOptionPane.WARNING_MESSAGE);
                }
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入一个整数", "错误", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        dispose();
    }

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

    private void searchSQLMaker() {
        this.bookSearchSQL = "SELECT * FROM books WHERE book_id =" + this.bID + ";";
        this.studentSearchSQL = "SELECT * FROM students WHERE student_id =" + this.sID + ";";
    }

    private void updateSQLMaker() {
        this.updateSQL = "UPDATE books SET borrowed_by = " + this.sID + " WHERE book_id =" + this.bID + ";";
    }
}
