// GET: /Edit
        public ActionResult Edit(int id, string languageCode)
        {
            //Get Item
            PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage = new PolicyCarVendorGroupItemLanguage();

            policyCarVendorGroupItemLanguage = policyCarVendorGroupItemLanguageRepository.GetItem(id, languageCode);

            //Check Exists
            if (policyCarVendorGroupItemLanguage == null)
            {
                ViewData["ActionMethod"] = "EditGet";
                return(View("RecordDoesNotExistError"));
            }

            //Check AccessRights
            PolicyCarVendorGroupItem policyCarVendorGroupItem = new PolicyCarVendorGroupItem();

            policyCarVendorGroupItem = policyCarVendorGroupItemRepository.GetPolicyCarVendorGroupItem(id);
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToPolicyGroup(policyCarVendorGroupItem.PolicyGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //Language SelectList
            SelectList languageList = new SelectList(policyCarVendorGroupItemLanguageRepository.GetUnUsedLanguages(id).ToList(), "LanguageCode", "LanguageName");

            ViewData["Languages"] = languageList;

            policyCarVendorGroupItemLanguageRepository.EditItemForDisplay(policyCarVendorGroupItemLanguage);
            return(View(policyCarVendorGroupItemLanguage));
        }
        public ActionResult Delete(int id, string languageCode)
        {
            //Get Item
            PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage = new PolicyCarVendorGroupItemLanguage();

            policyCarVendorGroupItemLanguage = policyCarVendorGroupItemLanguageRepository.GetItem(id, languageCode);

            //Check Exists
            if (policyCarVendorGroupItemLanguage == null)
            {
                ViewData["ActionMethod"] = "DeleteGet";
                return(View("RecordDoesNotExistError"));
            }

            //Check AccessRights
            PolicyCarVendorGroupItem policyCarVendorGroupItem = new PolicyCarVendorGroupItem();

            policyCarVendorGroupItem = policyCarVendorGroupItemRepository.GetPolicyCarVendorGroupItem(id);
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToPolicyGroup(policyCarVendorGroupItem.PolicyGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //Add Linked Information
            policyCarVendorGroupItemLanguageRepository.EditItemForDisplay(policyCarVendorGroupItemLanguage);

            //Return View
            return(View(policyCarVendorGroupItemLanguage));
        }
        //Add Data From Linked Tables for Display
        public void EditItemForDisplay(PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage)
        {
            //Add LanguageName

            if (policyCarVendorGroupItemLanguage.LanguageCode != null)
            {
                LanguageRepository languageRepository = new LanguageRepository();
                Language           language           = new Language();
                language = languageRepository.GetLanguage(policyCarVendorGroupItemLanguage.LanguageCode);
                if (language != null)
                {
                    policyCarVendorGroupItemLanguage.LanguageName = language.LanguageName;
                }
            }

            //Add PolicyGroupName
            PolicyCarVendorGroupItemRepository policyCarVendorGroupItemRepository = new PolicyCarVendorGroupItemRepository();
            PolicyCarVendorGroupItem           policyCarVendorGroupItem           = new PolicyCarVendorGroupItem();

            policyCarVendorGroupItem = policyCarVendorGroupItemRepository.GetPolicyCarVendorGroupItem(policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId);


            if (policyCarVendorGroupItem != null)
            {
                policyCarVendorGroupItemRepository.EditItemForDisplay(policyCarVendorGroupItem);
                policyCarVendorGroupItemLanguage.PolicyGroupName = policyCarVendorGroupItem.PolicyGroupName;
                policyCarVendorGroupItemLanguage.PolicyGroupId   = policyCarVendorGroupItem.PolicyGroupId;
            }
        }
        public ActionResult Create(PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage)
        {
            PolicyCarVendorGroupItem policyCarVendorGroupItem = new PolicyCarVendorGroupItem();

            policyCarVendorGroupItem = policyCarVendorGroupItemRepository.GetPolicyCarVendorGroupItem(policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId);

            //Check Exists
            if (policyCarVendorGroupItem == null)
            {
                ViewData["ActionMethod"] = "CreatePost";
                return(View("RecordDoesNotExistError"));
            }

            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToPolicyGroup(policyCarVendorGroupItem.PolicyGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //Update  Model from Form
            try
            {
                UpdateModel(policyCarVendorGroupItemLanguage);
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }



            try
            {
                policyCarVendorGroupItemLanguageRepository.Add(policyCarVendorGroupItemLanguage);
            }
            catch (SqlException ex)
            {
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }

            return(RedirectToAction("List", new { id = policyCarVendorGroupItem.PolicyCarVendorGroupItemId }));
        }
        //Add to DB
        public void Add(PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

            db.spDesktopDataAdmin_InsertPolicyCarVendorGroupItemCarAdvice_v1(
                policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId,
                policyCarVendorGroupItemLanguage.LanguageCode,
                policyCarVendorGroupItemLanguage.CarAdvice,
                adminUserGuid
                );
        }
        //Delete From DB
        public void Delete(PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

            db.spDesktopDataAdmin_DeletePolicyCarVendorGroupItemCarAdvice_v1(
                policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId,
                policyCarVendorGroupItemLanguage.LanguageCode,
                adminUserGuid,
                policyCarVendorGroupItemLanguage.VersionNumber
                );
        }
        //GET: View
        public ActionResult View(int id, string languageCode)
        {
            PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage = new PolicyCarVendorGroupItemLanguage();

            policyCarVendorGroupItemLanguage = policyCarVendorGroupItemLanguageRepository.GetItem(id, languageCode);
            if (policyCarVendorGroupItemLanguage == null)
            {
                ViewData["ActionMethod"] = "ViewGet";
                return(View("RecordDoesNotExistError"));
            }
            policyCarVendorGroupItemLanguageRepository.EditItemForDisplay(policyCarVendorGroupItemLanguage);
            return(View(policyCarVendorGroupItemLanguage));
        }
        public ActionResult Delete(int id, string languageCode, FormCollection collection)
        {
            //Get Item
            PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage = new PolicyCarVendorGroupItemLanguage();

            policyCarVendorGroupItemLanguage = policyCarVendorGroupItemLanguageRepository.GetItem(id, languageCode);

            //Check Exists
            if (policyCarVendorGroupItemLanguage == null)
            {
                ViewData["ActionMethod"] = "DeletePost";
                return(View("RecordDoesNotExistError"));
            }

            //Check AccessRights
            PolicyCarVendorGroupItem policyCarVendorGroupItem = new PolicyCarVendorGroupItem();

            policyCarVendorGroupItem = policyCarVendorGroupItemRepository.GetPolicyCarVendorGroupItem(id);
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToPolicyGroup(policyCarVendorGroupItem.PolicyGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //Delete Item
            try
            {
                policyCarVendorGroupItemLanguage.VersionNumber = Int32.Parse(collection["VersionNumber"]);
                policyCarVendorGroupItemLanguageRepository.Delete(policyCarVendorGroupItemLanguage);
            }
            catch (SqlException ex)
            {
                //Versioning Error - go to standard versionError page
                if (ex.Message == "SQLVersioningError")
                {
                    ViewData["ReturnURL"] = "/CarAdvice.mvc/Delete/" + policyCarVendorGroupItem.PolicyCarVendorGroupItemId.ToString() + "/" + policyCarVendorGroupItemLanguage.LanguageCode;
                    return(View("VersionError"));
                }
                //Generic Error
                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }


            //Return
            return(RedirectToAction("List", new { id = policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId }));
        }
        public ActionResult Edit(int policyCarVendorGroupItemId, string languageCode, string carAdvice)
        {
            //Get Item
            PolicyCarVendorGroupItemLanguage policyCarVendorGroupItemLanguage = new PolicyCarVendorGroupItemLanguage();

            policyCarVendorGroupItemLanguage = policyCarVendorGroupItemLanguageRepository.GetItem(policyCarVendorGroupItemId, languageCode);

            //Check Exists
            if (policyCarVendorGroupItemLanguage == null)
            {
                ViewData["ActionMethod"] = "EditPost";
                return(View("RecordDoesNotExistError"));
            }

            //Check AccessRights
            PolicyCarVendorGroupItem policyCarVendorGroupItem = new PolicyCarVendorGroupItem();

            policyCarVendorGroupItem = policyCarVendorGroupItemRepository.GetPolicyCarVendorGroupItem(policyCarVendorGroupItemId);
            RolesRepository rolesRepository = new RolesRepository();

            if (!rolesRepository.HasWriteAccessToPolicyGroup(policyCarVendorGroupItem.PolicyGroupId))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //Update Item from Form
            try
            {
                UpdateModel(policyCarVendorGroupItemLanguage);
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }
            //Update CountryAdvice
            try
            {
                policyCarVendorGroupItemLanguageRepository.Update(policyCarVendorGroupItemLanguage);
            }
            catch (SqlException ex)
            {
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    ViewData["ReturnURL"] = "/CarAdvice.mvc/Edit/" + policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId.ToString() + "/" + languageCode;
                    return(View("VersionError"));
                }

                //Generic Error
                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }

            return(RedirectToAction("List", new { id = policyCarVendorGroupItemLanguage.PolicyCarVendorGroupItemId }));
        }