var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    allItems:[],
    item_list: {},
    rule_tree: {},
    types:'',
    activeIndex:'',//一级索引
    activeItemIndex:'',//二级索引
    item_child_list:[],//子数据集合
    isItem:true,//判断是否显示左侧
    selectItem:[],//选择的数据
  },
  props: {
    onClose: (data) => console.log(data),
    onConfirm:(data) => console.log(data),
    isGzType:'',
    defaultId:'',//默认选中
  },
  didMount() {
    that = this;
    this.setData({types:app.globalData.types})
    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
        })

    },
    //点击一级
    activeTree(e){
      var item=e.target.dataset.item
      var item_list=this.data.allItems;
      if(item.id==this.data.activeIndex){
        this.setData({
          activeIndex:'',
          item_child_list:[],
          item_list: item_list,
        })
        return false;
      }
      this.setData({item_child_list:[]});
      if(item.child&&item.child.length>0){
          this.setData({
            activeItemIndex:item.child[0].id
          })
        if(item.child[0].child&&item.child[0].child.length>0){
          this.setData({
            item_child_list:item.child[0].child
          })
        }
      }
      this.setData({
        activeIndex:item.id
      })
      this.getItem(item.id);
    },
    //点击二级
    activeItem(e){
      var item=e.target.dataset.item
      this.setData({
        activeItemIndex:item.id,
        item_list:[]
      });
      if(item.child&&item.child.length>0){
          this.setData({
            item_child_list:item.child
          })
      }else{
          this.setData({
            item_child_list:[]
          })
      }
     this.getItem(item.id);
    },
    openDetail(e){
        var id=e.target.dataset.item.id;
        this.setData({
          activeItemIndex:id,
          item_child_list:[]
        });
        this.getItem(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,
        })
    },
    radioChange: function (e) {
      this.props.onConfirm(e.detail.value);
      this.plusOne();
    },
    getData() {
      var pt_id,typeObj;
      if(that.props.isGzType){
        typeObj=app.getTypesItem(that.props.isGzType);
        pt_id=typeObj.id;
      }
      app.$get("api/integral/rule/trees", { pt_id: pt_id }).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=that.props.isGzType? typeObj:app.getTypesItem(item_list[i][k].pt_id);
            list.push(item_list[i][k])
          }
        }
        if(this.props.defaultId){
          list.map(item=>{
            if(this.props.defaultId==item.id){
              item.checked=true;
            }
          })
        }
        that.setData({
          item_list: list,
          rule_tree:rule_tree,
          allItems:list,
        })
      })
    },
    //关闭
    plusOne(e) {
      this.props.onClose();
    },
  },
});
