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

namespace WorkFlowCheck.Web.Pages.BaseStock.Locations
{
    public class LocationEditPageModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IBaseStockService _baseStockService;

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

        public LocationEditPageModel(ILogger<IndexModel> logger, IBaseStockService baseStockService)
        {
            _baseStockService = baseStockService;
            _logger = logger;
        }

        public async Task OnGet(int id)
        {
            LocationDTO = await _baseStockService.GetLocation(id);
        }
        public async Task<IActionResult> OnPostSave([FromBody] LocationDTO LocationDTO)
        {
            try
            {
                var result = await _baseStockService.UpdateLocation(LocationDTO);
                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 });
        }
    }
}
