var app = getApp();
var that;
Page({
  data: {
    userList: [],
    all_rule_list: [],
    rule_tree: [],
    terr: [{ name: '全公司', id: 0 }],
    searchUser: [],//搜索
    isItem: true//是否为搜索的值
  },
  onLoad(query) {
    that = this;
    dd.setNavigationBar({ title: "部门/员工" });
    this.setData({
      terr: [{ name: '全公司', id: 0 }],
    })
    this.getMessage();
    this.getBmData();
    this.getUserData(0);
  },
  tbDd(){
    var http1= app.$post("api/ding/employee_sync",{});
    var http2= app.$post("api/ding/department_sync",{});
    Promise.all([http1,http2]).then((item)=>{
      app.globalData.showToast(item[1].data.msg);
      console.log(item);
    })
  },
  bindKeyInput(e) {
    app.$get("api/employee/list", { dept_id: 0 }).then((res) => {
      var list = res.data.data.list;
      var items = list.filter((item) => {
        return item.name.indexOf(e.detail.value) >= 0
      });
      this.setData({
        searchUser: items,
        isItem: e.detail.value ? false : true
      })
    })
  },
  //点击导航栏
  activeItem(e) {
    console.log(e);
    var item = e.target.dataset.item;
    var index = e.target.dataset.index;
    var terr = this.data.terr;
    if ((index + 1) == terr.length) { return false };
    this.setData({ rule_tree: [] });
    if (index == 0) {
      this.setData({
        terr: [{ name: '全公司', id: 0 }],
        rule_tree: this.data.all_rule_list,
      })
    } else {
      var arr = terr.slice(0, index + 1);
      this.setData({
        rule_tree: item._child,
        terr: arr
      })
    }
    this.getUserData(item.id);
  },
  //点击一级
  activeTree(e) {
    console.log(e);
    var item = e.target.dataset.item
    var terr = this.data.terr;
    terr.push(item)
    this.setData({
      rule_tree: item._child,
      terr: terr
    });
    this.getUserData(item.id);
  },
      //获取部门列表
  getBmData() {
      app.$get("api/department/tree", {}).then((res) => {
        var list = res.data.data.list;
        this.setData({
          rule_tree: list,
          all_rule_list: list
        })
      })
  },
  //获取员工信息
  getUserData(dept_id) {
    app.$get("api/employee/list", { dept_id: dept_id }).then((res) => {
      var list = res.data.data.list;
      this.setData({
        userList: list
      })
    })
  },
  getMessage(){
    app.$get("api/site/info").then((res) => {
      this.setData({
        gsData: res.data.data
      })
    })
  }
});
