//GET: /Proyecto/AsignarProfesorProyecto/5
        public ActionResult AsignarProfesorProyecto(int? id)
        {
            if (Request.UrlReferrer != null)
            {
                ViewBag.returnUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.returnUrl = null;
            }

            Project project = db.Projects.Find(id);
            ScheduleProjectViewModel projectViewModel = new ScheduleProjectViewModel();
            projectViewModel.Projects = project.Name;

            ViewBag.Professors = new SelectList(db.Professors.OrderBy(p => p.Name), "ID", "Name");
            ViewBag.ProjectID = project.ID.ToString();
            return View(projectViewModel);
        }
        public ActionResult AsignarProfesorProyecto(ScheduleProjectViewModel pSchedule)
        {
            var vPeriod = Request.Cookies["Periodo"].Value;
            var vPeriodID = db.Periods.Find(int.Parse(vPeriod)).ID;

            string vHourCharge = pSchedule.HourCharge;
            int vProjectID = Convert.ToInt32(pSchedule.Projects);
            int vProfessorID = Convert.ToInt32(pSchedule.Professors);

            List<ScheduleProject> vSchedules = pSchedule.ScheduleProject;

            string validate = dbHelper.validationProject(vProjectID,vProfessorID,vPeriodID,vSchedules);
            if(validate.Equals("true"))
            {
                int totalHourAssign = 0;

                //Save Projectvbg Professor
                ProjectXProfessor vProjectProfessor = new ProjectXProfessor();
                vProjectProfessor.ProjectID = Convert.ToInt32(vProjectID);
                vProjectProfessor.ProfessorID = Convert.ToInt32(vProfessorID);
                if (vHourCharge.Equals("1"))
                {
                    vProjectProfessor.HourAllocatedTypeID = Convert.ToInt32(vHourCharge);
                }
                vProjectProfessor.PeriodID = vPeriodID;
                vProjectProfessor.Schedule = new List<Schedule>();

                //Calculate the total hour assign
                foreach (ScheduleProject vSchedule in vSchedules)
                {
                    Schedule vTempSchedule = existSchedule(vSchedule.Day, vSchedule.StartHour, vSchedule.EndHour);
                    if (vTempSchedule != null)
                    {
                        //Get id schedule

                        vTempSchedule.ProjectsXProfessors.Add(vProjectProfessor);

                    }

                    //Convert StartHour to DateTime
                    var vStartHour = DateTime.Parse(vSchedule.StartHour);
                    var vEndHour = DateTime.Parse(vSchedule.EndHour);

                    var vSTRDiffHours = Math.Ceiling(vEndHour.Subtract(vStartHour).TotalHours);

                    int vDiffHours = Convert.ToInt32(vSTRDiffHours);

                    totalHourAssign = totalHourAssign + vDiffHours;
                }

                vProjectProfessor.Hours = totalHourAssign;

                db.ProjectsXProfessors.Add(vProjectProfessor);

                db.SaveChanges();
                TempData[TempDataMessageKeySuccess] = "El profesor fue asignado correctamente.";

                return RedirectToAction("Details", new { id = vProjectID });
            }
            else if(validate.Equals("falseIsGroupShock"))
            {
                TempData[TempDataMessageKeyError] = "Existe choque de horario con grupos, no se asigno al profesor al proyecto";
                return RedirectToAction("AsignarProfesorProyecto");
            }
            else if(validate.Equals("falseIsProjectShock"))
            {
                TempData[TempDataMessageKeyError] = "Existe choque de horario con proyectos, no se asigno al profesor al proyecto";
                return RedirectToAction("AsignarProfesorProyecto");
            }
            else if(validate.Equals("falseIsCommissionShock"))
            {
                TempData[TempDataMessageKeyError] = "Existe choque de horario con comisiones, no se asigno al profesor al proyecto";
                return RedirectToAction("AsignarProfesorProyecto");
            }
            else if (validate.Equals("falseIsProfessorShock"))
            {
                TempData[TempDataMessageKeyError] = "El profesor ya esta asignado a este proyecto, no se permite asignar dos veces a un profesor a un proyecto ";
                return RedirectToAction("AsignarProfesorProyecto");
            }
            return View();
        }
        public ActionResult EditarAsignacion(ScheduleProjectViewModel pSchedule)
        {
            var vPeriod = Request.Cookies["Periodo"].Value;
            var vPeriodID = db.Periods.Find(int.Parse(vPeriod)).ID;

            var vProjectXProfessorID = Convert.ToInt32(pSchedule.Projects);
            var vHourChange = Convert.ToInt32(pSchedule.HourCharge);
            var vProfessorID = Convert.ToInt32(pSchedule.Professors);

            if (ModelState.IsValid)
            {

                ProjectXProfessor vProjectXProfessor = db.ProjectsXProfessors.Find(vProjectXProfessorID);
                var vProjectID = vProjectXProfessor.ProjectID;

                List<ScheduleProject> vSchedules = pSchedule.ScheduleProject;
                string validate = dbHelper.validationsEditProject(vProjectID, vProfessorID,vPeriodID, vSchedules);
                if (validate.Equals("true"))
                {
                    vProjectXProfessor.ProfessorID = vProfessorID;
                    vProjectXProfessor.HourAllocatedTypeID = Convert.ToInt32(vHourChange);
                    var totalHourAssign = 0;
                    vProjectXProfessor.Schedule.Clear();

                    //Calculate the total hour assign
                    foreach (ScheduleProject vSchedule in vSchedules)
                    {
                        Schedule vTempSchedule = existSchedule(vSchedule.Day, vSchedule.StartHour, vSchedule.EndHour);

                        if (vTempSchedule != null)
                        {
                            //Get id schedule

                            vTempSchedule.ProjectsXProfessors.Add(vProjectXProfessor);
                            vProjectXProfessor.Schedule.Add(vTempSchedule);
                        }

                        //Convert StartHour to DateTime
                        var vStartHour = DateTime.Parse(vSchedule.StartHour);
                        var vEndHour = DateTime.Parse(vSchedule.EndHour);

                        var CargaC = Math.Ceiling(vEndHour.Subtract(vStartHour).TotalHours);

                        int vDiferencia = Convert.ToInt32(CargaC);

                        totalHourAssign = totalHourAssign + vDiferencia;
                    }

                    vProjectXProfessor.Hours = totalHourAssign;
                    db.SaveChanges();

                    TempData[TempDataMessageKeySuccess] = "Profesor asignado correctamente";
                    return RedirectToAction("Details", new { id = vProjectXProfessor.ProjectID });
                }
                else if (validate.Equals("falseIsProjectShock"))
                    TempData[TempDataMessageKeyError] = "Existe choque de horario con proyectos, no se asigno al profesor al proyecto";

                else if (validate.Equals("falseIsGroupShock"))
                    TempData[TempDataMessageKeyError] = "Existe choque de horario con grupos, no se asigno al profesor al proyecto";

                else if (validate.Equals("falseIsCommissionShock"))
                    TempData[TempDataMessageKeyError] = "Existe choque de horario con comisiones, no se asigno al profesor a la comisión";

                /* Get the list of professor related with project */
                if (Request.UrlReferrer != null)
                {
                    ViewBag.returnUrl = Request.UrlReferrer.ToString();
                }
                else
                {
                    ViewBag.returnUrl = null;
                }

                var ProjectsXProfessors = db.ProjectsXProfessors.Find(vProjectXProfessorID);
                var projectID = ProjectsXProfessors.ProjectID;
                Project project = db.Projects.Find(projectID);
                ScheduleProjectViewModel projectViewModel = new ScheduleProjectViewModel();
                projectViewModel.Projects = project.Name;

                ViewBag.Professors = new SelectList(db.Professors, "ID", "Name");
                ViewBag.ProjectID = projectID.ToString();
                return View(projectViewModel);
            }

            return View();
        }
        //GET: Comision / EditarAsignacion/5
        public ActionResult EditarAsignacion(int id)
        {
            if (Request.UrlReferrer != null)
            {
                ViewBag.returnUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                ViewBag.returnUrl = null;
            }
            var project_professor = db.ProjectsXProfessors.Find(id);
            var projectID = project_professor.ProjectID;

            Project project = db.Projects.Find(projectID);

            ScheduleProjectViewModel projectViewModel = new ScheduleProjectViewModel();
            projectViewModel.Projects = project.Name;

            ViewBag.Professors = new SelectList(db.Professors, "ID", "Name");
            ViewBag.ProjectID = projectID.ToString();
            return View(projectViewModel);
        }