using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Catalog26.Data;
using PricingCatalog.Models;

namespace Catalog26.Pages.TDSynnex;

public class EditModel : PageModel
{
    private readonly Catalog26.Data.Catalog26Context _context;

    public EditModel(Catalog26.Data.Catalog26Context context)
    {
        _context = context;
    }

    [BindProperty]
    public TDSPriceAvailability TDSPriceAvailability { get; set; } = default!;

    public async Task<IActionResult> OnGetAsync(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var tdspriceavailability =  await _context.TDSCatalogItems.FirstOrDefaultAsync(m => m.Id == id);
        if (tdspriceavailability == null)
        {
            return NotFound();
        }
        TDSPriceAvailability = tdspriceavailability;
        return Page();
    }

    // To protect from overposting attacks, enable the specific properties you want to bind to.
    // For more information, see https://aka.ms/RazorPagesCRUD.
    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Attach(TDSPriceAvailability).State = EntityState.Modified;

        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!TDSPriceAvailabilityExists(TDSPriceAvailability.Id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return RedirectToPage("./Index");
    }

    private bool TDSPriceAvailabilityExists(int id)
    {
        return _context.TDSCatalogItems.Any(e => e.Id == id);
    }
}
