// if(!app){
// app.filter('propsFilter', function() {
//         return function(items, props) {
//             var out = [];
//             if (angular.isArray(items)) {
//                 var keys = Object.keys(props);
//                 items.forEach(function(item) {
//                     var itemMatches = false;
//                     for (var i = 0; i < keys.length; i++) {
//                         var prop = keys[i];
//                         var text = props[prop].toLowerCase();
//                         if (item[prop] && item[prop].toString().toLowerCase().indexOf(text) !== -1) {
//                             itemMatches = true;
//                             break;
//                         }
//                     }
//                     if (itemMatches) {
//                         out.push(item);
//                     }
//                 });
//             } else {
//                 // Let the output be the input untouched
//                 out = items;
//             }
//             return out;
//         };
//     });
// }
var messagequotes_pagesize = $('#messagequotes_pagesize');
angular.module('messagequotesCtrl', []).controller('messagequotesController', function ($cordovaToast, $ionicPopup, $ionicLoading, $scope, //
    $rootScope, $http, $window, $interval, Login, Messagequotes, $ionicScrollDelegate) {
    $scope.messagequotes = [{
        id: "",
        author: "",
        title: ""
    }];
    $scope.currentPage = 1;
    // var pager = localStorage.getItem('messagequotes_pagesize');
    // $scope.itemsPerPage = pager ? pager : 50;
    $scope.itemsPerPage = 200;
    $scope.entryLimit = $scope.itemsPerPage;
    $scope.searchMessagequotes = {
        rows: $scope.itemsPerPage,
        current_page: $scope.currentPage,
        search: ""
    };
    withoutPopup_LG = $scope;
    /*$scope.getUsers = function(){
     User.get().success(function(data){
     $scope.users= data;
     });
     }
     $scope.getUsers();
     */
    $scope.getMessagequotes = function () {
        $ionicLoading.show({
            template: templateLoading
        });
        Messagequotes.getPaging($scope.searchMessagequotes).success(function (data) {
            $rootScope.$applyAsync(function () {
                $scope.messagequotes = data.data;
            });
            $scope.currentPage = data.page;
            $scope.itemsPerPage = $scope.searchMessagequotes.rows;
            messagequotes_pagesize.val($scope.itemsPerPage);
            $scope.totalItems = data.total_rows;
            $ionicLoading.hide();
        });
    };
    //  $scope.getMessagequotes();
    $scope.getMessageQuoteLogin = function () {
        Messagequotes.getMessageQuoteLogin().success(function (data) {
            console.log(data);
            $scope.messagequoteslogin = data;
        });
    };
    $scope.searchChanged = function () {
        $scope.getMessagequotes();
    }
    $scope.$watch('responsive', function (val) {
        // if (val) {
        //  $('#tableMessagequotes').tableRespondsive({
        //   paging: true,
        //   pagingArg: {
        //    totalRow: 105,
        //    rowPerPage: 50,
        //    currentPage: 2
        //   }
        //  });
        //  $('.table-login-quote').tableRespondsive();
        // }
    });
    /*$scope.getSelectMessagequotes = function(){
     Messagequotes.getSelect().success(function(data){
     $scope.selectMessagequotes = data;
     });
     }
     $scope.getSelecMessagequotes();*/
    /*
     xem demo upload file tai day:
     http://jsfiddle.net/danialfarid/1m6fn6re/
     $scope.upload = function (file) {
     Upload.upload({
     url: '/todo',
     fields: $scope.todoData,
     file: file
     }).progress(function (evt) {
     var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
     console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
     }).success(function (data, status, headers, config) {
     console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
     }).error(function (data, status, headers, config) {
     console.log('error status: ' + status);
     })
     };
     */
    $scope.sizeChanged = function () {
        $scope.searchMessagequotes.rows = $scope.entryLimit;
        messagequotes_pagesize.val($scope.searchMessagequotes.rows);
        localStorage.setItem('messagequotes_pagesize', $scope.searchMessagequotes.rows);
        $scope.searchMessagequotes.current_page = 1;
        $scope.getMessagequotes();
    }
    $scope.pageChanged = function () {
        console.log($scope.currentPage);
        $scope.searchMessagequotes.current_page = $scope.currentPage;
        $scope.getMessagequotes();
    };
    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
        console.log('doi page');
    };
    $scope.addMessagequotesData = {};
    $scope.createMessagequotes = function (formValidate) {
        if (formValidate.$invalid) {
            $cordovaToast.showLongTop('Please fill all the required information.!!!');
            return;
        }
        $ionicLoading.show({
            template: templateLoading
        });
        Messagequotes.save($scope.addMessagequotesData).success(function (data) {
            $scope.getMessagequotes();
            closeTarget('#settingLoginManagementAdd');
            if (data.status) {
                $scope.addMessagequotesData = {};
                $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.editMessagequotesData = {};
    $scope.editMessagequotes = function (id) {
        Messagequotes.show(id).success(function (data) {
            $scope.editMessagequotesData = data;
        }).error(function (data, status, headers, config) {
            $ionicLoading.hide();
            console.log('error status: ' + status);
            $cordovaToast.showLongTop('Connection Error!!!');
        });
    }
    $scope.updateMessagequotes = function (formValidate) {
        if (formValidate.$invalid) {
            $cordovaToast.showLongTop('Please fill all the required information.!!!');
            return;
        }
        $ionicLoading.show({
            template: templateLoading
        });
        var id = $scope.editMessagequotesData.id;
        Messagequotes.update(id, $scope.editMessagequotesData).success(function (data) {
            $scope.getMessagequotes();
            closeTarget("#settingLoginManagementEdit");
            if (data.status) {
                $scope.editMessagequotesData = {};
                $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.deleteMessagequotesData = {};
    $scope.confrimMessagequotes = function (item) {
        $scope.deleteMessagequotesData = item;
    }
    // $scope.deleteMessagequotes = function(id, confirm) {
    //     Messagequotes.destroy(id).success(function(data) {
    //         $scope.deleteMessagequotesData = {};
    //         if (data.status) {
    //             toastr.success(data.msg);
    //             confirm.switchStateDialog("close");
    //         } else {
    //             toastr.error(data.msg);
    //             confirm.switchStateDialog("close");
    //         }
    //         $scope.getMessagequotes();
    //     });
    // }
    $scope.deleteMessagequotes = function () {
        var id=$scope.deleteMessagequotesData.id;
        $ionicLoading.show({
            template: templateLoading
        });
        Messagequotes.destroy(id).success(function (data) {
            closeTarget('#settingLoginManagementDelete');
            $ionicLoading.hide();
            var idx = _.findLastIndex($scope.messagequotes, {id: id});
            // $scope.getMessagequotes();
            if (data.status) {
                $scope.messagequotes.splice(idx, 1);
                $ionicScrollDelegate.$getByHandle('scrollMessagequotes').resize();
                $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.getUserLogin = function () {
        return Login.getUserLogin().success(function (data) {
            if (data == false) {
                $scope.displayname = "Profile";
            } 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;
        });
    }
    $scope.checkLogin = function () {
        return Login.checkLogin().success(function (data) {
            if (data.status == false) {
                $window.location.href = url_login;
            } else {
                $scope.getUserLogin();
            }
        });
    }
    if ((window.location.pathname).substring((window.location.pathname).lastIndexOf('/') + 1) == 'messagequotes.html') {
        $scope.checkLogin();
        stopTime = $interval($scope.checkLogin, time_refresh);
    }
    $scope.infoMessageQuotes = {};
    $scope.openPopupNoteTaskOption = function (id) {
        $scope.infoMessageQuotes.id_message = parseInt(id);
        $scope.editMessagequotes(id);
        appDialogToggle('open', '#popupSettingLoginOption');
    }
});
// function loadContentMessagequotes(){
//      //$rootScope.$apply(function(){$scope.getMessagequotes()});
//      //$scope.getMessagequotes();
//      var scope = angular.element($("#tab-login")).scope();
//      console.log(scope);
//      scope.$apply(function () {
//          scope.$$childHead.$$nextSibling.$$childHead.$$nextSibling.getMessagequotes();
//      });
//  }
