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

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

    public CreateModel(FakturyWeb.Data.FakturyWebContext context)
    {
        _context = context;
        FAKTURA = new FAKTURA();
        FAKTURA.Init(_context);
    }

    public IActionResult OnGet()
    {
      ViewData["Zakaznici"] = new SelectList(_context.ZAKAZNIK, "ID", "PRAJM");

      ViewData["FormyUhrady"] = new SelectList(Ciselniky.CreateFormaUhradyDictionary(), "Key", "Value");

      return Page();
    }

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

    // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.FAKTURA.Add(FAKTURA);
        await _context.SaveChangesAsync();

        return RedirectToPage("./Index");
    }

     
    public ActionResult OnGetZakaznik(string id)
    {
      List<ZAKAZNIK> myZakaznik = _context.ZAKAZNIK.Where(fruit => fruit.ID == Convert.ToInt32(id)).ToList();
      return new JsonResult(myZakaznik);
    }
  }
}
