using LHDN.CRM.BC.Common;
using LHDN.CRM.BC.Repositories;
using LHDN.CRM.Models.Models;
using LHDN.CRM.Models.ViewModels;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System;
using System.Data;
using System.Transactions;
using System.Web;

namespace LHDN.CRM.BC.Services
{
    public class AdminService
    {
        private Repositories.LookupParamRepository repo = new Repositories.LookupParamRepository();
        private Repositories.TPFileTypeRepository repoTP = new Repositories.TPFileTypeRepository();
        private Repositories.SearchRepository repoSearch = new Repositories.SearchRepository();
        private Repositories.AdminRepository repoAdmin = new Repositories.AdminRepository();
        private Repositories.GeneralRepository repoGeneral = new Repositories.GeneralRepository();
        private static List<StatusLevel1> _lstStatLvl1;
        private static List<StatusLevel2> _lstStatLvl2;
        private static List<MainKategori> _lstMainKategori;
        private static List<MainKategori> _lstKategori1;
        private static List<SubKategori> _lstKategori2;
        private static List<InnerSubKategori> _lstKategori3;
        private static List<Sumber> _lstSumber;
        private static List<ActivityChannel> _lstActChannel;
        private static List<JenisKempen> _lstJenisKempen;
        private static List<LookupParam> _lstLookupParam;
        private static List<AuditEvent> _lstAuditEvent;
        private static List<DocList> _lstDocList;
        private static List<FieldOperatorViewModel> _lstOperators;
        private static List<FieldMappingViewModel> _lstAllFieldMapping;
        private static List<FieldMappingViewModel> _lstTPFieldMapping;
        private static List<FieldMappingViewModel> _lstCaseFieldMapping;
        private static List<FieldMappingViewModel> _lstActivityFieldMapping;
        private static List<FieldMappingViewModel> _lstOutboundFieldMapping;
        private static List<FieldMappingViewModel> _lstEmailBlastFieldMapping;
        private static List<LookupParamSys> _lstLookupParamSys;
        private static List<ParameterSettings> _lstParamSettings;
        private static List<KodSelesai> _lstKodSelesai;
        private static List<KodSelesaiViewModel> _lstKodSelesaiCC;
        private static List<TindakanSusulanViewModel> _lstAllTindakanSusulan;
        private static List<TindakanSusulan> _lstTindakanSusulanOnly;
        private static List<TindakanSusulanViewModel> _lstAdminTindakanSusulan;
        private static List<TindakanSusulanKempenViewModel> _lstAdminTindakanSusulanKempen;
        private static List<TPFileType> _lstTPFileType;
        private static List<Unit> _lstUnitList;
        private static List<UserViewModel> _lstUsers;
        private static List<OutboundCampaign> _lstOutboundCampaign;
        private static List<CCTemplate> _lstCCTemplate;
        private static List<JenisCawangan> _lstCawanganList;
        private static List<UnitType> _lstUnitTypeList;
        private static List<FunctionalRole> _lstFunctionalRole;
        private static List<AdminCCCallResultViewModel> _lstCCCallResult;
        private static List<Template> _lstTemplate;
        private static List<AutoCloseSysViewModel> _lstAutoCloseSys;
        private static List<StatusLevel1> _lstAdminStatLvl1;
        private static List<MainKategori> _lstAdminMainKategori;
        private static List<TPFileType> _lstAdminTPFileType;
        private static List<DocList> _lstAdminDocList;
        private static List<Months> _lstMonths;
        private static List<FunctionalRoleMember> _lstFunctionalRoleMember;
        private static List<UnitType> _lstAdminUnitTypeList;
        private static List<ChartAction> _lstChartAction;
        private static List<UserProfile> _lstUserProfile;

