using DemoGrids.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using System.Text;
namespace DemoGrids.BussinessService
{
    public class BussinessServices
    {
        
        
        public variable GetCustomers(String Word, int Page, int rows, string searchString)
        {
            variable v = new variable();
            List<CustomerViewModel> customermodel = new List<CustomerViewModel>(); 
                   
            using (DatabaseContext db = new DatabaseContext())
            {
                v.PageIndex = Convert.ToInt32(Page) - 1;
                v.PageSize = rows;
                customermodel = (from customer in db.Customers
                                 select new CustomerViewModel
                                 {
                                     CustomerID = customer.CustomerID,
                                     ContactName = customer.ContactName,
                                     ContactTitle = customer.ContactTitle,
                                     City = customer.City,
                                     PostalCode = customer.PostalCode,
                                     Country = customer.Country,
                                     Phone = customer.Phone
                                 }).ToList();
                v.totalRecords = customermodel.Count();
                v.totalPages = (int)Math.Ceiling((float)v.totalRecords / (float)rows);
                if (v.sort.ToUpper() == v.DESC)   // getting error here 
                  //  if (v.sort.ToUpper() == "DESC")
                {
                    customermodel = customermodel.OrderByDescending(s => s.CustomerID).ToList();
                    customermodel = customermodel.Skip(v.PageIndex * v.PageSize).Take(v.PageSize).ToList();
                }
                else
                {
                    customermodel = customermodel.OrderBy(s => s.CustomerID).ToList();
                    customermodel = customermodel.Skip(v.PageIndex * v.PageSize).Take(v.PageSize).ToList();
                }
                if (!string.IsNullOrEmpty(searchString))
                {
                    customermodel = customermodel.Where(m => m.Country == searchString).ToList();
                }
                var jsonData = new
                {
                    total = v.totalPages,
                    Page,
                    records = v.totalRecords,
                    rows = customermodel
                };
                return new variable {total=v.totalPages,PageIndex=v.PageIndex,records=v.records,customerModel= customermodel };
                               
            }
        }
    }
}