var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    all_rule_list: {},
    isShow: false,
    terr: [
      { name: '全部', id: 0 }
    ],
    rule_tree: [],
    selectSenction: [],
  },
  props: {
    onConfirm: (data) => console.log(data),
    onClose: (data) => console.log(data),
    defaultSection: [],
  },
  didMount() {
    that = this;
    this.setData({
      isShow: this.props.isShow,
      terr: [{ name: '全部', id: 0 }],
      selectSenction: this.props.defaultSection,
    })
    this.getBmData();
  },
  methods: {
    onConfirm() {
      if (!this.data.selectSenction) {
        app.globalData.showToast("请选择部门");
        return;
      }
      this.props.onConfirm(this.data.selectSenction);
      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) {
       var all_rule_list= this.data.all_rule_list;
       var selectSenction=this.data.selectSenction;
        all_rule_list.map((item) => { //设置列表的选中状态
          item.checked = false;
          if (selectSenction.length > 0) {
            selectSenction.forEach(item2 => {
              if (item.id == item2.id) {
                item.checked = true;
              }
            })
          }
        })
        this.setData({
          terr: [{ name: '全部', id: 0 }],
          rule_tree: 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;
      var selectSenction=this.data.selectSenction;
      terr.push(item);
      if (item._child.length > 0) {
        item._child.map(item => {
          if (selectSenction.length > 0) {
            selectSenction.forEach(item2 => {
              if (item.id == item2.id) {
                item.checked = true;
              }
            })
          }
        })
        that.setData({
          rule_tree: item._child,
          terr: terr
        })
      }
    },
    radioChange: function (e) {
      var selectSenction = this.data.selectSenction;
      var isExist = false;
      let item = e.target.dataset.item;
      let index = e.target.dataset.index;
      let checked = `rule_tree[${index}].checked`;//获取当前控制选中的字段
      let elm = this.data.rule_tree[index].checked;//当前的状态
      this.setData({
        [checked]: !elm,
      })
      if (e.detail.value) {//当有值时选中
        selectSenction.forEach((element, _this) => {
          if (element.id == item.id) {
            isExist = true;
          }
        });
        if (!isExist) {
          selectSenction.push(item);
        }
      } else {//取消选中
        selectSenction.forEach((element, index, _this) => {
          if (element.id == item.id) {
            _this.splice(index, 1);
          }
        });
      }
      this.setData({
        selectSenction: selectSenction
      })
    },
    //获取部门列表
    getBmData() {
      app.$get2("api/department/tree", {}).then((res) => {
        var list = res.data.data.list || [];
        var defaultSection = that.props.defaultSection;
        list.map((item) => { //设置列表的选中状态
          item.checked = false;
          if (defaultSection.length > 0) {
            defaultSection.forEach(item2 => {
              if (item.id == item2.id) {
                item.checked = true;
              }
            })
          }
        })
        this.setData({
          rule_tree: list,
          all_rule_list: list,
        })
      })
    },
    //关闭
    onClose(e) {
      this.props.onClose();
    },
  },
});
