crmApp.controller("AppController", AppController);

function AppController($scope, localStorageService, GeneralService, TaskSI, $filter) {
    var vm = this;
    vm.recentActivityLst = [];
    $scope.sideBarToggle = false;
    // datepicker
    $scope.DateFormat = 'dd MMM yyyy';
    $scope.TimeFormat = 'hh:mm a';
    $scope.DateTimeFormat = 'dd MMM yyyy hh:mm a';
    $scope.ShowWeeks = false;
    $scope.MinDate = '2018-12-31T00:00:00Z';
    $scope.MaxDate = '2019-12-31T00:00:00Z';
    //var date = new Date()
    //$scope.timezone = ((date.getTimezoneOffset() / 60) * -100);
    //console.log('Timezone : ',$scope.timezone);
    $scope.DefaultTime = '23:59:59';

    $scope.buttonBar = {
        show: true,
        defaultTime: $scope.DefaultTime,
        now: {
            show: true,
            text: 'Sekarang',
            cls: 'btn-sm btn-default'
        },
        today: {
            show: true,
            text: 'Hari Ini',
            cls: 'btn-sm btn-default'
        },
        clear: {
            show: true,
            text: 'Kosongkan',
            cls: 'btn-sm btn-default'
        },
        date: {
            show: true,
            text: 'Tarikh',
            cls: 'btn-sm btn-default'
        },
        time: {
            show: true,
            text: 'Masa',
            cls: 'btn-sm btn-default'
        },
        close: {
            show: true,
            text: 'Tutup',
            cls: 'btn-sm btn-default'
        },
        cancel: {
            show: false,
            text: 'Batal',
            cls: 'btn-sm btn-default'
        }
    };
    $scope.ClearDateToolTip = 'Kosongkan';
    $scope.DatePickerToolTip = 'Pilihan Tarikh';
    $scope.TimePickerToolTip = 'Pilihan Masa';
    $scope.DateTimePickerToolTip = 'Pilihan Tarikh dan Masa';

    $scope.init = function () {
        getUserInfo();
    }

    $scope.init();

    $scope.$on('getUsrInfoEvt', function (events, args) {
        console.log("getUsrInfoEvt event called");
        getUserInfo();
    })

    $scope.ToggleSidebar = function () {
        $scope.sideBarToggle = !$scope.sideBarToggle;

        //$('#mySidenav').toggle("slide", { direction: "right" }, 1500);
        if ($scope.sideBarToggle) {
            TaskSI.getRecentActivity().then(function (r) {
                if (r.status == 200) {
                    vm.recentActivityLst = r.data;
                };
            });

        }

        angular.element(document.querySelector('body')).toggleClass('sidebar-opened', $scope.sideBarToggle);
        angular.element(document.querySelector('body')).toggleClass('shortname-menu-open', $scope.sideBarToggle);
    }

    vm.scrollTop = function () {
        $("html, body").animate({ scrollTop: 0 }, 400);
    };

    function getUserInfo() {
        if (localStorage.getItem("crmApp.UserInfo") === null) {
            TaskSI.getLoginInfo().then(function (r) {

                vm.loginResponse = r.data;
                //localStorageService.clearAll();

                vm.loginResponse.UP_TimeStamp = vm.loginResponse.LoginID + "_" + moment(vm.loginResponse.LastUpdatedDateTime).format('YYYY-MM-DD+HH:mm:ss');

                localStorageService.set('UserInfo', vm.loginResponse);
                vm.UserInfo = localStorageService.get('UserInfo');
                $scope.UserInfo = vm.UserInfo;

                console.log('-----Get Initial Login Info-----');
                console.log(localStorageService.get("UserInfo"));
                console.log(localStorageService.get("UserInfo").UP_TimeStamp);

                vm.IsManagerRole = GeneralService.checkCurrentUserRole('2', vm.UserInfo);

                getHCLHRCCRole();
                getDashBoardListViewAccess();
                
            });
        } else {
            console.log('-----Get Stored Login Info-----');
            console.log(localStorageService.get("UserInfo"));
            console.log(localStorageService.get("UserInfo").UP_TimeStamp);
            vm.UserInfo = localStorageService.get("UserInfo");
            $scope.UserInfo = vm.UserInfo;
            vm.IsManagerRole = GeneralService.checkCurrentUserRole('2', vm.UserInfo);
            getHCLHRCCRole();
            getDashBoardListViewAccess();
        }

       
    }

    function getDashBoardListViewAccess() {
        var arr = $filter('filter')(vm.UserInfo.RolesObj, {FunctionalTaskID: 76},true);
        if (arr && (arr.length > 0)) { 
            vm.IsShowReMSBatch = true;
            arr = null;
        }

        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 77 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowEmailBlastBatch = true;
            arr = null;
        }

        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 78 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowOutboundAssignments = true;
            arr = null;
        }

        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 79 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowManagerAssigmentButton = true;
            arr = null; 
        }

        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 80 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowCFFPool = true;
            arr = null;
        }


        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 81 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowCasePool = true;
            arr = null;
        }


        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 82 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowActPool = true;
            arr = null;
        }

        var arr = $filter('filter')(vm.UserInfo.RolesObj, { FunctionalTaskID: 84 }, true);
        if (arr && (arr.length > 0)) {
            vm.IsShowBulkAssignment = true;
            arr = null;
        }

    }

    function getHCLHRCCRole() {
        var arrSysAdmin = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 15 }, true);
        if (arrSysAdmin && (arrSysAdmin.length > 0))
            vm.isSysAdmin = true;

        var arrHRCCAgent = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 16 }, true);
        var arrHRCCAgentUAT = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 26 }, true);
        if ((arrHRCCAgent && (arrHRCCAgent.length > 0)) || (arrHRCCAgentUAT && (arrHRCCAgentUAT.length > 0)))
            vm.isHRCCAgent = true;


        var arrHCLAgent = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 10 }, true);
        var arrHCLAgentUAT = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 35 }, true);
        if ((arrHCLAgent && (arrHCLAgent.length > 0)) || (arrHCLAgentUAT && (arrHCLAgentUAT.length > 0)))
            vm.isHCLAgent = true;


        var arrManager = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 17 }, true);
        var arrManagerUAT = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 22 }, true);
        if ((arrManager && (arrManager.length > 0)) || (arrManagerUAT && (arrManagerUAT.length > 0)))
            vm.isManager = true;

        var arrCCO = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 4 }, true);
        var arrCCOUAT = $filter('filter')(vm.UserInfo.RoleGroups, { FunctionalRoleID: 29 }, true);
        if ((arrCCO && (arrCCO.length > 0)) || (arrCCOUAT && (arrCCOUAT.length > 0)))
            vm.isCCO = true;
    };
    $scope.numberKeydown = function (event) {
        // Allow: backspace, delete, tab, escape, enter
        if ([8, 9, 27, 13, 110, 46, 127].indexOf(event.keyCode) !== -1 ||
            // Allow: Ctrl+A, Ctrl+C, Ctrl+V
            (event.ctrlKey === true && (event.keyCode === 65 || event.keyCode === 67 || event.keyCode === 86)) ||
            // Allow: home, end, left, right
           (event.keyCode >= 35 && event.keyCode <= 39)) {
            return;
        }

        // Ensure that it is a number and stop the keypress
        if ((event.shiftKey || (event.keyCode < 48 || event.keyCode > 57)) && (event.keyCode < 96 || event.keyCode > 105)) {
            event.stopPropagation();
            event.preventDefault();
        }
    };

}

