public async Task <IActionResult> Create([Bind("ApptId,ApptDate,AppointmentReason,PatientEmail,PatientId,VisitRecord,DoctorId")] Appointment appointment)
        {
            if (ModelState.IsValid)
            {
                var findbyemail = _context.Appointment.Where(c => c.PatientEmail == User.Identity.Name).Count();

                //If the email is found, reject creating the appointment and redirect to the page that says unsuccessful

                if (findbyemail != 0)
                {
                    return(View("AppointmentExists"));
                }
                //Add the user to misused user database

                if (User.IsInRole("Patient"))
                {
                    if (!UserExists(User.Identity.Name))
                    {
                        var newuser = new MisusedUser {
                            UserEmail = User.Identity.Name, Misusedcount = 0
                        };
                        _context.MisusedUser.Add(newuser);
                        await _context.SaveChangesAsync();
                    }
                }
                _context.Add(appointment);
                await _context.SaveChangesAsync();

                //Remove from available time
                var appointment_context = _context.DoctorAvailability.Where(a => a.AvailableTime == appointment.ApptDate && a.DoctorId == appointment.DoctorId).FirstOrDefault();

                _context.DoctorAvailability.Remove(appointment_context);
                await _context.SaveChangesAsync();

                //Add to unavailable time

                var addtime = new DoctorUnavailability {
                    DoctorId = appointment.DoctorId, Unavailable = appointment.ApptDate
                };
                _context.DoctorUnavailability.Add(addtime);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DoctorId"]  = new SelectList(_context.Doctor, "DoctorId", "DoctorName", appointment.DoctorId);
            ViewData["PatientId"] = new SelectList(_context.Patient, "PatientId", "PatientEmail", appointment.PatientId);
            return(View(appointment));
        }
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var appointment = await _context.Appointment.FindAsync(id);


            var appointment1 = from p in _context.Appointment
                               where p.ApptId == id
                               select p;
            var apptid = appointment1.ToList();


            var misusecount = from p in _context.MisusedUser
                              where p.UserEmail == User.Identity.Name
                              select p.Misusedcount;
            int count = misusecount.FirstOrDefault();

            //Add the user to misused user database

            if (User.IsInRole("Patient"))
            {
                if (!UserExists(User.Identity.Name))
                {
                    var newuser = new MisusedUser {
                        UserEmail = User.Identity.Name, Misusedcount = 0
                    };
                    _context.MisusedUser.Add(newuser);
                    await _context.SaveChangesAsync();
                }
            }
            //Update misuse count or deny permission to cancel appointment based on misuse count.
            if (User.IsInRole("Patient"))
            {
                if (count > 5)
                {
                    return(View("PermissionDenied"));
                }
                else
                {
                    count += 1;
                    var findemail = await _context.MisusedUser.FindAsync(User.Identity.Name);

                    findemail.Misusedcount = count;
                    await _context.SaveChangesAsync();
                }
            }


            foreach (var x in apptid)
            {
                //Remove from unvailable time
                var appointment_context = _context.DoctorUnavailability.Where(a => a.Unavailable == x.ApptDate && a.DoctorId == x.DoctorId).FirstOrDefault();
                _context.DoctorUnavailability.Remove(appointment_context);
                await _context.SaveChangesAsync();

                //Add to available time

                var addtime = new DoctorAvailability {
                    DoctorId = x.DoctorId, AvailableTime = x.ApptDate
                };
                _context.DoctorAvailability.Add(addtime);
                await _context.SaveChangesAsync();
            }



            _context.Appointment.Remove(appointment);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }