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: '全部', dept_id:0}],
      selectTissue:{name:'全公司',dept_id:''}
    })
    this.getBmData();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onConfirm(){
        if(!this.data.selectTissue){
          app.globalData.showToast("请选择部门");
          return;
        }
        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: '全部', dept_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){
        item._child.map(item=>{
          if(item.dept_id==this.props.defaultSection){
            item.checked=true;
          }
        }) 
        that.setData({
          rule_tree:item._child,
          terr:terr
        })
       }
    },
    radioChange: function(e) {
        this.setData({
          selectTissue:e.target.dataset.item,
          activeId:e.target.dataset.item.dept_id,
        })
    },
    //获取部门列表
    getBmData() {
      app.$get2("api/per/user/department", {}).then((res) => {
        var list = res.data.data.dept_tree||[];
        list.unshift({
          name:'全公司',
          dept_id:''
        })
        this.setData({
          rule_tree: list,
          all_rule_list:list,
        })
      })
    },
    //关闭
    onClose(e) {
      this.props.onClose();
    },
  },
});
