/*

        Frontend page: Report page
        Title: Get active loans of user for grid
        Designed: Irfan MAM
        User story: DFP- 446
        Developed: Irfan MAM
        Date created: 06/23/2016

*/

            public JsonResult GetActiveLoans(string sidx, string sord, int page, int rows, bool _search)
        {
            ReportAccess ra = new ReportAccess();

            List<Account> loanNumbers;

            // if user is a superadmin, get account details of his company
            if (_userData.RoleId == 1) {
                 loanNumbers = ra.GetAccountDetails(_userData.Company_Id, _userData.RoleId);

            }

            // if user is a admin, get account details of his branch
            else if (_userData.RoleId == 2)
            {
                loanNumbers = ra.GetAccountDetails(_userData.BranchId, _userData.RoleId);
            }
            // if user is a user or dealer user, get account details of his assigned loans
            else if(_userData.RoleId == 3 || _userData.RoleId == 4)
            {
                
                loanNumbers = ra.GetAccountDetailsForUser(_userData.UserId);

                // if selected dashboard loan has no rights delete the session
            }
            else
            {
                loanNumbers = new List<Account>();
            }

            // these varibles are for JqGrid purpose

            int count = loanNumbers.Count(); // number of rows
            int pageIndex = page; // number of pages on the grid
            int pageSize = rows; // maximum page sige
            int startRow = (pageIndex * pageSize) + 1;
            int totalRecords = count;
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
           
            // for super admin or admin, we use different json object
            if (_userData.RoleId == 1 || _userData.RoleId == 2) {
                // json object for jqGrid
                var result = new
            {
                total = totalPages,
                page = pageIndex,
                records = count,
                rows = loanNumbers.Select(x => new
                {
                    x.LoanId,
                    x.LoanCode,
                    x.BranchName,
                    x.PatBranchName,
                    x.LoanNumber,
                   
                    x.LoanAmount,
                   
                   
                    x.UsedAmount,
                    x.ActiveUnits,
                    x.PatBranchAddress1,
                    x.PatBranchAddress2,
                    x.PatCity
                }
                                         ).ToArray().Select(x => new
                                         {
                                             id = x.LoanId.ToString(),
                                             cell = new string[] { 
                                                        x.LoanCode,
                                                        x.BranchName,
                                                        x.PatBranchName,
                                                        x.LoanNumber.ToString(),
                                                        x.LoanAmount.ToString(),
                                                        x.UsedAmount.ToString(),
                                                        x.ActiveUnits.ToString(),
                                                        x.PatBranchAddress1,
                                                        x.PatBranchAddress2,
                                                        x.PatCity
                                                      }
                                         }
                      ).ToArray()

               

            };

                // returning json object
                return Json(result, JsonRequestBehavior.AllowGet);

            }
             // for others (user/ dealer user) , we use differnt json object
            else
            {
                // json object for jqGrid
                var result = new
                {
                    total = totalPages,
                    page = pageIndex,
                    records = count,
                    rows = loanNumbers.Select(x => new
                    {
                        x.LoanId,
                        x.LoanCode,
                        x.PatBranchName,
                        x.LoanNumber,
                        x.LoanAmount,
                        x.UsedAmount,
                        x.ActiveUnits,
                        x.PatBranchAddress1,
                        x.PatBranchAddress2,
                        x.PatCity,
                        x.userReportRights
                    }
                                             ).ToArray().Select(x => new
                                             {
                                                 id = x.LoanId.ToString(),
                                                 cell = new string[] {
                                                        x.LoanCode,
                                                        x.PatBranchName,
                                                        x.LoanNumber.ToString(),
                                                        x.LoanAmount.ToString(),
                                                        x.UsedAmount.ToString(),
                                                        x.ActiveUnits.ToString(),
                                                        x.PatBranchAddress1,
                                                        x.PatBranchAddress2,
                                                        x.PatCity,
                                                        x.userReportRights
                                                          }
                                             }
                          ).ToArray()



                };

                // returning json object
                return Json(result, JsonRequestBehavior.AllowGet);

            }


           

        }