CooWorkApp.controller('headController', function ($rootScope, $scope, $location, $sce, locdbServices, cooConfig, g,rest,ngDialog) {
    
    var html = '';

    var fs = require('fs');
 

    var openAlert = function (msg) {
        $scope.message = msg;
        $scope.cDlgId = ngDialog.open({
            template: 'tmpl/alert.html',
            className: 'ngdialog-theme-default-dialog',
            controller: 'dialogController',
            overlay: false,
            scope: $scope
        });
    };
   

    $scope.nav = [];
    $scope.XingMing = '';

    $scope.updateCache = function (key) {

        var etor = {
            key:key
        };

        $.when(rest.updateCache(etor)).done(function (data) {
            console.log(data);
        });
        console.log(key);
    };

    $scope.clearCache = function () {
        
        
        locdbServices.deleteDatabase();


        var config = fs.readFileSync('js/service/cooConfig.js').toString();
        config = config.replace(/isAutoLogin:[a-z]+/ig, 'isAutoLogin:false');
        config = config.replace(/isAdmin:[a-z]+/ig, 'isAdmin:false');
        fs.writeFileSync('js/service/cooConfig.js', config);

        $.when(rest.logOff()).done(function (data) {

            console.log(data);
        });




        cooConfig.configObj.isStart = false;
        cooConfig.configObj.isLogin = false;
        cooConfig.configObj.isAutoLogin = false;
        $scope.nav = [];
        $scope.$$phase || $scope.$apply();
        openAlert('已清空缓存，请重新打开应用程序');
      //  window.location.href = location.protocol + '//' + location.host + location.pathname + '#/login';


       

    };


    $scope.refresh = function () {
        window.location.href = window.location.href + "/";
        $scope.$$phase || $scope.$apply();
    };




    $scope.logout = function () {


        var config = fs.readFileSync('js/service/cooConfig.js').toString();
        config = config.replace(/isAutoLogin:[a-z]+/ig, 'isAutoLogin:false');
        config = config.replace(/isAdmin:[a-z]+/ig, 'isAdmin:false');
        fs.writeFileSync('js/service/cooConfig.js', config);

        $.when(rest.logOff()).done(function (data) {

            console.log(data);
        });

        


        cooConfig.configObj.isStart = false;
        cooConfig.configObj.isLogin = false;
        cooConfig.configObj.isAutoLogin = false;
        $scope.nav = [];
        $scope.$$phase || $scope.$apply();
        window.location.href = location.protocol + '//' + location.host + location.pathname + '#/login';
    };


    var headerStart = function () {

        if (cooConfig.configObj.isStart) {





            $scope.XingMing = cooConfig.configObj.xingMing;
            $scope.isAdmin = cooConfig.configObj.isAdmin;   
            $scope.nav = [];
                $(cooConfig.allcategory).each(function (i, c) {

                        var pages = g.pageFilterByCategory(c.ID);
                        var subPages = [];
                        $(pages).each(function (j, k) {

                            var userDfn = false;
                            switch(k.pageType)
                            {
                                case 10:
                                    if (fs.existsSync('custom/'+cooConfig.configObj.companyId+'/list_' + k.ID + '.html')) {
                                        userDfn = true;
                                    }
                                    subPages.push({ page: k, define: userDfn });
                                    break;
                                case 16:
                                    if (fs.existsSync('custom/' + cooConfig.configObj.companyId + '/report_' + k.ID + '.html')) {
                                        userDfn = true;
                                    }
                                    subPages.push({ page: k, define: userDfn });
                                    break;
                                case 15:
                                    if (fs.existsSync('custom/' + cooConfig.configObj.companyId + '/create_' + k.ID + '.html')) {
                                        userDfn = true;
                                    }
                                    subPages.push({ page: k, define: userDfn });

                                    break;

                                case 99:
                                    if (fs.existsSync('custom/' + cooConfig.configObj.companyId + '/complexcreate_' + k.ID + '.html')) {
                                        userDfn = true;
                                    }
                                    subPages.push({ page: k, define: userDfn });

                                    break;

                            }

                          
                        });
                        $scope.nav.push({ c: c, p: subPages});
                
 
                });



        

                $scope.$$phase || $scope.$apply();

                g.navInit();
          

          
        }

    };
    


    $scope.$watch(function () { return cooConfig.configObj.isStart }, function (value) {
        
        headerStart();

    });


});





CooWorkApp.controller('passwordController', function ($rootScope, $scope, $location, $sce, locdbServices, cooConfig, g, rest, ngDialog) {
    var openAlert = function (msg) {
        $scope.message = msg;
        $scope.cDlgId = ngDialog.open({
            template: 'tmpl/alert.html',
            className: 'ngdialog-theme-default-dialog',
            controller: 'dialogController',
            overlay: false,
            scope: $scope
        });
    };

    $scope.submit = function () {

        var etor ={
            OldPassword: $.trim($('#OldPassword').val()),
            NewPassword: $.trim($('#NewPassword').val()),
            ConfirmPassword: $.trim($('#ConfirmPassword').val())
        };

        $.when(rest.changePassword(etor)).done(function (ret) {

            openAlert(ret);

        });

    };


});