package co.dc.aptiv;

import co.dc.aptiv.pojo.UserPojo;
import co.dc.aptiv.utils.KeyboardUtil;
import co.dc.aptiv.utils.ResetJButton;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;

/**
 * @describe: 主界面
 * @author: liuxinglong
 * @version: 2018/4/17 15:21
 */
public class MainFrame extends JFrame {
    private JPanel contentPane;
    private UserPojo userPojo = new UserPojo();
    private MyDialog_exit myDialog_exit;


    /**
     * Create the frame.
     */
    public MainFrame() {
        //获取当前登录的用户信息
        userPojo = AptivApp.session;
        setResizable(false);
        setUndecorated(true);
        setTitle("德尔福激光工控V1.1");
        setBounds(100, 100, 1024, 768);
        setLocationRelativeTo(null);
        setAlwaysOnTop(!isAlwaysOnTop());
        contentPane = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Image image = new ImageIcon(MainFrame.class.getResource("img/bg.png")).getImage();
                g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
            }
        };
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //开始打印
        JButton printButton = new ResetJButton("/co/dc/aptiv/img/btn01.png", "/co/dc/aptiv/img/btn01-h.png", "/co/dc/aptiv/img/btn01.png");
        printButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new ScanFrame().setVisible(true);
                MainFrame.this.dispose();
            }
        });
        if (userPojo.getRoleid() == 1) {
            printButton.setBounds(114, 313, 175, 214);
        } else {
            printButton.setBounds(200, 313, 175, 214);
        }
        contentPane.add(printButton);

        //生产统计
        JButton billButton = new ResetJButton("/co/dc/aptiv/img/btn02.png", "/co/dc/aptiv/img/btn02-h.png", "/co/dc/aptiv/img/btn02.png");
        billButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new BillFrame().setVisible(true);
                MainFrame.this.dispose();
            }
        });
        if (userPojo.getRoleid() == 1) {
            billButton.setBounds(321, 313, 175, 214);
        } else {
            billButton.setBounds(420, 313, 175, 214);
        }
        contentPane.add(billButton);

        //用户管理
        JButton userButton = new ResetJButton("/co/dc/aptiv/img/btn03.png", "/co/dc/aptiv/img/btn03-h.png", "/co/dc/aptiv/img/btn03.png");
        userButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new UserFrame().setVisible(true);
                MainFrame.this.dispose();
            }
        });
        if (userPojo.getRoleid() == 1) {
            userButton.setBounds(528, 313, 175, 214);
        } else {
            userButton.setBounds(640, 313, 175, 214);
        }
        contentPane.add(userButton);

        //物料管理
        if (userPojo.getRoleid() == 1) {
            JButton materielButton = new ResetJButton("/co/dc/aptiv/img/btn05.png", "/co/dc/aptiv/img/btn05-h.png", "/co/dc/aptiv/img/btn05.png");
            materielButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    new MaterielFrame().setVisible(true);
                    MainFrame.this.dispose();
                }
            });
            materielButton.setBounds(735, 313, 175, 214);
            contentPane.add(materielButton);
        }

        //退出系统
        JButton exitButton = new ResetJButton("/co/dc/aptiv/img/exit.png", "/co/dc/aptiv/img/exit.png", "/co/dc/aptiv/img/exit.png");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //确定退出且验证密码成功后执行关闭系统的操作
                if (myDialog_exit == null) {
                    myDialog_exit = new MyDialog_exit(MainFrame.this, true);
                }
                myDialog_exit.show();
            }
        });
        exitButton.setBounds(850, 660, 132, 35);
        contentPane.add(exitButton);

    }

    /**
     * 主页退出的自定义弹框
     *
     * @author lyy
     */
    class MyDialog_exit extends Dialog implements ActionListener {
        /**
         * 加载面板
         */
        JLabel label = new JLabel("输入当前操作员密码进行退出！");
        JTextField textField = new JPasswordField();

        JButton ok = new JButton("确定");
        JButton cancel = new JButton("取消");

        MyDialog_exit(MainFrame parent, boolean modal) {
            super(parent, modal);
            setTitle("退出系统");
            setSize(300, 240);
            setLocationRelativeTo(parent);
            setResizable(false);
            new KeyboardUtil(textField, true);
//         setUndecorated(true);
            this.addWindowListener(new WindowListener() {
                public void windowOpened(WindowEvent e) {
                }

                public void windowIconified(WindowEvent e) {
                }

                public void windowDeiconified(WindowEvent e) {
                }

                public void windowDeactivated(WindowEvent e) {
                }

                public void windowClosing(WindowEvent e) {
                    myDialog_exit.dispose();
                }

                public void windowClosed(WindowEvent e) {
                }

                public void windowActivated(WindowEvent e) {
                }
            });
            setLayout(null);
            add(label);
            label.setBounds(30, 50, 280, 15);
            add(textField);
            textField.setBounds(30, 80, 225, 40);
            textField.setColumns(10);
            textField.addKeyListener(new KeyListener() {
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        String value = textField.getText();
                        if (value != null && value.length() > 0) {
                            if (userPojo != null) {
                                if (userPojo.getPassword().equals(value)) {
                                    dispose();
                                    System.exit(0);
                                } else {
                                    JOptionPane.showMessageDialog(MainFrame.this, "密码错误.", "提示", JOptionPane.ERROR_MESSAGE);
                                }
                            } else {
                                dispose();
                                System.exit(0);
                            }
                        } else {
                            JOptionPane.showMessageDialog(MainFrame.this, "请输入密码.", "提示", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }

                public void keyReleased(KeyEvent e) {
                }

                public void keyTyped(KeyEvent e) {
                }
            });
            add(ok);
            add(cancel);
            ok.setBounds(43, 160, 80, 40);
            cancel.setBounds(165, 160, 80, 40);
            ok.addActionListener(this);
            cancel.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == ok) {
                String value = textField.getText();
                if (value != null) {
                    if (userPojo != null) {
                        if (userPojo.getPassword().equals(value)) {
                            dispose();
                            System.exit(0);
                        } else {
                            JOptionPane.showMessageDialog(MainFrame.this, "密码错误.", "提示", JOptionPane.ERROR_MESSAGE);
                        }
                    } else {
                        dispose();
                        System.exit(0);
                    }
                } else {
                    JOptionPane.showMessageDialog(MainFrame.this, "请输入密码.", "提示", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                dispose();
            }
        }
    }
}
