package view;

import javax.swing.*;

import backend.model.User;
import backend.service.AuthService;

import java.awt.*;

public class EditUserDialog extends JDialog {
    private JTextField nameTextField;
    private JTextField emailTextField;
    private JComboBox roleCombobox;
    private JButton saveButton;
    private JButton cancelButton;
    private int type;
    private User user;
    private AuthService userService = new AuthService();

    // 颜色常量定义
    private static final Color PRIMARY_COLOR = new Color(70, 130, 180);  // 钢蓝色
    private static final Color BACKGROUND_COLOR = new Color(248, 249, 250);  // 浅灰色背景
    private static final Color BORDER_COLOR = new Color(220, 220, 220);  // 边框颜色
    private static final Color HIGHLIGHT_COLOR = new Color(255, 255, 204);  // 高亮背景

    public EditUserDialog(int type, User user) {
        this.type = type;
        this.user = user;
        initView();
    }

    public void initListener(){
        saveButton.addActionListener(e->{
            String name = nameTextField.getText().trim();
            String email = emailTextField.getText().trim();
            if(name.equals("")|| email.equals("")) {
                JOptionPane.showMessageDialog(this, "请填写完整！");
                return;
            }
            User userPo = new User();
            userPo.setUsername(name);
            userPo.setEmail(email);


            Integer index= roleCombobox.getSelectedIndex();
            userPo.setRole(index==0 ? "user":"admin");
            String message;
            boolean res;
            if(type==0) {
                userPo.setPassword("123456");
                userPo.setCreatedAt();
                res = userService.addUser(userPo);
                message = "添加";
            }
            else{
                userPo.setCreatedAt(user.getCreatedAt());
                userPo.setPassword(user.getPassword());
                userPo.setId(user.getId());
                res = userService.updateUser(userPo);
                message = "修改";
            }
            if(res)
                dispose();
            else
                JOptionPane.showMessageDialog(this, message+"失败");
        });
        cancelButton.addActionListener(e->{
            dispose();
        });
    }

    private void initView(){
        setSize(400, 300);
        setLocationRelativeTo(null);//居中
        setLayout(new BorderLayout());

        // 创建顶部标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        titlePanel.setBackground(PRIMARY_COLOR);
        JLabel titleLabel = new JLabel(type == 0 ? "添加用户" : "编辑用户");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        titleLabel.setForeground(Color.WHITE);
        titlePanel.add(titleLabel);
        add(titlePanel, BorderLayout.NORTH);

        // 添加主内容面板
        add(initPanel(), BorderLayout.CENTER);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setModal(true);//设置模态
        if(type==0)
            setTitle("添加用户");
        else{
            setTitle("编辑用户");
            nameTextField.setText(user.getUsername());
            emailTextField.setText(user.getEmail());
            roleCombobox.setSelectedIndex(user.getRole().equals("admin")? 1:0);
        }
        //细节：要先初始化监听器，再初始化界面
        //初始化监听器
        initListener();
        //显示界面
        setVisible(true);
    }

    private JPanel initPanel(){
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBackground(BACKGROUND_COLOR);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        // 第一行 - 用户名
        gbc.gridx = 0; gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(new JLabel("用户名:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        nameTextField = new JTextField();
        nameTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        nameTextField.setPreferredSize(new Dimension(200, 25));
        mainPanel.add(nameTextField, gbc);

        // 第二行 - 邮箱
        gbc.gridx = 0; gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        mainPanel.add(new JLabel("邮箱:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        emailTextField = new JTextField();
        emailTextField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        emailTextField.setPreferredSize(new Dimension(200, 25));
        mainPanel.add(emailTextField, gbc);

        // 第三行 - 权限
        gbc.gridx = 0; gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        mainPanel.add(new JLabel("权限:"), gbc);

        gbc.gridx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        roleCombobox = new JComboBox<>(new String[]{"user","admin"});
        roleCombobox.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        roleCombobox.setPreferredSize(new Dimension(200, 25));
        mainPanel.add(roleCombobox, gbc);

        // 第四行 - 按钮
        gbc.gridx = 0; gbc.gridy = 3;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0.0;
        gbc.anchor = GridBagConstraints.CENTER;
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
        buttonPanel.setBackground(BACKGROUND_COLOR);

        saveButton = new JButton("保存");
        cancelButton = new JButton("取消");

        // 设置按钮样式
        setButtonStyle(saveButton);
        setButtonStyle(cancelButton);

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        mainPanel.add(buttonPanel, gbc);

        // 添加边距
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        return mainPanel;
    }

    // 设置按钮样式
    private void setButtonStyle(JButton button) {
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        button.setPreferredSize(new Dimension(80, 30));
        button.setBackground(PRIMARY_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createRaisedBevelBorder());
    }

//    public static void main(String[] args) {
//        new EditUserDialog(0,null);
//    }
}
