public async Task <IActionResult> Edit(int?id)
        {
            int TableOrganizations = _context.TableOrganizations.Include(i => i.users).FirstOrDefault
                                         (i => User.Identity.Name == i.users.UserName).TableOrganizationsId;

            VerificationOfEducationViewModel verificationOfEducationViewModel = new VerificationOfEducationViewModel();

            if (id != null)
            {
                ReserveOfPersonnel reserve = await _context.reserveOfPersonnels
                                             .Include(p => p.employeeRegistrationLog)
                                             .Include(p => p.tablePosition)
                                             .Include(p => p.employeeRegistrationLog.Worker)
                                             .FirstOrDefaultAsync(p => p.ReserveId == id);

                ReserveOfPersonnelViewModel reserveOfPersonnelViewModel = new ReserveOfPersonnelViewModel
                {
                    ReserveId                 = reserve.ReserveId,
                    TablePositionId           = reserve.TablePositionId,
                    EmployeeRegistrationLogId = reserve.EmployeeRegistrationLogId
                };

                if (reserve != null)
                {
                    return(View(reserveOfPersonnelViewModel));
                }
            }
            return(NotFound());
        }
        public async Task <IActionResult> Edit(ReserveOfPersonnelViewModel model)
        {
            ReserveOfPersonnel reserve = await _context.reserveOfPersonnels
                                         .FirstOrDefaultAsync(p => p.ReserveId == model.ReserveId);

            var TablePosition = _context.TablePosition.Find(reserve.TablePositionId);


            reserve.StatusReserve  = "Назначен на должность";
            reserve.EndDateReserve = DateTime.Now;
            TableHistoryOfAppointments VacantPosition = await _context.TableHistoryOfAppointments
                                                        .Where(p => p.DateOfDismissal == null)
                                                        .FirstOrDefaultAsync(p => p.TablePositionId == model.TablePositionId);

            if (TablePosition.CountPosition == 0 && VacantPosition == null)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                var СurrentЗosition = await _context.TableHistoryOfAppointments
                                      .Where(p => p.DateOfDismissal == null)
                                      .FirstOrDefaultAsync(p => p.EmployeeRegistrationLogId == reserve.EmployeeRegistrationLogId)
                ;

                if (СurrentЗosition != null)
                {
                    var tableposition = await _context.TablePosition.FirstOrDefaultAsync(p => p.TablePositionId == СurrentЗosition.TablePositionId);

                    tableposition.CountPosition = tableposition.CountPosition + 1;
                    СurrentЗosition.TheReasonForTheDismissal = "Перевод на другую должность";
                    СurrentЗosition.DateOfDismissal          = DateTime.Now;
                    _context.TableHistoryOfAppointments.Update(СurrentЗosition);
                    _context.TablePosition.Update(tableposition);
                    await _context.SaveChangesAsync();
                }
                TableHistoryOfAppointments historyofappointments = new TableHistoryOfAppointments
                {
                    DateOfAppointment         = DateTime.Now,
                    TablePositionId           = reserve.TablePositionId,
                    EmployeeRegistrationLogId = reserve.EmployeeRegistrationLogId,
                };
                TablePosition.CountPosition = TablePosition.CountPosition - 1;

                _context.TableHistoryOfAppointments.Add(historyofappointments);
                _context.reserveOfPersonnels.Update(reserve);
                _context.TablePosition.Update(TablePosition);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }
        public async Task <IActionResult> Delete(int?id)
        {
            if (id != null)
            {
                ReserveOfPersonnel reserve = await _context.reserveOfPersonnels.FirstOrDefaultAsync(p => p.ReserveId == id);

                if (reserve != null)
                {
                    _context.reserveOfPersonnels.Remove(reserve);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            return(NotFound());
        }
        public async Task <IActionResult> Remove(int?id)
        {
            if (id != null)
            {
                ReserveOfPersonnel reserve = await _context.reserveOfPersonnels.FirstOrDefaultAsync(p => p.ReserveId == id);

                if (reserve != null)
                {
                    reserve.StatusReserve  = "Выведен из резерва";
                    reserve.EndDateReserve = DateTime.Now;
                    _context.reserveOfPersonnels.Update(reserve);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            return(NotFound());
        }
        public async Task <IActionResult> Create(ReserveOfPersonnelViewModel model)
        {
            try
            {
                int TableOrganizations = _context.TableOrganizations.Include(i => i.users).FirstOrDefault
                                             (i => User.Identity.Name == i.users.UserName).TableOrganizationsId;
                TableHistoryOfAppointments historyOfAppointments = await _context.TableHistoryOfAppointments
                                                                   .Where(p => p.TablePositionId == model.TablePositionId)
                                                                   .Where(p => p.DateOfDismissal == null)
                                                                   .FirstOrDefaultAsync(p => p.EmployeeRegistrationLogId == model.EmployeeRegistrationLogId);

                if (historyOfAppointments == null)
                {
                    if (ModelState.IsValid)
                    {
                        ReserveOfPersonnel reserveOfPersonnel = new ReserveOfPersonnel
                        {
                            StatusReserve             = "В резерве",
                            TablePositionId           = model.TablePositionId,
                            StartDateReserve          = DateTime.Now,
                            EmployeeRegistrationLogId = model.EmployeeRegistrationLogId
                        };
                        await _context.reserveOfPersonnels.AddAsync(reserveOfPersonnel);

                        await _context.SaveChangesAsync();

                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    return(RedirectToAction("Create"));
                }
                return(View(model));
            }
            catch
            {
                return(RedirectToAction("Create"));
            }
        }