<template>
  <!-- 工位程序页面 -->
  <div>
    <div class="divborder newSearchDiv" style="margin: 0px;">
      <div class="content">
        <Tree class="treeDiv" :data="treeList" ref="tree" empty-text="" @on-select-change="getList()"></Tree>
      </div>
      <div class="content">
        <Tree
          :data="treedata1"
          show-checkbox
          multiple
          ref="tree1"
          empty-text=""
           @on-check-change="addPost()"
        ></Tree>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sectionid:0,
      treeList:[],
      treedata1:[],
      menuids:[]
    }
  },
  computed: {
      
  },
  created () {
    this.getTrees();
  },
  methods:{
    //获取产线层级数据
    getTrees(){
      this.axios
        .request({
            url: '/config/ProjectLevelSection/GetSectionTree',
            method: 'get'
        })
        .then(res => {
          let data = res.data.data;
          this.treeList = []
          if(res.data.ret===1&&data!=null&&data!=undefined&&data.length >0){
            data.forEach((item,index)=>{
              let arr = {};
              arr.value = item.value;
              arr.title = item.label;
              arr.expand = true;
              arr.disabled = true;
              arr.children = this.initChild(item);
              this.treeList.push(arr);
            })
          }
        })
    },
    initChild(data){
      let arr = [];
      if(data.children!==null && data.children!==undefined &&data.children!==""&&data.children.length >0){
        data.children.forEach((item,index)=>{
          let arr1 = {};
          arr1.value = item.value;
          arr1.title = item.label;
          arr1.expand = true;
          arr1.disabled = Array.isArray(item.children)&&item.children.length>0?true:false;
          arr1.children = this.initChild(item);
          arr.push(arr1);
        })
      }
      return arr;
    },
    getList(){
      let node = this.$refs.tree.getSelectedNodes();
      if(node.length > 0){
        let sectionid = node[0].value;
        this.sectionid = sectionid;
        //获取产线层级关联的菜单
        this.getMenus(sectionid);
      }
    },
    //获取产线层级获取的菜单
    getMenus(id){
      var params = {
        section:id
      }
      this.axios
        .request({
            url: '/acc/Power/GetMenusBySectionId',
            params,
            method: 'get'
        })
        .then(res => {
          let data = res.data.data;
          let arr = [];
          if(res.data.ret===1&&data!=null&&data!=undefined&&data.length >0){
            arr = data.map((item)=>{
              return parseInt(item.menuId);
            })
          }
          this.menuids = arr;
          //获取acc页面菜单
          this.getAccMenus();
        })
    },
    //获取acc页面菜单
    getAccMenus(){
      this.treedata1 = [];
      this.axios
        .request({
            url: '/acc/Power/GetAccMenu',
            method: 'get'
        })
        .then(res => {
          let data = res.data.data;
          data.forEach((item,index)=>{
            let arr = {};
            arr.id = item.id;
            arr.title = item.title;
            arr.expand = true;
            arr.checked = this.menuids.indexOf(item.id)!=-1?true:false;
            arr.children = this.getChildren(item);
            this.treedata1.push(arr)
          })
        })
    },
    getChildren(data){
      let arr = [];
      if(data.children!==null && data.children!==undefined &&data.children!==""&&data.children.length >0){
        data.children.forEach((item,index)=>{
          let arr1 = {};
          arr1.id = item.id;
          arr1.title = item.title;
          arr1.expand = true;
          arr1.checked = this.menuids.indexOf(item.id)!=-1?true:false;
          arr1.children = this.getChildren(item);
          arr.push(arr1);
        })
      }
      return arr;
    },
    //添加产线菜单关系
    addPost(){
      let nodes = this.$refs.tree1.getCheckedNodes();
      let arr = [];
      if(nodes.length > 0){
        arr = nodes.map((item)=>{
          return parseInt(item.id);
        })
      }
      var data = {
          section:this.sectionid,
          power:arr.join()
      }
      this.axios.request({
          url: '/acc/Power/SavePowerSectionData',
          data,
          method: 'post'
      })
      .then(res => {
        
      })
    }
  }
};
</script>

<style lang="less" scoped>
  .content{
    width:49%;
    height:86vh;
    border:1px solid #ddd;
    float: left;
    margin:0 0.5% 10px;
    border-radius:5px;
    overflow:auto;
  }
</style>