package Pages;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import Store.Users;
import Util.BasicOperation;

public class Login {
    public static void main() {
        ImageIcon backImage = new ImageIcon("src/Images/OIP-C.jpeg");
        JLabel bac = new JLabel(backImage);
        // 初始JFrame
        JFrame frame = new JFrame("图书管理系统");
        frame.setSize(400,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 初始JPanel
        JPanel panel = new JPanel();
        // 将背景图片添加到frame
        panel.add(bac);
        // 将panel添加到frame
        frame.add(panel);
        // 调用组件构造函数
        placeLoginComponents(frame,panel);
        // 设置可视化
        frame.setVisible(true);
    }

    private static void placeLoginComponents(JFrame frame,JPanel panel) {
        // placeLoginComponents方法
        panel.setLayout(null);

        JLabel label = new JLabel("欢迎进入图书管理系统");
        label.setBounds(130,65,160,25);
        panel.add(label);

        JLabel username = new JLabel("账号");
        username.setBounds(120,95,40,25);
        panel.add(username);

        JTextField nameField = new JTextField(20);
        nameField.setBounds(160,95,100,25);
        panel.add(nameField);

        JLabel password = new JLabel("密码");
        password.setBounds(120,130,40,25);
        panel.add(password);

        JPasswordField passwordField = new JPasswordField(20);
        passwordField.setBounds(160,130,100,25);
        panel.add(passwordField);

        ButtonGroup group = new ButtonGroup();

        JRadioButton jRadioButton = new JRadioButton("用户");
        jRadioButton.setBounds(130,165,60,25);
        jRadioButton.setSelected(true);
        group.add(jRadioButton);
        panel.add(jRadioButton);

        JRadioButton jRadioButton1 = new JRadioButton("管理员");
        jRadioButton1.setBounds(190,165,80,25);
        group.add(jRadioButton1);
        panel.add(jRadioButton1);

        JButton login = new JButton("登录");
        login.setBounds(120,195,140,25);
        panel.add(login);
        login.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = nameField.getText();
                String password = new String(passwordField.getPassword());
                int admin = jRadioButton.isSelected() ? 0 : 1;
                BasicOperation basicOperation = new BasicOperation();
                if (basicOperation.loginCheck(username,password,admin)) {
                    Users users = new Users(username, admin);
                    frame.dispose();
                    try {
                        Home.main(users);
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    System.out.println("登录成功");
                } else {
                    JOptionPane.showMessageDialog(panel,"账号或者密码输入错误");
                }
            }
        });

        JButton register = new JButton("注册账号");
        register.setBounds(-10,335,100,25);
        register.setContentAreaFilled(false);
        register.setBorder(null);
        panel.add(register);
        register.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                Register.main();
            }
        });

        JButton forget = new JButton("忘记密码");
        forget.setBounds(300,335,100,25);
        forget.setContentAreaFilled(false);
        forget.setBorder(null);
        panel.add(forget);
        forget.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(panel,"功能待完善");
//                frame.dispose();
            }
        });
    }
}
