<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="目录分类">
                        <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> -->
                    <el-form-item label="是否加密" v-if="!(FolderRole && !free )">
                            <el-switch v-model="boserForm.isEncrypt" active-value="Y" inactive-value="N" />
                    </el-form-item>
                    <el-form-item label="加密等级" v-if="!(FolderRole && !free)">
                        <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>
                </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 } from 'vue';
import documents from '../../../api/document/document';
import { getLevel } from '@/api/level/level.js';
import { ElMessage } from 'element-plus';
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
        },
        thisFolderRole: {
            type: Object,
            required: true,
            default: ()=>{},
        },
        getAllTop: Function,
    },
    setup(props, { emit }) {
        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 = props.thisFolderRole
        console.log("FolderRole",props);
        let addBoser = ref(false)
        let boserForm = ref({
            dirName: "",
            dirType:1,
            isEncrypt:"Y",
            encryptLevel:"L1"
        })
        const levelList = ref([])
        function inital() {
            console.log(free, 'free');
            addBoser.value = result
        }
        function backTo() {
            addBoser.value = false
            emit("getChildrenC", addBoser.value)
        }
        function addFloder() {
            if (boserForm.value.dirName === '') {
                ElMessage({
                    type: "error",
                    message: "目录名称不能为空"
                })
            } else {
                if (free === 1) {
                    documents.addDocument(
                        {
                            "dirName": boserForm.value.dirName,
                            "parentId": drId - 0,
                            "spaceId": spId - 0,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null
                        }
                    ).then(res => {
                        if (res.code === 200) {
                            ElMessage({
                                message: '添加一级目录成功',
                                type: 'success',
                            })
                        }
                        addBoser.value = false
                        emit("getChildrenC", addBoser.value)
                        props.getAllTop()
                    })
                } else {
                    let query = {}
                    if(FolderRole){
                        query={
                            "dirName": boserForm.value.dirName,
                            "parentId": newdrid - 0,
                            "spaceId": newspid - 0,
                            "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,
                            "isEncrypt":boserForm.value.isEncrypt,
                            "encryptLevel":boserForm.value.isEncrypt == "Y"?boserForm.value.encryptLevel:null
                        }
                    }
                    documents.addDocument(
                      query
                    ).then(res => {
                        if (res.code === 200) {
                            ElMessage({
                                message: '添加成功',
                                type: 'success',
                            })
                        }
                        addBoser.value = false
                        emit("getChildrenC", addBoser.value)
                        props.getAllTop()
                    })
                }
            }

        }
        const getLevelList = async ()=>{
            const res = await getLevel()
            if(res.rows){
                levelList.value = res.rows
            }
            // console.log(res);
        }
        onMounted(() => {
            inital()
            getLevelList()
        })
        return {
            addBoser,
            boserForm,
            inital,
            addBack: backTo,
            sureAddBoser: addFloder,
            diClose: backTo,
            spId,
            drId,
            newdrid,
            newspid,
            levelList,
            getLevelList,
            FolderRole,
            free
        }
    }
}
</script>

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