<template>
    <div>
      <el-form ref="form" label-width="120px">
        <el-form-item
          prop="role_name"
      >
      <el-tree
       :data="data"
        show-checkbox
        node-key="id"
        ref="tree"
        check-strictly="true"
        highlight-current
        default-expand-all="true"
        :props="defaultProps"
        :default-checked-keys="rule"
        @check="handleCheck"
        >
        </el-tree>
    </el-form-item>
    <el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="onSubmit" :disabled="Isdisabled" :loading="isloading">保存</el-button>
          <el-button style="color: #5075FC " @click="closeDlg">取消</el-button>
        </div>
      </el-form-item>
      </el-form>
    </div>
  </template>
  <script>
  export default {
    props: {
      authorityDataForm: {
        type: Object,
        default: () => {}
      },
      isloading:{
        type:Boolean,
        default: true
      },
      closeDlg:{
        type:Function
      }
    },
    data() {
      return {
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        Isdisabled: false,
        rule:[0],
        form:{}
      }
    },
    mounted() {
      if(this.authorityDataForm) {
        // this.form = Object.assign({} , this.authorityDataForm)
        // let obj = this.authorityDataForm
        // const arr = Object.keys(obj).map(key => obj[key]);
        // this.rule = arr
        this.rule = this.authorityDataForm.rule
        console.log("权限：",this.rule)
      }
      this.authorityList()
    },
    methods: {
      closeDlg() {
        this.$emit('closeDlg',1)
      },
      onSubmit() {

            this.$emit('authoritySubmitClick', this.$refs.tree.getCheckedKeys())
      },
      authorityList(){
        let _t= this
        let parames = {}
        this.$api.authority.getRecursionMenu(parames).then(res=>{
                if(res.code==200){
                   console.log(res.data)
                  _t.data = res.data
                }else{
                    this.$message.error("查询失败")
                }
            })
      },
      handleCheck(data, checked, indeterminate) {
      const checkedKeys = this.$refs.tree.getCheckedKeys();
      // const halfCheckedKeys = this.$refs.tree.getHalfCheckedKeys();
      // 将所有选中的节点的父节点也选中
      checkedKeys.forEach(key => {
        const parent = this.findParent(key, this.data);
        if (parent) {
          checkedKeys.push(parent.id);
        }
      });
      // 将所有半选中的节点的父节点也选中
      halfCheckedKeys.forEach(key => {
        const parent = this.findParent(key, this.data);
        if (parent) {
          checkedKeys.push(parent.id);
        }
      });
      // 去除重复的节点
      const uniqueKeys = [...new Set(checkedKeys)];
      // 处理uniqueKeys，进行后续操作，例如传递值等
      console.log(uniqueKeys);
      this.data = uniqueKeys
    },
    findParent(id, data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].id === id) {
          return null;
        }
        if (data[i].children) {
          const parent = this.findParent(id, data[i].children);
          if (parent) {
            return parent;
          }
        }
      }
      return null;
    }
    }
  }
  </script>

  