<template>
<div>
  <el-form :model="cubeForm"  ref="ruleForm" label-width="110px" class="demo-ruleForm">
    <el-form-item label="是否公开">
      <el-radio-group v-model="cubeForm.publicStatus">
        <el-radio :label="1" @change="cubeForm.dataPermIdList = []">公开</el-radio>
        <el-radio :label="0">权限组控制</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="选择默认权限组" v-if="cubeForm.publicStatus === 0 ">
      <el-button size="mini" type="primary" @click="cubeForm.dataPermIdList = list.map((i=> {return i.id}))">全选</el-button>
      <el-button size="mini" type="primary" @click="cubeForm.dataPermIdList = []">全不选</el-button>
      <el-checkbox-group v-model="cubeForm.dataPermIdList" size="small" style="margin-top: 10px">
        <el-checkbox :label="item.id" border v-for="item in list" :key="item.id">{{item.permName}}</el-checkbox>
      </el-checkbox-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </el-form-item>
  </el-form>
</div>
</template>
<script>
import {getCubeDataPerm, updateCubeDataPerm} from "@/api/onlineDev/visualDev";
import {groupPage} from "@/api/system/authGroup";
import {cubeDetail} from "@/api/newbi/dataset";

export default {
  name: "customCubePerm",
  props:['cubeId','activeName'],
  data(){
    return {
      list:[],
      relationList:[],
      cubeForm:{
        publicStatus:1,
        dataPermIdList:[]
      }
    }
  },
  created() {
    this.init()
  },
  watch: {
    activeName: {
      deep: true,
      handler(val) {
        if (val && val === 'cubeDataPerm') {
          this.init()
        }
      }
    }
  },
  methods:{
    async init() {
      let cubeRes = await cubeDetail({cubeDataId: this.cubeId})
      if (cubeRes.data.code === 200) {
      //   publicStatus 1公开 0私有
        this.cubeForm.publicStatus = cubeRes.data.data.publicStatus
      }
      groupPage({size: 1000, current: 1}).then((res) => {
        this.list = res.data.data.records || []
      })
      if (this.cubeForm.publicStatus !== 1) {
        getCubeDataPerm({cubeDataId: this.cubeId}).then((res) => {
          this.relationList = res.data.data || []
          if (this.relationList.length) this.cubeForm.dataPermIdList = res.data.data.map(item => {return item.dataPermId})
        })
      }
    },
    submitForm(){
      if (this.cubeForm.publicStatus === 1) {
        this.cubeForm.dataPermIdList = []
      }else {
        if (!this.cubeForm.dataPermIdList.length){
          this.$message({ message: '请选择至少一个权限组', type: 'warning', duration: 1500 })
          return;
        }
      }
      updateCubeDataPerm(this.cubeId,this.cubeForm.publicStatus,this.cubeForm.dataPermIdList).then((res) => {
        if (res.data.code === 200) {
          this.$message({ message: res.data.msg, type: 'success', duration: 1500 })
          this.init()
        }

      })
    },
  }
}
</script>
<style scoped lang="scss">

</style>
