public IActionResult DoDelete(string id, string f)
        {
            int?uid = HttpContext.Session.GetInt32("uid");

            if (uid == null)
            {
                return(RedirectToAction("Login", "Login"));
            }
            int todoId;

            if (!Int32.TryParse(id, out todoId))
            {
                return(RedirectToAction("Home", "Home"));
            }
            TodoTask todo = TodoTaskService.FindTodoTaskById(todoId);

            if (todo == null)
            {
                return(RedirectToAction("Home", "Home"));
            }

            todo.Valid      = false;
            todo.ModifyTime = DateTime.Now;
            TodoTaskService.UpdateTodoTask(todo);

            if (f != null && f.Equals("h"))
            {
                return(RedirectToAction("Home", "Home"));
            }
            else
            {
                return(RedirectToAction("List", "Todo"));
            }
        }
        public IActionResult List(string p)
        {
            const int PAGE_SIZE = 10;
            int?      uid       = HttpContext.Session.GetInt32("uid");

            if (uid == null)
            {
                return(RedirectToAction("Login", "Login"));
            }
            int page;

            if (!Int32.TryParse(p, out page))
            {
                page = 1;
            }

            int count = TodoTaskService.FindCountByUser((int)uid);
            int pages = count % PAGE_SIZE == 0 ? count / PAGE_SIZE : count / PAGE_SIZE + 1;

            if (page > pages)
            {
                page = pages;
            }

            if (page <= 0)
            {
                page = 1;
            }

            List <TodoTask> todos = TodoTaskService.FindTodoTaskByUserAndPageAndPagesize((int)uid, page, PAGE_SIZE);

            TodoListViewModel todoListViewModel = new TodoListViewModel(todos, pages, page);

            return(View(todoListViewModel));
        }
        public IActionResult Home(string ep, string tp)
        {
            const int PAGE_SIZE = 10;
            int?      uid       = HttpContext.Session.GetInt32("uid");

            if (uid == null)
            {
                return(RedirectToAction("Login", "Login"));
            }
            int eventPage, todoPage;

            if (!Int32.TryParse(ep, out eventPage))
            {
                eventPage = 1;
            }
            if (!Int32.TryParse(tp, out todoPage))
            {
                todoPage = 1;
            }

            Uzer u = UserService.FindUserByID((int)uid);

            int eventCount = AskForLeaveService.FindCountByCurrentAt(u);
            int todoCount  = TodoTaskService.FindUndoneCountByUser((int)uid);

            int eventPages = eventCount % PAGE_SIZE == 0 ? eventCount / PAGE_SIZE : eventCount / PAGE_SIZE + 1;
            int todoPages  = todoCount % PAGE_SIZE == 0 ? todoCount / PAGE_SIZE : todoCount / PAGE_SIZE + 1;

            if (eventPage > eventPages)
            {
                eventPage = eventPages;
            }
            if (todoPage > todoPages)
            {
                todoPage = todoPages;
            }

            if (eventPage <= 0)
            {
                eventPage = 1;
            }
            if (todoPage <= 0)
            {
                todoPage = 1;
            }

            List <AskForLeave> asks  = AskForLeaveService.FindAskForLeaveByCurrentAtAndPageAndPagesize(u, eventPage, PAGE_SIZE);
            List <TodoTask>    todos = TodoTaskService.FindUndoneTodoTaskByUserAndPageAndPagesize((int)uid, todoPage, PAGE_SIZE);

            List <Attendance> attendanceList    = AttendanceService.FindAttendanceByUserAndDateAndType((int)uid, DateTime.Now, AttendanceType.In);
            Boolean           need              = (attendanceList == null || attendanceList.Count <= 0) ? true : false;
            HomeHomeViewModel homeHomeViewModel = new HomeHomeViewModel(asks, eventPages, eventPage, todos, todoPages, todoPage, need);

            return(View(homeHomeViewModel));
        }
        public IActionResult Detail(string id, string r, string f)
        {
            int?uid = HttpContext.Session.GetInt32("uid");

            if (uid == null)
            {
                return(RedirectToAction("Login", "Login"));
            }
            int i;

            if (null == id || !Int32.TryParse(id, out i))
            {
                return(RedirectToAction("Home", "Home"));
            }

            TodoTask todo = TodoTaskService.FindTodoTaskById(i);
            TodoTaskDetailViewModel model = new TodoTaskDetailViewModel(todo);

            model.SetFrom(f);
            if (r == null)
            {
                model.LastUpdateSuccess = 0;
            }
            else if (r.Equals("0"))
            {
                model.LastUpdateSuccess = 1;
                // todoNewViewModel.LastNewPrompt = "You have applied a request of leave, please wait for your supervisors to review.";
            }
            else
            {
                model.LastUpdateSuccess = -1;
                model.LastUpdatePrompt  = r;
            }

            return(View(model));
        }
        public IActionResult DoNew(string deadline, string title, string content, string f)
        {
            string result = "0";

            if (deadline == null ||
                title == null ||
                deadline.Trim().Equals("") ||
                title.Trim().Equals("")
                )
            {
                result = "Fields of 'Title' and 'DeadLine' should be filled.";
                return(RedirectToAction("New", "Todo", new { r = result }));
            }

            int?uid = HttpContext.Session.GetInt32("uid");

            DateTime now = DateTime.Now;
            DateTime dl;

            if (!DateTime.TryParse(deadline, out dl))
            {
                result = "Time format not valid.";
                return(RedirectToAction("New", "Todo", new { r = result }));
            }
            if (dl < now)
            {
                result = "DeadLine is earlier than now.";
                return(RedirectToAction("New", "Todo", new { r = result }));
            }

            if (uid == null)
            {
                return(RedirectToAction("Login", "Login"));
            }

            double spanDays = (dl - now).TotalDays;

            TodoTaskStatus status;

            if (spanDays > 90)
            {
                status = TodoTaskStatus.Faraway;
            }
            else if (spanDays > 30)
            {
                status = TodoTaskStatus.Approching;
            }
            else
            {
                status = TodoTaskStatus.DueSoon;
            }

            TodoTaskService.AddTodoTask((int)uid, title, content, dl, status);

            if (f != null && f.Equals("h"))
            {
                return(RedirectToAction("Home", "Home"));
            }
            else
            {
                return(RedirectToAction("List", "Todo"));
            }
        }
        public IActionResult DoUpdate(string id, string title, string content, string deadline, string f)
        {
            int?uid = HttpContext.Session.GetInt32("uid");

            if (uid == null)
            {
                return(RedirectToAction("Login", "Login"));
            }
            int todoId;

            if (!Int32.TryParse(id, out todoId))
            {
                return(RedirectToAction("Home", "Home"));
            }
            TodoTask todo = TodoTaskService.FindTodoTaskById(todoId);

            if (todo == null)
            {
                return(RedirectToAction("Home", "Home"));
            }

            string result = "0";

            if (deadline == null ||
                title == null ||
                deadline.Trim().Equals("") ||
                title.Trim().Equals("")
                )
            {
                result = "Fields of 'Title' and 'DeadLine' should be filled.";
                return(RedirectToAction("Detail", "Todo", new { id = todoId, r = result }));
            }

            DateTime now = DateTime.Now;
            DateTime dl;

            if (!DateTime.TryParse(deadline, out dl))
            {
                result = "Time format not valid.";
                return(RedirectToAction("Detail", "Todo", new { id = todoId, r = result }));
            }
            if (dl < now)
            {
                result = "DeadLine is earlier than now.";
                return(RedirectToAction("Detail", "Todo", new { id = todoId, r = result }));
            }


            double spanDays = (dl - now).TotalDays;

            TodoTaskStatus status;

            if (spanDays > 90)
            {
                status = TodoTaskStatus.Faraway;
            }
            else if (spanDays > 30)
            {
                status = TodoTaskStatus.Approching;
            }
            else
            {
                status = TodoTaskStatus.DueSoon;
            }

            todo.Content    = content;
            todo.Title      = title;
            todo.DeadLine   = dl;
            todo.Status     = (int)status;
            todo.ModifyTime = DateTime.Now;
            TodoTaskService.UpdateTodoTask(todo);

            if (f != null && f.Equals("h"))
            {
                return(RedirectToAction("Home", "Home"));
            }
            else
            {
                return(RedirectToAction("List", "Todo"));
            }
        }