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

namespace WorkFlowCheck.Web.Pages.CheckList.CheckListRow
{
    public class CheckListRowEditPageModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly ICheckListService _checkListService;
        private readonly IBaseStockService _baseStockService;


        [BindProperty]
        public CheckListRowDTO CheckListRow { get; set; }

        public CheckListRowEditPageModel(ILogger<IndexModel> logger, ICheckListService checkListService, IBaseStockService baseStockService)
        {
            _checkListService = checkListService;
            _baseStockService = baseStockService;
            _logger = logger;
        }

        public async Task OnGet(int id)
        {
            CheckListRow = await _checkListService.GetCheckListRow(id);
        }

        public async Task<IActionResult> OnPostSave([FromBody] CheckListRowDTO checkListRowDTO)
        {
            try
            {
                var result = await _checkListService.UpdateCheckListRow(checkListRowDTO);
                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 });
        }
    }
}
