public ActionResult UpdateChecklist(RepairStatusView repair, string upc)
        {
            Trello t     = new Models.Trello();
            var    cards = t.GetCards(SessionVariables.CurrentLocation.ToString());

            foreach (var card in cards)
            {
                if (card.name == upc)
                {
                    var checklists = t.GetChecklists(card.id);
                    var checklist  = checklists.Last().checkItems;
                    // check if checklist item was changed
                    for (var i = 0; i < checklist.Count(); i++)
                    {
                        if ((repair.Checklist[i].state == true && checklist[i].state == "incomplete") || (repair.Checklist[i].state == false && checklist[i].state == "complete"))
                        {
                            var newState = "incomplete";
                            if (repair.Checklist[i].state == true)
                            {
                                newState = "complete";
                            }
                            var result = t.PutChangeChecklistItem(card.id, checklist[i].id, newState);
                        }
                        if (repair.Checklist[i].delete == true)
                        {
                            t.DeleteChecklistItem(card.id, checklist[i].id);
                        }
                    }
                }
            }


            return(RedirectToAction("Index"));
        }
        // updates the due date on an open repair
        public ActionResult UpdateRepairAddComment(string itemUpc, string itemLoc, string newComment)
        {
            Trello t      = new Models.Trello();
            var    cards  = t.GetCards(itemLoc);
            string cardId = null;

            foreach (var card in cards)
            {
                if (card.name == itemUpc)
                {
                    cardId = card.id;
                    continue;
                }
            }

            if (cardId != null)
            {
                var user = db.tb_CSULabTechs.FirstOrDefault(m => m.ENAME == SessionVariables.CurrentUserId);
                newComment = newComment + "  -- Posted by: " + user.First_Name + " " + user.Last_Name + " (" + DateTime.Now.ToString() + ")";
                var date = t.PostCardComment(cardId, newComment);

                TempData["message"] = "Added new comment for Item #" + itemUpc + ".";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["message"] = "There was an error locating the repair history for Item #" + itemUpc + ". Please try again.";
                return(RedirectToAction("Index"));
            }
        }
        // updates the due date on an open repair
        public ActionResult UpdateRepairDueDate(string itemUpc, string itemLoc, string newDueDate)
        {
            // convert the string to a DateTimeOffset type
            var newDate = DateTime.Parse(newDueDate).Date;
            //string x = null;

            Trello t      = new Models.Trello();
            var    cards  = t.GetCards(itemLoc);
            string cardId = null;

            foreach (var card in cards)
            {
                if (card.name == itemUpc)
                {
                    cardId = card.id;
                    continue;
                }
            }

            if (cardId != null)
            {
                var date = t.PutNewDueDate(cardId, newDate);

                TempData["message"] = "Updated due date for Item #" + itemUpc + " to " + newDueDate;
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["message"] = "There was an error locating the repair history for Item #" + itemUpc + ". Please try again.";
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult AddItemToChecklist(string upc, string newCheckItem)
        {
            Trello t     = new Models.Trello();
            var    cards = t.GetCards(SessionVariables.CurrentLocation.ToString());

            foreach (var card in cards)
            {
                if (card.name == upc)
                {
                    var checklists = t.GetChecklists(card.id);
                    //var checklist = checklists.First().checkItems;
                    var result = t.PostCardChecklistItem(checklists.Last().id, newCheckItem);
                }
            }

            return(RedirectToAction("UpdateRepair", new { upc = upc }));
        }
        // opens a view to updates an individual repair
        public ActionResult UpdateRepair(string upc)
        {
            RepairStatusView item = new RepairStatusView();
            Trello           t    = new Models.Trello();
            var cards             = t.GetCards(SessionVariables.CurrentLocation.ToString());

            foreach (var card in cards)
            {
                if (card.name == upc)
                {
                    item.ItemUpc  = card.name;
                    item.Comments = t.GetCardComments(card.id);
                    var checklists = t.GetChecklists(card.id);
                    var checklist  = checklists.Last().checkItems;
                    item.Checklist = new List <Models.Trello.CheckItemView>();
                    foreach (var checkitem in checklist)
                    {
                        Trello.CheckItemView civ = new Models.Trello.CheckItemView();
                        civ.id          = checkitem.id;
                        civ.name        = checkitem.name;
                        civ.nameData    = checkitem.nameData;
                        civ.pos         = checkitem.pos;
                        civ.idChecklist = checkitem.idChecklist;
                        if (checkitem.state == "complete")
                        {
                            civ.state = true;
                        }
                        else
                        {
                            civ.state = false;
                        }
                        item.Checklist.Add(civ);
                    }
                    item.RequestDate = checklists.Last().name;
                    item.DueDate     = card.due;
                    foreach (var b in t.GetBoards())
                    {
                        if (b.id == card.idBoard)
                        {
                            item.ItemLocation = b.name;
                        }
                    }
                }
            }
            return(View(item));
        }
        // Marks any existing checklist items as complete, closes the due date, and marks the repair request as closed
        public ActionResult CloseRepair(string itemUpc, string itemLoc, string description, string confirm)
        {
            if (itemUpc == null || itemLoc == null || description == "" || confirm != "close")
            {
                TempData["message"] = "To close a request for repair, you must include a closing note and confirm that the repair has been resolved. Please try again.";
                return(RedirectToAction("Index"));
            }
            if (confirm == "nevermind")
            {
                TempData["message"] = "To close a request for repair, you must confirm that the repair has been resolved. Please try again.";
                return(RedirectToAction("Index"));
            }

            bool   success = false;
            Trello t       = new Models.Trello();

            // get card id first
            var    cards  = t.GetCards(itemLoc);
            string cardId = null;

            foreach (var card in cards)
            {
                if (card.name == itemUpc)
                {
                    cardId = card.id;
                    continue;
                }
            }

            if (cardId != null)
            {
                var user = db.tb_CSULabTechs.FirstOrDefault(m => m.ENAME == SessionVariables.CurrentUserId);

                // retrieves the checklist and close any open checklist items
                var checklists  = t.GetChecklists(cardId);
                var checklistId = checklists.Last().id;
                List <Trello.CheckItem> checklistItems = t.GetChecklistItems(checklistId);
                foreach (var item in checklistItems)
                {
                    var result = t.PutChangeChecklistItem(cardId, item.id, "complete");
                }

                // post the close note as a comment
                description = "CLOSED --> " + description + "  -- Posted by: " + user.First_Name + " " + user.Last_Name + "(" + DateTime.Now.ToString() + ")";
                var commentId = t.PostCardComment(cardId, description);

                // mark the due date as complete
                var dueDateClosed = t.PutCloseDueDate(cardId);

                // determine success
                if (commentId != null && dueDateClosed != null)
                {
                    success = true;
                }
            }

            if (success) // success
            {
                TempData["message"] = "The repair request for Item #" + itemUpc + " has been closed.";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["message"] = "There was a problem closing this repair. Please try again.";
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult RequestRepair(string upc, string description, string duedate)
        {
            if (upc == null || description == null)
            {
                TempData["message"] = "To submit a request for repair, you must include both a valid UPC and a description of the issue to be resolved. Please try again.";
                return(RedirectToAction("Index"));
            }

            bool   success = false;
            Trello t       = new Models.Trello();

            // get card id first
            var    cards  = t.GetCards(SessionVariables.CurrentLocation.ToString());
            string cardId = null;

            foreach (var card in cards)
            {
                if (card.name == upc)
                {
                    cardId = card.id;
                    continue;
                }
            }

            if (cardId != null)
            {
                var user = db.tb_CSULabTechs.FirstOrDefault(m => m.ENAME == SessionVariables.CurrentUserId);
                // submit request by creating a new checklist on the card and adding the description as a comment
                description = description + "  -- Posted by: " + user.First_Name + " " + user.Last_Name + " (" + DateTime.Now.ToString() + ")";
                var commentId   = t.PostCardComment(cardId, description);
                var checklistId = t.PostCardChecklist(cardId, DateTime.Now.ToString());

                DateTime newDate;
                if (duedate == "")
                {
                    newDate = DateTime.Now.Date.AddDays(14);
                }
                else
                {
                    newDate = Convert.ToDateTime(duedate).Date;
                }

                string newDueDate    = t.PutNewDueDate(cardId, newDate);
                string checkItem1    = t.PostCardChecklistItem(checklistId, "Diagnose");
                string checkItem2    = t.PostCardChecklistItem(checklistId, "Resolve issue");
                var    dueDateOpened = t.PutOpenDueDate(cardId);


                if (commentId != null && checklistId != null && newDueDate != null) // id's were returned, so success
                {
                    success = true;
                }
            }

            if (success) // success
            {
                TempData["message"] = "Item " + upc + " was submitted for repair.";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["message"] = "There was a problem submitting this request. Please try again.";
                return(RedirectToAction("Index"));
            }
        }