public ActionResult Create([Bind(Include = "ID,DateTime")] Appointment appointment, int? requestID)
        {
            if (requestID != null)
            {
                var Request = db.AppointmentRequest
                    .Include(v => v.VehicleUser)
                    .Include(v => v.VehicleUser.User)
                    .Include(v => v.ServiceType)
                    .FirstOrDefault(r => r.ID == requestID);
                ViewBag.AppRequest = Request;
                if (ModelState.IsValid)
                {
                    db.Appointment.Add(appointment);
                    Request.Appointment = appointment;

                    Notification notification = new Notification();
                    notification.Description = "Foi feita uma marcação para o seu pedido do veiculo " +
                        Request.VehicleUser.getVehicleIdentification +
                        " para o serviço " + Request.ServiceType.Name +
                        " para o dia " + appointment.DateTime.ToShortDateString();
                    notification.User = Request.VehicleUser.User;
                    notification.Appointment = appointment;
                    db.Notification.Add(notification);
                    db.SaveChanges();

                    return RedirectToAction("AppointmentList");
                }

                return View(appointment);
            }
            return RedirectToAction("RequestList", new { controller = "AppointmentRequests" });
        }
        public ActionResult Create([Bind(Include = "ID,Description")] AppointmentRequest appointmentRequest, int? servicetype, DateTime startdate, DateTime enddate, int? Vehicle)
        {
            ServiceType selectedServiceType = null;
            VehicleUser selectedVehicle = null;
            if (servicetype != null && Vehicle != null)
            {
                selectedServiceType = db.ServiceType.FirstOrDefault(s => s.ID == servicetype);
                selectedVehicle = db.VehicleUser.FirstOrDefault(v => v.ID == Vehicle);

                if (ModelState.IsValid && startdate != null && enddate != null)
                {
                    if (!String.IsNullOrWhiteSpace(appointmentRequest.Description))
                    {
                        db.AppointmentRequest.Add(appointmentRequest);
                        appointmentRequest.VehicleUser = selectedVehicle;
                        selectedServiceType.AppointmentRequest.Add(appointmentRequest);
                        DateInterval newinterval = new DateInterval();
                        newinterval.InitialDate = startdate;
                        newinterval.EndDate = enddate;
                        db.DateInterval.Add(newinterval);
                        appointmentRequest.AvailableDateInterval = newinterval;
                        Notification notification = new Notification();
                        string userID = User.Identity.GetUserId();
                        var user = db.Users.FirstOrDefault(u => u.Id == userID);
                        notification.Description = "Foi feito um pedido de marcação para o utilizador " +
                            user.UserFullName + " entre as datas " + newinterval.InitialDate.ToShortDateString() + " - " + newinterval.EndDate.ToShortDateString() +
                            " para o serviço " + appointmentRequest.ServiceType.Name;
                        notification.User = user;
                        db.Notification.Add(notification);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewBag.appointmentrequestdescriptionerror = "Não foi colocada nenhuma descrição para o requesito de marcação";
                    }
                }
            }
            if(selectedServiceType != null)
                PopulateServiceTypeDropDownList(selectedServiceType.ID);
            else
                PopulateServiceTypeDropDownList();
            if (selectedVehicle != null)
                PopulateVehicleDropDownList(selectedVehicle.ID);
            else
                PopulateVehicleDropDownList(selectedVehicle.ID);
            return View(appointmentRequest);
        }
        public ActionResult RepairRefuse(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            string UserID = User.Identity.GetUserId();
            var repair = db.VehicleCheckIn
                .Include(v => v.Repair)
                .Include(v => v.Appointment)
                .Include(v => v.Appointment.AppointmentRequest)
                .Include(v => v.Appointment.AppointmentRequest.ServiceType)
                .Include(v => v.VehicleUser)
                .Include(v => v.VehicleUser.User)
                .Include(r => r.Repair.AppliedComponents)
                .FirstOrDefault(a => a.ID == id && a.VehicleUser.User.Id == UserID);
            if(repair == null)
            {
                return RedirectToAction("Index");
            }
            if (repair.Repair.State == State.ToBeVerified)
            {
                repair.Repair.State = State.Refused;
                Notification notification = new Notification();
                notification.Description = "O orçamento para o veiculo "
                    + repair.VehicleUser.getVehicleIdentification +
                    " da marcação do dia " + repair.Appointment.DateTime.ToShortDateString()
                    + " para o serviço " + repair.Appointment.AppointmentRequest.ServiceType.Name
                    + " do utilizador " + repair.VehicleUser.User.UserFullName + " foi recusado";
                notification.User = repair.VehicleUser.User;
                db.Notification.Add(notification);
                db.SaveChanges();
            }

            return RedirectToAction("Details", new { id = id });
        }
        public ActionResult RepairConclude(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("WorkShopList");
            }
            var repair = db.VehicleCheckIn
                .Include(v => v.Repair)
                .Include(v => v.VehicleUser)
                .Include(v => v.VehicleUser.User)
                .Include(v => v.Appointment)
                .Include(v => v.Appointment.AppointmentRequest)
                .Include(v => v.Appointment.AppointmentRequest.ServiceType)
                .Include(r => r.Repair.AppliedComponents)
                .FirstOrDefault(a => a.ID == id);
            if (repair == null)
            {
                return RedirectToAction("WorkShopList");
            }
            if (repair.Repair.State == State.Aproved)
            {
                repair.Repair.State = State.ReadyToTake;
                Notification notification = new Notification();
                notification.Description = "Esta disponivel para levar o veiculo "
                    + repair.VehicleUser.getVehicleIdentification +
                    " da marcação do dia " + repair.Appointment.DateTime.ToShortDateString()
                    + " para o serviço " + repair.Appointment.AppointmentRequest.ServiceType.Name;
                notification.Appointment = repair.Appointment;
                notification.User = repair.VehicleUser.User;
                db.Notification.Add(notification);
                db.SaveChanges();
            }

            return RedirectToAction("RepairDetails", new { id = id });
        }
        public ActionResult RepairAprove(int? id, string[] AppliedComponent)
        {
            if(id == null)
            {
                return RedirectToAction("Index");
            }
            string UserID = User.Identity.GetUserId();
            var repair = db.VehicleCheckIn
                .Include(v => v.Appointment)
                .Include(v => v.Appointment.AppointmentRequest)
                .Include(v => v.Appointment.AppointmentRequest.ServiceType)
                .Include(v => v.Repair)
                .Include(v => v.VehicleUser)
                .Include(v => v.VehicleUser.User)
                .Include(r => r.Repair.AppliedComponents)
                .FirstOrDefault(a => a.ID == id && a.VehicleUser.User.Id == UserID);
            if (repair == null)
            {
                return RedirectToAction("Index");
            }
            if (repair.Repair.State == State.ToBeVerified)
            {
                foreach (var component in repair.Repair.AppliedComponents)
                {
                    component.Accepted = false;
                }
                int cID = 0;
                if (AppliedComponent != null)
                {
                    foreach (string acID in AppliedComponent)
                    {
                        cID = Convert.ToInt32(acID);
                        db.AppliedComponent.FirstOrDefault(a => a.ID == cID).Accepted = true;
                    }
                }
                repair.Repair.State = State.Aproved;
                Notification notification = new Notification();
                notification.Description = "O orçamento para o veiculo "
                    + repair.VehicleUser.getVehicleIdentification +
                    " da marcação do dia " + repair.Appointment.DateTime.ToShortDateString()
                    + " para o serviço " + repair.Appointment.AppointmentRequest.ServiceType.Name
                    + " do utilizador " + repair.VehicleUser.User.UserFullName + " foi aprovado";
                notification.User = repair.VehicleUser.User;
                db.Notification.Add(notification);

                db.SaveChanges();
            }
            return RedirectToAction("Details", new { id = id });
        }
 public ActionResult AddRepair([Bind(Include = "ID,LaborCost,LaborHours")] Repair repair, int? CheckInID)
 {
     if (CheckInID != null)
     {
         var checkin = db.VehicleCheckIn
         .Include(v => v.VehicleUser)
         .Include(v => v.VehicleUser.User)
         .Include(v => v.Appointment)
         .Include(v => v.Appointment.AppointmentRequest)
         .Include(v => v.Appointment.AppointmentRequest.ServiceType)
         .Include(v => v.Repair)
         .FirstOrDefault(v => v.ID == CheckInID);
         if (checkin.Repair == null)
         {
             repair.State = State.ToBeVerified;
             if (ModelState.IsValid)
             {
                 checkin.Repair = repair;
                 db.Repair.Add(repair);
                 Notification notification = new Notification();
                 notification.Description = "Esta disponivel um orçamento para o veiculo "
                     + checkin.VehicleUser.getVehicleIdentification +
                     " da marcação do dia " + checkin.Appointment.DateTime.ToShortDateString()
                     + " para o serviço " + checkin.Appointment.AppointmentRequest.ServiceType.Name;
                 notification.Appointment = checkin.Appointment;
                 notification.User = checkin.VehicleUser.User;
                 db.Notification.Add(notification);
                 db.SaveChanges();
                 return RedirectToAction("RepairDetails", new { id = checkin.ID});
             }
             ViewBag.vehicle = checkin;
             return View(repair);
         }
     }
     return RedirectToAction("WorkShopList");
 }