/// <summary>
        /// Frontend page: Title(loan setup)
        /// Title: Get details of title related to given loanId
        /// Designed: Piyumi
        /// User story:
        /// Date created: 2016/2/9
        /// </summary>
        /// <param name="loanId"></param>
        /// <returns>TitleObject</returns>
        public Title getTitleDetails(int loanId)
        {
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@loan_id", loanId });

            try
            {
                DataSet dataSet = dataHandler.GetDataSet("spGetTitleDetailsByLoanId", paramertList);
                Title obj1 = new Title();
                //Check dataset is not null and table count >0
                if (dataSet != null && dataSet.Tables.Count != 0)
                {
                   
                    foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                    {
                        
                        obj1.LoanId = int.Parse(dataRow["loan_id"].ToString());
                        obj1.IsTitleTrack = bool.Parse(dataRow["is_title_tracked"].ToString());
                        //Check whether title need to be tracked
                        if (obj1.IsTitleTrack)
                        {
                        
                            obj1.ReceivedTimeLimit = dataRow["title_received_time_period"].ToString();
                            //Check auto remind period is null or empty
                            if (!string.IsNullOrEmpty(dataRow["auto_remind_period"].ToString()))
                            {
                                obj1.RemindPeriod = int.Parse(dataRow["auto_remind_period"].ToString());
                            }
                            //Check auto remind email is null or empty
                            if (!string.IsNullOrEmpty(dataRow["auto_remind_email"].ToString()))
                            {
                                obj1.RemindEmail = dataRow["auto_remind_email"].ToString();
                            }
                            else
                            {
                            //get auto remind email given in loan details page
                                LoanSetupAccess st = new LoanSetupAccess();
                                obj1.RemindEmail = st.getAutoRemindEmailByLoanId(obj1.LoanId);
                            }

                        }
                        else 
                        {
                            //get auto remind email given in loan details page
                            LoanSetupAccess st = new LoanSetupAccess();
                            obj1.RemindEmail = st.getAutoRemindEmailByLoanId(obj1.LoanId);
                        }
                        obj1.IsReceipRequired = bool.Parse(dataRow["is_receipt_required"].ToString());
                        //Check whether receipts are required
                        if (obj1.IsReceipRequired)
                        {

                            obj1.ReceiptRequiredMethod = dataRow["receipt_required_method"].ToString();
                        }
                        //Check need scan copy value is null or empty
                        if (!string.IsNullOrEmpty(dataRow["need_scan_copy"].ToString()))
                        {
                            obj1.NeedScanCopy = bool.Parse(dataRow["need_scan_copy"].ToString());
                            //Check need scan copy value is true
                            if (obj1.NeedScanCopy)
                            {
                                obj1.TitleAcceptMethod = "Scanned Title Adequate";
                            }
                        }
                    }
                    return obj1;
                }
                else
                {
                  return obj1;
                }
             }
            catch (Exception ex)
            {
                throw ex;

            }
           
        }
        public ActionResult Step9(Title title)
        {
            int userId = userData.UserId;
            int branchId = loanData.BranchId;
            //check user is user or dealer user
            if (userData.RoleId >= 3)
            {
                //return to login page
                return RedirectToAction("UserLogin", "Login", new { lbl = "You are not Allowed." });
            }
         
            TitleAccess ta = new TitleAccess();
            LoanSetupAccess la = new LoanSetupAccess();
            StepAccess sa = new StepAccess();
          
            int loanId = loanData.loanId;
            title.LoanId = loanId;

            if (title.ReceivedTimeLimit == "1")
            {
                title.ReceivedTimeLimit = "Title required to advance";
            }
            else if (title.ReceivedTimeLimit == "2")
            {
                title.ReceivedTimeLimit = "Title can arrive at any time";
            }
            else if (title.ReceivedTimeLimit == "3")
            {
                title.ReceivedTimeLimit = "Title must arrive within a specified time";
            }

            if (title.ReceiptRequiredMethod == "1")
            {
                title.ReceiptRequiredMethod = "Must be physically present";
            }
            else if (title.ReceiptRequiredMethod == "2")
            {
                title.ReceiptRequiredMethod = "Scanned copy only";
            }
            else if (title.ReceiptRequiredMethod == "3")
            {
                title.ReceiptRequiredMethod = "Physically present or scanned copy";
            }
            //insert or update title details and get result
            int reslt = ta.insertTitleDetails(title);
            //check result is not 0
            if (reslt >= 0)
            {
                //update loan setup step table
                if (sa.UpdateLoanSetupStep(userData.UserId,loanData.CompanyId, loanData.BranchId, loanData.nonRegisteredBranchId, loanData.loanId, 5))
                {
                    //check current step value of loanData object is less than 5
                    if (loanData.stepId < 5)
                    {
                        //update step
                        loanData.stepId = 5;
                    }
                    //if title update or insert result is 0
                    if (reslt == 0)
                    {
                        //insert record in to log table as insert
                        Log log = new Log(userData.UserId, userData.Company_Id, loanData.BranchId, title.LoanId, "Title", "Edited title details of loan : " + title.LoanId, DateTime.Now);

                        int islog = (new LogAccess()).InsertLog(log);
                    }
                    if (reslt > 0)
                    {
                        //insert record in to log table as update
                        Log log = new Log(userData.UserId, userData.Company_Id, loanData.BranchId, title.LoanId, "Title", "Inserted title details of loan : " + title.LoanId, DateTime.Now);

                        int islog = (new LogAccess()).InsertLog(log);
                    }
                    //convert loan data object to session variable
                    Session["loanStep"] = loanData;
                    //return to curtailment page
                    return RedirectToAction("Step10");
                }
                //if update loan setup step result is false return to login page
                else
                {
                    return RedirectToAction("UserLogin", "Login", new { lbl = "Due to inactivity your session has timed out, please log in again." });
                }
            }
           
            else
            {
                return new HttpStatusCodeResult(404);
            }
            
        }
        /// <summary>
        /// CreatedBy:Piyumi
        /// CreatedDate:2016/2/9
        /// Insert details of title related to a loanId
        /// </summary>
        /// <param name="loanId"></param>
        /// <returns>countVal</returns>
        public int insertTitleDetails(Title title)
        {
            DataHandler dataHandler = new DataHandler();
            List<object[]> paramertList = new List<object[]>();
            paramertList.Add(new object[] { "@is_title_tracked", title.IsTitleTrack });
            if (title.IsTitleTrack)
            {
                //paramertList.Add(new object[] { "@title_accept_method", title.TitleAcceptMethod });
                paramertList.Add(new object[] { "@title_received_time_period", title.ReceivedTimeLimit });
                paramertList.Add(new object[] { "@auto_remind_period", title.RemindPeriod });
                paramertList.Add(new object[] { "@auto_remind_email", title.RemindEmail });
                
               
            }
            else
            {
               // paramertList.Add(new object[] { "@title_accept_method", null });
                paramertList.Add(new object[] { "@title_received_time_period", null });
                paramertList.Add(new object[] { "@auto_remind_period", null });
                paramertList.Add(new object[] { "@auto_remind_email", null});
                //paramertList.Add(new object[] { "@need_scan_copy", 0 });
            }

            paramertList.Add(new object[] { "@is_receipt_required", title.IsReceipRequired });
            if (title.IsReceipRequired)
            {

                paramertList.Add(new object[] { "receipt_required_method", title.ReceiptRequiredMethod });
            }
            else
            {
                paramertList.Add(new object[] { "receipt_required_method",null});
            }
            if (title.NeedScanCopy)
            {
                paramertList.Add(new object[] { "@need_scan_copy", 1 });
            }
            else
            {
                paramertList.Add(new object[] { "@need_scan_copy", 0 });
            }
            paramertList.Add(new object[] { "@loan_id", title.LoanId });

            try
            {
                return dataHandler.ExecuteSQLReturn("spInsertTitleDetails", paramertList);
            }
            catch(Exception ex)
            {
                throw ex;
            }
          
        }
        public ActionResult Step9(int? edit)
        {
            int uId = userData.UserId;
            int branchId = loanData.BranchId;
            //check user is user or dealer user
            if (userData.RoleId >= 3)
            {
                //return to login
                return RedirectToAction("UserLogin", "Login", new { lbl = "You are not Allowed." });
            }
            //yes no list
            List<SelectListItem> isTitleTrackList = new List<SelectListItem>();

            isTitleTrackList.Add(new SelectListItem
            {
                Text = "Yes",
                Value = "true"
            });
            isTitleTrackList.Add(new SelectListItem
            {
                Text = "No",
                Value = "false"
            });
            ViewBag.isTitleTrack = new SelectList(isTitleTrackList, "Value", "Text");

            List<SelectListItem> isReceiptList = new List<SelectListItem>();

            isReceiptList.Add(new SelectListItem
            {
                Text = "Yes",
                Value = "true"
            });
            isReceiptList.Add(new SelectListItem
            {
                Text = "No",
                Value = "false"
            });
            ViewBag.IsReceipRequired = new SelectList(isReceiptList, "Value", "Text");
        
            //Time Limit Options
            List<SelectListItem> timeLimitList = new List<SelectListItem>();

            timeLimitList.Add(new SelectListItem
            {
                Text = "Title required to advance",
                Value = "1"
            });


            timeLimitList.Add(new SelectListItem
            {
                Text = "Title can arrive at any time",
                Value = "2"
            });

            timeLimitList.Add(new SelectListItem
            {
                Text = "Title must arrive within a specified time",
                Value = "3"
            });
            ViewBag.ReceivedTimeLimit = new SelectList(timeLimitList, "Value", "Text");
            //Receipt required methods
            List<SelectListItem> receiptRequiredMethodList = new List<SelectListItem>();

            receiptRequiredMethodList.Add(new SelectListItem
            {
                Text = "Must be physically present",
                Value = "1"
            });


            receiptRequiredMethodList.Add(new SelectListItem
            {
                Text = "Scanned copy only",
                Value = "2"
            });

            receiptRequiredMethodList.Add(new SelectListItem
            {
                Text = "Physically present or scanned copy",
                Value = "3"
            });
            ViewBag.ReceiptRequiredMethod = new SelectList(receiptRequiredMethodList, "Value", "Text");
            if (uId > 0)
            {
                LoanSetupAccess la = new LoanSetupAccess();
                TitleAccess ta = new TitleAccess();
                Title title = new Title();
                
                int loanId = loanData.loanId;
                //check loan id is greater than 0
                if (loanId > 0)
                {
                    //check is update
                    if (loanData.stepId>4)
                    {
                        //get title details
                        var titleObj = ta.getTitleDetails(loanId);
                        ViewBag.Edit = 1;
                        //check object is not null
                        if (titleObj != null)
                        {
                            if (titleObj.ReceivedTimeLimit == "Title required to advance")
                            {
                                titleObj.ReceivedTimeLimit = "1";
                            }
                            else if (titleObj.ReceivedTimeLimit == "Title can arrive at any time")
                            {
                                titleObj.ReceivedTimeLimit = "2";
                            }
                            else if (titleObj.ReceivedTimeLimit == "Title must arrive within a specified time")
                            {
                                titleObj.ReceivedTimeLimit = "3";
                            }

                            if (titleObj.ReceiptRequiredMethod == "Must be physically present")
                            {
                                titleObj.ReceiptRequiredMethod = "1";
                            }
                            else if (titleObj.ReceiptRequiredMethod == "Scanned copy only")
                            {
                                titleObj.ReceiptRequiredMethod = "2";
                            }
                            else if (titleObj.ReceiptRequiredMethod == "Physically present or scanned copy")
                            {
                                titleObj.ReceiptRequiredMethod = "3";
                            }
                           
                            ViewBag.DefaultEmail = titleObj.RemindEmail;
                        }
                        //check object is null
                        else if(titleObj == null)
                        {
                            //return to login
                            return RedirectToAction("UserLogin", "Login");
                        }
                        //check ajax request
                        if (HttpContext.Request.IsAjaxRequest())
                        {
                            ViewBag.AjaxRequest = 1;
                            return PartialView(titleObj);
                        }
                        else
                        {

                            return View(titleObj);
                        }

                    }

                    else
                    {
                        ViewBag.Edit = 0;
                        //get auto remind email given in loan setup step 1
                        string defaultEmail = la.getAutoRemindEmailByLoanId(loanId);

                        ViewBag.Email = defaultEmail;
                       //check ajax request
                        if (HttpContext.Request.IsAjaxRequest())
                        {
                            ViewBag.AjaxRequest = 1;
                            return PartialView();
                        }
                        else
                        {

                            return View();
                        }

                    }
                   
                }
                //if loan id is 0 or less than 0
                else
                {
                    //return to login page
                    return RedirectToAction("UserLogin", "Login", new { lbl = "Due to inactivity your session has timed out, please log in again." });
                }
            }
            //if logged user id is 0 or less than 0
            else
            {
                //return to login page
                return RedirectToAction("UserLogin", "Login", new { lbl = "Due to inactivity your session has timed out, please log in again." });
            }

        }