using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;

namespace WorkFlowCheck.Web.Pages.UserAndRole
{
    public class RoleEditPageModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IUserService _userService;

        [BindProperty]
        public RoleDTO RoleDTO { get; set; }

        public RoleEditPageModel(ILogger<IndexModel> logger, IUserService userService)
        {
            _userService = userService;
            _logger = logger;
        }

        public async Task OnGet(int id)
        {
            RoleDTO = await _userService.GetRole(id);
        }
        public async Task<IActionResult> OnPostSave([FromBody] RoleDTO roleDTO)
        {
            try
            {
                var result = await _userService.UpdateRole(roleDTO);
                if (result.IsSuccess)
                {
                    return new JsonResult(new { success = true });
                }
                else
                {
                    return new JsonResult(new { success = false });
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }

            return new JsonResult(new { success = false });
        }
    }
}
