<template>
    <div>
        <div>
            <el-dialog v-model="addBoser" title="新建文件夹" width="30%" @close="diClose">
                <el-form :model="boserForm" label-width="120px">
                    <el-form-item label="目录名称">
                        <el-input v-model="boserForm.dirName" />
                    </el-form-item>
                    <el-form-item label="目录分类" v-if="free || (!free && thisType.dirType != '2' )">
                        <el-radio-group v-model="boserForm.dirType">
                            <el-radio :label="1">公开目录</el-radio>
                            <el-radio :label="2">组织目录</el-radio>
                        </el-radio-group>
                    </el-form-item>
                   <template v-if="boserForm.dirType === 2 || thisType.dirType == '2'">
                     <el-form-item label="是否加密" v-if="!FolderRole.levelCode || free">
                            <el-switch v-model="boserForm.isEncrypt" active-value="Y" inactive-value="N" />
                    </el-form-item>
                    <el-form-item label="加密等级" v-if=" (!FolderRole.levelCode || free) && boserForm.isEncrypt === 'Y'">
                        <el-radio-group v-model="boserForm.encryptLevel">
                            <el-radio :label="item.levelCode" v-for="item in levelList" :key="item">{{item.levelName}}</el-radio>
                        </el-radio-group>
                    </el-form-item>
                        <template v-if="thisRole.length<1 || free">
                            <el-form-item label="成员权限">
                                <el-checkbox-group v-model="boserForm.memberRoles">
                                    <el-checkbox v-for="item in dir_rules" :key="item.value"  :label="item.value">{{item.label}}</el-checkbox>
                                </el-checkbox-group>
                            </el-form-item>
                            <el-form-item label="选择成员" v-if="boserForm.dirType === 2">
                            </el-form-item>
                            <CreateTasnsfer v-if="boserForm.dirType === 2" @setUsers="setUsers" :openTrans="openTrans"></CreateTasnsfer>
                        </template>
                   </template>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="addBack">取消</el-button>
                        <el-button type="primary" @click="sureAddBoser">
                            确认
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { ref, onMounted, toRef,toRaw } from 'vue';
import documents from '../../../api/document/document';
import {listUser} from '../../../api/biz/user';
import { getLevel } from '@/api/level/level.js';
import { ElMessage } from 'element-plus';
import CreateTasnsfer from '../components/createTasnsfer.vue'

