// GET: /Device/
        public ActionResult Index(string err = null)
        {
            ViewBag.Error = err;
            ViewBag.Functions = new SelectList(new SelectListItem[] {
                    new SelectListItem() { Text = "Prepare Tasker File", Value = funcPrepareTaskerFile.ToString() },
                    new SelectListItem() { Text = "Coming soon", Value = "2" }
            }, "Value", "Text");

            var model = new DeviceSelectionViewModel();
            var items =
                from d in db.Devices
                //join r in db.Requests on d.ID equals r.DeviceID into dr
                //join l in db.DeviceLogs on d.ID equals l.DeviceID into dl
                select new SelectDeviceEditorViewModel
                {
                    ID = d.ID,
                    AndriodVer = d.AndriodVer,
                    CreationDate = d.CreationDate,
                    CustomerID = d.CustomerID,
                    ModelNo = d.ModelNo,
                    DeviceCode = d.DeviceCode,
                    DeviceType = d.DeviceType,
                    TaskerVersion = d.TaskerVersion,
                    TodayNetworkConnectivity = d.TodayNetworkConnectivity,
                    Last30DaysNetworkConnectivity = d.Last30DaysNetworkConnectivity,
                    LastRequestID = d.LastRequestId,
                    LastRequestDate = d.LastRequestTime,
                    LastLogID = d.LastLogId,
                    LastLogDate = d.LastLogTime,
                    LastRunMinute = d.LastRunMinute,
                    TodayShowRate = d.TodayShowRate,
                    LastRunIndex = d.LastRunIndex,
                    LastCommandIssued = d.LastCommandIssued,
                    LastCommandIssueTime = d.LastCommandIssueTime,
                    LastCommandExecuted = d.LastCommandExecuted,
                    LastCommandExecuteTime = d.LastCommandExecuteTime,
                    //Status = (Device.DeviceStatus)d.Status, // d.GetStatus(), //.HasValue? (Device.DeviceStatus)d.Status.Value: Device.DeviceStatus.Unknown,

                    Selected = true
                    //LastRequest = dr.OrderByDescending(r => r.ID).Take(1).FirstOrDefault(),
                    //LastLog = dl.OrderByDescending(r => r.ID).Take(1).FirstOrDefault()
                };

            foreach(var item in items)
                model.Devices.Add(item);

            ////var items = db.Devices
            ////    .Join(db.DeviceLogs, d => d.ID, l => l.DeviceID, (d, l) => new {device = d, log = l}) //.OrderBy(d => d.device.ID).ThenByDescending(d => d.log.ID)
            ////    .Join(db.Requests, d => d.device.ID, r => r.DeviceID, (d, r) => new {device = d.device, log = d.log, req = r})
            ////    .OrderBy(d => d.device.ID)
            ////    .ThenByDescending(d => d.req.ID)
            ////    .ThenByDescending(d => d.log.ID);
            ////var devices =
            ////    from d in db.Devices
            ////    join r in db.Requests on d.ID equals r.DeviceID into dr
            ////    join l in db.DeviceLogs on d.ID equals l.DeviceID into dl
            ////    orderby d.ID
            ////    orderby r. descending
            ////    select new { Devices = d, Requests = dr, Logs = dl};
            ////foreach (var device in db.Devices.Include(d => d.Requests.OrderByDescending(o => o.ID).Take(10)).Include(d => d.DeviceLogs.OrderByDescending(l => l.ID).Take(10)))
            //foreach (var item in items) // (var device in db.Devices.Include(d => d.Requests.Where(dw => DateTime.Now.Subtract(dw.RequestDate).TotalDays ).Include(d => d.DeviceLogs))
            //{
            //    var editorViewModel = new SelectDeviceEditorViewModel()
            //    {
            //        ID = item.Device.ID,
            //        AndriodVer = item.Device.AndriodVer,
            //        CreationDate = item.Device.CreationDate,
            //        CustomerID = item.Device.CustomerID,
            //        DeviceID = item.Device.DeviceID,
            //        DeviceType = item.Device.DeviceType,
            //        ModelNo = item.Device.ModelNo,
            //        LastRequestDate = item.LastRequest == null? (DateTime?)null: item.LastRequest.RequestDate,
            //        LastRequestID = item.LastRequest == null ? (long?)null : item.LastRequest.ID,
            //        LastLogDate = item.LastLog == null ? (DateTime?)null : item.LastLog.CreationDate,
            //        LastLogID = item.LastLog == null ? (long?)null : item.LastLog.ID,

            //        //ID = device.ID,
            //        //AndriodVer = device.AndriodVer,
            //        //CreationDate = device.CreationDate,
            //        //CustomerID = device.CustomerID,
            //        //DeviceID = device.DeviceID,
            //        //DeviceType = device.DeviceType,
            //        //ModelNo = device.ModelNo,
            //        Selected = true
            //    };
            //    //if (device.Requests != null && device.Requests.Count > 0)
            //    //{
            //    //    var lastReq = device.Requests.OrderByDescending(o => o.ID).First();
            //    //    editorViewModel.LastRequestDate = lastReq.RequestDate;
            //    //    editorViewModel.LastRequestID = lastReq.ID;
            //    //}
            //    //if (device.DeviceLogs != null && device.DeviceLogs.Count > 0)
            //    //{
            //    //    var lastRLog = device.DeviceLogs.OrderByDescending(o => o.ID).First();
            //    //    editorViewModel.LastLogDate = lastRLog.CreationDate;
            //    //    editorViewModel.LastLogID = lastRLog.ID;
            //    //}
            //    model.Devices.Add(editorViewModel);
            //}
            //return View(model);
            return View(model);
        }
        public ActionResult Index(int? page)
        {
            ttTVMS.Models.MessageViewModel viewModel = new MessageViewModel();
            int pageSize = 20;
            int pageNumber = (page ?? 1);

            var messages = db.Messages.OrderByDescending(r => r.ID);

            List<SelectListItem> selectItems = new List<SelectListItem>();
            Array msgConstants = Enum.GetValues(typeof(MessageContants));
            Array.Sort(msgConstants);
            foreach (int msg in msgConstants)
            {
                selectItems.Add(new SelectListItem() { Text = Enum.GetName(typeof(MessageContants), msg), Value = msg.ToString() });
            }

            ViewBag.Functions = new SelectList(selectItems.ToArray(), "Value", "Text");

            var model = new DeviceSelectionViewModel();
            var items =
                from d in db.Devices
                    select d;
                //join r in db.Requests on d.ID equals r.DeviceID into dr
                //join l in db.DeviceLogs on d.ID equals l.DeviceID into dl
                //select new
                //{
                //    Device = d,
                //    LastRequest = dr.OrderByDescending(r => r.ID).Take(1).FirstOrDefault(),
                //    LastLog = dl.OrderByDescending(r => r.ID).Take(1).FirstOrDefault()
                //};

            foreach (var item in items) // (var device in db.Devices.Include(d => d.Requests.Where(dw => DateTime.Now.Subtract(dw.RequestDate).TotalDays ).Include(d => d.DeviceLogs))
            {
                var editorViewModel = new SelectDeviceEditorViewModel()
                {
                    ID = item.ID, // item.Device.ID,
                    AndriodVer = item.AndriodVer, // .Device.AndriodVer,
                    CreationDate = item.CreationDate, // .Device.CreationDate,
                    CustomerID = item.CustomerID, // .Device.CustomerID,
                    DeviceCode = item.DeviceCode, // .Device.DeviceCode,
                    DeviceType = item.DeviceType, // .Device.DeviceType,
                    ModelNo = item.ModelNo, // .Device.ModelNo,
                    LastRequestDate = item.LastRequestTime, // item.LastRequest == null ? (DateTime?)null : item.LastRequest.RequestDate,
                    LastRequestID = item.LastRequestId, // item.LastRequest == null ? (long?)null : item.LastRequest.ID,
                    LastLogDate = item.LastLogTime, // item.LastLog == null ? (DateTime?)null : item.LastLog.CreationDate,
                    LastLogID = item.LastLogId, // item.LastLog == null ? (long?)null : item.LastLog.ID,

                    Selected = item.DeviceType != 1
                };
                model.Devices.Add(editorViewModel);
            }
            viewModel.Devices = model;
            viewModel.Messages = messages.ToPagedList(pageNumber, pageSize);

            return View(viewModel);
        }
        public ActionResult Execute(DeviceSelectionViewModel model)
        {
            string err = null;
            // get the ids of the items selected:
            List<long> selectedIds = model.getSelectedIds();

            // Use the ids to retrieve the records for the selected people
            // from the database:
            var selectedDevices = from x in db.Devices
                                  where selectedIds.Contains(x.ID)
                                  select x;

            switch(model.SelectedFunction)
            {
                case funcPrepareTaskerFile:
                    err = CreateDeviceTemplate(selectedDevices);
                    break;
            }

            return RedirectToAction("Index", new { err = err });
        }