crmApp.controller("DatePickerController", DatePickerController);

function DatePickerController($scope) {

    var $ctrlDate = this;
    $ctrlDate.popup = {
        opened: false
    };
    $ctrlDate.ClearDateToolTip = $scope.ClearDateToolTip;
    $ctrlDate.DatePickerToolTip = $scope.DatePickerToolTip;
    $ctrlDate.TimePickerToolTip = $scope.TimePickerToolTip;
    $ctrlDate.DateTimePickerToolTip = $scope.DateTimePickerToolTip;
    $ctrlDate.DefaultTime = '02:00 pm';
    $ctrlDate.MinDate = $scope.MinDate;
    $ctrlDate.MaxDate = $scope.MaxDate;
    $ctrlDate.ShowWeeks = $scope.ShowWeeks;
    $ctrlDate.DateDisabled = false,
    // date picker
    //$ctrlDate.picker1 = {
    //    date: new Date('2015-03-01T00:00:00Z'),
    //    datepickerOptions: {
    //        dateDisabled: true,
    //        startingDay: 1
    //        //showWeeks: false,
    //        //startingDay: 1,
    //        //dateDisabled: function (data) {
    //        //    return (data.mode === 'day' && (new Date().toDateString() == data.date.toDateString()));
    //        //}
    //    }
    //};
    $ctrlDate.DateFormat = $scope.DateFormat;
    $ctrlDate.TimeFormat = $scope.TimeFormat;
    $ctrlDate.DateTimeFormat = $scope.DateTimeFormat;
    $ctrlDate.ButtonBar = $scope.buttonBar;

    $ctrlDate.DefaultTime = $scope.DefaultTime;
    $ctrlDate.dateOptions = {
        minDate: (new Date($ctrlDate.MinDate)).setHours(0, 0, 0, 0), // Use this property to enable a min selected date
        maxDate: (new Date($ctrlDate.MaxDate)).setHours(0, 0, 0, 0), // Use this property to enable a max selected date
        showWeeks: $ctrlDate.ShowWeeks,
        dateDisabled: $ctrlDate.DateDisabled,
        startingDay: 1,
        buttonBar: $scope.buttonBar
    };

    $ctrlDate.open = function () {

        $ctrlDate.popup.opened = true;
    }

}


crmApp.controller("DownloadManualController", DownloadManualController);

function DownloadManualController($scope) {
    vm = this;

    vm.init = function () {
        
    }

    vm.DownloadFile = function (manualID) {
        var model = { ManualID: manualID };

        //angular.element('#model').val(angular.toJson(model));
        //angular.element('#btn-submitDownloadManual').click();
    }
}