export default {
    props: {
        folderCase: {
            type: Boolean,
            required: true
        },
        oneOrTwo: {
            type: Number,
            required: true
        },
        impDirId: {
            type: Number,
            required: true
        },
        impSpaceId: {
            type: Number,
            required: true
        },
        newDirId: {
            type: Number,
            required: true
        },
        newSpaceId: {
            type: Number,
            required: true
        },
        thisType: {
            type: String,
            required: true
        },
        thisFolderRole: {
            type: Object,
            required: true,
            default: ()=>{},
        },
        thisRole: {
            type: Object,
            required: true,
            default: ()=>{},
        },
        refreshFile: Function,
    },
    setup(props, { emit }) {
        const {
            proxy
        } = getCurrentInstance();
        const {
            dir_rules
        } = proxy.useDict('dir_rules');
        let { addBack, sureAddBoser, diClose } = toRef(props)
        let result = props.folderCase
        let free = props.oneOrTwo
        let spId = props.impSpaceId
        let drId = props.impDirId
        let newspid = props.newSpaceId
        let newdrid = props.newDirId
        const FolderRole =free?'': props.thisFolderRole
        const thisRole = free?'': props.thisRole
        const thisType = free?'': props.thisType
        const users = ref()
        const openTrans = ref(true)
        console.log("FolderRole",FolderRole);
        console.log("thisRole",thisRole);
        console.log("thisType",thisType);
        let addBoser = ref(false)
        let boserForm = ref({
            dirName: "",
            dirType:1,
            isEncrypt:"Y",
            encryptLevel:"L1",
            memberRoles:[],
            users:[]
        })
        const levelList = ref([])
        function inital() {
            console.log(free, 'free');
            addBoser.value = result
        }
        function backTo() {
            addBoser.value = false
            emit("getChildrenC", addBoser.value)
        }
        function addFloder() {
            console.log('boserForm',boserForm.value);
            if (boserForm.value.dirName === '') {
                ElMessage({
                    type: "error",
                    message: "目录名称不能为空"
                })
            } else {
                if (free === 1) {
                    let query = {}
                    if(boserForm.value.dirType === 2){
                        query = {
                            "dirName": boserForm.value.dirName,
                            "parentId": drId - 0,
                            "spaceId": spId - 0,
                            "dirType":thisType.dirType=='2'?'2':boserForm.value.dirType,
                            "dirRole":boserForm.value.memberRoles.join(','),
                            "users":boserForm.value.users,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null
                        }
                    }else{
                        query = {
                            "dirName": boserForm.value.dirName,
                            "parentId": drId - 0,
                            "spaceId": spId - 0,
                            "dirType":thisType.dirType=='2'?'2':boserForm.value.dirType,
                            
                    }
                    }
                    documents.addDocument(
                       query
                    ).then(res => {
                        if (res.code === 200) {
                            ElMessage({
                                message: '添加一级目录成功',
                                type: 'success',
                            })
                        }
                        addBoser.value = false
                        emit("getChildrenC", addBoser.value)
                        props.refreshFile()
                    })
                } else {
                    let query = {}
                    if(FolderRole.levelCode){
                        //父级有权限自动继承 没有就用添加的值
                        query={
                            "dirName": boserForm.value.dirName,
                            "parentId": newdrid - 0,
                            "spaceId": newspid - 0,
                            "dirType":boserForm.value.dirType,
                            "isEncrypt":"Y",
                            "encryptLevel":FolderRole.levelCode
                        }
                        console.log('query',query);
                        console.log('FolderRole',FolderRole);
                    }else{
                        query={
                            "dirName": boserForm.value.dirName,
                            "parentId": newdrid - 0,
                            "spaceId": newspid - 0,
                            "dirType":boserForm.value.dirType,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null
                        }
                    }
                    if(thisRole.length){
                        query.dirRole = thisRole.join(',')
                    }else{
                          query.dirRole = boserForm.value.memberRoles.join(',')
                          query.users = boserForm.value.users
                    }
                    console.log('free0query',query);
                    documents.addDocument(
                      query
                    ).then(res => {
                        if (res.code === 200) {
                            ElMessage({
                                message: '添加成功',
                                type: 'success',
                            })
                        }
                        addBoser.value = false
                        emit("getChildrenC", addBoser.value)
                        props.refreshFile()
                    })
                }
            }

        }
        const getLevelList = async ()=>{
            const res = await getLevel()
            if(res.rows){
                levelList.value = res.rows
            }
            // console.log(res);
        }
        const setUsers = (arr)=>{
            const oldArr = toRaw(arr)
            // boserForm.value.users = oldArr.map(item=>toRaw(item))
            const newArr = oldArr.map(item=>toRaw(item))
            boserForm.value.users = newArr.map(item=>{
                delete item['disabled']
                return item
            })
            console.log('newArr',boserForm.value.users);
        }
        const getUser =async  ()=>{
           const  res = await listUser({dirId:thisType.dirId})
             console.log('getUser',res);
             users.value = res.rows
        }
        onMounted(() => {
            inital()
            getLevelList()
            if(thisRole.length){
                getUser()
            }
        })
        return {
            addBoser,
            boserForm,
            inital,
            addBack: backTo,
            sureAddBoser: addFloder,
            diClose: backTo,
            spId,
            drId,
            newdrid,
            newspid,
            levelList,
            getLevelList,
            FolderRole,
            free,
            openTrans,
            setUsers,
            dir_rules,
            proxy,
            thisRole,
            thisType,
            getUser,
            users
        }
    },
    components: {
        CreateTasnsfer,
    },
}
</script>

<style lang="scss" scoped></style>