//
        // GET: /Isentos/

        public ActionResult Index(IsentosListViewModel viewModel, string sort, string direction, int? page)
        {
            viewModel.SetParameters(1, page == null ? 1 : page.Value, sort, direction, Request.Params);

            IsentosListViewModel result = getIsentos(viewModel);
            return View(result);
        }
        public ActionResult exportIsentosToCsv(IsentosListViewModel viewModel, string sort, string direction)
        {
            viewModel.PageSize = 0;
            viewModel.SetParameters(1, 1, sort, direction, Request.Params);

            IsentosListViewModel result = getIsentos(viewModel);

            List<ApoliceIsentoToCsv> isentosCsv = new List<ApoliceIsentoToCsv>();
            foreach (ApoliceIsento apo in result.apolicesIsentos)
                isentosCsv.Add(new ApoliceIsentoToCsv
                {
                    entidade = apo.entidade.nome,
                    matricula = apo.matricula,
                    dataFim = apo.dataFim.ToString(),
                    dataInicio = apo.dataInicio.ToString(),
                    confidencial = apo.confidencial ? "Sim" : "Não",
                    dataModificacao = apo.dataModificacao.ToString()
                });

            CsvExport<ApoliceIsentoToCsv> csv = new CsvExport<ApoliceIsentoToCsv>(isentosCsv);

            byte[] fileBytes = csv.ExportToBytes();
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "Isentos_" + DateTime.Now.ToShortDateString() + ".csv");
        }
        protected IsentosListViewModel getIsentos(IsentosListViewModel viewModel)
        {
            IPagedList<ApoliceIsento> apolicesIsentosToView = null;
            IQueryable<ApoliceIsento> apoliceIsentos = apoliceIsentosRepository.All;

            if (!(viewModel.entidadeResponsavel == null || viewModel.entidadeResponsavel == string.Empty))
            {
                apoliceIsentos = apoliceIsentos.Where(a => a.entidadeResponsavel.Contains(viewModel.entidadeResponsavel));
            }
            if (!(viewModel.matricula == null || viewModel.matricula == string.Empty))
            {
                apoliceIsentos = apoliceIsentos.Where(a => a.matricula.Contains(viewModel.matricula) || a.matriculaCorrigida.Contains(viewModel.matricula));
            }
            if (viewModel.arquivados != null && viewModel.arquivados == true)
            {
                apoliceIsentos = apoliceIsentos.Where(a => a.arquivo == true);
            }
            else
            {
                apoliceIsentos = apoliceIsentos.Where(a => a.arquivo == false);
            }


            viewModel.totalNumberOfIsentos = apoliceIsentos.Count();

            if (viewModel.PageSize == 0)
                viewModel.PageSize = viewModel.totalNumberOfIsentos;

            switch (viewModel.SortColumn)
            {
                case "Entidade":
                    if (viewModel.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.entidade.nome).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    else
                        apolicesIsentosToView = apoliceIsentos.OrderBy(s => s.entidade.nome).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
                case "Matricula":
                    if (viewModel.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.matricula).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    else
                        apolicesIsentosToView = apoliceIsentos.OrderBy(s => s.matricula).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
                case "DataInicio":
                    if (viewModel.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.dataInicio).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    else
                        apolicesIsentosToView = apoliceIsentos.OrderBy(s => s.dataInicio).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
                case "DataFim":
                    if (viewModel.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.dataFim).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    else
                        apolicesIsentosToView = apoliceIsentos.OrderBy(s => s.dataFim).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
                case "Confidencial":
                    if (viewModel.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.confidencial).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    else
                        apolicesIsentosToView = apoliceIsentos.OrderBy(s => s.confidencial).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
                case "DataModificacao":
                    if (viewModel.SortDirection == System.ComponentModel.ListSortDirection.Descending)
                        apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.dataModificacao).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    else
                        apolicesIsentosToView = apoliceIsentos.OrderBy(s => s.dataModificacao).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
                default:
                    apolicesIsentosToView = apoliceIsentos.OrderByDescending(s => s.dataModificacao).ToPagedList(viewModel.CurrentPageNumber, viewModel.PageSize);
                    break;
            }
            
            viewModel.apolicesIsentos = apolicesIsentosToView.ToList();

            return viewModel;
        }