// if(!app){
//  var app = angular.module('App',[
//  //'ui.bootstrap',
//  'LoginService',
//  'notetasktypesService',
//  'notetasktypesCtrl',
//  'notetasktypesEditDirective',
//  'notetasktypesDeleteDirective',
//  'notetasktypesDirective',
//  'notetasktypesAddDirective',
//  'navDirective',
//  'ngFileUpload',
//  'ckDirective',
//  'datetimepicker',
//  'modalDirective',
//  //'ngSanitize',
//  //'expDirective',// remove comment if use file explorer.
//  //'explorerDirective',
//  //'ui.select'
//  ]).config([
//  'datetimepickerProvider',
//  function (datetimepickerProvider) {
//      datetimepickerProvider.setOptions({
//          locale: 'en'
//      });
//  }
//  ])
//  .run([
//      '$rootScope',
//      function ($rootScope) {
//          $rootScope.scoped = {
//              format: 'HH:mm:ss'
//          };
//          $rootScope.vm = {
//              datetime: '05/13/2011 6:30 AM'
//          }
//      }
//      ]);
// }
// var notetasktypes_pagesize = $('#notetasktypes_pagesize');
angular.module('notetasktypesCtrl', []).controller('notetasktypesController', function($cordovaToast, $ionicPopup, $ionicLoading, //
    $scope, $http, $window, $interval, Login, Notetasktypes,$ionicScrollDelegate,$timeout) {
    $scope.currentPageNotetasktypes = 1;
        // var pager = localStorage.getItem('notetasktypes_pagesize');
        // $scope.itemsPerPage= pager?pager:50;
        $scope.itemsPerPage = 200;
        $scope.entryLimit = $scope.itemsPerPage;
        $scope.searchNotetasktypes = {
            rows: $scope.itemsPerPage,
            current_page: $scope.currentPageNotetasktypes,
            search: "",
            show_all: false
        };
        withoutPopup_NT = $scope;
        $scope.Input_value = {};
        /*$scope.getUsers = function(){
            User.get().success(function(data){
                $scope.users= data;
            });
        }
        $scope.getUsers();
        */
        $scope.getNotetasktypes = function() {
            $ionicLoading.show({
                template: templateLoading
            });
            Notetasktypes.getPaging($scope.searchNotetasktypes).success(function(data) {
                $scope.notetasktypes = data.data;
                $scope.currentPageNotetasktypes = data.page;
                $scope.itemsPerPageNotetasktypes = $scope.searchNotetasktypes.rows;
                // notetasktypes_pagesize.val($scope.itemsPerPage);
                $scope.totalItemsNotetasktypes = data.total_rows;
                $ionicLoading.hide();
            });
        };
        $scope.searchChanged = function() {
            $scope.getNotetasktypes();
        }
        $scope.ShowActive_NT = function(show_all) {
            $scope.searchNotetasktypes.show_all = show_all;
            $scope.getNotetasktypes();
        }
            /*$scope.getSelectNotetasktypes = function(){
                Notetasktypes.getSelect().success(function(data){
                    $scope.selectNotetasktypes = data;
                });
            }
            $scope.getSelecNotetasktypes();*/
            /*
            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.searchNotetasktypes.rows = $scope.entryLimit;
                notetasktypes_pagesize.val($scope.searchNotetasktypes.rows);
                localStorage.setItem('notetasktypes_pagesize', $scope.searchNotetasktypes.rows);
                $scope.searchNotetasktypes.current_page = 1;
                $scope.getNotetasktypes();
            }
            $scope.pageChanged = function(itemsPerPageNotetasktypes) {
                $scope.searchNotetasktypes.currentPageNotetasktypes = itemsPerPageNotetasktypes;
                $scope.getNotetasktypes();
            };
        // $scope.getNotetasktypes();
        $scope.setPage = function(pageNo) {
            $scope.currentPage = pageNo;
            console.log('doi page');
        };
        $scope.addNotetasktypesData = {};
        $scope.addNotetasktypesData = {};
        $scope.createNotetasktypes = function(formValidate) {
            if (formValidate.$invalid) {
                $cordovaToast.showLongTop('Please fill all the required information.!!!');
                return;
            }
            $ionicLoading.show({
                template: templateLoading
            });
            console.log($scope.addNotetasktypesData);
            Notetasktypes.save($scope.addNotetasktypesData).success(function(data) {
                $scope.getNotetasktypes();
                closeTarget('#settingNoteTaskManagementAdd');
                if (data.status) {
                    $scope.addNotetasktypesData = {};
                    $scope.Input_value = {};
                    $cordovaToast.showLongTop(data.msg);
                } else {
                    $cordovaToast.showLongTop(data.msg);
                }
            });
        }
        $scope.editNotetasktypesData = {};
        $scope.editNotetasktypes = function(id) {
            Notetasktypes.show(id).success(function(data) {
                $scope.editNotetasktypesData = data;
                // console.log($scope.editNotetasktypesData);
                // $scope.editNotetasktypesData.status = String(data.status);
            });
        }
        $scope.updateNotetasktypes = function(formValidate) {
            if (formValidate.$invalid) {
                $cordovaToast.showLongTop('Please fill all the required information.!!!');
                return;
            }
            $ionicLoading.show({
                template: templateLoading
            });
            var id = $scope.editNotetasktypesData.id;
            Notetasktypes.update(id, $scope.editNotetasktypesData).success(function(data) {
                closeTarget('#settingNoteTaskManagementEdit');
                $scope.getNotetasktypes();
                if (data.status) {
                    $scope.editNotetasktypesData = {};
                    $cordovaToast.showLongTop(data.msg);
                } else {
                    $cordovaToast.showLongTop(data.msg);
                }
            });
        }
        $scope.deleteNotetasktypesData = {};
        $scope.confrimNotetasktypes = function(item) {
            $scope.deleteNotetasktypesData=item;
        }
        // $scope.deleteNotetasktypes = function(id, confirm, status) {
        //     Notetasktypes.destroy(id, status).success(function(data) {
        //         $scope.deleteNotetasktypesData = {};
        //         if (data.status) {
        //             toastr.success(data.msg);
        //             confirm.switchStateDialog("close");
        //         } else {
        //             toastr.error(data.msg);
        //             confirm.switchStateDialog("close");
        //         }
        //         $scope.getNotetasktypes();
        //     });
        // }
        $scope.DeleteNoteTask = function() {
            id = $scope.deleteNotetasktypesData.id;
            status = $scope.deleteNotetasktypesData.status;
            $ionicLoading.show({
              template: templateLoading
          });

            Notetasktypes.destroy(id, status).success(function(data) {
                $ionicLoading.hide();
                $scope.deleteNotetasktypesData = {};
                closeTarget('#settingNoteTaskManagementDelete');
                var idx = _.findLastIndex($scope.notetasktypes, { id: id.toString() });
                if (data.status) {
                    if ($scope.searchNotetasktypes.show_all) {
                        $scope.notetasktypes[idx].status = status == "1" ? "0" : "1";
                    } else {
                        $scope.notetasktypes.splice(idx, 1);
                    }
                    $ionicScrollDelegate.$getByHandle('scrollNoteTask').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.setIndexViewOptionNoteTask = function(index){
            $scope.index_option_note_task=index;
            $timeout( function(){
                $('#btn-view-option-note-task-'+index).next('.quick-popup').addClass('show');
            }, 100);

        }
        $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) == 'notetasktypes.html') {
            $scope.checkLogin();
            stopTime = $interval($scope.checkLogin, time_refresh);
        }
        /*
        <p>Selected: {{address.selectAddesses}}</p>
                <ui-select multiple 
                    ng-model="address.selectAddesses"
                    theme="bootstrap"
                    ng-disabled="false" 
                    close-on-select="false" 
                    style="width: 300px;"
                    title="Choose an address">
                    <ui-select-match placeholder="Enter an address...">{{$item.name}}</ui-select-match>
                    <ui-select-choices repeat="add in addresses| propsFilter: {name: $select.search, id: $select.search}"
                        refresh="refreshAddresses($select.search)"
                        refresh-delay="0">
                        <div ng-bind-html="add.name | highlight: $select.search"></div>
                    </ui-select-choices>
                </ui-select> 
        $scope.address = {};
        $scope.address.selectAddesses = [{id:"1",name:"abc"}];
        $scope.refreshAddresses = function(address) {
            if(address.length>0){
                var params = {search: address};
                Colors.getSelect2(params).success(function(data){
                 var diff = differentArray(data,$scope.address.selectAddesses);
                    $scope.addresses =diff;
                })  
            }
        };*/
        // edit by haudao
        // $scope.setValueForNoteTaskType = function(id) {
        //     var arr = ['Inactive', 'Active', '']
        //     $scope.Input_value.status = arr[id];
        // }
        $scope.infoNoteTaskAction = {};
        $scope.openPopupNoteTaskOption = function(id, status) {
            $scope.infoNoteTaskAction.id = parseInt(id);
            $scope.infoNoteTaskAction.status = parseInt(status);
                // console.log($scope.infoNoteTaskAction);
                $scope.editNotetasktypes(id);
                appDialogToggle('open', '#popupSettingNoteTaskOption');
            }
            // end
        })
    // function loadContentNoteTask(){
    //  var scope = angular.element($("#tab-note-task")).scope();
    //  console.log(scope);
    //  scope.$apply(function () {
    //      scope.$$childHead.getNotetasktypes();
    //  });
    // }
