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 FakturyWeb.Data;
using RazorPagesMovie.Models;

namespace FakturyWeb.Pages.Faktury
{
    public class EditModel : PageModel
    {
        private readonly FakturyWeb.Data.FakturyWebContext _context;

        public EditModel(FakturyWeb.Data.FakturyWebContext context)
        {
            _context = context;
        }

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

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

            FAKTURA = await _context.FAKTURA
                .Include(f => f.ZAKAZNIK).FirstOrDefaultAsync(m => m.ID == id);

            if (FAKTURA == null)
            {
                return NotFound();
            }
           ViewData["ZakaznikID"] = new SelectList(_context.ZAKAZNIK, "ID", "ID");
            return Page();
        }

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

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FAKTURAExists(FAKTURA.ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./Index");
        }

        private bool FAKTURAExists(int id)
        {
            return _context.FAKTURA.Any(e => e.ID == id);
        }
    }
}
