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: "积分规则" });
    this.setData({ types: app.globalData.types })
    this.getData();
  },
  //搜索
  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
    })

  },
      //点击导航栏
  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.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,
    })
  },
  getData() {
    app.$get("api/integral/rule/trees", { pt_id: 3 }).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 = rule_tree.filter((item) => {
            return item_list[i][k].rule_id == item.id
          })
          list.push(item_list[i][k])
        }
      }
      that.setData({
        item_list: list,
        rule_tree: rule_tree,
        allItems: list,
        allTree:rule_tree,
      })
      console.log(item_list)
      console.log(rule_tree)
    }, (err) => {

    })
  },
});
