crmApp.controller("HomeController", HomeController);

function HomeController($scope, $mdDialog, $window, $compile, $q, $http, $location, DTOptionsBuilder, DTColumnBuilder, $filter, CaseSI, TaskSI, ActivitySI, GeneralSI, SearchSI, OutboundSI, UserSI, AntiForgeryService) {
    var vm = this;
    vm.ScenarioID = $window.scenarioID;
    AntiForgeryService.cookieToken = $window.cookietoken;
    AntiForgeryService.formToken = $window.formtoken;
    vm.OutboundManualAssignIDValue = [];
    vm.selected = {};
    vm.dtInstanceOCLManualAssigned = {};
    vm.dtInstanceMyPendingActivityFromList = {};
    vm.dtInstanceMyPendingCaseBlmSelesai = {};
    vm.dtInstanceMyCreateCaseAssignToList = {};
    vm.dtInstanceMyCreatedTask = {};
    vm.dtInstanceMyPendingTask = {};
    vm.dtInstanceAssignedUnitActivity = {};
    vm.dtInstanceAssignedUnitCase = {};
    vm.dtInstanceMyPendingOutbound = {};
    vm.dtInstanceOLRemsBatchData = {};
    vm.dtInstanceOLEmailBlastBatchData = {};
    vm.dtInstanceAssignedCFFDraftActivity = {};
    vm.dtInstanceUserRequestAssignToList = {};
    vm.dtInstanceUserRequestFromList = {};
    vm.DateTimeValue = null;
    // Load Workflow data that Assign to Other People (My Created Case)
    var ajaxCallback = function (data, callback, settings) {
        // Me Assign to Other People based on workflow
        CaseSI.getDashboardMyCreatedCase(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    vm.dateValue = '';
    vm.timeValue = '';
    vm.dateTimeValue = '';

    vm.dateTimeFromValue = '';
    vm.dateTimeToValue = '';

    vm.DefaultTime = '11:59 pm';

    vm.isHasAssignedInboundCFFDraftActivity = false;

    // Load user Request Workflow data that Assign To Me (My Pending User request)
    var ajaxCallbackUserRequestFrom = function (data, callback, settings) {
        //Other People Assign to Me based on User workflow
        UserSI.getDashboardMyPendingUserRequest(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    // Load user Request Workflow data that me Assign to other people (My Created User request)
    var ajaxCallbackUserRequestAssignTo = function (data, callback, settings) {
        UserSI.getDashboardMyCreatedUserRequest(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    // Load Workflow data that Assign To Me (My Pending Case)
    var ajaxCallbackAssignFrom = function (data, callback, settings) {
        //Other People Assign to Me based on workflow
        CaseSI.getDashboardMyPendingCase(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    // Load Workflow data for Unit by 
    var ajaxCallbackAssignedUnitCase = function (data, callback, settings) {
        //Other People Assign to Me based on workflow
        CaseSI.getAssignCaseForUnit(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    // Load My Task
    var ajaxCallbackMyTask = function (data, callback, settings) {
        TaskSI.getDashboardMyTask(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    // Load My Task Submission
    var ajaxCallbackMyTaskSubmission = function (data, callback, settings) {
        TaskSI.getDashboardMyTaskSubmission(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    // Load My Pending Activity
    var ajaxPendingActivityCallback = function (data, callback, settings) {
        //Other People Assign to Me based on workflow
        ActivitySI.getMyPendingActInDashboard(data).then(function (r) {
            if (r.status == 200) { 
                callback(r.data);
            }
        });
    }

    // Load Outbound Manual Assign 
    var ajaxOCLManualAssignCallback = function (data, callback, settings) {
        data.JenisKempenID = vm.JenisKempenID;
        data.TPName = vm.TPName;
        OutboundSI.getOutboundManualAssignData(data).then(function (r) {
            if (r.status == 200) {
                if (r.data.recordsTotal > 0)
                    vm.OpenButton = true
                currentItems = r.data.data;
                callback(r.data);
            }
        });
    }

    var ajaxMyPendingOutboundCallback = function (data, callback, settings) {
        OutboundSI.getMyPendingOutboundInDashboard(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    var ajaxOLRemsBatchData = function (data, callback, settings) {
        OutboundSI.getAllReMSBatchList(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    var ajaxOLEmailBlastBatchData = function (data, callback, settings) {
        OutboundSI.getAllEmailBlastBatchList(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }

    var ajaxManagerPoolActPendingData = function (data, callback, settings) {
        ActivitySI.getAssignActivityForUnit(data).then(function (r) {
            if (r.status == 200) {
                callback(r.data);
            }
        });
    }
     
    var ajaxInboundCFFDraftActivityData = function (data, callback, settings) {
        ActivitySI.getAssignedInboundCFFDraftActivity(data).then(function (r) {
            if (r.status == 200) {
                if (r.data.data && r.data.data.length > 0)
                    isHasAssignedInboundCFFDraftActivity = true;

                callback(r.data);
            }
        });
    }
    //Update Outbound That Click By Agent
    vm.UpdateOutboundAssigned = function (OutboundId, JenisKempenId, RemsDataId) {

        swal({
            title: "To view the detail, this item will auto assign to you. Do you want to proceed?",
            text: "",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "Yes",
            cancelButtonText: "No",
            closeOnConfirm: false,
            closeOnCancel: false
        }, function (isConfirm) {
            if (isConfirm) {
                var data = {
                    OutboundID: OutboundId
                }

                OutboundSI.updatedOCLAgentAssign(data).then(function (r) {
                    if (r.status == 200) {
                        if (r.data.DataID > 0)
                        {
                            vm.redirectTo('/Outbound/profile/detail/' + JenisKempenId + '/' + RemsDataId);
                        }
                        else
                        {
                            swal("Failed!", "Task has been taken by other agent. Please click refresh button on right upper to get latest list. ", "error");
                        }
                       
                    }
                }, function (err) {
                    console.log(err);
                });

            } else {
                swal("Batal!", "Permohonan telah dibatalkan.", "error");
            }
        });

    };


    vm.init = function () {
        vm.checkboxmodel = false;
        vm.CreatedBy = false;
        //if (localStorage.getItem("crmApp.UserInfo") == null) {
        //    TaskSI.getLoginInfo().then(function (r) {
        //        if (r.status == 200) {
        //            vm.loginResponse = r.data;
        //        }
        //    });
        //    localStorageService.clearAll();
        //    localStorageService.set('UserInfo', vm.loginResponse);
        //    $scope.UserInfo = localStorageService.get('UserInfo');
        //    console.log($scope.UserInfo);
        //}
        //else {
        //    $scope.UserInfo = localStorageService.get('UserInfo');
        //    console.log($scope.UserInfo);
        //}

        //Get Tiles Count
        GeneralSI.getDashboardTiles().then(function (r) {
            if (r.status === 200) {
                for (var key in r.data) {
                    vm[key] = $filter('number')(r.data[key], 0);
                }
            }
        });

        GeneralSI.getActDashboardWidget().then(function (r) {
            if (r.data != null) {
                vm.ActBlmSelesaiStatusID = r.data.ActBlmSelesaiStatusID;
                vm.ActSelesaiStatusID = r.data.ActSelesaiStatusID
            }
        });

        // Dashboard Link to Search Case Screen
        vm.GetCaseBelumSelesai = function () {

            angular.element('#Status1ID').val();
            angular.element('#IsTodayCase').val();
            var form = angular.element('#DashboardCaseBlmSelesaiForm');
            form.submit();
        }

        vm.GetCaseSelesai = function () {

            angular.element('#Status1ID').val();
            angular.element('#IsTodayCase').val();
            var form = angular.element('#DashboardCaseSelesaiForm');
            form.submit();
        }

        vm.GetCaseBelumSelesaiToday = function () {

            angular.element('#Status1ID').val();
            angular.element('#IsTodayCase').val();
            var form = angular.element('#DashboardCaseBlmSelesaiToday');
            form.submit();
        }

        vm.GetCaseSelesaiToday = function () {

            angular.element('#Status1ID').val();
            angular.element('#IsTodayCase').val();
            var form = angular.element('#DashboardCaseSelesaiTOday');
            form.submit();
        }
        // Dashboard Link to Search Case Screen

        // Dashboard Link to Search Activity Screen
        vm.GetActDashboardTiles = function (actStatusID) {
            angular.element('#Status1ID').val(actStatusID);
            var form = angular.element('#DashboardActivityForm');
            form.submit();
        }
        // Dashboard Link to Search Activity Screen

        // Dashboard Link to Search Task Screen
        vm.GetTaskBelumSelesai = function () {

            angular.element('#Status1ID').val();
            var form = angular.element('#TaskBlmSelesaiForm');
            form.submit();
        }

        vm.GetTaskSelesai = function () {

            angular.element('#Status1ID').val();
            var form = angular.element('#TaskSelesaiForm');
            form.submit();
        }
        // Dashboard Link to Search Task Screen

        ////Bind table My Pending Activity--start
        vm.dtColumnsPendingActivityFromList = [

            DTColumnBuilder.newColumn('ActRefNo').withTitle('No. Aktiviti').renderWith(renderActivitylink).withOption('width', '40%'),
            DTColumnBuilder.newColumn('CaseRefNo').withTitle('No. Kes').renderWith(renderCaselink).withOption('width', '30%'),
            DTColumnBuilder.newColumn('Kategori1Name').withTitle('Kategori').renderWith(renderKategori).withOption('width', '15%'),
            DTColumnBuilder.newColumn('ActChannelName').withTitle('Aktiviti').withOption('width', '5%'),
            DTColumnBuilder.newColumn('Aliran').withTitle('Aliran').withOption('width', '5%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '5%')
        ];

        vm.dtOptionsPendingActivityFromList = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxPendingActivityCallback)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end


        ////Bind table Assign To Other People- start 
        vm.dtColumnsCaseBlmSelesaiAssignToList = [

            DTColumnBuilder.newColumn('CaseRefNo').withTitle('No. Kes').renderWith(renderCaselink).withOption('width', '30%'),
            DTColumnBuilder.newColumn('TPName').withTitle('Nama P.C.').withOption('width', '20%'),
            DTColumnBuilder.newColumn('Kategori1Name').withTitle('Kategori').renderWith(renderKategori).withOption('width', '30%'),
            DTColumnBuilder.newColumn('AssignToName').withTitle('Diagihkan Kepada').withOption('width', '20%'),
            DTColumnBuilder.newColumn('ActChannelName').withTitle('Aktiviti').withOption('width', '10%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '10%')
        ];

        vm.dtOptionsCaseBlmSelesaiAssignToList = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallback)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end

        ////Bind table Assign To Me- start 
        vm.dtColumnsCaseBlmSelesaiAssignFromList = [

            DTColumnBuilder.newColumn('IsExtraActAddToExistingCase').withTitle('').renderWith(function (data, type) {
                if (data != 'Yes') {
                    return '';
                }
                else {
                    return '<i class="case fa fa-exclamation-circle" style="color:red;" title="New Activity Added to existing case"></i >';

                }
            }),
            DTColumnBuilder.newColumn('CaseRefNo').withTitle('No. Kes').renderWith(renderCaselink).withOption('width', '30%'),
            DTColumnBuilder.newColumn('TPName').withTitle('Nama P.C.').withOption('width', '20%'),
            DTColumnBuilder.newColumn('Kategori1Name').withTitle('Kategori').renderWith(renderKategori).withOption('width', '30%'),
            DTColumnBuilder.newColumn('AssignFrom').withTitle('Diagihkan Oleh').withOption('width', '20%'),
            DTColumnBuilder.newColumn('ActChannelName').withTitle('Aktiviti').withOption('width', '10%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '10%'),

        ];

        vm.dtOptionsCaseBlmSelesaiAssignFromList = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallbackAssignFrom)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end

        ////Bind table Assign Unit
        vm.dtColumnsAssignedUnitCase = [

            DTColumnBuilder.newColumn('CaseRefNo').withTitle('No. Kes').renderWith(renderCaselink).withOption('width', '30%'),
            DTColumnBuilder.newColumn('TPName').withTitle('Nama P.C.').withOption('width', '20%'),
            DTColumnBuilder.newColumn('Kategori1Name').withTitle('Kategori').renderWith(renderKategori).withOption('width', '30%'),
            DTColumnBuilder.newColumn('AssignFrom').withTitle('Diagihkan Oleh').withOption('width', '20%'),
            DTColumnBuilder.newColumn('ActChannelName').withTitle('Aktiviti').withOption('width', '10%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '10%'),
        ];

        vm.dtOptionsAssignedUnitCase = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallbackAssignedUnitCase)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end


        ////Bind Tugasan Belum Selesai- start 
        vm.dtColumnsMyTask = [

            DTColumnBuilder.newColumn('TaskRefNo').withTitle('No. Tugasan').renderWith(function (data, type, full) {
                return '<a target="_blank" href="/task/detail/' + full.TaskID + '">' + data + '</a>';
            }).withOption('width', '27%'),
            DTColumnBuilder.newColumn('Title').withTitle('Perkara').withOption('width', '17%'),
            DTColumnBuilder.newColumn('RefNo').withTitle('Berkenaan').renderWith(function (data, type, full) {
                var link;
                if (data == null) {
                    link = "-";
                }
                else if (data.substring(0, 3).toUpperCase() == "ACT") {
                    link = '<a target="_blank" href="/activity/profile/detail/' + full.ActivityID + '">' + data + '</a>';
                }
                else if (data.substring(0, 3).toUpperCase() == "CAS") {
                    link = '<a target="_blank" href="/case/profile/detail/' + full.CaseID + '">' + data + '</a>';
                }
                return link;
            }).withOption('width', '27%').withOption('defaultContent', '-'),
            DTColumnBuilder.newColumn('AssignByName').withTitle('Diagihkan Oleh').withOption('width', '15%'),
            DTColumnBuilder.newColumn('DueDate').withTitle('Tarikh Susulan').renderWith(function (data, type) {
                return $filter('date')(data?data:'-', 'dd MMM yyyy');
            }).withOption('width', '15%'),
        ];

        vm.dtOptionsMyTask = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallbackMyTask)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end

        ////Bind Tugasan Dihasilkan - start 
        vm.dtColumnsMyTaskSubmission = [

            DTColumnBuilder.newColumn('TaskRefNo').withTitle('No. Tugasan').renderWith(function (data, type, full) {
                return '<a target="_blank" href="/task/detail/' + full.TaskID + '">' + data + '</a>'; //replace - to -[space] .replace(/(-)+/gi, '$1&#8203;')
            }).withOption('width', '15%'),
            DTColumnBuilder.newColumn('Title').withTitle('Perkara').withOption('width', '20%'),
            DTColumnBuilder.newColumn('RefNo').withTitle('Berkenaan').renderWith(function (data, type, full) {
                var link;
                if (data == null) {
                    link = "-";
                }
                else if (data.substring(0, 3).toUpperCase() == "ACT") {
                    link = '<a target="_blank" href="/activity/profile/detail/' + full.ActivityID + '">' + data + '</a>'; 
                }
                else if (data.substring(0, 3).toUpperCase() == "CAS") {
                    link = '<a target="_blank" href="/case/profile/detail/' + full.CaseID + '">' + data + '</a>'; 
                }
                return link;
            }).withOption('width', '15%').withOption('defaultContent', '-'),
            DTColumnBuilder.newColumn('AssignToName').withTitle('Pegawai Bertugas').withOption('width', '20%'),
            //DTColumnBuilder.newColumn('TaskStatus').withTitle('Status').withOption('width', '20%'),
            DTColumnBuilder.newColumn('DueDate').withTitle('Tarikh Susulan').renderWith(function (data, type) {
                return $filter('date')(data?data:'-', 'dd MMM yyyy');
            }).withOption('width', '10%')
        ];

        vm.dtOptionsMyTaskSubmission = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallbackMyTaskSubmission)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end

        //Bind Table For My Pending Outbound In Dashboard
        vm.dtColumnsMyPendingOutbound = [
       //   DTColumnBuilder.newColumn('OutboundID').withTitle('ID').renderWith(function (data, type, full) {
       //    var link;
       //    link = '<a href=/Outbound/profile/detail/' + full.JenisKempenID + '/' + full.DataID + '>' + data + '</a>';
       //    return link;
       //}),
            DTColumnBuilder.newColumn('TPName').withTitle('Nama').renderWith(function (data, type, full) {
                var link;
                link = '<a target="_blank" href=/Outbound/profile/detail/' + full.JenisKempenID + '/' + full.DataID + '>' + data + '</a>';
                return link;
            }),
         DTColumnBuilder.newColumn('JenisKempenName').withTitle('Jenis Kempen'),
         DTColumnBuilder.newColumn('NoRujukanCukai').withTitle('No. Rujukan Cukai'),
         DTColumnBuilder.newColumn('REMSDate').withTitle('Tarikh ReMS').renderWith(function (data, type) {
             if (data == null) {
                 return '-';
             }
             else {
                 return $filter('date')(data, 'dd MMM yyyy');
             }

         }),
        //DTColumnBuilder.newColumn('OutboundStatus').withTitle('Status'),

        ];

        vm.dtOptionsMyPendingOutbound = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxMyPendingOutboundCallback)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5)
              .withOption('headerCallback', function (header) {
                  if (!vm.headerCompiled) {
                      // Use this headerCompiled field to only compile header once
                      vm.headerCompiled = true;
                      $compile(angular.element(header).contents())($scope);
                  }
              })
            .withOption('createdRow', function (row, data, dataIndex) {
                $compile(angular.element(row).contents())($scope);
            });
        //Bind Table - end

        // Bind Table - Manual Assign Outbound in Dashboard By Agent
        vm.dtColumnsManualOLDashboard = [
                //DTColumnBuilder.newColumn('OutboundID').withTitle('ID').renderWith(function (data, type, full) {
                //    var link;
                //    //link = '<a href=/Outbound/profile/detail/' + full.JenisKempenID + '/' + full.DataID + ' ng-click="home.UpdateOutboundAssigned(' + data + ');">' + data + '</a>';
                //    link = '<a href="#" ng-click="home.UpdateOutboundAssigned(' + data + ',' + full.JenisKempenID + ',' + full.DataID + ');">' + data + '</a>';

                //    return link;
                //}),
                DTColumnBuilder.newColumn('TPName').withTitle('Nama').renderWith(function (data, type, full) {
                    var link;

                    link = '<a href="#" ng-click="home.UpdateOutboundAssigned(' + full.OutboundID + ',' + full.JenisKempenID + ',' + full.DataID + ');">' + data + '</a>';

                    return link;
                }),
                DTColumnBuilder.newColumn('JenisKempenName').withTitle('Jenis Kempen'),
                DTColumnBuilder.newColumn('NoRujukanCukai').withTitle('No. Rujukan Cukai'),

                //DTColumnBuilder.newColumn('OutboundStatus').withTitle('Status'),
                DTColumnBuilder.newColumn('REMSDate').withTitle('Tarikh ReMS').renderWith(function (data, type) {
                    if (data == null || data == '0001-01-01T00:00:00') {
                        return '-';
                    }
                    else {
                        return $filter('date')(data, 'dd MMM yyyy');
                    }

                }),
        ];

        vm.dtOptionsManualOLDashboard = DTOptionsBuilder.newOptions()
                        .withOption('ajax', ajaxOCLManualAssignCallback)
                        .withDataProp('data')
                        .withOption('processing', true)
                        .withOption('searching', true)
                        .withOption('serverSide', true)
                        .withOption('lengthChange', false)
                        .withOption('autoWidth', false)
                        .withPaginationType('full_numbers')
                        .withDisplayLength(5)
                            .withOption('headerCallback', function (header) {
                                if (!vm.headerCompiled) {
                                    // Use this headerCompiled field to only compile header once
                                    vm.headerCompiled = true;
                                    $compile(angular.element(header).contents())($scope);
                                }
                            })
                        .withOption('createdRow', function (row, data, dataIndex) {
                            $compile(angular.element(row).contents())($scope);
                        });

        //Bind Table - end

        // Bind Table - Top 5 Batch Rems Data in Dashboard 
        vm.dtColumnsOLRemsBatchData = [
                 //DTColumnBuilder.newColumn('RowNum').withOption('bSortable', false).withTitle('No.').renderWith(function (data, type, row) {
                 //    var link = '<a><span style="cursor:pointer;" ng-click="home.viewBatchClicked(' + row.BatchID + ',' + row.JenisKempenID + ')">' + data + '</span></a>';
                 //    if (row.ReturnedByName)
                 //        link += '<div class="tooltip bg-gray margin-r-5" style="border-radius: 10px;width: 20px;height: 20px;line-height:20px;text-align:center;margin-left:5px;"><span class="fa fa-reply"></span><span class="tooltiptext">Returned</span></div>';
                 //    return link;
                 //}),
            DTColumnBuilder.newColumn('JenisKempenName').withTitle('Jenis Kempen'),
            DTColumnBuilder.newColumn('BatchRefNo').withTitle('No. Rujukan Kelompok').renderWith(function (data, type, row) {
                var link = '<a><span style="cursor:pointer;" ng-click="home.viewBatchClicked(' + row.BatchID + ',' + row.JenisKempenID + ')">' + data + '</span></a>';
                if (row.BatchHasError == true)
                    link += '&nbsp;<i class="case fa fa-exclamation-circle" style="color:red;" title="Mengandungi data tidak sah"></i>';
                return link;
            }),
            DTColumnBuilder.newColumn('TotalRecords').withTitle('Jumlah Rekod'),
            DTColumnBuilder.newColumn('ImportedDateTime').withTitle('Tarikh Dimuat Naik').renderWith(function (data, type) {
                return $filter('date')(data, $scope.DateTimeFormat);
            }),
        ];

        vm.dtOptionsOLRemsBatchData = DTOptionsBuilder.newOptions()
                        .withOption('ajax', ajaxOLRemsBatchData)
                        .withDataProp('data')
                        .withOption('processing', true)
                        .withOption('searching', true)
                        .withOption('serverSide', true)
                        .withOption('lengthChange', false)
                        .withOption('autoWidth', false)
                        .withPaginationType('full_numbers')
                        .withDisplayLength(5)
                        .withOption('createdRow', function (row, data, dataIndex) {
                            $compile(angular.element(row).contents())($scope);
                        });

        //Bind Table - end

        // Bind Table - Top 5 Batch Email Blast Data in Dashboard 
        vm.dtColumnsOLEmailBlastBatchData = [
            DTColumnBuilder.newColumn('JenisKempenName').withTitle('Jenis Kempen'),
            DTColumnBuilder.newColumn('EmailBatchRefNo').withTitle('No. Rujukan Kelompok').renderWith(function (data, type, row) {
                var link = '<a><span style="cursor:pointer;" ng-click="home.viewBatchClicked(' + row.EmailBatchID + ',' + row.JenisKempenID + ', true)">' + data + '</span></a>';
                if (row.BatchHasError == true)
                    link += '&nbsp;<i class="case fa fa-exclamation-circle" style="color:red;" title="Mengandungi data tidak sah"></i>';
                //link += '&nbsp;&nbsp;<i class="case fa fa-exclamation-circle" style="color:red;" title="Batch contains one or more invalid records."></i >';
                return link;
            }),
            DTColumnBuilder.newColumn('TotalRecords').withTitle('Jumlah Rekod'),
            DTColumnBuilder.newColumn('UploadedDateTime').withTitle('Tarikh Dimuat Naik').renderWith(function (data, type) {
                return $filter('date')(data, $scope.DateTimeFormat);
            }),
        ];

        vm.dtOptionsOLEmailBlastBatchData = DTOptionsBuilder.newOptions()
                        .withOption('ajax', ajaxOLEmailBlastBatchData)
                        .withDataProp('data')
                        .withOption('processing', true)
                        .withOption('searching', true)
                        .withOption('serverSide', true)
                        .withOption('lengthChange', false)
                        .withOption('autoWidth', false)
                        .withPaginationType('full_numbers')
                        .withDisplayLength(5)
                        .withOption('createdRow', function (row, data, dataIndex) {
                            $compile(angular.element(row).contents())($scope);
                        });

        //Bind Table - end

        // Bind Table - Pending Activity Manager Pool Data in Dashboard 
        vm.dtColumnsAssignedUnitActivity = [

            DTColumnBuilder.newColumn('ActRefNo').withTitle('No. Aktiviti').renderWith(renderActivitylink).withOption('width', '30%'),
            DTColumnBuilder.newColumn('ActChannelName').withTitle('Aktiviti').withOption('width', '15%'),
            DTColumnBuilder.newColumn('Aliran').withTitle('Aliran').withOption('width', '10%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '15%')
        ];

        vm.dtOptionsAssignedUnitActivity = DTOptionsBuilder.newOptions()
                        .withOption('ajax', ajaxManagerPoolActPendingData)
                        .withDataProp('data')
                        .withOption('processing', true)
                        .withOption('searching', true)
                        .withOption('serverSide', true)
                        .withOption('lengthChange', false)
                        .withOption('autoWidth', false)
                        .withPaginationType('full_numbers')
                        .withDisplayLength(5)
                        .withOption('createdRow', function (row, data, dataIndex) {
                            $compile(angular.element(row).contents())($scope);
                        });

        //Bind Table - end

        // Bind Table -Inbound CFF Draft Activity in Dashboard 
        vm.dtColumnsAssignedCFFDraftActivity = [

            DTColumnBuilder.newColumn('ActRefNo').withTitle('No. Aktiviti').renderWith(renderActivitylink).withOption('width', '30%'), 
            DTColumnBuilder.newColumn('CaseRefNo').withTitle('No. Kes').renderWith(renderCaselink).withOption('width', '30%'),
            DTColumnBuilder.newColumn('Kategori1Name').withTitle('Kategori').renderWith(renderKategori).withOption('width', '15%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '15%')
        ];

        vm.dtOptionsAssignedCFFDraftActivity = DTOptionsBuilder.newOptions()
                        .withOption('ajax', ajaxInboundCFFDraftActivityData)
                        .withDataProp('data')
                        .withOption('processing', true)
                        .withOption('searching', true)
                        .withOption('serverSide', true)
                        .withOption('lengthChange', false)
                        .withOption('autoWidth', false)
                        .withPaginationType('full_numbers')
                        .withDisplayLength(5)
                        .withOption('createdRow', function (row, data, dataIndex) {
                            $compile(angular.element(row).contents())($scope);
                        });

        //Bind Table - end

        ////Bind table User Request(Sendiri)- start 
        vm.dtColumnsUserRequestFromList = [

            DTColumnBuilder.newColumn('LoginID').withTitle('Login ID').withOption('width', '30%'),
            DTColumnBuilder.newColumn('DisplayName').withTitle('Nama').renderWith(renderWorkflowUserProfilelink).withOption('width', '20%'),
            DTColumnBuilder.newColumn('UnitName').withTitle('Nama Unit').withOption('width', '30%'),
            DTColumnBuilder.newColumn('AssignFrom').withTitle('Diagihkan Oleh').withOption('width', '20%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '10%'),

        ];

        vm.dtOptionsUserRequestFromList = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallbackUserRequestFrom)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table - end

        ////Bind table User Request Assign To Other People- start 
        vm.dtColumnsUserRequestAssignToList = [

            DTColumnBuilder.newColumn('LoginID').withTitle('Login ID').withOption('width', '30%'),
            DTColumnBuilder.newColumn('DisplayName').withTitle('Nama').renderWith(renderWorkflowUserProfilelink).withOption('width', '20%'),
            DTColumnBuilder.newColumn('UnitName').withTitle('Nama Unit').withOption('width', '30%'),
            DTColumnBuilder.newColumn('AssignTo').withTitle('Diagihkan Kepada').withOption('width', '20%'),
            DTColumnBuilder.newColumn('DayTaken').withTitle('Bil. Hari').withOption('width', '10%')
        ];

        vm.dtOptionsUserRequestAssignToList = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallbackUserRequestAssignTo)
            .withDataProp('data')
            .withOption('processing', true)
            .withOption('searching', true)
            .withOption('serverSide', true)
            .withOption('lengthChange', false)
            .withOption('autoWidth', false)
            .withPaginationType('full_numbers')
            .withDisplayLength(5);
        //Bind Table User Request Assign To Other People - end
    };

    vm.viewBatchClicked = function (batchID, jenisKempenID, isHCLEmailBlast) {
        if (isHCLEmailBlast)
            $window.open('/Outbound/HCLEmailBlastBatch/Detail/' + jenisKempenID + '/' + batchID, '_blank');
        else
            $window.open('/Outbound/Batch/Detail/' + jenisKempenID + '/' + batchID, '_blank');
    };

    vm.OpenCase = function () {
        $('#assignModal').modal('show');
    };

    vm.AssignCase = function () {
        window.open('/Case/Profile/6', '_blank');
    };

    vm.CancelAssignCase = function () {

        $('#assignModal').modal('hide');
    };

    vm.redirectTo = function (url) {

        $window.beforeUnloadCanceled = true;
        $window.location.href = url;
    }

    vm.next = function () {
        vm.redirectTo('/Outbound/Assignment');
    }

    vm.ViewAllRemsBatchData = function () {

        var form = angular.element('#ViewAllBatchData');
        form.submit();
    }

    vm.ViewAllEmailBlastBatchData = function () {

        var form = angular.element('#ViewAllEmailBlastBatchData');
        form.submit();
    }

    vm.BulkAssignCaseUnit = function () {
        vm.redirectTo('/Case/Assignment/Unit');
    }
    vm.BulkAssignCase = function () {
        vm.redirectTo('/Case/Assignment/Users');
    }


    function renderCaselink(data, type, row, meta) {

        var link = '<a href="/Case/Profile/detail/' + row.CaseID + '" target="_blank">' + row.CaseRefNo + '</a>';
        if (row.CaseRefNo == undefined || row.CaseRefNo == '0' || row.CaseRefNo == "") {
            link = '-';
        }
     

        return link;
    }


    function renderOutboundlink(data, type, row, meta) {

        var link = '<a href="/Outbound/Profile/detail/' + row.OutboundId + '" target="_blank">' + row.OutboundId + '</a>';
        if (row.OutboundId == undefined || row.OutboundId == '0') {
            link = '-';
        }

        return link;
    }

    function renderActivitylink(data, type, row, meta) {

        var link = '<a href="/Activity/Profile/detail/' + row.ActivityID + '" target="_blank">' + row.ActRefNo + '</a>';
        if (row.ActRefNo == undefined || row.ActRefNo == '0') {
            link = '-';
        }

        return link;
    }

    function renderWorkflowUserProfilelink(data, type, row, meta) {

        var link = '<a href="/UserRequest/Profile/Detail/' + row.WFUserProfileID + '" target="_blank">' + row.DisplayName + '</a>';
        return link;
    }

    function renderStatus(data, type, row, meta) {
        var statusDesc = '';
        if (row.StatusLevel1Name != '' && row.StatusLevel1Name != undefined)
            statusDesc += '<ul class="list-inline"><li><b>' + row.StatusLevel1Name + ' </b></li>';
        if (row.StatusLevel2Name != '' && row.StatusLevel2Name != undefined)
            statusDesc += '<li class="arrow-separator" style="font-size:0.785em;"><b> ' + row.StatusLevel2Name + ' </b></li>';
        if (row.StatusLevel3Name != '' && row.StatusLevel3Name != undefined)
            statusDesc += '<li class="arrow-separator" style="font-size:0.785em;"><span> ' + row.StatusLevel3Name + '</span></li>';
        if (statusDesc != '')
            statusDesc = statusDesc + '</ul >';

        var label = '<span>' + statusDesc + '</span>';

        return label;
    }

    function renderAliran(data, type, row, meta) {
        var link = '';

        return data == 'Masuk' ? ' <span class="arrow" style="padding:5px;"><i style="transform:rotate(-45deg)" class="fa fa-arrow-down"  title="Masuk"></i></span>'
                          : '<span class="arrow outboundArrow" style="padding:5px;"> <i style = "transform:rotate(-45deg)" class="fa fa-arrow-up" title="Keluar"></i>' + link + '</span>';

    };

    function renderKategori(data, type, row, meta) {
        var kategoriDesc = '';
        if (row.Kategori1Name != '' && row.Kategori1Name != undefined)
            kategoriDesc += '<ul class="list-inline"><li><b>' + row.Kategori1Name + ' </b></li>';
        if (row.Kategori2Name != '' && row.Kategori2Name != undefined)
            kategoriDesc += '<li class="arrow-separator" style="font-size:0.785em;"><b> ' + row.Kategori2Name + ' </b></li>';
        if (row.Kategori3Name != '' && row.Kategori3Name != undefined)
            kategoriDesc += '<li class="arrow-separator" style="font-size:0.785em;"><span> ' + row.Kategori3Name + '</span></li>';
        if (kategoriDesc != '')
            kategoriDesc = kategoriDesc + '</ul >';

        var label = '<span>' + kategoriDesc + '</span>';

        return label
    }

    vm.LoadMyPendingActData = function () {
        vm.dtInstanceMyPendingActivityFromList.reloadData();
    }

    vm.LoadMyPendingCaseData = function () {
        vm.dtInstanceMyPendingCaseBlmSelesai.reloadData();
    }

    vm.LoadMyCreatedCaseData = function () {
        vm.dtInstanceMyCreateCaseAssignToList.reloadData();
    }

    vm.LoadMyPendingTaskData = function()
    {
        vm.dtInstanceMyPendingTask.reloadData();
    }

    vm.LoadMyCreatedTaskData = function () {
        vm.dtInstanceMyCreatedTask.reloadData();
    }

    vm.LoadManagerPendingActData = function (){
        vm.dtInstanceAssignedUnitActivity.reloadData();
    }

    vm.LoadManagerPendingCaseData = function () {
        vm.dtInstanceAssignedUnitCase.reloadData();
    }

    vm.LoadPendingOutboundData = function () {
        vm.dtInstanceMyPendingOutbound.reloadData();
    }

    vm.LoadPendingOutboundManualData = function () {
        vm.dtInstanceOCLManualAssigned.reloadData();
    }

    vm.LoadReMSBatchData = function () {
        vm.dtInstanceOLRemsBatchData.reloadData();
    }

    vm.LoadEmailBlastBatchData = function()
    {
        vm.dtInstanceOLEmailBlastBatchData.reloadData();
    }

    vm.LoadCFFPendingActData = function () {
        vm.dtInstanceAssignedCFFDraftActivity.reloadData();
    }

    vm.LoadMyCreatedUserProfileRequest = function () {
        vm.dtInstanceUserRequestAssignToList.reloadData();
    }

    vm.LoadMyPendingUserProfileRequestData = function () {
        vm.dtInstanceUserRequestFromList.reloadData();
    }
}
