var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    all_rule_list: [],
    isShow: false,
    terr: [
      { name: '全部', id: 0 }
    ],
    rule_tree: [],
    activeId:'',
  },
  props: {
    onConfirm: (data) => console.log(data),
    onClose: (data) => console.log(data),
    defaultSection: '',
  },
  didMount() {
    that = this;
    this.setData({
      isShow: this.props.isShow,
      activeId:this.props.defaultSection,
      terr: [{ name: '全部', id: 0 }],
      selectTissue: { name: '全部分类', id: '' }
    })
    this.getData();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onConfirm() {
      this.props.onConfirm(this.data.selectTissue);
      this.props.onClose();
    },
    //点击导航栏
    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.all_rule_list
        })
      } else {
        var arr = terr.slice(0, index + 1);
        this.setData({
          rule_tree: item.child,
          terr: arr
        })
      }
    },
    // 点击下一级
    openDown(e) {
      that.setData({ rule_tree:[]});
      var item = e.target.dataset.item;
      var terr = this.data.terr;
      terr.push(item);
      that.setData({
        rule_tree: item.child,
        terr: terr
      })
    },
    radioChange: function (e) {
      this.setData({
        selectTissue: e.target.dataset.item,
        activeId:e.target.dataset.item.id
      })
    },
    getData() {
      app.$get2("api/integral/rule/trees", { pt_id: 3,cycle_type: 1 }).then((res) => {
        var rule_tree = res.data.data.rule_tree;
        rule_tree.unshift({
          name:'全部分类',
          id:'',
          child:[]
        })
        that.setData({
          rule_tree: rule_tree,
          all_rule_list:rule_tree
        })
      })
    },
    
    //关闭
    onClose(e) {
      this.props.onClose();
    },
  },
});
