'use strict';

angular.module('app').controller('SettingController', ['$scope', '$state', '$stateParams', function ($scope, $state, $stateParams) {

  lxtalkClient.Invoke('{B36B3985-B13D-45F3-A755-AFA6937B4F33}', '_Register', '', $scope);
  $scope.user = {
    name: '陆趣趣',
    l:'0578+0-lslqq',
    shotL:'lslqq',
    comp:'浙江万赛软件科技有限公司',
    branch:'运维部',
    sex:'1',
    mob:'15215756180',
    sMob:'666666',
    tel:'2189026',
    duty:'运维部经理',
    officeID:'运维室001',
    email:'lqq@wswin.cn',
    shortL:'lslqq'
  };
  $scope.sys = {
    autoRun:true
  }
  $scope.hotkey = {
    showMsg:'CTRL+ALT+X',
    cutScreen:'CTRL+ALT+S',
    sendMsg:'Ctrl+Enter'
  }
  $scope.net = {
    ip:"255.255.255.255",
    port:443
  }

  $scope.userChanged = false;
  $scope.sysChanged = false;
  $scope.hotkeyChanged = false;

  $scope.$on('onNetRestore', function(event, data){
    var net = JSON.parse(data);
    $scope.net = net;
    $scope.$apply();
  });
  
  $scope.onUserInfoChange = function() {
    console.log($scope.checkMoblie($scope.user.mob));
    console.log($scope.user.mob);
    $scope.userChanged = $scope.checkMoblie($scope.user.mob);
  }

  $scope.onNetChange = function(){
    $scope.netChanged = $scope.checkIP($scope.net.ip) && $scope.checkPort($scope.net.port);
  }
  $scope.onSysChange = function() {
    $scope.sysChanged = true;
  }
  $scope.onHotkeyChange = function() {
    $scope.hotkeyChanged = true;
  }

  $scope.update = function (params) {
    var settings = JSON.parse(params);
    $scope.user = settings.user;
    var reg = /-([\W\w]*)/; 
    var group = reg.exec($scope.user.l);
    $scope.user.shortL = group == null? $scope.user.l : group[1];
    $scope.sys = settings.sys;
    $scope.webPanels = settings.webPanels;
    console.log(JSON.stringify($scope.sys));
    alert(JSON.stringify($scope.webPanels));
    //宁夏企税通企业版使用
    // if ($scope.user.shortL.length >= 15)
    //   $state.go('setting-enterprise');
  }

  $scope.save = function() {
    var settings = {};
    if($scope.userChanged)
      settings.user = $scope.user;
    if($scope.sysChanged)
      settings.sys = $scope.sys;
    if($scope.hotkeyChanged)
      settings.hotkey = $scope.hotkey;
    if ($scope.netChanged)
      settings.net = $scope.net;
    lxtalkClient.Invoke('{B36B3985-B13D-45F3-A755-AFA6937B4F33}', 'Save', JSON.stringify(settings), null);
    $scope.userChanged = false;
    $scope.sysChanged = false;
    $scope.hotkeyChanged = false;
    $scope.netChanged = false;
  }

  $scope.checkMoblie = function(params) {
    return (/^1[3|4|5|7|8]\d{9}$/.test(params));
  }
  $scope.checkEmail = function (params) {
    return (/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(params);
  }
  $scope.checkIP = function (params){
    return (/((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))/).test(params);
  }
  $scope.checkPort = function (params){
    return (/^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/).test(params);
  }
}]);