/** 设备管理错误页面 */
'use strict';

var angular = require('angular');
require('../modules');
require('../services/DeviceManager');
require('../../login/services/LoginService');

var module = angular.module('devicemanager');

module.controller('DeviceError', function($scope, DeviceManager, $q, $ionicActionSheet, LoginService, $state, AppConfig) {

  $scope.syncRemote = function() {
    return DeviceManager.syncRemote();
  };
  /**
   * 重新登录
   */
  $scope.toLogin = function() {
    return actionSheet().then(function(value) {
      return value ? LoginService.logout() : true;
    }).then(function() {
      if(!AppConfig.getCustomResult('passwordLoginEnable')) {
        $state.go('calogin');
      } else {
        $state.go('login'); // 定制是否有密码登录页面
      }
    });
  };

  function actionSheet() {
    var deferred = $q.defer();
    $scope.needLogout = true;
    // if ($scope.needLogout) {
      $ionicActionSheet.show({
        buttons: [{
          text: '<i class="icon ion-log-out sino-logout-icon"></i>退出登录'
        }],
        cancelText: '取消',
        cancel: function() {
          deferred.reject('取消登录');
          return true;
        },
        buttonClicked: function() {
          deferred.resolve(true);
          return true;
        }
      });
    // } else {
    //   deferred.resolve(false);
    // }
    return deferred.promise;
  }

});
