<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>
                <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 { 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
        },
        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
        let addBoser = ref(false)
        let boserForm = ref({
            dirName: "",
        })
        function inital() {
            console.log(free,'free');
            addBoser.value = result
        }
        function backTo() {
            addBoser.value = false
            emit("getChildrenC", addBoser.value)
        }
        function addFloder() {
            if (free === 1) {
                documents.addDocument(
                    {
                        "dirName": boserForm.value.dirName,
                        "parentId": drId - 0,
                        "spaceId": spId - 0,
                    }
                ).then(res => {
                    if (res.code === 200) {
                        ElMessage({
                            message: '添加一级目录成功',
                            type: 'success',
                        })
                    }
                    addBoser.value = false
                    emit("getChildrenC", addBoser.value)
                    props.getAllTop()
                })
            } else {
                documents.addDocument(
                    {
                        "dirName": boserForm.value.dirName,
                        "parentId": newdrid - 0,
                        "spaceId": newspid - 0,
                    }
                ).then(res => {
                    if (res.code === 200) {
                        ElMessage({
                            message: '添加成功',
                            type: 'success',
                        })
                    }
                    addBoser.value = false
                    emit("getChildrenC", addBoser.value)
                    props.getAllTop()
                })
            }
        }
        onMounted(() => {
            inital()
        })
        return {
            addBoser,
            boserForm,
            inital,
            addBack: backTo,
            sureAddBoser:addFloder,
            diClose:backTo,
            spId,
            drId,
            newdrid,
            newspid,
        }
    }
}
</script>

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