'use strict';

angular.module('app').controller('GSmsController', ['$scope', function ($scope) {
  //lxtalkClient.Invoke('{FB60F992-A0FD-47B3-AAA7-E80DF209C5A4}', '_Register', '', $scope);
  $scope.zNodes_c = [
    { id: 0, pId: 0, name: "王大锤一家", open:true,
      children: [
        { id: 1, pId: 0, name: "爷爷", t: "爷爷"},
        { id: 11, pId: 1, name: "父亲", t: "父亲"},
        { id: 111, name: "大儿子", t: "老大" },
        { id: 112, name: "王大锤", t: "老二" },
      ]
    }
  ];
  $scope.zNodes_b = [
    { id: 0, pId: 0, name: "王大锤住宅区", open:true},
    { id: 1, pId: 0, name: "王大锤一家", t: "王大锤一家",
      children: [
        { id: 10, name: "爷爷", t: "爷爷"},
        { id: 11,  name: "父亲", t: "父亲"},
        { id: 111, name: "大儿子", t: "老大" },
        { id: 112, name: "王大锤", t: "老二" },
      ]
    },
    { id: 2, pId: 0, name: "大爷家", t: "大爷"},
    { id: 21, pId: 2, name: "大伯家", t: "大伯",
      children: [
        { id: 211, name: "大堂哥", t: "老大" },
        { id: 212, name: "二堂哥", t: "老二" },
      ]
    },
    { id: 3, pId: 0, name: "张爷爷家", t: "张爷爷"},
    { id: 31, pId: 3, name: "张叔家", t: "张叔"},
    { id: 310, pId: 31, name: "张二叔家", t: "张叔",
      children: [
        { id: 311, name: "张 三", t: "哥们" },
        { id: 312, name: "张小花", t: "可爱的小花" },
      ]
    }
  ];
  $scope.zNodes_a = [
    { id: 3, pId: 0, name: "我的好友", t: "好友",open:true,
      children: [
        { id: 30, name: "张爷爷", t: "张爷爷" },
        { id: 31, name: "张叔", t: "张叔"},
        { id: 32, name: "张二叔", t: "张叔"},
        { id: 33, name: "张 三", t: "哥们" },
        { id: 34, name: "张小花", t: "可爱的小花" },
      ]
    }
  ];

  $scope.groups = [];
  
  $scope.group1 = [
    { id: 3, pId: 0, name: "浙江万赛群", t: "浙江万赛群", open:true, iconSkin:"father",
      children: [
        { id: 30, name: "张爷爷", t: "张爷爷", iconSkin:"children"},
        { id: 31, name: "张叔", t: "张叔", iconSkin:"children"},
        { id: 32, name: "张二叔", t: "张叔", iconSkin:"children"},
        { id: 33, name: "张 三", t: "哥们" , iconSkin:"children"},
        { id: 34, name: "张小花", t: "可爱的小花", iconSkin:"children"},
        ]
    }
  ];

  $scope.update = function (userJson) {
    var user = JSON.parse(userJson);
    $scope.user = user;
  };

//显示群组树
  $scope.onSelectedChange = function () {
    var nodes = $scope.groupUsers.getNodes();
    for (var i=0, len=nodes.length; i<len; i++) {
      $scope.groupUsers.removeNode(nodes[0]);
    }
    if ($scope.teamName == '浙江万赛群')
    {
      console.log($scope.teamName);
      $scope.groupUsers.addNodes(null, $scope.group1);
    }
  };
//查找联系人
  $scope.onSelectUser = function () {
    console.log($scope.userKey);
    var nodes = $scope.treeBranchs.getNodes();
    for (var i=0, len=nodes.length; i<len; i++) {
      $scope.treeBranchs.removeNode(nodes[0]);
    }//移除主树
    if ($scope.userKey != "") {
      angular.forEach($scope.zNodes_b, function (info) {
        if (info.children != null) {
          $scope.nodes_child = info.children;
          $scope.users.addNodes(null, $scope.nodes_child);
        }
      });//生成虚拟子树
      $scope.keyNodes = $scope.users.getNodesByParamFuzzy("name", $scope.userKey, null);
      $scope.treeBranchs.addNodes(null, $scope.keyNodes);//生成查询结果树
      var nodes_ch = $scope.users.getNodes();
      for (var i = 0, len = nodes_ch.length; i < len; i++) {
        $scope.users.removeNode(nodes_ch[0]);
      }//清空虚拟子树
    }
    else {
      $scope.treeBranchs.addNodes(null, $scope.zNodes_b);
    }
  };
//刷新树
  $scope.onRefreshTree = function () {
    var nodes_b = $scope.treeBranchs.getNodes();
    for (var i=0, len=nodes_b.length; i<len; i++) {
      $scope.treeBranchs.removeNode(nodes_b[0]);
    }
    $scope.treeBranchs.addNodes(null, $scope.zNodes_b);
  };
//清空子树
  $scope.delSelectedUser = function () {
    var nodes_u = $scope.userSelected.getNodes();
    for (var i=0, len=nodes_u.length; i<len; i++) {
      $scope.userSelected.removeNode(nodes_u[0]);
    }
  };
//单机节点展开
  $scope.onClick = function () {
    var nodes = $scope.treeBranchs.getSelectedNodes();
    if (nodes.length > 0) {
      $scope.treeBranchs.expandNode(nodes[0], true, false, true);
    }
  };
//显示已选中人员
  $scope.onCheck = function () {
    var nodes_c = $scope.treeCompany.getCheckedNodes(true);
    var nodes_b = $scope.treeBranchs.getCheckedNodes(true);
    var nodes_a = $scope.treeAddressL.getCheckedNodes(true);
    var nodes_g = $scope.groupUsers.getCheckedNodes(true);
    var nodes = $scope.userSelected.getNodes();
    for (var i=0, len=nodes.length; i<len; i++) {
      $scope.userSelected.removeNode(nodes[0]);
    }//清空树 
    angular.forEach(nodes_c, function (info) {
      if (info.children === undefined){
        $scope.nodes_u = info;
        $scope.userSelected.addNodes(null, $scope.nodes_u);
      }
    });
    angular.forEach(nodes_b, function (info) {
      if (info.children === undefined){
        $scope.nodes_u = info;
        $scope.userSelected.addNodes(null, $scope.nodes_u);
      }
    });
    angular.forEach(nodes_a, function (info) {
      if (info.children === undefined){
        $scope.nodes_u = info;
        $scope.userSelected.addNodes(null, $scope.nodes_u);
      }
    });
    angular.forEach(nodes_g, function (info) {
      if (info.children === undefined){
        $scope.nodes_u = info;
        $scope.userSelected.addNodes(null, $scope.nodes_u);
      }
    });
  };
}]);