var app = getApp();
var that;
Page({
  data: {
    allItems: [],
    allTree: [],
    item_list: {},
    rule_tree: {},
    types: '',
    isItem: true,//判断是否显示左侧
    selectItem: [],//选择的数据
    terr: [
      { name: '全部', id: 0 }
    ],
  },
  onLoad(query) {
    that = this;
    this.setData({
      terr: [
        { name: '全部', id: 0 }
      ],
    })
    dd.setNavigationBar({ title: "积分分类" });
  },
  onShow() {
    this.setData({ types: app.globalData.types })
    this.getData();
  },
  onReset() {
    this.setData({
      selectItem: [],
      isItem: true,
      isVal: false
    })
  },
  //搜索
  bindKeyInput(e) {
    var item_list = this.data.allItems;
    var items = item_list.filter((item) => {
      return item.remark.indexOf(e.detail.value) >= 0
    });
    this.setData({
      selectItem: items,
      isItem: e.detail.value ? false : true,
      isVal: e.detail.value ? true : false
    })

  },
  //点击导航栏
  activeItem(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.allTree,
        item_list: this.data.allItems
      })
    } else {
      var arr = terr.slice(0, index + 1);
      this.setData({
        rule_tree: item.child,
        terr: arr
      })
      this.getItem(item.id);
    }

  },
  //点击一级
  activeTree(e) {
    var item = e.target.dataset.item
    var terr = this.data.terr;
    terr.push(item)
    this.setData({
      rule_tree: item.child,
      terr: terr
    });

    this.getItem(item.id);
  },
  //从全部中过滤
  getItem(id) {
    var item_list = this.data.allItems;
    var items = item_list.filter((item) => { return item.rule_id == id });
    that.setData({
      item_list: items,
    })
  },
  // 递归
  getItems(id, arr) {
    var obj = {};
    for (let i = 0; i < arr.length; i++) {
      if (id == arr[i].id) {
        obj = arr[i];
        break;
      } else if (arr[i].child.length > 0) {
        if(this.getItems(id, arr[i].child).id){
           obj =this.getItems(id, arr[i].child)
        } 
      }
    }
    return obj;
  },
  getData() {
    app.$get("api/integral/rule/trees", { cycle_type: 1 }).then((res) => {
      var item_list = res.data.data.item_list
      var rule_tree = res.data.data.rule_tree
      var list = []
      for (let i in item_list) {
        for (let k in item_list[i]) {
          item_list[i][k].pt_Obj = this.data.types.filter((item) => {
            return item_list[i][k].pt_id == item.id
          })
          item_list[i][k].typeName = that.getItems(item_list[i][k].rule_id, rule_tree);
          list.push(item_list[i][k])
        }
      }
      that.setData({
        item_list: list,
        rule_tree: rule_tree,
        allItems: list,
        allTree: rule_tree,
      })
      console.log(this.data.item_list,this.data.rule_tree)
    })
  },
});
