// GET: Expenses/Create
      public IActionResult Create()
      {
          //create an instance of the ExpenseCreateViewModel to get the list of ExpenseTypes in dropdown
          ExpenseAndTypeViewModel ViewModel = new ExpenseAndTypeViewModel();

          //then use the view model rather than view data for more flexibility
          ViewModel.expenseTypes = _context.ExpenseTypes.Select(c => new SelectListItem
            {
                Text  = c.Label,
                Value = c.ExpenseTypeId.ToString()
            }
                                                                ).ToList();

          //Forces user to select from the drop down
          //Error displays if not due to data annotation on product type model
          ViewModel.expenseTypes.Insert(0, new SelectListItem()
            {
                Value = "0", Text = "--Select Deduction Category--"
            });

          return(View(ViewModel));

          //ViewData["ExpenseTypeId"] = new SelectList(_context.ExpeseTypes, "ExpenseTypeId", "Label");
          //ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id");
          //return View();
      }
      public async Task <IActionResult> Create([Bind("ExpenseId,ExpenseTypeId,Total,DateTime,ImageFile,UserId")] Expense expense)
      {
          //adding user information automatically rather than in the form
          ModelState.Remove("expense.User");
          ModelState.Remove("expense.UserId");

          ExpenseAndTypeViewModel ViewModel = new ExpenseAndTypeViewModel();

          //Forced user to upload a file by making ImageFile Required with an error message in model

          //saving image to wwwRoot/receipt

          string wwwRootPath = _hostEnvironment.WebRootPath;
          string fileName    = Path.GetFileNameWithoutExtension(expense.ImageFile.FileName);
          string extension   = Path.GetExtension(expense.ImageFile.FileName);

          expense.ImagePath = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
          string path = Path.Combine(wwwRootPath + "/receipt/", fileName);

          using (var fileStream = new FileStream(path, FileMode.Create))
          {
              await expense.ImageFile.CopyToAsync(fileStream);
          }

          if (ModelState.IsValid)
          {
              //add user to get the user information
              var user = await GetCurrentUserAsync();

              expense.UserId = user.Id;
              _context.Add(expense);
              await _context.SaveChangesAsync();

              //redirects to expense details view
              return(RedirectToAction("Details", new
                {
                    id = expense.ExpenseId
                }));
          }
          ViewModel.expenseTypes = _context.ExpenseTypes.Select(c => new SelectListItem
            {
                Text  = c.Label,
                Value = c.ExpenseTypeId.ToString()
            }).ToList();
          return(View(ViewModel));
          //ViewData["ExpenseTypeId"] = new SelectList(_context.ExpeseTypes, "ExpenseTypeId", "Label", expense.ExpenseTypeId);
          //ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", expense.UserId);
          //return View(expense);
      }
      // GET: Expenses/Edit/5
      public async Task <IActionResult> Edit(int?id)
      {
          ApplicationUser loggedInUser = await GetCurrentUserAsync();

          ExpenseAndTypeViewModel ViewModel = new ExpenseAndTypeViewModel();

          if (id == null)
          {
              return(NotFound());
          }
          //trying to force edit form to show picture already uploaded
          //var imagePath = Path.Combine(_hostEnvironment.WebRootPath, "receipt", ViewModel.expense.ImagePath);
          // if (System.IO.File.Exists(imagePath))
          //figure out how to turn imagefile back into filepath
          //original code below
          ViewModel.expense = await _context.Expenses

                              .Include(i => i.User)
                              .Where(expense => expense.UserId == loggedInUser.Id)
                              // .FindAsync(id);
                              .FirstOrDefaultAsync(m => m.ExpenseId == id);



          if (ViewModel.expense == null)
          {
              return(NotFound());
          }
          ViewModel.expenseTypes = _context.ExpenseTypes.Select(c => new SelectListItem
            {
                Text  = c.Label,
                Value = c.ExpenseTypeId.ToString()
            }
                                                                ).ToList();
          // ViewModel.expense.ImageFile = imageFile;
          //ViewData["ExpenseTypeId"] = new SelectList(_context.ExpenseTypes, "ExpenseTypeId", "Label", expense.ExpenseTypeId);
          //ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", expense.UserId);
          return(View(ViewModel));
      }
      public async Task <IActionResult> Edit(int id, [Bind("ExpenseId,ExpenseTypeId,Total,DateTime,ImageFile,UserId")] Expense expense)
      {
          ModelState.Remove("expense.User");
          ModelState.Remove("expense.UserId");
          ExpenseAndTypeViewModel ViewModel = new ExpenseAndTypeViewModel();

          //saving image to wwwRoot/receipt


          if (id != expense.ExpenseId)
          {
              return(NotFound());
          }

          string wwwRootPath = _hostEnvironment.WebRootPath;
          string fileName    = Path.GetFileNameWithoutExtension(expense.ImageFile.FileName);
          string extension   = Path.GetExtension(expense.ImageFile.FileName);

          expense.ImagePath = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
          string path = Path.Combine(wwwRootPath + "/receipt/", fileName);

          using (var fileStream = new FileStream(path, FileMode.Create))
          {
              await expense.ImageFile.CopyToAsync(fileStream);
          }


          if (ModelState.IsValid)
          {
              //string oldFilePath = ViewModel.expense.ImagePath;
              try
              {
                  var user = await GetCurrentUserAsync();

                  expense.UserId = user.Id;
                  _context.Update(expense);
                  await _context.SaveChangesAsync();
              }
              catch (DbUpdateConcurrencyException)
              {
                  if (!ExpenseExists(expense.ExpenseId))
                  {
                      return(NotFound());
                  }
                  else
                  {
                      throw;
                  }
              }
              return(RedirectToAction("Details", new
                {
                    id = expense.ExpenseId
                }));
          }
          ViewModel.expenseTypes = _context.ExpenseTypes.Select(c => new SelectListItem
            {
                Text  = c.Label,
                Value = c.ExpenseTypeId.ToString()
            }).ToList();
          //ViewData["ExpenseTypeId"] = new SelectList(_context.ExpenseTypes, "ExpenseTypeId", "Label", expense.ExpenseTypeId);
          //ViewData["UserId"] = new SelectList(_context.ApplicationUsers, "Id", "Id", expense.UserId);
          return(View(ViewModel));
      }