using LHDN.CRM.BC.Services;
using LHDN.CRM.Models.ViewModels;
using LHDN.CRM.Models.Models;
using LHDN.CRM.WebAPI.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Caching;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using LHDN.CRM.BC.Common;

namespace LHDN.CRM.WebAPI.Controllers.Api
{
    public class AdminApiController : BaseApiController
    {
        private AdminService svc = new AdminService();
        private UserProfileService userSvc = new UserProfileService();

        [HttpPost]
        [ApiErrorHandler]
        //[Authorize(Roles = "role2")]
        public IHttpActionResult UpdateAdvanceRules(List<AdvanceRulesViewModel> advanceRules)
        {
            //List<AdvanceRulesViewModel> _lst = null;
            //_lst = lstAdvanceRules;
            return Json(advanceRules);
        }

        [HttpGet]
        [ApiErrorHandler]
        //[Authorize(Roles = "role2")]
        public IHttpActionResult GetAllKodSelesai()
        {
            List<KodSelesai> _lst = null;

            _lst = AdminService.KodSelesai;

            return Json(_lst);
        }


        [HttpGet]
        [ApiErrorHandler]
        //[Authorize(Roles = "role2")]
        public IHttpActionResult GetTindakanSusulan()
        {
            List<TindakanSusulanViewModel> _lst = null;

            _lst = AdminService.TindakanSusulan;

            return Json(_lst);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAdvanceRulesDropdown(int intTableType, int intJenisKempenID)
        {
            return Json(svc.GetAdvanceRulesDropdown(intTableType, intJenisKempenID));
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult GetLookupParam(LookupParam model)
        {
            return Json(svc.GetLookupParam(model.LookupCode));
        }

        //[HttpPost]
        //[ApiErrorHandler]
        //public IHttpActionResult AddLookupParam(LookupParam model)
        //{
        //    model.LastUpdatedBy = userSvc.LoginID;
        //    model.LastUpdatedDateTime = DateTime.Now;
        //    if (model.LookupParamID == 0) //if new
        //    {
        //        model.CreatedBy = userSvc.LoginID;
        //        model.CreatedDateTime = DateTime.Now;
        //    }

        //    bool blnIsSuccess = svc.AddLookupParam(model);
        //    //TODO: Audit

        //    return Json(blnIsSuccess);
        //}

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult AddLookupParam(LookupParamViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;
            if (model.LookupParamID == 0) //if new
            {
                model.CreatedBy = userSvc.LoginID;
                model.CreatedDateTime = DateTime.Now;
            }

            var data = svc.AddLookupParam(model);

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            if (model.LookupCode == Constant.LookupCode.BahasaPehubungan)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Bahasa Perhubungan.Lookup Param ID = " + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.StatusBayaranS104S22)
            {

                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Status Bayaran S104S22.Lookup Param ID = " + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.JenisPelanggan)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Jenis Pelanggan.Lookup Param ID = " + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.CCCampaign)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin CC Campaign.Lookup Param ID = " + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.CCCampaignHCL)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin CC Campaign HCL.Lookup Param ID = " + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.DMSStatus)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin DMS Status.Lookup Param ID = " + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.ReturnedReason)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Outbound Return Status.Lookup Param ID =" + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.Tahap)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Tahap.Lookup Param ID =" + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.KategoriPenyelesaian)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Kategori Penyelesaian.Lookup Param ID =" + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.KategoriPenyelesaian)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Kategori Penyelesaian.Lookup Param ID =" + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.CCFromEmail)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin CCFromEmail.Lookup Param ID =" + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.CCFromSMS)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin CCFromSMS.Lookup Param ID =" + data.LookupParamID,
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            data.LookupParamID);
            }

            return Json(data);

        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult AddDefaultLookupParam(List<LookupParamViewModel> model)
        {
            LookupParamViewModel data = new LookupParamViewModel();
            foreach (var item in model)
            {
                item.IsDefault = !item.IsDefault;
                item.LastUpdatedBy = userSvc.LoginID;
                item.LastUpdatedDateTime = DateTime.Now;
                if (item.LookupParamID == 0) //if new
                {
                    item.CreatedBy = userSvc.LoginID;
                    item.CreatedDateTime = DateTime.Now;
                }

                data = svc.AddLookupParam(item);

                AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
                AuditService.ActionType actionType = new AuditService.ActionType();
                List<AuditDetailViewModel> lstAuditDetail = null;

                if (item.LookupCode == Constant.LookupCode.BahasaPehubungan)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin Bahasa Perhubungan.Lookup Param ID = " + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.StatusBayaranS104S22)
                {

                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin Status Bayaran S104S22.Lookup Param ID = " + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.JenisPelanggan)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin Jenis Pelanggan.Lookup Param ID = " + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.CCCampaign)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin CC Campaign.Lookup Param ID = " + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.CCCampaignHCL)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin CC Campaign HCL.Lookup Param ID = " + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.DMSStatus)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin DMS Status.Lookup Param ID = " + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.ReturnedReason)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin Outbound Return Status.Lookup Param ID =" + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.Tahap)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin Tahap.Lookup Param ID =" + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.KategoriPenyelesaian)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin Kategori Penyelesaian .Lookup Param ID =" + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.CCFromEmail)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin CCFromEmail.Lookup Param ID =" + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
                else if (item.LookupCode == Constant.LookupCode.CCFromSMS)
                {
                    auditEvent = AuditService.AuditEventID.ManageParameter;
                    actionType = AuditService.ActionType.Update;
                    lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                    {
                        FieldName = a.FieldName,
                        OldValue = a.OldValue,
                        NewValue = a.NewValue
                    }).ToArray().ToList();

                    AuditLogTxn(auditEvent,
                                AuditService.ResultType.Success,
                                actionType,
                                userSvc.LoginID,
                                DateTime.Now,
                                "Update Admin CCFromSMS.Lookup Param ID =" + data.LookupParamID,
                                lstAuditDetail,
                                "Ref_LookupParam ",
                                data.LookupParamID);
                }
            }

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetTPFileType()
        {
            return Json(AdminService.AdminTPFileType);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateFileType(TPFileTypeViewModel model)
        {
            List<TPFileType> FileTypeChild = new List<TPFileType>();

            TPFileTypeViewModel tpfiletypemodel = new TPFileTypeViewModel();
            model.Createdby = userSvc.LoginID;
            model.CreatedDateTime = DateTime.Now;
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            //tpfiletypemodel = svc.UpdateFileType(model);
            var data = svc.UpdateFileType(model);

            if (data.TPFileTypePrefixID > 0 || data.TPFileTypeID > 0)
            {
                FileTypeChild = AdminService.AdminTPFileType;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            if (model.EditMenuMode == 2)  //2 mean edit File Type
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin File Type. TP File Type ID =" + model.TPFileTypeID,
                            lstAuditDetail,
                            "Ref_TPFileType ",
                            model.TPFileTypeID);
            }
            else  //Add or Edit FileTypePrefix
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin File Type Prefix. TP File Type Prefix ID =" + data.TPFileTypePrefixID,
                            lstAuditDetail,
                            "Ref_TPFileTypePrefix ",
                            data.TPFileTypePrefixID);
            }


            return Json(FileTypeChild);

        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetWorkflowConfig()
        {
            List<WorkflowConfigurationViewModel> model = new List<WorkflowConfigurationViewModel>();
            model = svc.GetWorkflowConfig();
            return Json(model);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetWorkflowConfigDropdown()
        {
            return Json(svc.GetWorkflowConfigDropdown());
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult AddNewWorkflowConfig(WorkflowConfigurationUpdateViewModel model)
        {
            model.WorkflowConfig.CreatedBy = userSvc.LoginID;
            model.WorkflowConfig.LastUpdatedBy = userSvc.LoginID;
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.AddNewWorkflowConfig(model);

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Web Config Workflow. WF Config ID:" + data.WorkflowConfig.WFConfigID.ToString(),
                        lstAuditDetail,
                        "WF_Configuration ",
                        model.WorkflowConfig.WFConfigID);

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteCurrentWorkflowConfig(WorkflowConfigurationViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            DateTime dtNow = DateTime.Now;

            var data = svc.DeleteCurrentWorkflowConfig(model, dtNow);

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.AssignBackOffice;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Web Config Workflow. WF Config ID:" + data[0].ID.ToString(),
                        lstAuditDetail,
                        "WF_Configuration ",
                        model.WFConfigID);

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult InactivateLookupParam(LookupParam model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            var data = svc.InactivateLookupParam(model);
            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            if (model.LookupCode == Constant.LookupCode.BahasaPehubungan)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Delete;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Bahasa Perhubungan. Lookup Param ID:" + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            model.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.StatusBayaranS104S22)
            {

                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Status Bayaran S104S22.Lookup Param ID = " + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            model.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.JenisPelanggan)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Jenis Pelanggan.Lookup Param ID = " + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                           model.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.CCCampaign)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin CC Campaign.Lookup Param ID = " + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            model.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.DMSStatus)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin DMS Status.Lookup Param ID = " + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                            model.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.ReturnedReason)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Outbound Return Status.Lookup Param ID =" + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                           model.LookupParamID);
            }
            else if (model.LookupCode == Constant.LookupCode.Tahap)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Tahap.Lookup Param ID =" + data[0].ID.ToString(),
                            lstAuditDetail,
                            "Ref_LookupParam ",
                           model.LookupParamID);
            }

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllRefActChannel()
        {
            var data = svc.GetAllRefActChannel();

            return Json(data);
        }


        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateRefActChannel(RefActivityChannelUpdateViewModel ActModel)
        {
            ActModel.ActChannel.LastUpdatedBy = userSvc.LoginID;
            ActModel.ActChannel.LastUpdatedDatetime = DateTime.Now;

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateRefActChannel(ActModel);

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Activity Channel. Activity Channel ID = " + ActModel.ActChannel.ActChannelID,
                        lstAuditDetail,
                        "Ref_ActChannel",
                        ActModel.ActChannel.ActChannelID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult SearchUnitByName(string strName)
        {
            return Json(userSvc.SearchUnitByName(strName));
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllSumber()
        {
            var data = svc.GetAllSumber();

            return Json(data);
        }

        [HttpGet]
        public IHttpActionResult GetAdminDDLOptions()
        {
            return Json(svc.GetAdminDDLOptions());
        }

        public IHttpActionResult UpdateSumberDetails(SumberUpdateViewModel model)
        {
            model.Sumber.LastUpdatedBy = userSvc.LoginID;
            model.Sumber.LastUpdatedDateTime = DateTime.Now;

            if (model.Sumber.SumberID == 0)
            {
                model.Sumber.CreatedBy = userSvc.LoginID;
                model.Sumber.CreatedDateTime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateSumberDetails(model);

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Sumber. Sumber Id =" + data.Sumber.SumberID,
                        lstAuditDetail,
                        "Ref_Sumber",
                        data.Sumber.SumberID);

            return Json(data);
        }


        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult RemoveCurrentSumberDetail(RefSumberViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            var data = svc.RemoveCurrentSumberDetail(model);
            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Sumber.",
                        lstAuditDetail,
                        "Ref_Sumber ",
                        model.SumberID);

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult GetAllJenisKempen(JenisKempenViewModel model)
        {
            List<JenisKempenViewModel> JenisKempenDetail = new List<JenisKempenViewModel>();
            JenisKempenDetail = svc.GetAllJenisKempenHRCC(model);

            return Json(JenisKempenDetail);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateJenisKempen(JenisKempenUpdateViewModel model)
        {
            model.JenisKempen.LastUpdatedBy = userSvc.LoginID;
            model.JenisKempen.LastUpdatedDatetime = DateTime.Now;

            if (model.JenisKempen.JenisKempenID == 0)
            {
                model.JenisKempen.CreatedBy = userSvc.LoginID;
                model.JenisKempen.CreatedDatetime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateJenisKempen(model);

            if (model.JenisKempen.IsHRCC)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Jenis Kempen HRCC.",
                            lstAuditDetail,
                            "Ref_JenisKempen",
                            model.JenisKempen.JenisKempenID);
            }
            else
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Jenis Kempen HCL.",
                            lstAuditDetail,
                            "Ref_JenisKempen",
                            model.JenisKempen.JenisKempenID);
            }


            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult RemoveCurrentKempenDetail(JenisKempenViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDatetime = DateTime.Now;

            var data = svc.RemoveCurrentKempenDetail(model);

            //TODO: Audit
            if (model.IsHRCC)
            {
                AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
                AuditService.ActionType actionType = new AuditService.ActionType();
                List<AuditDetailViewModel> lstAuditDetail = null;

                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Delete;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin HRCC.",
                            lstAuditDetail,
                            "Ref_JenisKempen ",
                            model.JenisKempenID);
            }
            else
            {
                AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
                AuditService.ActionType actionType = new AuditService.ActionType();
                List<AuditDetailViewModel> lstAuditDetail = null;

                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Delete;
                lstAuditDetail = data.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin HCL.",
                            lstAuditDetail,
                            "Ref_JenisKempen ",
                            model.JenisKempenID);
            }


            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllKategori()
        {
            var data = AdminService.AdminKategoris;

            return Json(data);
        }

        //[HttpGet]
        //[ApiErrorHandler]
        //public IHttpActionResult GetAllKategoris()
        //{
        //    List<AdminKategoryViewModel> model = new List<AdminKategoryViewModel>();
        //    model = svc.GetAllKategoris();

        //    return Json(model);
        //}

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateKategoriLevel(AdminKategoryUpdateViewModel model)
        {
            model.MainKategori.LastUpdatedBy = userSvc.LoginID;
            model.MainKategori.LastUpdatedDatetime = DateTime.Now;

            if (model.MainKategori.Kategori1ID == 0 || model.MainKategori.Kategori2ID == 0 || model.MainKategori.Kategori3ID == 0)
            {
                model.MainKategori.CreatedBy = userSvc.LoginID;
                model.MainKategori.CreatedDatetime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateKategoriLevel(model);
            var kategoriData = AdminService.AdminKategoris;

            if (model.MainKategori.EditMenuMode == 2 || model.MainKategori.EditMenuMode == 1)  //2 mean Add or Edit Status Level 1
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Ref Kategori 1. Kategori 1 ID = " + data.MainKategori.Kategori1ID,
                            lstAuditDetail,
                            "Ref_Kategori1",
                            data.MainKategori.Kategori1ID);
            }
            else if (model.MainKategori.EditMenuMode == 3 || model.MainKategori.EditMenuMode == 4)  //Add or Edit Status Level 2
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Ref Kategori 2. Kategori 2 ID= " + data.MainKategori.Kategori2ID,
                            lstAuditDetail,
                            "Ref_Kategori2",
                            data.MainKategori.Kategori2ID);
            }
            else
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Ref Kategori 3. Kategori 3 ID = " + data.MainKategori.Kategori3ID,
                            lstAuditDetail,
                            "Ref_Kategori3",
                            data.MainKategori.Kategori3ID);
            }

            return Json(kategoriData);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteKategoriLevel(MainKategoriViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDatetime = DateTime.Now;

            var data = svc.DeleteKategoriLevel(model);
            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Kategori.",
                        lstAuditDetail,
                        "Ref_Kategori1 ",
                        model.Kategori1ID);

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult RemoveCurrentSubKategoryDetail(AdminKategoryViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDatetime = DateTime.Now;

            var data = svc.RemoveCurrentSubKategoryDetail(model);

            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Kategori 2.",
                        lstAuditDetail,
                        "Ref_Kategori2 ",
                        model.Kategori2ID);


            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllAdminKodSelesai()
        {
            List<KodSelesaiViewModel> model = new List<KodSelesaiViewModel>();
            model = svc.GetAllAdminKodSelesai();

            return Json(model);
        }


        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateKodSelesai(KodSelesaiUpdateViewModel model)
        {
            model.AdminKodSelesai.LastUpdatedBy = userSvc.LoginID;
            model.AdminKodSelesai.LastUpdatedDateTime = DateTime.Now;

            if (model.AdminKodSelesai.KodSelesaiID == 0)
            {
                model.AdminKodSelesai.CreatedBy = userSvc.LoginID;
                model.AdminKodSelesai.CreatedDateTime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateKodSelesai(model);

            if (data.AdminKodSelesai.IsSystemUsed == false)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Kod Selesai. Kod Selesai ID = " + data.AdminKodSelesai.KodSelesaiID,
                            lstAuditDetail,
                            "Ref_KodSelesai",
                            data.AdminKodSelesai.KodSelesaiID);
            }
            else
            {
                data.AdminKodSelesai.KodSelesaiID = -1;
            }


            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult RemoveCurrentAdminKodSelesai(KodSelesaiViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            var data = svc.RemoveCurrentAdminKodSelesai(model);
            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Kod Selesai.",
                        lstAuditDetail,
                        "Ref_Sumber ",
                        model.KodSelesaiID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllStatusLevel()
        {
            var data = AdminService.AdminStatusLevels;

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllCCTemplateName(string IsEmailCCTemplate)
        {
            var data = AdminService.CCTemplate.Where(s => s.CCTemplateType == IsEmailCCTemplate).ToList();

            return Json(data);
        }

        public IHttpActionResult UpdateCCTemplateName(AdminCCTemplateNameUpdateViewModel model)
        {
            model.CCTemplateName.LastUpdatedBy = userSvc.LoginID;
            model.CCTemplateName.LastUpdatedDateTime = DateTime.Now;

            if (model.CCTemplateName.CCTemplateNameID == 0)
            {
                model.CCTemplateName.CreatedBy = userSvc.LoginID;
                model.CCTemplateName.CreatedDateTime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateCCTemplateName(model);

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin CCTemplateName. CCTemplateNameID =" + data.CCTemplateName.CCTemplateNameID,
                        lstAuditDetail,
                        "Ref_CCTemplateName",
                        data.CCTemplateName.CCTemplateNameID);

            return Json(data);
        }

        public IHttpActionResult RemoveCurrentAdminCCTemplateName(AdminCCTemplateNameViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            var data = svc.RemoveCurrentAdminCCTemplateName(model);
            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin CCTemplateName .",
                        lstAuditDetail,
                        "Ref_CCTemplateName ",
                        model.CCTemplateNameID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetSenaraiPenerimaanDoc()
        {
            var data = AdminService.AdminDocList;

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdatedDocumentName(DocListUpdateViewModel model)
        {
            List<DocList> DocList = new List<DocList>();

            DocListViewModel DocListmodel = new DocListViewModel();
            model.DocList.CreatedBy = userSvc.LoginID;
            model.DocList.CreatedDateTime = DateTime.Now;
            model.DocList.LastUpdatedBy = userSvc.LoginID;
            model.DocList.LastUpdatedDateTime = DateTime.Now;

            var data = svc.UpdatedDocumentName(model);

            if (data.DocList.DocListID > 0 || data.DocList.DocListDetailID > 0)
            {
                DocList = AdminService.AdminDocList;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            if (model.DocList.EditMenuMode == 2 || model.DocList.EditMenuMode == 1)  //2 mean edit File Type
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Senarai Penerimaan Document List . DocListID:" + data.DocList.DocListID,
                            lstAuditDetail,
                            "Ref_DocList ",
                            data.DocList.DocListID);
            }
            else  //Add or Edit FileTypePrefix
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin  Senarai Penerimaan Document List Detail. DocListDetailID:" + data.DocList.DocListDetailID,
                            lstAuditDetail,
                            "Ref_DocListDetail ",
                            data.DocList.DocListDetailID);
            }


            return Json(DocList);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult RemoveAdminKategoriDocument(DocListViewModel model)
        {
            List<DocList> DocList = new List<DocList>();
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            var data = svc.RemoveAdminKategoriDocument(model);

            DocList = AdminService.DocList;

            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Penerimaan Doc List ." + model.DocListID,
                        lstAuditDetail,
                        "Ref_DocList ",
                        model.DocListID);

            return Json(DocList);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdatedStatusLevel(AdminStatusLevelUpdateViewModel statusModel)
        {
            statusModel.Statuslevel1.LastUpdatedBy = userSvc.LoginID;
            statusModel.Statuslevel1.LastUpdatedDatetime = DateTime.Now;

            if (statusModel.Statuslevel1.StatusLevel1ID == 0 || statusModel.Statuslevel1.StatusLevel2ID == 0
                || statusModel.Statuslevel1.StatusLevel3ID == 0)
            {
                statusModel.Statuslevel1.CreatedBy = userSvc.LoginID;
                statusModel.Statuslevel1.CreatedDatetime = DateTime.Now;
            }

            var data = svc.UpdatedStatusLevel(statusModel);

            var StatusData = AdminService.AdminStatusLevels;

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            if (statusModel.Statuslevel1.EditMenuMode == 2 || statusModel.Statuslevel1.EditMenuMode == 1)  //2 mean Add or Edit Status Level 1
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Status Level 1. StatusLevel1ID:" + statusModel.Statuslevel1.StatusLevel1ID,
                            lstAuditDetail,
                            "Ref_StatusLevel1",
                            statusModel.Statuslevel1.StatusLevel1ID);
            }
            else if (statusModel.Statuslevel1.EditMenuMode == 3 || statusModel.Statuslevel1.EditMenuMode == 4)  //Add or Edit Status Level 2
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Status Level 2. StatusLevel2ID:" + data.Statuslevel1.StatusLevel2ID,
                            lstAuditDetail,
                            "Ref_StatusLevel2",
                            data.Statuslevel1.StatusLevel2ID);
            }
            else
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.FieldName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Status Level 3. StatusLevel3ID:" + data.Statuslevel1.StatusLevel3ID,
                            lstAuditDetail,
                            "Ref_StatusLevel3",
                            data.Statuslevel1.StatusLevel3ID);
            }


            return Json(StatusData);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult RemoveAdminStatusLevel(AdminStatusLevel1 model)
        {
            List<StatusLevel1> DocList = new List<StatusLevel1>();
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDatetime = DateTime.Now;

            var data = svc.RemoveAdminStatusLevel(model);

            DocList = AdminService.StatusLevels;

            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Status Level 1  ." + model.StatusLevel1ID,
                        lstAuditDetail,
                        "Ref_StatusLevel1 ",
                        model.StatusLevel1ID);

            return Json(DocList);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllCFFKategori()
        {
            List<RefCFFKategoriUpdateViewModel> objCffKategori = new List<RefCFFKategoriUpdateViewModel>();
            objCffKategori = svc.GetAllCFFKategori();

            return Json(objCffKategori);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateCFFKategori(AdminRefCFFKategoriUpdateViewModel model)
        {
            model.CFFKategori.LastUpdatedBy = userSvc.LoginID;
            model.CFFKategori.LastUpdatedDateTime = DateTime.Now;

            if (model.CFFKategori.CFFKategoriID == 0)
            {
                model.CFFKategori.CreatedBy = userSvc.LoginID;
                model.CFFKategori.CreatedDateTime = DateTime.Now;
            }
            var data = svc.UpdateCFFKategori(model);

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin CFF_Kategori. CFFKategoriID =" + data.CFFKategori.CFFKategoriID,
                        lstAuditDetail,
                        "Ref_CFFKategori",
                        data.CFFKategori.CFFKategoriID);

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteCFFKategori(RefCFFKategoriUpdateViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDateTime = DateTime.Now;

            var data = svc.DeleteCFFKategori(model);

            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin CFF Kategori." + model.CFFKategoriID,
                        lstAuditDetail,
                        "Ref_CFFKategori ",
                        model.CFFKategoriID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllUnitType()
        {
            List<UnitType> _UnitType = null;

            _UnitType = AdminService.AdminUnitType;

            return Json(_UnitType);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateUnitType(UnitTypeUpdateViewModel model)
        {
            model.UnitType.LastUpdatedBy = userSvc.LoginID;
            model.UnitType.LastUpdatedDatetime = DateTime.Now;
            List<UnitType> _UnitType = null;

            if (model.UnitType.UnitTypeID == 0)
            {
                model.UnitType.CreatedBy = userSvc.LoginID;
                model.UnitType.CreatedDatetime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateUnitType(model);
            if (data.UnitType.UnitTypeID > 0)
                _UnitType = AdminService.UnitType;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin UnitType. UnitTypeID =" + data.UnitType.UnitTypeID,
                        lstAuditDetail,
                        "Ref_UnitType",
                        data.UnitType.UnitTypeID);

            return Json(_UnitType);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteUnitType(UnitTypeViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedDatetime = DateTime.Now;

            var data = svc.DeleteUnitType(model);

            //TODO: Audit
            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Delete;
            lstAuditDetail = data.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Unit Type." + model.UnitTypeID,
                        lstAuditDetail,
                        "Ref_UnitType ",
                        model.UnitTypeID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllParameterSetting()
        {
            List<ParameterSettings> _ParameterSettings = null;

            _ParameterSettings = AdminService.ParameterSettings;

            return Json(_ParameterSettings);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateParameterSetting(AdminParameterSettingUpdateViewModel model)
        {
            model.ParameterSetting.LastUpdatedBy = userSvc.LoginID;
            model.ParameterSetting.LastUpdatedDateTime = DateTime.Now;

            var data = svc.UpdateParameterSetting(model);

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Paramter Setting. Parameter ID  =" + data.ParameterSetting.ParameterID,
                        lstAuditDetail,
                        "Ref_ParameterSettings",
                        data.ParameterSetting.ParameterID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllTindakanSusulanByID()
        {
            List<TindakanSusulanViewModel> _lst = null;

            _lst = AdminService.TindakanSusulan;

            return Json(_lst);

        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateTindakanSusulan(TindakanSusulanUpdateViewModel model)
        {
            model.AdminTindakanSusulan.LastUpdatedBy = userSvc.LoginID;
            model.AdminTindakanSusulan.LastUpdatedDatetime = DateTime.Now;
            //List<UnitType> _UnitType = null;

            if (model.AdminTindakanSusulan.TindakanSusulanID == 0)
            {
                model.AdminTindakanSusulan.CreatedBy = userSvc.LoginID;
                model.AdminTindakanSusulan.CreatedDatetime = DateTime.Now;
            }

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            var data = svc.UpdateTindakanSusulan(model);
            model.LstTindakanSusulan = AdminService.AdminTindakanSusulan;
            model.LstTindakanSusulanKempen = AdminService.AdminTindakanSusulanKempen;

            if (model.AdminTindakanSusulan.EditMenuMode == 3)
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Tindakan Susulan. Tindakan Susulan ID =" + data.AdminTindakanSusulan.TindakanSusulanID,
                            lstAuditDetail,
                            "Ref_TindakanSusulan,Ref_TindakanSusulanKempen",
                            data.AdminTindakanSusulan.TindakanSusulanID);
            }
            else
            {
                auditEvent = AuditService.AuditEventID.ManageParameter;
                actionType = AuditService.ActionType.Update;
                lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
                {
                    FieldName = a.DisplayName,
                    OldValue = a.OldValue,
                    NewValue = a.NewValue
                }).ToArray().ToList();

                AuditLogTxn(auditEvent,
                            AuditService.ResultType.Success,
                            actionType,
                            userSvc.LoginID,
                            DateTime.Now,
                            "Update Admin Tindakan Susulan. Tindakan Susulan ID =" + model.AdminTindakanSusulan.TindakanSusulanID,
                            lstAuditDetail,
                            "Ref_TindakanSusulan,Ref_TindakanSusulanKempen",
                            model.AdminTindakanSusulan.TindakanSusulanID);
            }



            return Json(model);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAllEmailTemplate()
        {
            var data = AdminService.Template;

            return Json(data);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateTemplate(TemplateUpdateViewModel model)
        {
            model.AdminTemplate.LastUpdatedBy = userSvc.LoginID;
            model.AdminTemplate.LastUpdatedByName = userSvc.DisplayName;
            model.AdminTemplate.LastUpdatedDateTime = DateTime.Now;
            //List<UnitType> _UnitType = null;

            if (model.AdminTemplate.TemplateID == 0)
            {
                model.AdminTemplate.CreatedBy = userSvc.LoginID;
                model.AdminTemplate.CreatedByName = userSvc.DisplayName;
                model.AdminTemplate.CreatedDateTime = DateTime.Now;
            }

            var data = svc.UpdateTemplate(model);

            AuditService.AuditEventID auditEvent = new AuditService.AuditEventID();
            AuditService.ActionType actionType = new AuditService.ActionType();
            List<AuditDetailViewModel> lstAuditDetail = null;

            auditEvent = AuditService.AuditEventID.ManageParameter;
            actionType = AuditService.ActionType.Update;
            lstAuditDetail = data.ChangeHistory.Select(a => new AuditDetailViewModel
            {
                FieldName = a.DisplayName,
                OldValue = a.OldValue,
                NewValue = a.NewValue
            }).ToArray().ToList();

            AuditLogTxn(auditEvent,
                        AuditService.ResultType.Success,
                        actionType,
                        userSvc.LoginID,
                        DateTime.Now,
                        "Update Admin Template. Template ID =" + data.AdminTemplate.TemplateID,
                        lstAuditDetail,
                        "Template",
                        data.AdminTemplate.TemplateID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetAdminEmailTemplateBookmarkDDLOptions(int intTemplateID)
        {
            var data = svc.GetAdminBookmarkTemplateByID(intTemplateID);

            return Json(data);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetSearchViewDetailList(int id)//TableType
        {
            var data = svc.GetSearchViewDetailList(id);

            return Json(data);
        }

        //[HttpGet]
        //[ApiErrorHandler]
        //public IHttpActionResult GetConfigViewDDL(int id)
        //{
        //    return Json(svc.GetSearchDropdown(id));
        //}


        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateAdvRulesView(AdvancedRulesMainViewModel vm)//TableType
        {
            string strUserID = userSvc.LoginID;
            DateTime dtUpdated = DateTime.Now;


            int intViewID = svc.UpdateAdvRulesView(vm, strUserID, dtUpdated);

            return Json(new { ViewID = intViewID });
        }



        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetViewPermissionDropdown()
        {
            //AuditLogTxn("test", "field");

            return Json(svc.GetViewPermissionDropdown());
        }


        [HttpPost]
        [ApiErrorHandler]
        //[ValidateAjaxRequest]
        public IHttpActionResult GetDashboardListing(DashboardListingViewModel paging)
        {
            int intCount = 0;

            var lst = svc.GetDashboardListing(paging, out intCount);
            var objRecord = new
            {
                draw = paging.Draw,
                recordsTotal = intCount, //data.Count,
                recordsFiltered = intCount, //data.Count,
                data = lst,
            };

            return Json(objRecord);

        }


        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetDashboardDetails(int intDashboardID)
        {
            var lst = svc.GetDashboardDetails(intDashboardID);

            return Json(lst);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetDashboardZonList(int intDashboardID)
        {
            var lst = svc.GetDashboardZonList(intDashboardID);

            return Json(lst);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult UpdateDashboard(DashboardViewModel vm)
        {
            string strUserID = userSvc.LoginID;
            DateTime dtUpdated = DateTime.Now;

            int intDasboardID = svc.UpdateDashboard(vm, strUserID, dtUpdated);

            return Json(new { DashboardID = intDasboardID });
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteDashboard(DashboardViewModel vm)
        {
            string strUserID = userSvc.LoginID;
            DateTime dtUpdated = DateTime.Now;

            svc.DeleteDashboard(vm);

            return Json(new { DashboardID = vm.DashboardID });
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetDashboardSavedView()
        {
            var lst = svc.GetDashboardSavedView();

            return Json(lst);
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetChartAction()
        {
            List<ChartAction> _lst = null;

            _lst = AdminService.ChartAction;

            return Json(_lst);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult SearchDuplicateDashboardName(DashboardViewModel model)
        {
            return Json(svc.SearchDuplicateDashboardName(model));
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult SearchUnit(string strName)
        {
            return Json(svc.SearchUnit(strName));
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult SearchUnitByID(int intUnitID)
        {
            return Json(svc.SearchUnitByID(intUnitID));
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult SearchUser(string strName)
        {
            return Json(svc.SearchUser(strName));
        }

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult SearchUserByID(int intUserProfileID)
        {
            return Json(svc.SearchUserByID(intUserProfileID));
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteEmailTemplate(AdminTemplateViewModel model)
        {
            model.LastUpdatedBy = userSvc.LoginID;
            model.LastUpdatedByName = userSvc.DisplayName;
            model.LastUpdatedDateTime = DateTime.Now;

            var lst = svc.DeleteEmailTemplate(model);

            return Json(lst);
        }
    }
}