//
        // GET: /Home/
        public ActionResult Index()
        {
            try
            {
                DateTime date = DateTime.Today.AddDays(7);
                DateTime date2 = DateTime.Now.AddMonths(-1);
                IEnumerable<appointment> appointments = db.appointments.Where(i => i.employeeid == User.Identity.Name && i.starttime >= DateTime.Today && i.starttime <= date);
                IEnumerable<issue> issues = db.issues.Where(i => i.date <= date2 && i.status != "Complete");
                List<Events> Events = new List<Events>();
                foreach (appointment appointment in appointments)
                {
                    Events Event = new Events();
                    Event.id = appointment.appointmentid;
                    Event.title = appointment.subject;
                    Event.allDay = appointment.allday;
                    Event.start = ConvertToUnixTimestamp(appointment.starttime).ToString();
                    Event.end = ConvertToUnixTimestamp(appointment.endtime).ToString();
                    Event.url = Url.Action("Details/" + appointment.appointmentid.ToString());
                    switch (appointment.appointmenttype.Trim())
                    {
                        case "Personal":
                            Event.color = "#009B00";
                            break;

                        case "Advisement":
                            Event.color = "#36C";
                            break;

                        case "Office":
                            Event.color = "#800080";
                            break;
                    }
                    Events.Add(Event);
                }

                List<IssuesPOCO> _issues = new List<IssuesPOCO>();
                foreach (issue i in issues.OrderByDescending(e => e.date))
                {
                    IssuesPOCO temp = new IssuesPOCO();
                    temp.IssueID = i.issueid;
                    temp.Name = i.issuename;
                    temp.Date = ConvertToUnixTimestamp(i.date).ToString();
                    temp.Status = i.status;
                    temp.Urgency = i.urgency;

                    _issues.Add(temp);
                }

                IndexHomeModel model = new IndexHomeModel() { _Events = Events, _Issues = _issues };

                return View(model);
            }
            catch (Exception ex)
            {
                return View();
            }
        }
        //
        // GET: /Student/Details/5
        /// <summary>
        /// Grabs the object by ID and pushes the required objects in the Model (These are the worse comments I know but Im to tired)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(string id)
        {
            try
            {
                if (id != null)
                {
                    IEnumerable<issue> _issue = db.issues.Include("student").Where(i => i.studentid == id);
                    student student = db.students.Include("program").Single(s => s.studentid == id);
                    List<IssuesPOCO> issue = new List<IssuesPOCO>();
                    foreach (issue i in _issue.OrderByDescending(e => e.date))
                    {
                        IssuesPOCO temp = new IssuesPOCO();
                        temp.IssueID = i.issueid;
                        temp.Name = i.issuename;
                        temp.Date = ConvertToUnixTimestamp(i.date).ToString();
                        temp.Status = i.status;
                        temp.Urgency = i.urgency;

                        issue.Add(temp);
                    }

                    DetailsStudentRequestModel details = new DetailsStudentRequestModel() { _student = student, _issue = issue.OrderByDescending(i => i.Status) };
                    return View(details);
                }
                else
                {
                    return RedirectToAction("Index", "Student");
                }
            }
            catch (InvalidOperationException)
            {
                return RedirectToAction("Index", "Student");
            }
        }
        public ActionResult Index(IndexIssueRequestModel Model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var issues = db.issues.Include("student");
                    var program = db.programs;
                    var employee = db.employees;
                    List<String> employees = new List<String>();
                    employees.Add("Any");
                    foreach (employee emp in employee)
                    {
                        employees.Add(emp.employeeid.Trim() + " - " + emp.fname.Trim() + " " + emp.lname.Trim());
                    }
                    List<catagory> catagories = new List<catagory>();
                    catagory cat = new catagory() { catagory1 = "Any" };
                    catagories.Add(cat);
                    catagories.AddRange(db.catagories);
                    List<String> collection = new List<String>();
                    collection.Add("Any");
                    foreach (program prog in program)
                    {
                        collection.Add(prog.programcode.ToString().Trim() + " - " + prog.programname.ToString().Trim());
                    }
                    IEnumerable<issue> _issues = issues;
                    if (Model._name != null)
                        _issues = _issues.Where(i => i.issuename.Trim().ToUpper().Contains(Model._name.Trim().ToUpper()));
                    if (Model._employeeid != "Any")
                    {
                        StringBuilder sb = new StringBuilder(Model._employeeid.Trim().ToUpper());
                        sb.Remove(9, sb.Length - 9);
                        _issues = _issues.Where(i => i.employeeid.Trim().ToUpper().Contains(sb.ToString()));
                    }
                    if (Model._status != "Any")
                        _issues = _issues.Where(i => i.status.Trim().ToUpper().Contains(Model._status.Trim().ToUpper()));
                    if (Model._urgency != "Any")
                        _issues = _issues.Where(i => i.urgency.Trim().ToUpper().Contains(Model._urgency.Trim().ToUpper()));
                    if (Model._category != "Any")
                        _issues = _issues.Where(i => i.catagory.Trim().ToUpper().Contains(Model._category.Trim().ToUpper()));
                    if (Model._selectedpcode != "Any")
                    {
                        StringBuilder sb = new StringBuilder(Model._selectedpcode.Trim().ToUpper());
                        sb.Remove(5, sb.Length - 5);
                        _issues = _issues.Where(i => i.student.programcode.Trim().ToUpper().Contains(sb.ToString()));
                    }
                    if ((Model._date1 != null) && (Model._date2 != null))
                    {
                        if (Model._date1 > Model._date2)
                        {
                            _issues = _issues.Where(i => i.date >= Model._date2 && i.date <= Model._date1);
                        }
                        else if (Model._date2 > Model._date1)
                        {
                            _issues = _issues.Where(i => i.date <= Model._date2 && i.date >= Model._date1);
                        }
                        else
                        {
                            _issues = _issues.Where(i => i.date == Model._date1);
                        }
                    }
                    List<IssuesPOCO> _issue = new List<IssuesPOCO>();
                    foreach (issue i in _issues.OrderByDescending(e => e.date))
                    {
                        IssuesPOCO temp = new IssuesPOCO();
                        temp.IssueID = i.issueid;
                        temp.Name = i.issuename;
                        temp.Date = ConvertToUnixTimestamp(i.date).ToString();
                        temp.Status = i.status;
                        temp.Urgency = i.urgency;

                        _issue.Add(temp);
                    }
                    Model._issue = _issue;
                    Model._employee = employees;
                    Model._catagories = catagories;
                    Model._programcode = collection;

                    return View(Model);
                }
                return View(Model);
            }
            catch (Exception ex)
            {
                return View(Model);
            }
        }
        //
        // GET: /Issue/
        /// <summary>
        /// Ensures tha field have the proper information
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            try
            {
                var issues = db.issues.Include("student");
                List<IssuesPOCO> _issues = new List<IssuesPOCO>();
                foreach (issue i in issues.OrderByDescending(e => e.date))
                {
                    IssuesPOCO temp = new IssuesPOCO();
                    temp.IssueID = i.issueid;
                    temp.Name = i.issuename;
                    temp.Date = ConvertToUnixTimestamp(i.date).ToString();
                    temp.Status = i.status;
                    temp.Urgency = i.urgency;

                    _issues.Add(temp);
                }
                var program = db.programs;
                var employee = db.employees;
                List<String> employees = new List<String>();
                employees.Add("Any");
                foreach (employee emp in employee)
                {
                    employees.Add(emp.employeeid.Trim() + " - " + emp.fname.Trim() + " " + emp.lname.Trim());
                }
                List<catagory> catagories = new List<catagory>();
                catagory cat = new catagory() { catagory1 = "Any" };
                catagories.Add(cat);
                catagories.AddRange(db.catagories);
                List<String> collection = new List<String>();
                collection.Add("Any");
                foreach (program prog in program)
                {
                    collection.Add(prog.programcode.ToString().Trim() + " - " + prog.programname.ToString().Trim());
                }
                IndexIssueRequestModel Model = new IndexIssueRequestModel() { _issue = _issues.OrderByDescending(i => i.Status), _employee = employees, _catagories = catagories, _date1 = issues.OrderByDescending(i => i.date).First().date, _date2 = issues.OrderBy(i => i.date).First().date, _programcode = collection };
                return View(Model);
            }
            catch (Exception ex)
            {
                return View();
            }
        }