package com.msksbr.MainFrm;

import javax.swing.*;

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

import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MainFrm extends JFrame {
    private Connector connector;
    private JPanel panel1;
    private JPanel tablePanel;
    private JPanel bottomPanel;
    private JLabel backLabel;
    private JLabel countMessage = new JLabel();
    private MenuBar menuBar;

    public MainFrm() throws SQLException, ClassNotFoundException {
        setIconImage(new ImageIcon("src/com/msksbr/images/mainicon.png").getImage());
        connector = new Connector();
        setTitle("图书管理系统");
        setContentPane(panel1);
        pack();

        menuBar = new MenuBar();


        setJMenuBar(menuBar);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(960, 720);

        String countData = "本馆共藏书" + getCount("books") + "本，共记录学生信息" + getCount("students") + "条，已借阅" + getCount("rents") + "本书，人均借阅" + (double) getCount("rents") / getCount("students") + "本";
        countMessage.setText(countData);
        bottomPanel.add(countMessage);
        ImageIcon imageIcon = new ImageIcon("src/com/msksbr/images/backGround.png");
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(250, 250, 0));

        backLabel.setIcon(imageIcon);

        setMinimumSize(new Dimension(960, 720));

        setLocation();
        initDialogs();
        setVisible(true);
    }


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


    private void initDialogs() {
        menuBar.exitItem.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.exit(0);
                    }
                }
        );
    }

    private int getCount(String table) throws SQLException {
        ResultSet rs = connector.executeQuery("SELECT COUNT(*) FROM " + table + ";");
        rs.next();
        return rs.getInt("COUNT(*)");
    }
}