        public static List<StatusLevel1> StatusLevels
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstStatLvl1 = adminRepo.GetAllStatusLevel();
                }
                return _lstStatLvl1;
            }
        }

        public static List<StatusLevel2> StatusLevel2
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstStatLvl2 = adminRepo.GetStatusLevel2();
                }
                return _lstStatLvl2;
            }
        }

        public static List<StatusLevel1> AdminStatusLevels
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminStatLvl1 = adminRepo.GetAllAdminStatusLevel();
                }
                return _lstAdminStatLvl1;
            }
        }

        public static List<MainKategori> Kategoris
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstMainKategori = adminRepo.GetAllKategori();
                }
                return _lstMainKategori;
            }
        }

        public static List<MainKategori> Kategori1
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstKategori1 = adminRepo.GetKategori1();
                }
                return _lstKategori1;
            }
        }

        public static List<SubKategori> Kategori2
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstKategori2 = adminRepo.GetKategori2();
                }
                return _lstKategori2;
            }
        }

        public static List<InnerSubKategori> Kategori3
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstKategori3 = adminRepo.GetKategori3();
                }
                return _lstKategori3;
            }
        }
        public static List<Sumber> Sumber
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstSumber = adminRepo.GetSumberOnly();
                }
                return _lstSumber;
            }
        }

        public static List<MainKategori> AdminKategoris
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminMainKategori = adminRepo.GetAllAdminKategori();
                }
                return _lstAdminMainKategori;
            }
        }

        public static List<TPFileType> TPFileType
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstTPFileType = adminRepo.GetAllTPFIleType();
                }
                return _lstTPFileType;
            }
        }

        public static List<TPFileType> AdminTPFileType
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminTPFileType = adminRepo.GetAllAdminTPFIleType();
                }
                return _lstAdminTPFileType;
            }
        }


        public static List<ActivityChannel> ActivityChannels
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstActChannel = adminRepo.GetAllActivityChannel();
                }
                return _lstActChannel;
            }
        }

        public static List<JenisKempen> JenisKempens
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstJenisKempen = adminRepo.GetAllJenisKempen();
                }
                return _lstJenisKempen;
            }
        }

        public static List<LookupParam> LookupParams
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    //CachingService.Clear();
                    _lstLookupParam = adminRepo.GetAllLookupParam();
                }

                return _lstLookupParam;
            }
        }

        public static List<AuditEvent> AuditEvents
        {
            get
            {
                if (_lstAuditEvent == null)
                {
                    InitAllRefData();
                }

                return _lstAuditEvent;
            }
        }

        public static List<DocList> DocList
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstDocList = adminRepo.GetAllDocList();
                }
                return _lstDocList;
            }
        }

        public static List<DocList> AdminDocList
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminDocList = adminRepo.GetAllAdminDocList();
                }
                return _lstAdminDocList;
            }
        }


        public static List<LookupParamSys> LookupParamSys
        {
            get
            {
                return _lstLookupParamSys;
            }
        }

        public static List<ParameterSettings> ParameterSettings
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstParamSettings = adminRepo.GetAllParamSettingList();
                }
                return _lstParamSettings;
            }
        }

        public static List<FieldOperatorViewModel> Operators
        {
            get
            {
                return _lstOperators;
            }
        }
        //_lstKodSelesaiVM
        public static List<KodSelesai> KodSelesai
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstKodSelesai = adminRepo.GetAllKodSelesai();
                }
                return _lstKodSelesai;
            }
        }

        public static List<KodSelesaiViewModel> KodSelesaiCCResult
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstKodSelesaiCC = adminRepo.GetAllAdminKodSelesai();
                }
                return _lstKodSelesaiCC;
            }
        }


        public static List<TindakanSusulanViewModel> TindakanSusulan
        {
            get
            {
                return _lstAllTindakanSusulan;
            }
        }

        public static List<TindakanSusulan> TindakanSusulanOnly
        {
            get
            {
                return _lstTindakanSusulanOnly;
            }
        }


        //For Kod Selesai-->Tindakan Susulan Admin Used
        public static List<TindakanSusulanViewModel> AdminTindakanSusulan
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminTindakanSusulan = adminRepo.GetAdminAllTindakanSusulan();
                }
                return _lstAdminTindakanSusulan;
            }
        }

        public static List<TindakanSusulanKempenViewModel> AdminTindakanSusulanKempen
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminTindakanSusulanKempen = adminRepo.GetAdminAllTindakanSusulanKempen();
                }
                return _lstAdminTindakanSusulanKempen;
            }
        }

        public static List<FieldMappingViewModel> TPFieldMapping
        {
            get
            {
                return _lstTPFieldMapping;
            }
        }

        public static List<FieldMappingViewModel> CaseFieldMapping
        {
            get
            {
                return _lstCaseFieldMapping;
            }
        }

        public static List<FieldMappingViewModel> ActivityFieldMapping
        {
            get
            {
                return _lstActivityFieldMapping;
            }
        }

        public static List<FieldMappingViewModel> OutboundFieldMapping
        {
            get
            {
                return _lstOutboundFieldMapping;
            }
        }

        public static List<FieldMappingViewModel> EmailBlastFieldMapping
        {
            get
            {
                return _lstEmailBlastFieldMapping;
            }
        }

        public static List<Unit> Units
        {
            get
            {
                return _lstUnitList;
            }
        }

        //public static List<UserViewModel> Users
        //{
        //    get
        //    {
        //        return _lstUsers;
        //    }
        //}

        public static List<JenisCawangan> Cawangan
        {
            get
            {
                return _lstCawanganList;
            }
        }

        public static List<UnitType> UnitType
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstUnitTypeList = adminRepo.GetAllUnitType();
                }
                return _lstUnitTypeList;
            }
        }

        public static List<UnitType> AdminUnitType
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAdminUnitTypeList = adminRepo.GetAllAdminUnitType();
                }
                return _lstAdminUnitTypeList;
            }
        }

        public static List<FunctionalRole> FunctionalRole
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstFunctionalRole = adminRepo.GetAllFunctionalRoles();
                }
                return _lstFunctionalRole;
            }
        }

        public static List<UserViewModel> UserProfile
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstUsers = adminRepo.GetAllUserProfile();
                }
                return _lstUsers;
            }
        }

        public static List<AdminCCCallResultViewModel> CCCallResults
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstCCCallResult = adminRepo.GetAllCCCallResults();
                }

                return _lstCCCallResult;
            }
        }

        public static List<Template> Template
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstTemplate = adminRepo.GetAllTemplate();
                    foreach (var a in _lstTemplate)
                    {
                        a.TemplateHTML = HttpUtility.HtmlDecode(a.TemplateHTML.ToString());
                    }
                }

                return _lstTemplate;
            }
        }

        public static List<FunctionalRoleMember> FunctionRoleMember
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstFunctionalRoleMember = adminRepo.GetAllFunctionalRoleMember();

                }

                return _lstFunctionalRoleMember;
            }
        }

        public static string QRECCLevel
        {
            get
            {
                return Utilities.GetAppSetting("QRECCLevel");
            }
        }

        public static string QRPixel
        {
            get
            {
                return Utilities.GetAppSetting("QRPixel");
            }
        }

        public static string QRMimeType
        {
            get
            {
                return Utilities.GetAppSetting("QRMimeType");
            }
        }

        public static string QRQuality
        {
            get
            {
                return Utilities.GetAppSetting("QRQuality");
            }
        }

        public static List<Months> Months
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstMonths = adminRepo.GetMonths();
                }
                return _lstMonths;
            }
        }

        public static List<ChartAction> ChartAction
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstChartAction = adminRepo.GetChartAction();
                }
                return _lstChartAction;
            }
        }

        public static List<OutboundCampaign> OutboundCampaign
        {
            get
            {
                return _lstOutboundCampaign;
            }
        }

        public static List<CCTemplate> CCTemplate
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstCCTemplate = adminRepo.GetAllCCTemplate();
                }

                return _lstCCTemplate;
            }
        }


        public static List<AutoCloseSysViewModel> AutoCloseSys
        {
            get
            {
                using (AdminRepository adminRepo = new Repositories.AdminRepository())
                {
                    _lstAutoCloseSys = adminRepo.GetAutoCloseSys();
                }

                return _lstAutoCloseSys;
            }
        }

        public AdvanceRulesDropDown GetAdvanceRulesDropdown(int intTableType, int intJenisKempenID)
        {
            Constant.TableType eTableType = (Constant.TableType)intTableType;
            AdvanceRulesDropDown ddlVM = new AdvanceRulesDropDown();
            string strMEText = "Current Login User info";
            ddlVM.FieldOperatorOptions = Operators;
            switch (eTableType)
            {
                case Constant.TableType.TaxPayer:
                    ddlVM.FieldMappingOptions = TPFieldMapping.Where(x => x.IsSystemUse == false && x.IsActive == true).ToList();
                    break;
                case Constant.TableType.Case:
                    ddlVM.FieldMappingOptions = CaseFieldMapping.Where(x => x.IsSystemUse == false && x.IsActive == true).ToList();
                    break;
                case Constant.TableType.Activity:
                    ddlVM.FieldMappingOptions = ActivityFieldMapping.Where(x => x.IsSystemUse == false && x.IsActive == true).ToList();
                    break;
                case Constant.TableType.Outbound:
                    // Filter Fieldmapping options by Jenis Kempen ID
                    ddlVM.FieldMappingOptions = OutboundFieldMapping
                        .Where(x => x.JenisKempenID == (intJenisKempenID) && x.IsSystemUse == false && x.IsActive == true).ToList();
                    //ddlVM.FieldMappingOptions = OutboundFieldMapping.ToList();
                    //if (intViewID != 0)
                    //{
                    //    using (OutboundRepository outboundRepo = new Repositories.OutboundRepository())
                    //    {
                    //        ddlVM.JenisKempenID = outboundRepo.GetOutboundViewJenisKempen(intViewID);
                    //    }
                    //}
                    break;

                case Constant.TableType.Outbound_View: // referring to HCL email blast
                    ddlVM.FieldMappingOptions = EmailBlastFieldMapping.Where(x => x.IsSystemUse == false && x.IsActive == true).ToList();
                    break;
            }

            ddlVM.JenisPengenalanOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "JenisPengenalan" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());

            if (intTableType == (int)Common.Constant.TableType.TaxPayer)
            {
                ddlVM.HonorifidOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Honorifid" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.MaritalOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Marital" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.TitlenameOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "TitleName" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.VIPKategoriOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "VIPKategori" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.NegaraOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Negara" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.KelasOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Kelas" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.JantinaOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Jantina" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.StatusFailPenduaOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "StatusFailPendua" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.WarganegaraOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Warganegara" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.StatusFailOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "StatusFail" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.BahasaPerhubunganOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "BahasaPehubungan" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.UnitOptions = AdminService.Units.Where(x => x.HierarchyType == (int)Constant.LookupParamSys.HierarchyTypeCawangan && string.IsNullOrEmpty(x.BranchCode) == false && x.IsActive == true)
                                                        .Select(x => new DropdownDetailViewModel
                                                        { Value = x.UnitID, Text = x.UnitName, IsDefault = false, SeqNo = 0 }).
                                                        OrderBy(x => x.Text).ToList();
            }

            else if (intTableType == (int)Common.Constant.TableType.Case)
            {
                ddlVM.StatusLevel1Options = Utilities.SortDDL(AdminService.StatusLevels.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.StatusLevel1ID, Text = x.StatusLevel1Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.StatusLevel2Options = Utilities.SortDDL(AdminService.StatusLevel2.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.StatusLevel2ID, Text = x.StatusLevel2Name, IsDefault = false, SeqNo = 0 }).ToList());
                //TODO for status 2,3 kategori 2,3
                ddlVM.Kategori1Options = Utilities.SortDDL(AdminService.Kategori1.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.Kategori1ID, Text = x.Kategori1Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.Kategori2Options = Utilities.SortDDL(AdminService.Kategori2.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.Kategori2ID, Text = x.Kategori2Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.Kategori3Options = Utilities.SortDDL(AdminService.Kategori3.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.Kategori3ID, Text = x.Kategori3Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.ActivityChannelOptions = Utilities.SortDDL(AdminService.ActivityChannels.Select(x => new DropdownDetailViewModel { Value = x.ActChannelID, Text = x.ActChannelName, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.KodSelesaiOptions = Utilities.SortDDL(AdminService.KodSelesai.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.KodSelesaiID, Text = x.KodSelesaiDesc, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.TindakanSusulanOptions = Utilities.SortDDL(AdminService.TindakanSusulanOnly.Select(x => new DropdownDetailViewModel { Value = x.TindakanSusulanID, Text = x.TindakanSusulanDesc, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.AliranOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Aliran" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.MonthOptions = AdminService.Months.Select(x => new DropdownDetailViewModel { Value = x.MonthID, Text = x.Name, IsDefault = false, SeqNo = x.MonthID }).ToList();
                ddlVM.TahapOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Tahap" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                //ddlVM.UnitOptions = AdminService.Units.Where(x => x.IsActive == true)
                //                                            .Select(x => new DropdownDetailViewModel
                //                                            { Value = x.UnitID, Text = x.UnitName, AdditionalInfo = x.AdditionalInfo, IsDefault = false, SeqNo = 0 }).
                //                                            OrderBy(x => x.Text).ToList();
                //ddlVM.UnitOptions.Insert(0, new DropdownDetailViewModel
                //{ Value = -999, Text = "[ME]", IsDefault = false, SeqNo = 0, AdditionalInfo = strMEText });
                ddlVM.UnitOptions = new List<DropdownDetailViewModel>();
                ddlVM.UnitOptions.Add(new DropdownDetailViewModel
                { Value = -999, Text = "[ME]", IsDefault = false, SeqNo = 0, AdditionalInfo = strMEText });
                //ddlVM.UserOptions = AdminService.UserProfile//.Where(x => x.IsActive == true)
                //                                            .Select(x => new DropdownDetailViewModel
                //                                            { Value = x.UserProfileID.ToIntSafe(), Text = x.DisplayName, IsDefault = false, SeqNo = 0 }).
                //                                            OrderBy(x => x.Text).ToList();
                ddlVM.UserOptions = new List<DropdownDetailViewModel>();
                ddlVM.UserOptions.Insert(0, new DropdownDetailViewModel
                { Value = -999, Text = "[ME]", IsDefault = false, SeqNo = 0, AdditionalInfo = strMEText });
                ddlVM.SumberOptions = Utilities.SortDDL(AdminService.Sumber.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.SumberID, Text = x.SumberName, IsDefault = false, SeqNo = 0 }).ToList());

            }
            else if (intTableType == (int)Common.Constant.TableType.Activity)
            {
                ddlVM.ActivityStatusOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "ActivityStatus" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.StatusLevel1Options = Utilities.SortDDL(AdminService.StatusLevels.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.StatusLevel1ID, Text = x.StatusLevel1Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.StatusLevel2Options = Utilities.SortDDL(AdminService.StatusLevel2.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.StatusLevel2ID, Text = x.StatusLevel2Name, IsDefault = false, SeqNo = 0 }).ToList());
                //TODO for status 2,3 kategori 2,3 sumber
                ddlVM.Kategori1Options = Utilities.SortDDL(AdminService.Kategori1.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.Kategori1ID, Text = x.Kategori1Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.Kategori2Options = Utilities.SortDDL(AdminService.Kategori2.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.Kategori2ID, Text = x.Kategori2Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.Kategori3Options = Utilities.SortDDL(AdminService.Kategori3.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.Kategori3ID, Text = x.Kategori3Name, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.SumberOptions = Utilities.SortDDL(AdminService.Sumber.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.SumberID, Text = x.SumberName, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.ActivityChannelOptions = Utilities.SortDDL(AdminService.ActivityChannels.Select(x => new DropdownDetailViewModel { Value = x.ActChannelID, Text = x.ActChannelName, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.KodSelesaiOptions = Utilities.SortDDL(AdminService.KodSelesai.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.KodSelesaiID, Text = x.KodSelesaiDesc, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.TindakanSusulanOptions = Utilities.SortDDL(AdminService.TindakanSusulanOnly.Select(x => new DropdownDetailViewModel { Value = x.TindakanSusulanID, Text = x.TindakanSusulanDesc, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.BahasaPerhubunganOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "BahasaPehubungan" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.StatusBayaranS104S22Options = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "StatusBayaranS104S22" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.TahapOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Tahap" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.JenisPelangganOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "JenisPelanggan" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.AliranOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "Aliran" && x.IsActive == true && !string.IsNullOrEmpty(x.ParamDesc)).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
                ddlVM.MonthOptions = AdminService.Months.Select(x => new DropdownDetailViewModel { Value = x.MonthID, Text = x.Name, IsDefault = false, SeqNo = x.MonthID }).ToList();
                //ddlVM.UnitOptions = AdminService.Units.Where(x => x.IsActive == true)
                //                                            .Select(x => new DropdownDetailViewModel
                //                                            { Value = x.UnitID, Text = x.UnitName, AdditionalInfo = x.AdditionalInfo, IsDefault = false, SeqNo = 0 }).
                //                                            OrderBy(x => x.Text).ToList();
                ddlVM.UnitOptions = new List<DropdownDetailViewModel>();
                ddlVM.UnitOptions.Insert(0, new DropdownDetailViewModel
                { Value = -999, Text = "[ME]", IsDefault = false, SeqNo = 0, AdditionalInfo = strMEText });
                //ddlVM.UserOptions = AdminService.UserProfile.Where(x => x.IsActive == true)
                //                                            .Select(x => new DropdownDetailViewModel
                //                                            { Value = x.UserProfileID.ToIntSafe(), Text = x.DisplayName, IsDefault = false, SeqNo = 0 }).
                //                                            OrderBy(x => x.Text).ToList();
                ddlVM.UserOptions = new List<DropdownDetailViewModel>();
                ddlVM.UserOptions.Insert(0, new DropdownDetailViewModel
                { Value = -999, Text = "[ME]", IsDefault = false, SeqNo = 0, AdditionalInfo = strMEText });
                ddlVM.CFFMainKategoriOptions = repoAdmin.GetCFFMainKategori().Select(x=> new DropdownDetailViewModel { Value = Convert.ToInt32(x.CFFMainKategoriID),Text = x.CFFMainKategoriName } ).ToList();
                ddlVM.CFFKategoriOptions = repoAdmin.GetAllCFFSubKategori().OrderBy(x=>x.SubKategoriName).Select(x => new DropdownDetailViewModel { Value = Convert.ToInt32(x.CFFKategoriID), Text = x.SubKategoriName }).ToList();
            }
            else
            {
                int intJenisKempenSysID = 0;
                intJenisKempenSysID = AdminService.JenisKempens.Where(x => x.IsActive == true && x.JenisKempenID == intJenisKempenID)
                                                                .Select(x => x.JenisKempenKategoriSysID).FirstOrDefault();
                ddlVM.JenisKempenIDOptions = Utilities.SortDDL(AdminService.JenisKempens.Where(x => (x.JenisKempenKategoriSysID == (int)Constant.JenisKempenKategori.KesAnsuran ||
                                                                                                    x.JenisKempenKategoriSysID == (int)Constant.JenisKempenKategori.KesCDLYDL ||
                                                                                                    x.JenisKempenKategoriSysID == (int)Constant.JenisKempenKategori.KesCTL ||
                                                                                                    x.JenisKempenKategoriSysID == (int)Constant.JenisKempenKategori.KempenHCL) &&
                                                                                                    x.IsActive == true && x.JenisKempenKategoriSysID == (intJenisKempenSysID == 0 ? x.JenisKempenKategoriSysID : intJenisKempenSysID))
                                                                                                    .Select(x => new DropdownDetailViewModel
                                                                                                    {
                                                                                                        Value = x.JenisKempenID,
                                                                                                        Text = x.JenisKempenName,
                                                                                                        IsDefault = false,
                                                                                                        SeqNo = 1
                                                                                                    }).ToList());

                ddlVM.OutboundStatusIDOptions = Utilities.SortDDL(AdminService.LookupParamSys.Where(x => x.LookupCode == "OutboundStatus" &&
                                                                                                         x.LookupParamSysID != (int)Constant.OutboundStatus.NotYetCall &&
                                                                                                         x.LookupParamSysID != (int)Constant.OutboundStatus.CalledAndRescheduled &&
                                                                                                         x.LookupParamSysID != (int)Constant.OutboundStatus.CancelledByBatch &&
                                                                                                         x.LookupParamSysID != (int)Constant.OutboundStatus.Sent &&
                                                                                                         x.LookupParamSysID != (int)Constant.OutboundStatus.NotSent &&
                                                                                                         x.LookupParamSysID != (int)Constant.OutboundStatus.CancelledFromCC)
                                                                                                .Select(x => new DropdownDetailViewModel
                                                                                                {
                                                                                                    Value = x.LookupParamSysID,
                                                                                                    Text = x.ParamDesc,
                                                                                                    IsDefault = false,
                                                                                                }).ToList());
                ddlVM.KodSelesaiOptions = Utilities.SortDDL(AdminService.KodSelesai.Where(x => x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.KodSelesaiID, Text = x.KodSelesaiDesc, IsDefault = false, SeqNo = 0 }).ToList());
                ddlVM.TindakanSusulanOptions = Utilities.SortDDL(AdminService.TindakanSusulan.Where(x => x.JenisKempenID == intJenisKempenID).Select(x => new DropdownDetailViewModel { Value = x.TindakanSusulanID, Text = x.TindakanSusulanDesc, IsDefault = false, SeqNo = 0 }).ToList());
            }
            return ddlVM;
        }

        public List<DropdownDetailViewModel> SearchUnit(string strName)
        {
            List<DropdownDetailViewModel> ddlVM;
            //ddlVM.UnitOptions = AdminService.Units.Where(x => x.IsActive == true)
            //                                            .Select(x => new DropdownDetailViewModel
            //                                            { Value = x.UnitID, Text = x.UnitName, AdditionalInfo = x.AdditionalInfo, IsDefault = false, SeqNo = 0 }).
            //                                            OrderBy(x => x.Text).ToList();
            ddlVM = new List<DropdownDetailViewModel>();
            string strMEText = "Click to use Current User or Type to search new";
            if (!String.IsNullOrEmpty(strName) && strName.Length > 2)
            {
                ddlVM = AdminService.Units.Select(x => new DropdownDetailViewModel
                { Value = x.UnitID, Text = x.UnitName, AdditionalInfo = x.AdditionalInfo, IsDefault = false, SeqNo = 0 })
                                                       .Where(x => x.Text.ToUpper().Contains(strName.ToUpper())).OrderBy(x => x.Text).ToList();

            }

            ddlVM.Insert(0, new DropdownDetailViewModel
            {
                Value = -999,
                Text = "[ME]",
                IsDefault = false,
                SeqNo = 0,
                AdditionalInfo = strMEText
            });


            return ddlVM;
        }

        public DropdownDetailViewModel SearchUnitByID(int intUnitID)
        {
            string strMEText = "Click to use Current User or Type to search new";

            DropdownDetailViewModel ddlVM;
            ddlVM = new DropdownDetailViewModel();
            if (intUnitID == -999)
            {
                ddlVM = new DropdownDetailViewModel
                {
                    Value = -999,
                    Text = "[ME]",
                    IsDefault = false,
                    SeqNo = 0,
                    AdditionalInfo = strMEText
                };
            }
            else
            {
                ddlVM = AdminService.Units.AsEnumerable().Select(x => new DropdownDetailViewModel
                { Value = x.UnitID, Text = x.UnitName, AdditionalInfo = x.AdditionalInfo, IsDefault = false, SeqNo = 0 })
                                                        .Where(x => x.Value == intUnitID).OrderBy(x => x.Text).ToList()[0];
            }

            return ddlVM;
        }

        public List<DropdownDetailViewModel> SearchUser(string strName)
        {
            List<DropdownDetailViewModel> ddlVM;
            ddlVM = new List<DropdownDetailViewModel>();
            string strMEText = "Click to use Current User or Type to search new";

            if (!String.IsNullOrEmpty(strName) && strName.Length > 2)
            {
                ddlVM = AdminService.UserProfile.Select(x => new DropdownDetailViewModel
                { Value = x.UserProfileID.ToIntSafe(), Text = x.DisplayName, AdditionalInfo = x.AdditionalInfo + " " + x.UnitName, IsDefault = false, SeqNo = 0 })
                                                        .Where(x => x.Text.ToUpper().Contains(strName.ToUpper())).OrderBy(x => x.Text).ToList();
            }

            ddlVM.Insert(0, new DropdownDetailViewModel
            {
                Value = -999,
                Text = "[ME]",
                IsDefault = false,
                SeqNo = 0,
                AdditionalInfo = strMEText
            });


            return ddlVM;
        }

        public DropdownDetailViewModel SearchUserByID(int intUserprofileID)
        {
            string strMEText = "Click to use Current User or Type to search new";

            DropdownDetailViewModel ddlVM;
            ddlVM = new DropdownDetailViewModel();
            if (intUserprofileID == -999)
            {
                ddlVM = new DropdownDetailViewModel
                {
                    Value = -999,
                    Text = "[ME]",
                    IsDefault = false,
                    SeqNo = 0,
                    AdditionalInfo = strMEText
                };
            }
            else
            {
                ddlVM = AdminService.UserProfile.AsEnumerable().Select(x => new DropdownDetailViewModel
                { Value = x.UserProfileID.ToIntSafe(), Text = x.DisplayName, AdditionalInfo = x.AdditionalInfo + " " + x.UnitName, IsDefault = false, SeqNo = 0 })
                                                        .Where(x => x.Value == intUserprofileID)
                                                        .OrderBy(x => x.Text).ToList()[0];
            }

            return ddlVM;
        }

        public static void Init()
        {
            InitAllRefData();
        }

        private static void InitAllRefData()
        {
            using (AdminRepository adminRepo = new Repositories.AdminRepository())
            {
                //All Status level
                if (_lstStatLvl1 == null)
                {
                    _lstStatLvl1 = adminRepo.GetAllStatusLevel();
                }

                if (_lstStatLvl2 == null)
                {
                    _lstStatLvl2 = adminRepo.GetStatusLevel2();
                }

                // All Kategori level
                if (_lstMainKategori == null)
                {
                    _lstMainKategori = adminRepo.GetAllKategori();
                }

                if (_lstKategori1 == null)
                {
                    _lstKategori1 = adminRepo.GetKategori1();
                }
                if (_lstKategori2 == null)
                {
                    _lstKategori2 = adminRepo.GetKategori2();
                }
                if (_lstKategori3 == null)
                {
                    _lstKategori3 = adminRepo.GetKategori3();
                }
                if (_lstSumber == null)
                {
                    _lstSumber = adminRepo.GetSumberOnly();
                }

                // All activity channel and sumber 
                if (_lstActChannel == null)
                {
                    _lstActChannel = adminRepo.GetAllActivityChannel();
                }

                // All Jenis Kempen
                if (_lstJenisKempen == null)
                {
                    _lstJenisKempen = adminRepo.GetAllJenisKempen();
                }

                // All Lookup Param
                if (_lstLookupParam == null)
                {
                    _lstLookupParam = adminRepo.GetAllLookupParam();
                }

                if (_lstAuditEvent == null)
                {
                    _lstAuditEvent = adminRepo.GetAllAuditEvents();
                }

                //All Doc List
                if (_lstDocList == null)
                {
                    _lstDocList = adminRepo.GetAllDocList();
                }

                // All Lookup Param Sys
                if (_lstLookupParamSys == null)
                {
                    _lstLookupParamSys = adminRepo.GetAllLookupParamSysList();
                }

                //All Parameter Settings
                if (_lstParamSettings == null)
                {
                    _lstParamSettings = adminRepo.GetAllParamSettingList();
                }

                //All Kod Selesai
                if (_lstKodSelesai == null)
                {
                    _lstKodSelesai = adminRepo.GetAllKodSelesai();
                }

                //All Kod Selesai CCResult
                if (_lstKodSelesaiCC == null)
                {
                    _lstKodSelesaiCC = adminRepo.GetAllAdminKodSelesai();
                }

                //All Tindakan Susulan Only
                if (_lstTindakanSusulanOnly == null)
                {
                    _lstTindakanSusulanOnly = adminRepo.GetTindakanSusulanOnly();
                }

                //Tindakan Susulan
                if (_lstAllTindakanSusulan == null)
                {
                    _lstAllTindakanSusulan = adminRepo.GetAllTindakanSusulan();
                }

                // All CC Template
                if (_lstCCTemplate == null)
                {
                    _lstCCTemplate = adminRepo.GetAllCCTemplate();
                }

                // Advance Rules: Operators
                if (_lstOperators == null)
                {
                    _lstOperators = adminRepo.GetAllFieldOperator();
                }

                //// Advance Rules: All Field Mapping
                if (_lstAllFieldMapping == null)
                {
                    _lstAllFieldMapping = adminRepo.GetAllFieldMapping();
                }

                //// Advance Rules: TaxPayer Field Mapping
                if (_lstTPFieldMapping == null)
                {
                    _lstTPFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.TaxPayer).OrderBy(x => x.DisplayField).ToList();
                }

                if (_lstCaseFieldMapping == null)
                {
                    _lstCaseFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.Case).OrderBy(x => x.DisplayField).ToList();
                }

                if (_lstActivityFieldMapping == null)
                {
                    _lstActivityFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.Activity).OrderBy(x => x.DisplayField).ToList();
                }

                if (_lstOutboundFieldMapping == null)
                {
                    _lstOutboundFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.Outbound).OrderBy(x => x.DisplayField).ToList();
                }

                if (_lstEmailBlastFieldMapping == null)
                {
                    // Due to constant TableType 6 used, hence hardcode ==
                    _lstEmailBlastFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == 6).OrderBy(x => x.DisplayField).ToList();
                }

                // All TPFileType
                if (_lstTPFileType == null)
                {
                    _lstTPFileType = adminRepo.GetAllTPFIleType();
                }

                if (_lstUnitList == null)
                {
                    _lstUnitList = adminRepo.GetAllUnit().OrderBy(x => x.UnitName).ToList();
                }

                //if (_lstUsers == null)
                //{
                //    _lstUsers = adminRepo.GetAllUserProfile().ToList();
                //}

                if (_lstOutboundCampaign == null)
                {
                    _lstOutboundCampaign = adminRepo.GetAllCampaign();
                }

                if (_lstCawanganList == null)
                {
                    _lstCawanganList = adminRepo.GetAllJenisCawangan();
                }

                if (_lstUnitTypeList == null)
                {
                    _lstUnitTypeList = adminRepo.GetAllUnitType();
                }

                if (_lstFunctionalRole == null)
                {
                    _lstFunctionalRole = adminRepo.GetAllFunctionalRoles();
                }

                if (_lstCCCallResult == null)
                {
                    _lstCCCallResult = adminRepo.GetAllCCCallResults();
                }

                // Advance Rules: Case Field Mapping
                //if (_lstCaseFieldMapping == null)
                //{
                //    _lstCaseFieldMapping = adminRepo.GetCaseFieldMapping();
                //}

                // Advance Rules: Activity Field Mapping
                //if (_lstActFieldMapping == null)
                //{
                //    _lstActFieldMapping = adminRepo.GetActFieldMapping();
                //}

                //if (_lstFieldMapping == null)
                //{
                //    _lstFieldMapping.AddRange(
                //        _lstTPFieldMapping.Select(a => new FieldMappingViewModel{ 
                //            TableType = 1,
                //            FieldMappingID = a.FieldMappingID,
                //            DisplayField = a.DisplayField,
                //            FieldType = a.FieldType,
                //            FieldMapping = a.FieldMapping,
                //            TableName = a.TableName,
                //            FieldMappingRefCode = a.FieldMappingRefCode,
                //            IsInnerJoin = a.IsInnerJoin
                //            })
                //        );

                //    _lstFieldMapping.AddRange(
                //      _lstCaseFieldMapping.Select(a => new FieldMappingViewModel
                //      {
                //          TableType = 2,
                //          FieldMappingID = a.FieldMappingID,
                //          DisplayField = a.DisplayField,
                //          FieldType = a.FieldType,
                //          FieldMapping = a.FieldMapping,
                //          TableName = a.TableName,
                //          FieldMappingRefCode = a.FieldMappingRefCode,
                //          IsInnerJoin = a.IsInnerJoin
                //      })
                //      );

                //   _lstFieldMapping.AddRange(
                //    _lstActFieldMapping.Select(a => new FieldMappingViewModel
                //    {
                //        TableType = 3,
                //        FieldMappingID = a.FieldMappingID,
                //        DisplayField = a.DisplayField,
                //        FieldType = a.FieldType,
                //        FieldMapping = a.FieldMapping,
                //        TableName = a.TableName,
                //        FieldMappingRefCode = a.FieldMappingRefCode,
                //        IsInnerJoin = a.IsInnerJoin
                //    })
                //    );
                //}

            }
        }

        public static void ReInitAllRefData()
        {
            using (AdminRepository adminRepo = new Repositories.AdminRepository())
            {
                // Clear all userprofile cache
                CachingService.ClearAll();

                // reinit lookupparam
                _lstStatLvl1 = adminRepo.GetAllStatusLevel();
                _lstStatLvl2 = adminRepo.GetStatusLevel2();

                _lstMainKategori = adminRepo.GetAllKategori();

                _lstKategori1 = adminRepo.GetKategori1();
                _lstKategori2 = adminRepo.GetKategori2();
                _lstKategori3 = adminRepo.GetKategori3();
                _lstSumber = adminRepo.GetSumberOnly();
                _lstActChannel = adminRepo.GetAllActivityChannel();

                _lstJenisKempen = adminRepo.GetAllJenisKempen();

                _lstLookupParam = adminRepo.GetAllLookupParam();

                _lstAuditEvent = adminRepo.GetAllAuditEvents();

                _lstDocList = adminRepo.GetAllDocList();

                _lstLookupParamSys = adminRepo.GetAllLookupParamSysList();

                _lstParamSettings = adminRepo.GetAllParamSettingList();

                _lstKodSelesai = adminRepo.GetAllKodSelesai();
                _lstKodSelesaiCC = adminRepo.GetAllAdminKodSelesai();

                _lstAllTindakanSusulan = adminRepo.GetAllTindakanSusulan();

                _lstTindakanSusulanOnly = adminRepo.GetTindakanSusulanOnly();

                _lstCCTemplate = adminRepo.GetAllCCTemplate();

                _lstOperators = adminRepo.GetAllFieldOperator();

                _lstAllFieldMapping = adminRepo.GetAllFieldMapping();

                _lstTPFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.TaxPayer).OrderBy(x => x.DisplayField).ToList();

                _lstCaseFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.Case).OrderBy(x => x.DisplayField).ToList();

                _lstActivityFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.Activity).OrderBy(x => x.DisplayField).ToList();

                _lstOutboundFieldMapping = _lstAllFieldMapping.Where(x => x.TableType == (int)Constant.TableType.Outbound).OrderBy(x => x.DisplayField).ToList();
                _lstTPFileType = adminRepo.GetAllTPFIleType();

                _lstUnitList = adminRepo.GetAllUnit();

                //_lstUsers = adminRepo.GetAllUserProfile();

                _lstOutboundCampaign = adminRepo.GetAllCampaign();

                _lstFunctionalRole = adminRepo.GetAllFunctionalRoles();
            }
        }

        public List<LookupParam> GetLookupParam(string strLookupCode)
        {
            List<LookupParam> lookupParamList = new List<LookupParam>();
            if (AdminService.LookupParams != null)
            {
                lookupParamList = AdminService.LookupParams.Where(x => x.LookupCode == strLookupCode).ToList();
            }
            return lookupParamList;
        }

        //public bool AddLookupParam(LookupParam model)
        //{
        //    bool blnIsSuccess = repo.AddLookupParam(model);
        //    if (blnIsSuccess)
        //        ReloadLookupParam();

        //    return blnIsSuccess;
        //}
        public LookupParamViewModel AddLookupParam(LookupParamViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            LookupParam oriLookupParamList = new LookupParam();
            LookupParamViewModel objLookupParam = new LookupParamViewModel();
            bool blnIsSuccess = false;

            if (AdminService.LookupParams != null)
            {
                oriLookupParamList = AdminService.LookupParams.Where(x => x.LookupCode == model.LookupCode && x.LookupParamID == model.LookupParamID).FirstOrDefault();
                if (oriLookupParamList == null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(oriLookupParamList, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }
                else
                {
                    if (model.ChangeHistory != null)
                    {
                        //Get ChangeHistory
                        foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                        {
                            //Get object original value
                            string strOriValue = string.Empty;
                            var obj = Utilities.GetPropertyValue(oriLookupParamList, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                            else
                            {
                                strOriValue = "";
                            }

                            changeHistoryItem.OldValue = strOriValue;
                            changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);
                        }
                    }
                    else
                    {

                        lst.Add(BindAdminChangeHistVM(model.LookupParamID, "IsDefault", "Default?", oriLookupParamList.IsDefault.ToString(), model.IsDefault.ToString()));
                        objLookupParam.ChangeHistory = lst;
                    }
                }
            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                objLookupParam = repo.UpdateLookupParam(model);
                scope.Complete();
            }

            if (objLookupParam.LookupParamID > 0)
                ReloadLookupParam();

            if (model.ChangeHistory != null)
            {
                objLookupParam.ChangeHistory = model.ChangeHistory;
            }
            else
            {
                objLookupParam.ChangeHistory = lst;
            }

            return objLookupParam;
        }


        private void ReloadLookupParam()
        {
            _lstLookupParam = null;
            InitAllRefData();
        }

        //public TPFileTypeViewModel UpdateFileType(TPFileTypeViewModel model)
        //{
        //    TPFileTypeViewModel tpfiletypemodel = new TPFileTypeViewModel();
        //    tpfiletypemodel = repoTP.UpdateFileType(model);
        //    return tpfiletypemodel;
        //}

        public TPFileTypeViewModel UpdateFileType(TPFileTypeViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            TPFileTypeViewModel tpfiletypemodel = new TPFileTypeViewModel();
            TPFileType objOriTPFileType = new TPFileType();
            TPFileTypePrefixCls objOriTPFilePrefix = new TPFileTypePrefixCls();
            if (model.EditMenuMode == 2)
            {
                objOriTPFileType = AdminService.TPFileType.Where(s => s.TPFileTypeID == model.TPFileTypeID).FirstOrDefault();
                if (objOriTPFileType != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriTPFileType, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            var obj = Utilities.GetPropertyValue(objOriTPFileType, changeHistoryItem.FieldName);
                            strOriValue = obj == null ? "" : obj.ToString();
                        }


                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }

            }
            else
            {
                objOriTPFilePrefix = GetFileTypePrefixByFileTypeID(model.TPFileTypePrefixID);
                if (objOriTPFilePrefix != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriTPFilePrefix, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }

                        changeHistoryItem.OldValue = strOriValue;
                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }
                else
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriTPFilePrefix, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }

            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                tpfiletypemodel = repoTP.UpdateFileType(model);
                scope.Complete();
            }

            tpfiletypemodel.ChangeHistory = model.ChangeHistory;

            return tpfiletypemodel;
        }

        public TPFileTypePrefixCls GetFileTypePrefixByFileTypeID(int intTPFileTypePrefixID)
        {
            TPFileTypePrefixCls model = new TPFileTypePrefixCls();
            model = repoTP.GetFileTypePrefixByFileTypeID(intTPFileTypePrefixID);

            return model;
        }

        public List<WorkflowConfigurationViewModel> GetWorkflowConfig()
        {
            List<WorkflowConfigurationViewModel> model = new List<WorkflowConfigurationViewModel>();
            model = repoAdmin.GetWorkflowConfig();

            return model;
        }

        public DropdownViewModel GetWorkflowConfigDropdown()
        {
            DropdownViewModel ddlVM = new DropdownViewModel();
            ddlVM.MainKategoriOptions = AdminService.Kategoris.Where(x => x.IsActive == true).OrderBy(x => x.Kategori1Name).ToList<MainKategori>();
            ddlVM.JenisCawanganOptions = AdminService.Cawangan.Select(x => new DropdownDetailViewModel
            {
                Value = x.JenisCawanganID,
                Text = x.JenisCawanganName
            }).ToList();
            ddlVM.UnitTypeOptions = AdminService.UnitType.Select(x => new DropdownDetailViewModel
            {
                Value = x.UnitTypeID,
                Text = x.TypeName
            }).ToList();
            ddlVM.AutoCloseSysOptions = AdminService.AutoCloseSys.Select(x => new DropdownDetailViewModel
            {
                Value = x.AutocloseSysID,
                Text = x.Description
            }).ToList();


            ddlVM.JenisCawanganOptions = AdminService.Cawangan.Select(x => new DropdownDetailViewModel
            {
                Value = x.JenisCawanganID,
                Text = x.JenisCawanganName
            }).ToList();

            ddlVM.JenisCukaiOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "JenisCukai" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
            return ddlVM;
        }

        //public WorkflowConfigurationViewModel AddNewWorkflowConfig(WorkflowConfigurationViewModel model)
        public WorkflowConfigurationUpdateViewModel AddNewWorkflowConfig(WorkflowConfigurationUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            WorkflowConfigurationUpdateViewModel data = new WorkflowConfigurationUpdateViewModel();

            WorkflowConfigurationViewModel oriWfConfigDetails = GetWorkflowConfigByID(model.WorkflowConfig.WFConfigID);
            if (oriWfConfigDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriWfConfigDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;

                    var obj = Utilities.GetPropertyValue(oriWfConfigDetails, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }

                    changeHistoryItem.OldValue = strOriValue;
                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            data.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                data.WorkflowConfig = repoAdmin.AddNewWorkflowConfig(model.WorkflowConfig);
                scope.Complete();
            }

            return data;
        }

        public List<ChangeHistoryUpdateViewModel> DeleteCurrentWorkflowConfig(WorkflowConfigurationViewModel model, DateTime now)
        {
            WorkflowConfigurationViewModel data = new WorkflowConfigurationViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            WorkflowConfigurationViewModel oriWfConfigDetails = GetWorkflowConfigByID(model.WFConfigID);
            if (oriWfConfigDetails != null)
            {
                lst.Add(BindAdminChangeHistVM(model.WFConfigID, "IsActive", "Active?", oriWfConfigDetails.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.DeleteCurrentWorkflowConfig(model, now);
            }

            return lst;
        }

        public WorkflowConfigurationViewModel GetWorkflowConfigByID(long WFConfigID)
        {
            WorkflowConfigurationViewModel model = new WorkflowConfigurationViewModel();
            model = repoAdmin.GetWorkflowConfigByID(WFConfigID);

            return model;
        }

        public List<ChangeHistoryUpdateViewModel> InactivateLookupParam(LookupParam model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            LookupParam lookupParamList = new LookupParam();
            if (AdminService.LookupParams != null)
            {
                lookupParamList = AdminService.LookupParams.Where(x => x.LookupCode == model.LookupCode && x.LookupParamID == model.LookupParamID).FirstOrDefault();
                lst.Add(BindAdminChangeHistVM(model.LookupParamID, "IsActive", "Active?", lookupParamList.IsActive.ToString(), model.IsActive.ToString()));
                lookupParamList = repo.InactivateLookupParam(model);
            }

            return lst;
        }

        public ChangeHistoryUpdateViewModel BindAdminChangeHistVM(long lngDataID, string strFieldName, string strDisplayName, string strOldValue, string strNewValue)
        {
            ChangeHistoryUpdateViewModel changeHistVM = new ChangeHistoryUpdateViewModel();
            changeHistVM.ID = lngDataID;
            changeHistVM.FieldName = strFieldName;
            changeHistVM.DisplayName = strDisplayName;
            if (strOldValue == "True")
            {
                changeHistVM.OldValue = "Ya";
            }
            else
            {
                changeHistVM.OldValue = "Tidak";
            }

            if (strNewValue == "True")
            {
                changeHistVM.NewValue = "Ya";

            }
            else
            {
                changeHistVM.NewValue = "Tidak";
            }

            changeHistVM.IsChanged = !(changeHistVM.OldValue == changeHistVM.NewValue);

            return changeHistVM;
        }

        public List<RefActChannelViewModel> GetAllRefActChannel()
        {
            List<RefActChannelViewModel> model = new List<RefActChannelViewModel>();
            model = repoAdmin.GetAllActChannelForAdmin();

            return model;
        }

        public List<ChangeHistoryUpdateViewModel> UpdateRefActChannel(RefActivityChannelUpdateViewModel actmodel)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            RefActChannelViewModel data = new RefActChannelViewModel();

            RefActChannelViewModel oriActChannelDetail = repoAdmin.GetRefActChannelByID(actmodel.ActChannel.ActChannelID);
            if (oriActChannelDetail == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in actmodel.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriActChannelDetail, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in actmodel.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    var obj = Utilities.GetPropertyValue(oriActChannelDetail, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }

                    changeHistoryItem.OldValue = strOriValue;
                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            lst = actmodel.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                data = repoAdmin.UpdateRefActChannel(actmodel.ActChannel);
                scope.Complete();
            }

            return lst;
        }

        public List<RefSumberViewModel> GetAllSumber()
        {
            List<RefSumberViewModel> model = new List<RefSumberViewModel>();
            model = repoAdmin.GetSumberDetail();

            return model;
        }

        public DropdownViewModel GetAdminDDLOptions()
        {
            DropdownViewModel ddlVM = new DropdownViewModel();
            ddlVM.ActChannelOptions = AdminService.ActivityChannels;
            ddlVM.CCCallResultOptions = AdminService.CCCallResults;
            ddlVM.JenisKempenKategoryHRCCOption = AdminService.LookupParamSys.Where(x => x.LookupCode == "JenisKempenKategori" && (x.LookupParamSysID != (int)Constant.JenisKempenKategori.KempenHCL))
                      .Select(x => new DropdownDetailViewModel
                      {
                          Value = x.LookupParamSysID,
                          Text = x.ParamDesc,
                      }).ToList();
            ddlVM.JenisKempenKategoryHCLOption = AdminService.LookupParamSys.Where(x => x.LookupCode == "JenisKempenKategori" && (x.LookupParamSysID == (int)Constant.JenisKempenKategori.KempenHCL))
           .Select(x => new DropdownDetailViewModel
           {
               Value = x.LookupParamSysID,
               Text = x.ParamDesc,
           }).ToList();
            ddlVM.MainKategoriOptions = AdminService.Kategoris.Where(x => x.IsActive == true).OrderBy(x => x.Kategori1Name).ToList<MainKategori>();
            ddlVM.KategoriPenyelesaianOption = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "KategoriPenyelesaian" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, SeqNo = x.SeqNo }).ToList());
            ddlVM.JenisCawanganOptions = AdminService.Cawangan.Select(x => new DropdownDetailViewModel
            {
                Value = x.JenisCawanganID,
                Text = x.JenisCawanganName
            }).ToList();
            ddlVM.UnitTypeOptions = AdminService.AdminUnitType.Select(x => new DropdownDetailViewModel
            {
                Value = x.UnitTypeID,
                Text = x.TypeName
            }).ToList();
            ddlVM.FunctionalRoleOptions = AdminService.FunctionalRole;
            ddlVM.WorkflowAssignToSeksyenOption = AdminService.Units.Where(x => x.HierarchyType == (int)Constant.LookupParamSys.HierarchyTypeSeksyen && x.IsActive == true)
           .Select(x => new WorkflowUserViewModel
           {
               ID = x.UnitID.ToString(),
               DisplayName = x.UnitName,
               ParentUnitID = x.ParentUnitID,
               AdditionalInfo = x.AdditionalInfo,
               HierarchyType = x.HierarchyType,
               ManagerFunctionRoleID = x.ManagerFunctionRoleID,
               AssignToType = Convert.ToInt16(Constant.LookupParamSys.AssignToTypeUnit)
           }).OrderBy(x => x.DisplayName).ToList();

            ddlVM.WorkflowAssignToJabatanOption = AdminService.Units.Where(x => x.HierarchyType == (int)Constant.LookupParamSys.HierarchyTypeJabatan && x.IsActive == true)
                .Select(x => new WorkflowUserViewModel
                {
                    ID = x.UnitID.ToString(),
                    DisplayName = x.UnitName,
                    ParentUnitID = x.ParentUnitID,
                    AdditionalInfo = x.AdditionalInfo,
                    HierarchyType = x.HierarchyType,
                    ManagerFunctionRoleID = x.ManagerFunctionRoleID,
                    AssignToType = Convert.ToInt16(Constant.LookupParamSys.AssignToTypeUnit)
                }).OrderBy(x => x.DisplayName).ToList();

            ddlVM.WorkflowAssignToCawanganOption = AdminService.Units.Where(x => x.HierarchyType == (int)Constant.LookupParamSys.HierarchyTypeCawangan && x.IsActive == true)
                .Select(x => new WorkflowUserViewModel
                {
                    ID = x.UnitID.ToString(),
                    DisplayName = x.UnitName,
                    ParentUnitID = x.ParentUnitID,
                    AdditionalInfo = x.AdditionalInfo,
                    HierarchyType = x.HierarchyType,
                    ManagerFunctionRoleID = x.ManagerFunctionRoleID,
                    AssignToType = Convert.ToInt16(Constant.LookupParamSys.AssignToTypeUnit)
                }).OrderBy(x => x.DisplayName).ToList();

            ddlVM.WorkflowAssignToUnitOption = AdminService.Units.Where(x => x.HierarchyType == (int)Constant.LookupParamSys.HierarchyTypeUnit && x.IsActive == true)
                .Select(x => new WorkflowUserViewModel
                {
                    ID = x.UnitID.ToString(),
                    DisplayName = x.UnitName,
                    ParentUnitID = x.ParentUnitID,
                    AdditionalInfo = x.AdditionalInfo,
                    HierarchyType = x.HierarchyType,
                    ManagerFunctionRoleID = x.ManagerFunctionRoleID,
                    AssignToType = Convert.ToInt16(Constant.LookupParamSys.AssignToTypeUnit)
                }).OrderBy(x => x.DisplayName).ToList();
            ddlVM.TindakanSusulanOptions = AdminService.AdminTindakanSusulan;
            ddlVM.TindakanSusulanKempenOptions = AdminService.AdminTindakanSusulanKempen;
            ddlVM.TindakanSusulanOnlyOptions = AdminService.TindakanSusulanOnly;
            ddlVM.JenisKempenAdminOptions = AdminService.JenisKempens.Where(x => x.JenisKempenKategoriSysID != 26 && x.IsActive == true).ToList(); //exclude KempenHCL
            ddlVM.TemplateOption = AdminService.Template.Select(x => new DropdownDetailViewModel
            {
                Value = x.TemplateID,
                Text = x.TemplateName
            }).ToList();
            ddlVM.LanguageOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "BahasaPehubungan" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, Code = x.LookupValue, SeqNo = x.SeqNo }).ToList());
            ddlVM.JenisTemplateTypeOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "JenisTemplateCode" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, Code = x.LookupValue, SeqNo = x.SeqNo }).ToList());
            ddlVM.DefaultBookmarkTemplateOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "DefaultBookmarkCode" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, Code = x.LookupValue, SeqNo = x.SeqNo }).ToList());
            ddlVM.KategoriDispositionCodeOption = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "DispositionCode" && x.IsActive == true).Select(x => new DropdownDetailViewModel
            {
                Value = x.LookupParamID,
                Text = x.ParamDesc,
                IsDefault = x.IsDefault,
                SeqNo = x.SeqNo
            }).ToList());

            return ddlVM;
        }

        public SumberUpdateViewModel UpdateSumberDetails(SumberUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            RefSumberViewModel data = new RefSumberViewModel();
            SumberUpdateViewModel objModel = new SumberUpdateViewModel();

            RefSumberViewModel oriSumberDetails = GetSumberByID(model.Sumber.SumberID);
            if (oriSumberDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriSumberDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;

                    var obj = Utilities.GetPropertyValue(oriSumberDetails, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }

                    changeHistoryItem.OldValue = strOriValue;
                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            objModel.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                objModel.Sumber = repoAdmin.UpdateSumberDetail(model.Sumber);
                scope.Complete();
            }

            return objModel;
        }

        public RefSumberViewModel GetSumberByID(int lgSumberID)
        {
            RefSumberViewModel model = new RefSumberViewModel();
            model = repoAdmin.GetSumberByID(lgSumberID);

            return model;
        }

        public List<ChangeHistoryUpdateViewModel> RemoveCurrentSumberDetail(RefSumberViewModel model)
        {
            RefSumberViewModel data = new RefSumberViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            RefSumberViewModel oriSumberDetails = GetSumberByID(model.SumberID);
            if (oriSumberDetails != null)
            {
                lst.Add(BindAdminChangeHistVM(model.SumberID, "IsActive", "Active?", oriSumberDetails.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.RemoveCurrentSumberDetail(model);
            }

            return lst;
        }


        public List<JenisKempenViewModel> GetAllJenisKempenHRCC(JenisKempenViewModel model)
        {
            List<JenisKempenViewModel> JenisKempenDetail = new List<JenisKempenViewModel>();
            JenisKempenDetail = repoAdmin.GetAllJenisKempenHRCC(model);


            return JenisKempenDetail;
        }

        public JenisKempenUpdateViewModel UpdateJenisKempen(JenisKempenUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            JenisKempenUpdateViewModel data = new JenisKempenUpdateViewModel();

            JenisKempenViewModel oriJenisKempenDetails = GetJenisKempenByID(model.JenisKempen.JenisKempenID);
            if (oriJenisKempenDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriJenisKempenDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriJenisKempenDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        var obj = Utilities.GetPropertyValue(oriJenisKempenDetails, changeHistoryItem.FieldName);
                        strOriValue = obj == null ? "" : obj.ToString();
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                data.JenisKempen = repoAdmin.UpdateJenisKempen(model);
                if (data.JenisKempen.JenisKempenID > 0 && model.JenisKempen.JenisKempenID == 0)
                {
                    repoAdmin.InsertHCLEmailViewByKempenID(data.JenisKempen.JenisKempenID, model.JenisKempen.LastUpdatedBy, model.JenisKempen.LastUpdatedDatetime);
                }
                scope.Complete();
            }

            data.ChangeHistory = model.ChangeHistory;

            return data;
        }

        public JenisKempenViewModel GetJenisKempenByID(int JenisKempenID)
        {
            JenisKempenViewModel JenisKempenDetail = new JenisKempenViewModel();
            JenisKempenDetail = repoAdmin.GetJenisKempenByID(JenisKempenID);


            return JenisKempenDetail;
        }

        public List<ChangeHistoryUpdateViewModel> RemoveCurrentKempenDetail(JenisKempenViewModel model)
        {
            JenisKempenViewModel data = new JenisKempenViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            JenisKempenViewModel oriKempenDetails = GetJenisKempenByID(model.JenisKempenID);
            if (oriKempenDetails != null)
            {
                lst.Add(BindAdminChangeHistVM(model.JenisKempenID, "IsActive", "Active?", oriKempenDetails.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.RemoveCurrentKempenDetail(model);
            }

            return lst;
        }

        public List<AdminKategoryViewModel> GetAllKategoris()
        {
            List<AdminKategoryViewModel> model = new List<AdminKategoryViewModel>();
            model = repoAdmin.GetAllKategoris();

            return model;
        }

        public AdminKategoryUpdateViewModel UpdateKategoriLevel(AdminKategoryUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            AdminKategoryUpdateViewModel objKategoriModel = new AdminKategoryUpdateViewModel();
            MainKategori objOriMainKategori = new MainKategori();
            SubKategori objOriSubKategori = new SubKategori();
            InnerSubKategori objOriInnerSubKategori = new InnerSubKategori();

            if (model.MainKategori.EditMenuMode == 2)
            {
                objOriMainKategori = AdminService.AdminKategoris.Where(s => s.Kategori1ID == model.MainKategori.Kategori1ID).FirstOrDefault();
                if (objOriMainKategori != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriMainKategori, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }

                    model.MainKategori.CreatedBy = objOriMainKategori.CreatedBy;
                    model.MainKategori.CreatedDatetime = objOriMainKategori.CreatedDatetime;
                }
            }
            //3 mean Add Status level 2 and 4 mean edit status level 2
            else if (model.MainKategori.EditMenuMode == 3 || model.MainKategori.EditMenuMode == 4)
            {
                objOriSubKategori = GetSubKategoriById(model.MainKategori.Kategori2ID);
                if (objOriSubKategori != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriSubKategori, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;
                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }

                    model.MainKategori.CreatedBy = objOriSubKategori.CreatedBy;
                    model.MainKategori.CreatedDatetime = objOriSubKategori.CreatedDatetime;
                }
                else
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriSubKategori, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }

            }
            else
            {
                objOriInnerSubKategori = GetInnerSubKategoriById(model.MainKategori.Kategori3ID);
                if (objOriInnerSubKategori != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriInnerSubKategori, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;
                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }

                    model.MainKategori.CreatedBy = objOriInnerSubKategori.CreatedBy;
                    model.MainKategori.CreatedDatetime = objOriInnerSubKategori.CreatedDatetime;
                }
                else
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriInnerSubKategori, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }
            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                objKategoriModel.MainKategori = repoAdmin.UpdateKategoriLevel(model.MainKategori);
                scope.Complete();
            }
            objKategoriModel.ChangeHistory = model.ChangeHistory;

            return objKategoriModel;
            #region ddd
            //List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            //AdminKategoryUpdateViewModel data = new AdminKategoryUpdateViewModel();
            //AdminStatusLevelUpdateViewModel StatusLevelmodel = new AdminStatusLevelUpdateViewModel();
            //MainKategori objOriStatusLevel1 = new MainKategori();
            //SubKategori objOriStatusLevel2 = new SubKategori();
            //InnerSubKategori objOriStatusLevel3 = new InnerSubKategori();

            //AdminKategoryViewModel oriKategoryDetails = GetKategoriByID(model.AdminKategory.Kategori2ID);
            //if (oriKategoryDetails == null)
            //{
            //    //Get ChangeHistory
            //    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
            //    {
            //        //Get object original value
            //        string strOriValue = string.Empty;
            //        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
            //        {
            //            var obj = Utilities.GetPropertyValue(oriKategoryDetails, changeHistoryItem.FieldName);
            //            if (obj != null)
            //            {
            //                switch (obj.GetType().Name)
            //                {
            //                    case "DateTime":
            //                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
            //                        break;
            //                    case "Boolean":
            //                        bool flag = (bool)obj;
            //                        if (flag == true)
            //                            strOriValue = "Yes";
            //                        else
            //                            strOriValue = "No";
            //                        break;
            //                    case "Decimal":
            //                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
            //                        break;
            //                    default:
            //                        strOriValue = obj.ToString();
            //                        break;
            //                }
            //            }
            //        }
            //        else
            //        {
            //            strOriValue = "";
            //        }


            //        changeHistoryItem.OldValue = strOriValue;

            //        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

            //    }
            //}
            //else
            //{
            //    //Get ChangeHistory
            //    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
            //    {
            //        //Get object original value
            //        string strOriValue = string.Empty;
            //        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
            //        {
            //            var obj = Utilities.GetPropertyValue(oriKategoryDetails, changeHistoryItem.FieldName);
            //            if (obj != null)
            //            {
            //                switch (obj.GetType().Name)
            //                {
            //                    case "DateTime":
            //                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
            //                        break;
            //                    case "Boolean":
            //                        bool flag = (bool)obj;
            //                        if (flag == true)
            //                            strOriValue = "Yes";
            //                        else
            //                            strOriValue = "No";
            //                        break;
            //                    case "Decimal":
            //                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
            //                        break;
            //                    default:
            //                        strOriValue = obj.ToString();
            //                        break;
            //                }
            //            }
            //        }
            //        else
            //        {
            //            var obj = Utilities.GetPropertyValue(oriKategoryDetails, changeHistoryItem.FieldName);
            //            strOriValue = obj == null ? "" : obj.ToString();
            //        }


            //        changeHistoryItem.OldValue = strOriValue;

            //        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

            //    }
            //}

            //lst = model.ChangeHistory;

            //data.MainKategori = repoAdmin.UpdateKategoryDetail(model.AdminKategory);

            //return lst;
            #endregion
        }

        public List<ChangeHistoryUpdateViewModel> DeleteKategoriLevel(MainKategoriViewModel model)
        {
            MainKategoriViewModel data = new MainKategoriViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            MainKategori oriKategori1Details = AdminService.Kategoris.Where(s => s.Kategori1ID == model.Kategori1ID).FirstOrDefault(); ;
            if (oriKategori1Details != null)
            {
                lst.Add(BindAdminChangeHistVM(model.Kategori1ID, "IsActive", "Active?", oriKategori1Details.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.DeleteKategoriLevel(model);
            }

            return lst;
        }

        public AdminKategoryViewModel GetKategoriByID(int kategori1ID)
        {
            AdminKategoryViewModel model = new AdminKategoryViewModel();
            model = repoAdmin.GetKategoriByID(kategori1ID);

            return model;

        }

        public SubKategori GetSubKategoriById(int Kategori2ID)
        {
            SubKategori statuslvl2 = new SubKategori();
            statuslvl2 = repoAdmin.GetSubKategoriById(Kategori2ID);

            return statuslvl2;
        }

        public InnerSubKategori GetInnerSubKategoriById(int Kategori3ID)
        {
            InnerSubKategori statuslvl3 = new InnerSubKategori();
            statuslvl3 = repoAdmin.GetInnerSubKategoriById(Kategori3ID);

            return statuslvl3;
        }

        public List<ChangeHistoryUpdateViewModel> RemoveCurrentSubKategoryDetail(AdminKategoryViewModel model)
        {
            AdminKategoryViewModel data = new AdminKategoryViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            AdminKategoryViewModel oriKempenDetails = GetKategoriByID(model.Kategori2ID);
            if (oriKempenDetails != null)
            {
                lst.Add(BindAdminChangeHistVM(model.Kategori2ID, "IsActive", "Active?", oriKempenDetails.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.RemoveCurrentKempenDetail(model);
            }

            return lst;
        }

        public List<KodSelesaiViewModel> GetAllAdminKodSelesai()
        {
            List<KodSelesaiViewModel> kodSelesai = new List<KodSelesaiViewModel>();
            kodSelesai = repoAdmin.GetAllAdminKodSelesai();

            return kodSelesai;
        }

        public List<TindakanSusulanViewModel> GetAllTindakanSusulanByID(int kodSelesaiID)
        {
            List<TindakanSusulanViewModel> objTindakanSusulan = new List<TindakanSusulanViewModel>();

            return objTindakanSusulan;
        }

        public KodSelesaiUpdateViewModel UpdateKodSelesai(KodSelesaiUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            KodSelesaiUpdateViewModel objData = new KodSelesaiUpdateViewModel();
            KodSelesaiViewModel data = new KodSelesaiViewModel();

            if (model.AdminKodSelesai.IsActive == false)
            {
                //check CC_Callresult is in used or not.
                KodSelesaiViewModel cccallresult = repoAdmin.CheckCCResultUsedbySystem(model.AdminKodSelesai);
                if (cccallresult != null && cccallresult.IsSystemUsed)
                {
                    model.AdminKodSelesai.IsSystemUsed = cccallresult.IsSystemUsed;
                    objData.AdminKodSelesai = model.AdminKodSelesai;
                    return objData;
                }
            }

            KodSelesaiViewModel oriKodSelesaiDetails = GetKodSelesaiByID(model.AdminKodSelesai.KodSelesaiID);
            if (oriKodSelesaiDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriKodSelesaiDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;

                    var obj = Utilities.GetPropertyValue(oriKodSelesaiDetails, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }

                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            objData.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                objData.AdminKodSelesai = repoAdmin.UpdateKodSelesai(model.AdminKodSelesai);
                scope.Complete();
            }

            return objData;
        }

        public KodSelesaiViewModel GetKodSelesaiByID(int KodSelesaiID)
        {
            KodSelesaiViewModel model = new KodSelesaiViewModel();
            model = repoAdmin.GetKodSelesaiByID(KodSelesaiID);

            return model;
        }

        public List<ChangeHistoryUpdateViewModel> RemoveCurrentAdminKodSelesai(KodSelesaiViewModel model)
        {
            KodSelesaiViewModel data = new KodSelesaiViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            KodSelesaiViewModel oriKodSelesai = GetKodSelesaiByID(model.KodSelesaiID);
            if (oriKodSelesai != null)
            {
                lst.Add(BindAdminChangeHistVM(model.KodSelesaiID, "IsActive", "Active?", oriKodSelesai.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.RemoveCurrentAdminKodSelesai(model);
            }

            return lst;
        }

        public AdminCCTemplateNameUpdateViewModel UpdateCCTemplateName(AdminCCTemplateNameUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            AdminCCTemplateNameViewModel data = new AdminCCTemplateNameViewModel();
            AdminCCTemplateNameUpdateViewModel objModel = new AdminCCTemplateNameUpdateViewModel();

            AdminCCTemplateNameViewModel oriCCTemplateDetail = GetCCTemplateNameByID(model.CCTemplateName.CCTemplateNameID);
            if (oriCCTemplateDetail == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriCCTemplateDetail, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    var obj = Utilities.GetPropertyValue(oriCCTemplateDetail, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }

                    changeHistoryItem.OldValue = strOriValue;
                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            objModel.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                objModel.CCTemplateName = repoAdmin.UpdateCCTemplateName(model.CCTemplateName);
                scope.Complete();
            }

            return objModel;
        }

        public AdminCCTemplateNameViewModel GetCCTemplateNameByID(int CCTemplateNameID)
        {
            AdminCCTemplateNameViewModel CCTemplateName = new AdminCCTemplateNameViewModel();
            CCTemplateName = repoAdmin.GetCCTemplateNameByID(CCTemplateNameID);


            return CCTemplateName;
        }

        public List<ChangeHistoryUpdateViewModel> RemoveCurrentAdminCCTemplateName(AdminCCTemplateNameViewModel model)
        {
            AdminCCTemplateNameViewModel data = new AdminCCTemplateNameViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            AdminCCTemplateNameViewModel oriCCTemplateName = GetCCTemplateNameByID(model.CCTemplateNameID);
            if (oriCCTemplateName != null)
            {
                lst.Add(BindAdminChangeHistVM(model.CCTemplateNameID, "IsActive", "Active?", oriCCTemplateName.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.RemoveCurrentAdminCCTemplateName(model);
            }

            return lst;
        }

        public DocListUpdateViewModel UpdatedDocumentName(DocListUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            DocListViewModel DocListmodel = new DocListViewModel();
            DocListUpdateViewModel DocListUpdateModel = new DocListUpdateViewModel();
            DocList objOriParentDocList = new DocList();
            DocListDetail objOriChildDocList = new DocListDetail();
            if (model.DocList.EditMenuMode == 2)
            {
                objOriParentDocList = AdminService.AdminDocList.Where(s => s.DocListID == model.DocList.DocListID).FirstOrDefault();
                if (objOriParentDocList != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriParentDocList, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;
                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }

            }
            else
            {
                objOriChildDocList = GetDocListByID(model.DocList.DocListDetailID);
                if (objOriChildDocList != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriChildDocList, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            var obj = Utilities.GetPropertyValue(objOriChildDocList, changeHistoryItem.FieldName);
                            strOriValue = obj == null ? "" : obj.ToString();
                        }


                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }
                else
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriChildDocList, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }

            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                DocListUpdateModel.DocList = repoAdmin.UpdateDocList(model.DocList);
                scope.Complete();
            }

            DocListUpdateModel.ChangeHistory = model.ChangeHistory;

            return DocListUpdateModel;
        }

        public DocListDetail GetDocListByID(int DocListDetailID)
        {
            DocListDetail model = new DocListDetail();
            model = repoAdmin.GetDocListByID(DocListDetailID);


            return model;
        }

        public DocListUpdateViewModel RemoveAdminKategoriDocument(DocListViewModel model)
        {
            DocListUpdateViewModel mydata = new DocListUpdateViewModel();
            DocListViewModel data = new DocListViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            if (model.EditMenuMode == 2)
            {
                DocList oriKategoriDocument = AdminService.DocList.Where(x => x.DocListID == model.DocListID).FirstOrDefault();
                if (oriKategoriDocument != null)
                {
                    lst.Add(BindAdminChangeHistVM(model.DocListID, "IsActive", "Active?", oriKategoriDocument.IsActive.ToString(), model.IsActiveKategoryDoc.ToString()));
                    data = repoAdmin.RemoveAdminKategoriDocument(model);
                }
            }
            else
            {
                DocListDetail oriDocumentDetail = GetDocListByID(model.DocListDetailID);
                if (oriDocumentDetail != null)
                {
                    lst.Add(BindAdminChangeHistVM(model.DocListID, "IsActive", "Active?", oriDocumentDetail.IsActive.ToString(), model.IsActiveKategoryDoc.ToString()));
                    data = repoAdmin.RemoveAdminKategoriDocument(model);
                }
            }

            mydata.ChangeHistory = lst;
            //mydata.DocList.DocDetailList = data.DocDetailList;
            mydata.DocList = data;

            return mydata;
        }

        public AdminStatusLevelUpdateViewModel UpdatedStatusLevel(AdminStatusLevelUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            AdminStatusLevelUpdateViewModel StatusLevelmodel = new AdminStatusLevelUpdateViewModel();
            StatusLevel1 objOriStatusLevel1 = new StatusLevel1();
            StatusLevel2 objOriStatusLevel2 = new StatusLevel2();
            StatusLevel3 objOriStatusLevel3 = new StatusLevel3();

            if (model.Statuslevel1.EditMenuMode == 2)
            {
                objOriStatusLevel1 = AdminService.StatusLevels.Where(s => s.StatusLevel1ID == model.Statuslevel1.StatusLevel1ID).FirstOrDefault();
                if (objOriStatusLevel1 != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriStatusLevel1, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;
                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }

                    model.Statuslevel1.CreatedBy = objOriStatusLevel1.CreatedBy;
                    model.Statuslevel1.CreatedDatetime = objOriStatusLevel1.CreatedDatetime;
                }
            }
            //3 mean Add Status level 2 and 4 mean edit status level 2
            else if (model.Statuslevel1.EditMenuMode == 3 || model.Statuslevel1.EditMenuMode == 4)
            {
                objOriStatusLevel2 = GetStatusLevel2ById(model.Statuslevel1.StatusLevel2ID);
                if (objOriStatusLevel2 != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriStatusLevel2, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }

                    model.Statuslevel1.CreatedBy = objOriStatusLevel2.CreatedBy;
                    model.Statuslevel1.CreatedDatetime = objOriStatusLevel2.CreatedDatetime;
                }
                else
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriStatusLevel2, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }

            }
            else
            {
                objOriStatusLevel3 = GetStatusLevel3ById(model.Statuslevel1.StatusLevel3ID);
                if (objOriStatusLevel3 != null)
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        var obj = Utilities.GetPropertyValue(objOriStatusLevel3, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;
                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }

                    model.Statuslevel1.CreatedBy = objOriStatusLevel3.CreatedBy;
                    model.Statuslevel1.CreatedDatetime = objOriStatusLevel3.CreatedDatetime;
                }
                else
                {
                    //Get ChangeHistory
                    foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                    {
                        //Get object original value
                        string strOriValue = string.Empty;
                        if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                        {
                            var obj = Utilities.GetPropertyValue(objOriStatusLevel3, changeHistoryItem.FieldName);
                            if (obj != null)
                            {
                                switch (obj.GetType().Name)
                                {
                                    case "DateTime":
                                        strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                        break;
                                    case "Boolean":
                                        bool flag = (bool)obj;
                                        if (flag == true)
                                            strOriValue = "Ya";
                                        else
                                            strOriValue = "Tidak";
                                        break;
                                    case "Decimal":
                                        strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                        break;
                                    default:
                                        strOriValue = obj.ToString();
                                        break;
                                }
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                        changeHistoryItem.OldValue = strOriValue;

                        changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                    }
                }
            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                StatusLevelmodel.Statuslevel1 = repoAdmin.UpdatedStatusLevel(model.Statuslevel1);
                scope.Complete();
            }

            StatusLevelmodel.ChangeHistory = model.ChangeHistory;

            return StatusLevelmodel;
        }

        public StatusLevel2 GetStatusLevel2ById(int statusLevel2ID)
        {
            StatusLevel2 statuslvl2 = new StatusLevel2();
            statuslvl2 = repoAdmin.GetStatusLevel2ById(statusLevel2ID);

            return statuslvl2;
        }

        public StatusLevel3 GetStatusLevel3ById(int statusLevel3ID)
        {
            StatusLevel3 statuslvl3 = new StatusLevel3();
            statuslvl3 = repoAdmin.GetStatusLevel3ById(statusLevel3ID);

            return statuslvl3;
        }

        public AdminStatusLevelUpdateViewModel RemoveAdminStatusLevel(AdminStatusLevel1 model)
        {
            AdminStatusLevelUpdateViewModel mydata = new AdminStatusLevelUpdateViewModel();
            AdminStatusLevel1 data = new AdminStatusLevel1();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            if (model.EditMenuMode == 2)
            {
                StatusLevel1 oriStatusLevel1 = AdminService.StatusLevels.Where(x => x.StatusLevel1ID == model.StatusLevel1ID).FirstOrDefault();
                if (oriStatusLevel1 != null)
                {
                    lst.Add(BindAdminChangeHistVM(model.StatusLevel1ID, "IsActive", "Active?", oriStatusLevel1.IsActive.ToString(), model.IsActive.ToString()));
                    data = repoAdmin.RemoveAdminStatusLevel(model);
                }
            }
            else
            {
                StatusLevel2 oriStatusLevel2 = GetStatusLevel2ById(model.StatusLevel2ID);
                if (oriStatusLevel2 != null)
                {
                    lst.Add(BindAdminChangeHistVM(model.StatusLevel2ID, "IsActive", "Active?", oriStatusLevel2.IsActive.ToString(), model.IsActiveLevel2.ToString()));
                    data = repoAdmin.RemoveAdminStatusLevel(model);
                }
            }

            mydata.ChangeHistory = lst;
            mydata.Statuslevel1 = data;

            return mydata;
        }

        #region CFF Kategori
        public List<RefCFFKategoriUpdateViewModel> GetAllCFFKategori()
        {
            List<RefCFFKategoriUpdateViewModel> objCFFKategori = new List<RefCFFKategoriUpdateViewModel>();
            objCFFKategori = repoAdmin.GetAllCFFKategori();

            return objCFFKategori;
        }
        #endregion

        public UnitTypeUpdateViewModel UpdateUnitType(UnitTypeUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            UnitTypeUpdateViewModel data = new UnitTypeUpdateViewModel();
            bool success = false;

            UnitTypeViewModel oriUnitTypeDetails = GetUnitTypeByID(model.UnitType.UnitTypeID);
            if (oriUnitTypeDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriUnitTypeDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    var obj = Utilities.GetPropertyValue(oriUnitTypeDetails, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }

                    changeHistoryItem.OldValue = strOriValue;
                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            data.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                data.UnitType = repoAdmin.UpdateUnitType(model.UnitType);
                scope.Complete();
            }

            return data;
        }

        public UnitTypeViewModel GetUnitTypeByID(int UnitTypeID)
        {
            UnitTypeViewModel model = new UnitTypeViewModel();
            model = repoAdmin.GetUnitTypeByID(UnitTypeID);

            return model;
        }

        public List<ChangeHistoryUpdateViewModel> DeleteUnitType(UnitTypeViewModel model)
        {
            UnitTypeViewModel data = new UnitTypeViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            UnitTypeViewModel oriUnitType = GetUnitTypeByID(model.UnitTypeID);
            if (oriUnitType != null)
            {
                lst.Add(BindAdminChangeHistVM(model.UnitTypeID, "IsActive", "Active?", oriUnitType.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.DeleteUnitType(model);
            }

            return lst;
        }

        public AdminParameterSettingUpdateViewModel UpdateParameterSetting(AdminParameterSettingUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            AdminParameterSettingUpdateViewModel data = new AdminParameterSettingUpdateViewModel();
            bool success = false;

            ParameterSettingViewModel oriParameterSettingData = GetParamterSettingByID(model.ParameterSetting.ParameterID);
            if (oriParameterSettingData == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriParameterSettingData, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriParameterSettingData, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        var obj = Utilities.GetPropertyValue(oriParameterSettingData, changeHistoryItem.FieldName);
                        strOriValue = obj == null ? "" : obj.ToString();
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }

            data.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                data.ParameterSetting = repoAdmin.UpdateParameterSetting(model.ParameterSetting);
                scope.Complete();
            }

            return data;
        }

        public ParameterSettingViewModel GetParamterSettingByID(int ParameterID)
        {
            ParameterSettingViewModel model = new ParameterSettingViewModel();
            model = repoAdmin.GetParamterSettingByID(ParameterID);

            return model;
        }

        public AdminRefCFFKategoriUpdateViewModel UpdateCFFKategori(AdminRefCFFKategoriUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            AdminRefCFFKategoriUpdateViewModel data = new AdminRefCFFKategoriUpdateViewModel();

            RefCFFKategoriUpdateViewModel oriCFFKategoriDetails = GetCFFKategoriByID(model.CFFKategori.CFFKategoriID);
            if (oriCFFKategoriDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriCFFKategoriDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    var obj = Utilities.GetPropertyValue(oriCFFKategoriDetails, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }



                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
                model.CFFKategori.CreatedBy = oriCFFKategoriDetails.CreatedBy;
                model.CFFKategori.CreatedDateTime = oriCFFKategoriDetails.CreatedDateTime;
            }

            data.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                data.CFFKategori = repoAdmin.UpdateCFFKategori(model.CFFKategori);
                scope.Complete();
            }

            return data;
        }

        public RefCFFKategoriUpdateViewModel GetCFFKategoriByID(int CFFKategoriID)
        {
            RefCFFKategoriUpdateViewModel model = new RefCFFKategoriUpdateViewModel();
            model = repoAdmin.GetCFFKategoriByID(CFFKategoriID);

            return model;
        }

        public List<ChangeHistoryUpdateViewModel> DeleteCFFKategori(RefCFFKategoriUpdateViewModel model)
        {
            RefCFFKategoriUpdateViewModel data = new RefCFFKategoriUpdateViewModel();
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();

            RefCFFKategoriUpdateViewModel oriCFFKategori = GetCFFKategoriByID(model.CFFKategoriID);
            if (oriCFFKategori != null)
            {
                lst.Add(BindAdminChangeHistVM(model.CFFKategoriID, "IsActive", "Active?", oriCFFKategori.IsActive.ToString(), model.IsActive.ToString()));
                data = repoAdmin.DeleteCFFKategori(model);
            }

            return lst;
        }

        public TindakanSusulanUpdateViewModel UpdateTindakanSusulan(TindakanSusulanUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            TindakanSusulanUpdateViewModel data = new TindakanSusulanUpdateViewModel();
            bool success = false;

            TindakanSusulanViewModel oriTindakanSusulanDetails = GetTindakanSusulanByID(model.AdminTindakanSusulan.TindakanSusulanID);
            TindakanSusulanViewModel oriTindakanSusulanRoleDetails = GetTindakanSusulanRoleByID(model.AdminTindakanSusulan.TindakanSusulanID);
            List<TindakanSusulanViewModel> oriTindakanSusulanJenisKempenDetails = GetTindakanSusulanKempenByID(model.AdminTindakanSusulan.TindakanSusulanID);
            if (oriTindakanSusulanDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriTindakanSusulanDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //var obj = default(object);
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;

                    if (changeHistoryItem.FieldName == "JenisKempenName")
                    {
                        if (oriTindakanSusulanJenisKempenDetails.Count > 0)
                        {
                            // var OriValue = "";
                            foreach (var objValue in oriTindakanSusulanJenisKempenDetails)
                            {
                                var obj = Utilities.GetPropertyValue(objValue, changeHistoryItem.FieldName);
                                if (obj != null)
                                {
                                    switch (obj.GetType().Name)
                                    {
                                        case "DateTime":
                                            strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                            break;
                                        case "Boolean":
                                            bool flag = (bool)obj;
                                            if (flag == true)
                                                strOriValue = "Ya";
                                            else
                                                strOriValue = "Tidak";
                                            break;
                                        case "Decimal":
                                            strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                            break;
                                        default:
                                            if (strOriValue != "")
                                            {
                                                strOriValue = strOriValue + "," + obj.ToString();
                                            }
                                            else
                                            {
                                                strOriValue = obj.ToString();
                                            }

                                            break;
                                    }
                                }
                                else
                                {
                                    strOriValue = "";
                                }
                            }
                        }
                    }
                    else if (changeHistoryItem.FieldName == "RoleName")
                    {
                        var obj = Utilities.GetPropertyValue(oriTindakanSusulanRoleDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }
                    }
                    else
                    {
                        var obj = Utilities.GetPropertyValue(oriTindakanSusulanDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                        else
                        {
                            strOriValue = "";
                        }

                    }

                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
                model.AdminTindakanSusulan.CreatedBy = oriTindakanSusulanDetails.CreatedBy;
                model.AdminTindakanSusulan.CreatedDatetime = oriTindakanSusulanDetails.CreatedDatetime;
            }

            data.ChangeHistory = model.ChangeHistory;

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                var TindakanSusulanModel = repoAdmin.UpdateTindakanSusulan(model.AdminTindakanSusulan);
                if (TindakanSusulanModel.TindakanSusulanID > 0)
                {
                    model.AdminTindakanSusulan.TindakanSusulanID = TindakanSusulanModel.TindakanSusulanID;
                    string multipleKempen = model.AdminTindakanSusulan.JenisKempenIDMultiple;
                    int[] result = multipleKempen.Split(",").Select(int.Parse).ToArray();
                    var b = 0;
                    var c = 0;

                    if (oriTindakanSusulanJenisKempenDetails.Count > 0)
                    {
                        // For check existing kempen to avoid insert same record and add new added kempen
                        for (int i = 0; i < result.Length; i++)
                        {
                            foreach (var a in oriTindakanSusulanJenisKempenDetails)
                            {
                                if (a.JenisKempenID == result[i])
                                {
                                    b = 0;
                                    break;
                                }
                                else
                                {
                                    b = result[i];
                                }
                            }

                            if (b > 0)
                            {
                                model.AdminTindakanSusulan.JenisKempenID = b;
                                data.AdminTindakanSusulan = repoAdmin.UpdateTindakanSusulanKempen(model.AdminTindakanSusulan);
                            }
                        }

                        if (multipleKempen != null)
                        {
                            //Check latest kempen that select by user.If no longer use kempen,will be remove from db.
                            repoAdmin.DeleteUnusedKempen(multipleKempen, model.AdminTindakanSusulan.TindakanSusulanID);
                        }

                    }
                    else
                    {
                        for (int i = 0; i < result.Length; i++)
                        {
                            model.AdminTindakanSusulan.JenisKempenID = result[i];
                            data.AdminTindakanSusulan = repoAdmin.UpdateTindakanSusulanKempen(model.AdminTindakanSusulan);
                        }
                    }

                    // Update Function Role ID if gt change in term of role
                    if (oriTindakanSusulanRoleDetails != null)
                    {
                        if (oriTindakanSusulanRoleDetails.FunctionalRoleID != model.AdminTindakanSusulan.FunctionalRoleID)
                        {
                            data.AdminTindakanSusulan = repoAdmin.UpdateTindakanSusulanRoles(model.AdminTindakanSusulan);
                        }

                    }
                    else
                    {
                        data.AdminTindakanSusulan = repoAdmin.UpdateTindakanSusulanRoles(model.AdminTindakanSusulan);
                    }

                }
                scope.Complete();
            }

            return data;
        }

        public TindakanSusulanViewModel GetTindakanSusulanByID(int TindakanSusulanID)
        {
            TindakanSusulanViewModel model = new TindakanSusulanViewModel();
            model = repoAdmin.GetTindakanSusulanByID(TindakanSusulanID);

            return model;
        }
        public TindakanSusulanViewModel GetTindakanSusulanRoleByID(int TindakanSusulanID)
        {
            TindakanSusulanViewModel model = new TindakanSusulanViewModel();
            model = repoAdmin.GetTindakanSusulanRoleByID(TindakanSusulanID);

            return model;
        }
        public List<TindakanSusulanViewModel> GetTindakanSusulanKempenByID(int TindakanSusulanID)
        {
            List<TindakanSusulanViewModel> model = new List<TindakanSusulanViewModel>();
            model = repoAdmin.GetTindakanSusulanKempenByID(TindakanSusulanID);

            return model;
        }

        public TemplateUpdateViewModel UpdateTemplate(TemplateUpdateViewModel model)
        {
            List<ChangeHistoryUpdateViewModel> lst = new List<ChangeHistoryUpdateViewModel>();
            TemplateUpdateViewModel data = new TemplateUpdateViewModel();

            AdminTemplateViewModel oriTemplateDetails = GetTemplateByID(model.AdminTemplate.TemplateID);
            if (oriTemplateDetails == null)
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    if (changeHistoryItem.FieldName.IndexOf('_') > 0)
                    {
                        var obj = Utilities.GetPropertyValue(oriTemplateDetails, changeHistoryItem.FieldName);
                        if (obj != null)
                        {
                            switch (obj.GetType().Name)
                            {
                                case "DateTime":
                                    strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                    break;
                                case "Boolean":
                                    bool flag = (bool)obj;
                                    if (flag == true)
                                        strOriValue = "Ya";
                                    else
                                        strOriValue = "Tidak";
                                    break;
                                case "Decimal":
                                    strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                    break;
                                default:
                                    strOriValue = obj.ToString();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }


                    changeHistoryItem.OldValue = strOriValue;

                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }
            }
            else
            {
                //Get ChangeHistory
                foreach (ChangeHistoryUpdateViewModel changeHistoryItem in model.ChangeHistory)
                {
                    //Get object original value
                    string strOriValue = string.Empty;
                    var obj = Utilities.GetPropertyValue(oriTemplateDetails, changeHistoryItem.FieldName);
                    if (obj != null)
                    {
                        switch (obj.GetType().Name)
                        {
                            case "DateTime":
                                strOriValue = Convert.ToDateTime(obj).ToString("dd MMM yyyy");
                                break;
                            case "Boolean":
                                bool flag = (bool)obj;
                                if (flag == true)
                                    strOriValue = "Ya";
                                else
                                    strOriValue = "Tidak";
                                break;
                            case "Decimal":
                                strOriValue = String.Format("{0:N}", Convert.ToDouble(obj));
                                break;
                            default:
                                strOriValue = obj.ToString();
                                break;
                        }
                    }
                    else
                    {
                        strOriValue = "";
                    }

                    changeHistoryItem.OldValue = strOriValue;
                    changeHistoryItem.IsChanged = !(changeHistoryItem.OldValue == changeHistoryItem.NewValue);

                }

                model.AdminTemplate.CreatedBy = oriTemplateDetails.CreatedBy;
                model.AdminTemplate.CreatedByName = oriTemplateDetails.CreatedByName;
                model.AdminTemplate.CreatedDateTime = oriTemplateDetails.CreatedDateTime;
            }

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                model.AdminTemplate.TemplateHTML = model.AdminTemplate.TemplateHTML.Replace("&nbsp", "&#160;");
                model.AdminTemplate.TemplateHTML = HttpUtility.HtmlEncode(model.AdminTemplate.TemplateHTML);
                data.AdminTemplate = repoAdmin.UpdateTemplate(model.AdminTemplate);
                if (data.AdminTemplate.TemplateID > 0 && model.AdminTemplate.TemplateID == 0)
                {
                    repoAdmin.InsertBookmarkValue(model, data.AdminTemplate.TemplateID);
                }
                scope.Complete();
            }

            data.ChangeHistory = model.ChangeHistory;

            return data;
        }
        public AdminTemplateViewModel GetTemplateByID(int intTemplateID)
        {
            AdminTemplateViewModel model = new AdminTemplateViewModel();
            model = repoAdmin.GetTemplateByID(intTemplateID);

            return model;
        }

        public List<TemplateBookmark> GetAdminBookmarkTemplateByID(int intTemplateID)
        {
            List<TemplateBookmark> model = new List<TemplateBookmark>();
            model = repoAdmin.GetAdminBookmarkTemplateByID(intTemplateID);

            return model;
        }

        public List<SearchViewDetailViewModel> GetSearchViewDetailList(int intTableType)
        {
            List<SearchViewDetailViewModel> model = new List<SearchViewDetailViewModel>();

            model = repoSearch.GetSearchViewDetailList(intTableType);

            return model;
        }


        public int UpdateAdvRulesView(AdvancedRulesMainViewModel vm, string strLastUpdatedBy, DateTime dtLastUpdatedDateTime)
        {
            int intViewID = 0;


            if (vm.ViewID > 0)
            {
                // Check for update conflict
                int intViewType = 0;
                switch (vm.TableType)
                {
                    case (int)Constant.TableType.TaxPayer:
                        intViewType = (int)Constant.TableType.TP_View;
                        break;
                    case (int)Constant.TableType.Case:
                        intViewType = (int)Constant.TableType.Case_View;
                        break;
                    case (int)Constant.TableType.Activity:
                        intViewType = (int)Constant.TableType.Act_View;
                        break;
                }
                if (!Utilities.CompareByteArray(vm.ChangeStamp, repoSearch.GetChangeStampbyID(intViewType, vm.ViewID)))
                {
                    throw new UpdateConflictException();
                }

            }

            List<AdvanceRulesViewModel> lstViewRule = new List<AdvanceRulesViewModel>();
            List<RuleGroupViewModel> lstViewRuleGroup = new List<RuleGroupViewModel>();
            List<FieldValueViewModel> lstViewRuleGroupFields = new List<FieldValueViewModel>();

            vm.Rules = Utilities.GenerateRowIDForAdvanceRules(vm.Rules, false);

            foreach (AdvanceRulesViewModel item in vm.Rules)
            {
                AdvanceRulesViewModel vmViewRule = new AdvanceRulesViewModel();
                vmViewRule.ViewRuleID = item.ViewRuleID;
                vmViewRule.RuleType = item.RuleType;
                vmViewRule.RuleOperator = item.RuleOperator;
                vmViewRule.GroupingOperator = item.GroupingOperator;
                vmViewRule.Sequence = item.Sequence;
                vmViewRule.IsFromDB = item.IsFromDB;

                lstViewRule.Add(vmViewRule);

                foreach (RuleGroupViewModel itemRuleGrp in item.RuleGroup)
                {
                    RuleGroupViewModel vmViewRuleGroup = new RuleGroupViewModel();
                    vmViewRuleGroup.ViewRuleGroupID = itemRuleGrp.ViewRuleGroupID;
                    vmViewRuleGroup.ViewRuleID = itemRuleGrp.ViewRuleID;
                    vmViewRuleGroup.FieldMapping = itemRuleGrp.FieldMapping;
                    vmViewRuleGroup.FieldOperator = itemRuleGrp.FieldOperator;
                    vmViewRuleGroup.IsFromDB = itemRuleGrp.IsFromDB;

                    lstViewRuleGroup.Add(vmViewRuleGroup);

                    foreach (FieldValueViewModel itemFieldValue in itemRuleGrp.FieldValues)
                    {
                        FieldValueViewModel vmViewRuleGroupFields = new FieldValueViewModel();
                        vmViewRuleGroupFields.ViewRuleGroupFieldsID = itemFieldValue.ViewRuleGroupFieldsID;
                        vmViewRuleGroupFields.ViewRuleGroupID = itemFieldValue.ViewRuleGroupID;
                        vmViewRuleGroupFields.FieldValue = itemFieldValue.FieldValue;
                        //vmViewRuleGroupFields.IsFromDB = itemFieldValue.IsFromDB;
                        vmViewRuleGroupFields.IsFromDB = vmViewRuleGroup.IsFromDB;

                        lstViewRuleGroupFields.Add(vmViewRuleGroupFields);
                    }
                }
            }

            vm.Rules = null;//passed to lstViewRule



            intViewID = repoSearch.UpdateAdvRulesView(vm, lstViewRule,
                                            lstViewRuleGroup, lstViewRuleGroupFields,
                                            vm.DisplaySetting?.DisplayColumns?.ToList(),
                                            vm.DisplaySetting?.FieldsSorting?.ToList(), strLastUpdatedBy, dtLastUpdatedDateTime);



            if (intViewID > 0 && vm != null && vm.PermissionUsers != null)
            {
                repoGeneral.UpdatePermissionUser(vm.TableType, vm.PermissionUsers, Convert.ToInt32(intViewID));
            }

            if (intViewID > 0 && vm != null && vm.PermissionRoles != null)
            {
                repoGeneral.UpdatePermissionRole(vm.TableType, vm.PermissionRoles, Convert.ToInt32(intViewID));
            }

            return intViewID;

        }

        public DropdownViewModel GetViewPermissionDropdown()
        {
            DropdownViewModel ddlVM = new DropdownViewModel();
            ddlVM.PermissionTypeOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "PermissionType" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, Code = x.LookupValue, SeqNo = x.SeqNo }).ToList());


            return ddlVM;
        }

        public List<DashboardViewModel> GetDashboardListing(DashboardListingViewModel paging, out int intCount)
        {
            intCount = 0;
            DataSet ds = repoAdmin.GetDashboardListing(paging);
            List<DashboardViewModel> lstDashboard = new List<DashboardViewModel>();

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    lstDashboard = repo.DataTableToList<DashboardViewModel>(ds.Tables[0]).ToList();

                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                    intCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
            }
            return lstDashboard;

        }

        public DashboardViewModel GetDashboardDetails(int intDashboardID)
        {
            DashboardViewModel dashboard = new DashboardViewModel();
            dashboard = repoAdmin.GetDashboardDetails(intDashboardID);


            return dashboard;
        }

        public List<DashboardZonViewModel> GetDashboardZonList(int intDashboardID)
        {
            List<DashboardZonViewModel> dashboardZonList = new List<DashboardZonViewModel>();
            dashboardZonList = repoAdmin.GetDashboardZonList(intDashboardID);

            return dashboardZonList;
        }

        public int UpdateDashboard(DashboardViewModel vm, string strLastUpdatedBy, DateTime dtLastUpdatedDateTime)
        {
            int intDashboardID = 0;


            if (vm.DashboardID > 0)
            {
                //if (!Utilities.CompareByteArray(vm.ChangeStamp, repoSearch.GetChangeStampbyID(intViewType, vm.ViewID)))
                //{
                //    throw new UpdateConflictException();
                //}
            }

            intDashboardID = repoAdmin.UpdateDashboard(vm, vm.DashboardUnit, vm.DashboardZones, strLastUpdatedBy, dtLastUpdatedDateTime);

            return intDashboardID;
        }

        public void DeleteDashboard(DashboardViewModel vm)
        {
            repoAdmin.DeleteDashboard(vm.DashboardID);
        }

        public List<DashboardSavedViewViewModel> GetDashboardSavedView()
        {
            List<DashboardSavedViewViewModel> lstDashboardSavedView = new List<DashboardSavedViewViewModel>();
            lstDashboardSavedView = repoAdmin.GetDashboardSavedView();


            return lstDashboardSavedView;
        }

        public bool SearchDuplicateDashboardName(DashboardViewModel model)
        {
            bool blnIsDuplicate = false;
            int intCount = repoAdmin.SearchDuplicateDashboardName(model);
            if (intCount > 0) blnIsDuplicate = true;
            return blnIsDuplicate;
        }

        public AdminTemplateViewModel DeleteEmailTemplate(AdminTemplateViewModel model)
        {
            AdminTemplateViewModel emailModel = new AdminTemplateViewModel();

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                emailModel = repoAdmin.DeleteEmailTemplate(model);

                scope.Complete();
            }
           

            return emailModel;
        }

    }
}
