using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace sotd.Pages;

public class UserModel : PageModel
{
    private readonly ILogger<UserModel> _logger;

    public UserModel(ILogger<UserModel> logger)
    {
        _logger = logger;
        this.UserNickName = "";
        this.UserName = "";
    }

    public int userId { get; set; }

    public string UserName { get; set; }

    [BindProperty]
    public string UserNickName { get; set; }

    public void OnGet(int userIndex)
    {
        using (var dci = DataContext.Instance)
        {
            var user = dci.Users.Find(userIndex);
            this.UserName = user.Name;
            this.UserNickName = user.NickName;
            this.userId = userIndex;
        }
    }

    public void OnPost(int userIndex)
    {
        using (var dci = DataContext.Instance)
        {
            var user = dci.Users.Find(userIndex);
            user.NickName = this.UserNickName;
            dci.SaveChanges();
            this.UserName = user.Name;
        }
    }
}
