var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    all_user_list: {},
    isShow: false,
  },
  props: {
    onConfirm: (data) => console.log(data),
    onClose: (data) => console.log(data),
  },
  didMount() {
    that = this;
    this.setData({ isShow: this.props.isShow })
    this.getData();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    //全
    ruleActiveAll(e) {
      var index = e.target.dataset.index;
      var items = this.data.rule_tree[index].child;
      var elem = `rule_tree[${index}].checked`;
      var elemChild = `rule_tree[${index}].child`;
      items.map((item) => {
        item.checked = e.detail.value
      })
      this.setData({
        [elem]: e.detail.value,
        [elemChild]:items
      });
    },
    //单
    ruleActive(e) {
      var index = e.target.dataset.index;
      var indexChild = e.target.dataset.indexChild;
      var isChecked = this.data.rule_tree[index].child[indexChild].checked;
      var elem = `rule_tree[${index}].child[${indexChild}].checked`;
      this.setData({
        [elem]: !isChecked
      })
    },
    onConfirm() {
      var rule_tree=this.data.rule_tree;
      var arr=[];
      rule_tree.forEach(item => {
        if(item.child.length>0){
          item.child.forEach((a)=>{
              if(a.checked==true){
                arr.push(a.id);
              }
          })
        }
      });
      this.props.onConfirm(arr);
      this.props.onClose();
    },
    radioChange: function (e) {
      if (e.detail.value) {
        var arr = e.detail.value;
        this.setData({
          selectUserId: e.detail.value
        })
      } else {
        this.setData({
          selectUserId: []
        })
      }
    },

    getData() {
      app.$get("api/integral/rule/trees", { pt_id: 3 }).then((res) => {
        var rule_tree = res.data.data.rule_tree;
        rule_tree.map((item) => {
          item.checked = false;
          item.disabled = false
          if (item.child.length > 0) {
            item.child.map((a) => {
              a.checked = false;
            })
          } else {
            // item.disabled = true;
          }
        })
        that.setData({
          rule_tree: rule_tree
        })
      }, (err) => {

      })
    },
    //关闭
    onClose(e) {
      this.props.onClose();
    },
  },
});
