'use strict';

var angular = require('angular'),
  _ = require('jsUtil');
require('../modules');
require('../../login/services/LoginService');
require('../../../commons/services/AppConfig');

var module = angular.module('setting.directives');

  module.directive('sinoLogout', function(LoginService, $ionicActionSheet, $toast, $state, 
  AppConfig) {
  return {
    restrict: 'E',
    scope: true,
    templateUrl: 'views/setting/sinoLogout.tpl.html',
    link: function(scope) {
    /**
     * 退出登录。
     */
    scope.logout = function() {
      $ionicActionSheet.show({
        buttons: [{
          text: '<i class="icon ion-log-out sino-logout-icon"></i>退出登录'
        }],
        cancelText: '取消',
        cancel: function() {
          return true;
        },
        buttonClicked: function() {
          LoginService.logout().then(function(result) {
            if (result.status) {
              if(!AppConfig.getCustomResult('passwordLoginEnable')) {
                $state.go('calogin');
              } else {
                $state.go('login'); // 定制是够显示密码登录页面
              }
            } else {
              $toast.showLongCenter('退出失败。');
            }
          }, function() {
            $toast.showLongCenter('退出失败。');
          });
          return true;
        }
      });
    };

    }
  };
});
