package com.example.struts2;

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理 Action - CRUD 示例
 * 
 * 学习点：
 * - Session 范围数据
 * - 多个处理方法
 * - 动态方法调用
 */
public class UserFormAction extends ActionSupport {
    private String actionName;
    
    private static List<User> users = new ArrayList<>();
    private static Long idCounter = 1L;
    
    private User user = new User();
    private Long id;
    private List<User> userList;
    
    static {
        users.add(new User(idCounter++, "张三", "zhangsan@example.com", 25));
        users.add(new User(idCounter++, "李四", "lisi@example.com", 30));
        users.add(new User(idCounter++, "王五", "wangwu@example.com", 28));
    }
    
    // 列表
    public String list() {
        userList = new ArrayList<>(users);
        return SUCCESS;
    }
    
    // 添加
    public String add() {
        user.setId(idCounter++);
        users.add(user);
        return SUCCESS;
    }
    
    // 编辑页面
    public String edit() {
        for (User u : users) {
            if (u.getId().equals(id)) {
                user = u;
                break;
            }
        }
        return SUCCESS;
    }
    
    // 更新
    public String update() {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getId().equals(user.getId())) {
                users.set(i, user);
                break;
            }
        }
        return SUCCESS;
    }
    
    // 删除
    public String delete() {
        users.removeIf(u -> u.getId().equals(id));
        return SUCCESS;
    }
    
    @Override
    public void validate() {
        if (!("user_add".equals(actionName) || "user_update".equals(actionName))) {
            return;
        }
        if (user == null) {
            return;
        }
        if (user.getName() == null || user.getName().trim().isEmpty()) {
            addFieldError("user.name", "姓名不能为空");
        }
        if (user.getEmail() == null || !user.getEmail().contains("@")) {
            addFieldError("user.email", "邮箱格式不正确");
        }
        if (user.getAge() == null || user.getAge() < 1 || user.getAge() > 120) {
            addFieldError("user.age", "年龄必须在 1 到 120 之间");
        }
    }

    // Getters and Setters
    public User getUser() { return user; }
    public void setUser(User user) { this.user = user; }
    
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    
    public List<User> getUserList() { return userList; }
    public void setUserList(List<User> userList) { this.userList = userList; }
    public String getActionName() { return actionName; }
    public void setActionName(String actionName) { this.actionName = actionName; }
    
    // 内部类 - 用户实体
    public static class User {
        private Long id;
        private String name;
        private String email;
        private Integer age;
        
        public User() {}
        
        public User(Long id, String name, String email, Integer age) {
            this.id = id;
            this.name = name;
            this.email = email;
            this.age = age;
        }
        
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }
        public Integer getAge() { return age; }
        public void setAge(Integer age) { this.age = age; }
    }
}