var newfeedtypes_pagesize = $('#newfeedtypes_pagesize');
angular.module('newfeedtypesCtrl', []).controller('newfeedtypesController', function($scope, $http, $window, $interval, Login, Newfeedtypes, $ionicPopup, $cordovaToast, $ionicLoading,$timeout) {
        withoutPopup_NFT = $scope;
        $scope.currentPage = 1;
        // var pager = localStorage.getItem('newfeedtypes_pagesize');
        // $scope.itemsPerPage = pager ? pager : 200;
        $scope.itemsPerPage = 200;
        $scope.entryLimit = $scope.itemsPerPage;
        $scope.searchNewfeedtypes = {
            rows: $scope.itemsPerPage,
            current_page: $scope.currentPage,
            search: ""
        };
        /*$scope.getUsers = function(){
        	User.get().success(function(data){
        		$scope.users= data;
        	});
        }
        $scope.getUsers();
        */
        $scope.Status = [{
            id: 1,
            name: 'Active'
        }, {
            id: 2,
            name: 'Inactive'
        }]
        $scope.getNewfeedtypes = function() {
            $ionicLoading.show({
                template: templateLoading
            });
            Newfeedtypes.getPaging($scope.searchNewfeedtypes).success(function(data) {
                $scope.newfeedtypes = data.data;
                $scope.currentPage = data.page;
                $scope.itemsPerPage = $scope.searchNewfeedtypes.rows;
                newfeedtypes_pagesize.val($scope.itemsPerPage);
                $scope.totalItems = data.total_rows;
                $ionicLoading.hide();
            });
        };
        // $scope.replaceRes = function() {
        //           setTimeout(function() {
        //               $scope.$apply(function(val) {
        //                   if (val) {
        //                       $('#tableNewfeedtypes').tableRespondsive({
        //                           paging: true,
        //                           pagingArg: {
        //                               totalRow: 105,
        //                               rowPerPage: 50,
        //                               currentPage: 2
        //                           }
        //                       });
        //                   }
        //               });
        //           }, 1);
        //       }
        $scope.searchChanged = function() {
            $scope.getNewfeedtypes();
        }
        $scope.sizeChanged = function() {
            $scope.searchNewfeedtypes.rows = $scope.entryLimit;
            newfeedtypes_pagesize.val($scope.searchNewfeedtypes.rows);
            localStorage.setItem('newfeedtypes_pagesize', $scope.searchNewfeedtypes.rows);
            $scope.searchNewfeedtypes.current_page = 1;
            $scope.getNewfeedtypes();
        }
        $scope.pageChanged = function() {
            console.log($scope.currentPage);
            $scope.searchNewfeedtypes.current_page = $scope.currentPage;
            $scope.getNewfeedtypes();
        };
        $scope.setPage = function(pageNo) {
            $scope.currentPage = pageNo;
            console.log('doi page');
        };
        $scope.addNewfeedtypesData = {};
        $scope.createNewfeedtypes = function(formValidate) {
            if (formValidate.$invalid) {
                $cordovaToast.showLongTop('Please fill all the required information.!!!');
                return;
            }
            $ionicLoading.show({
                template: templateLoading
            });
            Newfeedtypes.save($scope.addNewfeedtypesData).success(function(data) {
                closeTarget('#settingNewsfeedTypeManagementAdd');
                $scope.getNewfeedtypes();
                if (data.status) {
                    $scope.addNewfeedtypesData = {};
                    $cordovaToast.showLongTop(data.msg);
                } else {
                    $cordovaToast.showLongTop(data.msg);
                }
            });
        }
        $scope.editNewfeedtypesData = {};
        $scope.editNewfeedtypes = function(id) {
            Newfeedtypes.show(id).success(function(data) {
                $scope.editNewfeedtypesData = data;
            });
        }
        $scope.updateNewfeedtypes = function(formValidate) {
        	 if (formValidate.$invalid) {
                $cordovaToast.showLongTop('Please fill all the required information.!!!');
                return;
            }
            $ionicLoading.show({
                template: templateLoading
            });
            var id = $scope.editNewfeedtypesData.id;
            Newfeedtypes.update(id, $scope.editNewfeedtypesData).success(function(data) {
                closeTarget('#settingNewsfeedTypeManagementEdit');
            	$scope.getNewfeedtypes();
                if (data.status) {
                    $scope.editNewfeedtypesData = {};
                    $cordovaToast.showLongTop(data.msg);
                } else {
                    $cordovaToast.showLongTop(data.msg);
                }
            }).error(function (data, status, headers, config) {
                    $ionicLoading.hide();
                    console.log('error status: ' + status);
                    $cordovaToast.showLongTop('Connection Error!!!');
                });
        }
        $scope.deleteNewfeedtypesData = {};
        $scope.deleteNewfeedtypesData = {};
        $scope.confrimNewfeedtypes = function(id, title, content) {
            $scope.deleteNewfeedtypesData = {
                id: id
            };
            var confirm = $.confirm(title, '<div class="text-content baseline">' + content + '</div>', {
                messageType: 'warning',
                titleIcon: 'remove',
                overlayTransparent: .75
            }, function() {
                $scope.deleteNewfeedtypes(id, confirm);
            });
        }
        $scope.deleteNewfeedtypes = function(id, confirm) {
            Newfeedtypes.destroy(id).success(function(data) {
                $scope.deleteNewfeedtypesData = {};
                if (data.status) {
                    $.notification('<div class="text-content">' + data.msg + '</div>', {
                        messageType: 'message-success',
                        autoClose: true
                    });
                    confirm.switchStateDialog("close");
                } else {
                    $.notification('<div class="text-content">' + data.msg + '</div>', {
                        messageType: 'message-success',
                        autoClose: true
                    });
                    confirm.switchStateDialog("close");
                }
                $scope.getNewfeedtypes();
            });
        }

        $scope.setIndexViewOptionNewfeedType = function(index){
            $scope.index_option_newfeed_type=index;
            $timeout( function(){
                $('#btn-view-option-newfeed-type-'+index).next('.quick-popup').addClass('show');
            }, 100);
        }

        $scope.getUserLogin = function() {
            return Login.getUserLogin().success(function(data) {
                if (data == false) {
                    localStorage.setItem("login_status", "1");
                redirectTo('index.html');
                } else {
                    var displayname = (data.first_name != null ? data.first_name : '') + ' ' + (data.last_name == null ? '' : data.last_name);
                    if ($.trim(displayname) == "") {
                        $scope.displayname = "Hello, " + data.email;
                    } else {
                        $scope.displayname = "Hello, " + displayname;
                    }
                }
            });
        }
        $scope.logout = function() {
            return Login.logout().success(function(data) {
                $window.location.href = url_login;
            });
        }
        stopTime = $interval($scope.checkLogin, time_refresh);
        // edit by haudao
        $scope.infoNewFeedType = {};
        $scope.openPopupNewFeedTypeOption = function(id) {
                $scope.infoNewFeedType.id = parseInt(id);
                $scope.editNewfeedtypes(id);
                appDialogToggle('open', '#popupSettingNewsfeedTypeOption');
            }
            // end edit
    })
    // function loadContentNewfeedType(){
    // 		var scope = angular.element($("#tabnewsfeedtype")).scope();
    // 		scope.$apply(function () {
    // 			scope.$$childHead.$$nextSibling.$$nextSibling.$$nextSibling.$$nextSibling.getNewfeedtypes();
    // 		});
    // }