var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    all_rule_list: {},
    rule_tree: {},
    terr: [
      { name: '全部', id: 0 }
    ],
    isCreator:false,
    creatorName:'',

  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm: (data) => console.log(data),
    isGzType: '',
    defaultId: '',//默认选中
  },
  didMount() {
    that = this;
    this.setData({
      terr: [{ name: '全部', id: 0 }],
      isCreator: app.globalData.isCreator,
      creatorName:app.globalData.corpMessage.creators
    })
    this.getData();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
        //搜索
    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) {
      this.setData({ rule_tree: [] });
      var item = e.target.dataset.item;
      var index = e.target.dataset.index;
      var terr = this.data.terr;
      if ((index + 1) == terr.length) { return false };
      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
        })
      }
    },
    // 点击下一级
    openDown(e) {
      this.setData({ rule_tree: [] });
      var item = e.target.dataset.item;
      var terr = this.data.terr;

      terr.push(item);
      if (item.child.length > 0) {
        that.setData({
          rule_tree: item.child,
          terr: terr
        })
      }
    },
    radioChange: function (e) {
      this.props.onConfirm(e.detail.value);
      this.plusOne();
    },
    recursion(data) {
      for (var i = 0; i < data.length; i++) {
        data[i].checked = false;
        if (data[i].child.length > 0) {
          this.recursion(data[i].child);
        }
      }
      return data;
    },
    getData() {
      var pt_id
      if (that.props.isGzType) {
        pt_id = app.getTypesItem(that.props.isGzType).id;
      }
      app.$get2("api/integral/rule/trees", { pt_id: pt_id,cycle_type: 1 }).then((res) => {
        var rule_tree = res.data.data.rule_tree
        if (this.props.defaultId) {
          rule_tree.map(item => {
            if (this.props.defaultId == item.id) {
              item.checked = true;
            }
            
            item.child.map(item2 => {
              if (this.props.defaultId == item2.id) {
                item2.checked = true;
              }
            })

          })
        }
        that.setData({
          rule_tree: rule_tree,
          all_rule_list: rule_tree
        })
      })
    },
    //关闭
    plusOne(e) {
      this.props.onClose();
    },
  },
});
