crmApp.controller("GeneralController", GeneralController);

function GeneralController($scope, $window, $compile, $q, $http, $location, DTOptionsBuilder, DTColumnBuilder, $filter, CaseSI, TaskSI, ActivitySI, GeneralSI, SearchSI, AntiForgeryService) {
    var vm = this;

    vm.ReadAlertList = function (isRead,TaskAlertID) {
        if (!isRead) {
            var data = {
                IsContentRead: true,
                TaskAlertID: TaskAlertID,
            }

            GeneralSI.updatedCurrentContent(data).then(function (r) {
                //if (r.data != null) {
                    //vm.redirectTo(r.data.URL); //py: comment it for open new tab, not redirect
                //}
            }, function (err) {
                console.log(err);
            });
        }
     
   };

    var ajaxCallback = function (data, callback, settings) {

        data.IsSeeFullList = true

        GeneralSI.getMyDashboardNotifcation(data).then(function (r) {
            if (r.status == 200) {
                vm.TaskListAlert = r.data.data;
                callback(r.data);
            }
        }, function (err) {
            console.log(err);
        });
    }

    vm.init = function () {
            // var data = {
            //    IsSeeFullList: true
            //}

            //GeneralSI.getMyDashboardNotifcation(data).then(function (r) {
            //    if (r.status == 200) {
            //        vm.TaskListAlert = r.data.data;
            //        vm.totalNotification = r.data.TotalNotification;
            //    }
            //}, function (err) {
            //    console.log(err);
            //});

        $scope.date = new Date();
        vm.TodayDate = $filter('date')($scope.date, 'dd MMM yyyy');


        vm.dtColumnsAlertList = [
            DTColumnBuilder.newColumn('IsRead').withTitle(' ').renderWith(function (data, type) {
                var input = data == 0 ? '<i class="case fa fa-exclamation-circle" style="color:red;" ></i >' : '';
                return input;
            }),
            DTColumnBuilder.newColumn('Description').withTitle('Title').renderWith(function (data, type, full) {
                var link;
                if (full.IsRead)
                {
                    if (full.IsNewActAddToCase == 'Yes')
                    {
                        link = '<a target="_blank" href=' + full.URL + '>' + data + ' ' + '<i class="case fa fa-exclamation-circle" style="color:red;" title="New Activity Added to existing case" ></i ></a>';
                    }
                    else
                    {
                        link = '<a target="_blank" href=' + full.URL + '>' + data + '</a>';
                    }
                    //link = '<a target="_blank" href=' + full.URL + '>' + data + '</a>';
                }
                else
                {
                    if (full.IsNewActAddToCase == 'Yes')
                    {
                        link = '<a target="_blank" href=' + full.URL + ' ng-click="general.ReadAlertList(' + full.IsRead + ',' + full.TaskAlertID + ');">' + data + ' ' + '<i class="case fa fa-exclamation-circle" style="color:red;" title="New Activity Added to existing case" ></i ></a>';
                    }
                    else
                    {
                        link = '<a target="_blank" href=' + full.URL + ' ng-click="general.ReadAlertList(' + full.IsRead + ',' + full.TaskAlertID + ');">' + data + '</a>';
                    }
                   
                }

                return link;
            }),
            DTColumnBuilder.newColumn('Description').withTitle('Description'),
            DTColumnBuilder.newColumn('CreatedDateTime').withTitle('Created Date').renderWith(function (data, type) {
            	    return $filter('date')(data, 'dd MMM yyyy');
            	})
            ];

        vm.dtOptionsAlertDetailList = DTOptionsBuilder.newOptions()
            .withOption('ajax', ajaxCallback)
            .withDataProp('data')
            .withOption('processing', 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);
            });
    }

    vm.redirectTo = function (url) {

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