<!--
数据授权 - 标签授权、人员授权、机构授权 -公用
@Author: linqian
@Date: 2021-04-09 16:37 
-->
<template>
    <div class="property-wapper__right">
        <dg-card     header="权限设置">
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="表授权" name="TABLE">
                    <table-column-auth
                         ref="tableAuth"
                        :key="key"
                        :subId="subId"
                        :subType="subType"
                        :saveApi="saveApi"
                        :singleAuthData="singleAuthData"
                        :reactTree="reactTree"
                        @checkChange="checkChange"
                        tab="TABLE"
                    ></table-column-auth>
                </el-tab-pane>
                <el-tab-pane label="列授权" name="COLUMN">
                    <table-column-auth
                         ref="columnAuth"
                        :key="key"
                        :subId="subId"
                        :subType="subType"
                        :saveApi="saveApi"
                        :reactTree="reactTree"
                        :singleAuthData="singleAuthData"
                        @checkChange="checkChange"
                        tab="COLUMN"
                    ></table-column-auth>
                </el-tab-pane>
            </el-tabs>
            <el-row class="property-wapper__right-btn">
                <!-- <el-button @click="handleViewAuthList" v-if="showCheckListBtn">清单</el-button> -->
                <dg-button v-if="subId" @click="handleReset" :icon="'重置' | optIcon">重置</dg-button>
                <dg-button v-if="subId" type="primary" @click="handleSave" :icon="'保存' | optIcon">保存</dg-button>
            </el-row>
        </dg-card>
    </div>
</template>

<script>
import tableColumnAuth from "./table-column-auth";
import * as api from "@/api/data-auth.js";

export default {
    props: {
        shadow: {
            type: String,
            default: "never1"
        },
        // 主体id
        subId: String,
        // 主体类型
        subType: String,
        // 保存接口
        saveApi: String
    },
    components: {
        tableColumnAuth
    },
    data() {
        return {
            // 激活的tab
            activeName: "TABLE",
            key: 0,
            singleAuthData: [],
            reactTree: false
         
        };
    },
    computed: {},
    methods: {
        /**
         * @description: 切换tab
         * @param {*} tab
         */
        handleClick(tab) {},
        /**
         * @description: 重置
         * @param {*} tab
         */
         handleReset() {
            this.reactTree = false;
            this.singleAuthData = [];
            this.key++;
        },
        checkChange(vo){
            this.reactTree = true;
            const params = {
                ...vo,
                authList: this.getSaveData().authVoList
            }
            api.reactTree(params).then(res => {
                this.singleAuthData = res.data.content;
            })
            
        },
        getSaveData(){
            let key = "";
            if (this.subType == "BUSINESS") {
                // 业务域
                key = "businessCode";
            } else if (this.subType == "ORG") {
                // 机构
                key = "orgCode";
            } else {
                // 人员
                key = "idcard";
            }
            let params1 = this.$refs.tableAuth.handleSave();
            let params2 = this.$refs.columnAuth.handleSave();
            let params = {
                authVoList: [...params1, ...params2]
            }
            params[key] = this.subId;  
            console.log("最终保存的参数", params);
            return params
        },
        handleSave() {
            const params = this.getSaveData();
            let obj = {};
             for (let i = 0; i < params.authVoList.length; i++) {
                const element = params.authVoList[i];
                if (!obj[element.dataType]) {
                    obj[element.dataType] = [element.dataId];
                } else {
                    obj[element.dataType].push(element.dataId)
                }
                
             }
             console.log("勾选联动接口参数",params);
            api[this.saveApi](params).then((res) => {
                this.$message.success("保存成功！");
            });
        }

    },
    created() {
        console.log("权限设置初始化");
    },
    mounted() {}
};
</script>

<style lang='scss' scoped>
.property-wapper__right {
    &-search {
        margin-bottom: 20px;
    }
    &-btn {
        text-align: right;
        margin-top: 20px;
    }
    /deep/ {
        .el-tabs__header {
            margin-bottom: 8px;
        }
        .el-card__body{
           height: 44.7rem;
         }
    }
}
</style>