<template>
    <div>
        <div>
            <el-dialog v-model="folderName" title="重命名" width="30%">
                <el-input v-model="newName" maxlength="10"></el-input>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="backToFile">取消</el-button>
                        <el-button type="primary" @click="sureFolderName">确认</el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { ref, onMounted, toRef } from "vue"
import { ElMessage } from "element-plus";
import documents from "../../../api/document/document";
export default {
    props: {
        rename: {
            type: Boolean,
            required: true
        },
        selectedBox:{
            type:Number,
            required:true
        },
        getAllTop:{
            type:Function,
            required:true
        },
        copyFolderName:{
            type:String,
            required:true
        }
    },
    setup(props, { emit }) {
        let { backToFile, sureFolderName } = toRef(props)
        let result = props.rename
        let reid = props.selectedBox
        let reSet = props.copyFolderName
        let folderName = ref(false)
        let newName = ref('')
        function inlineList() {
            folderName.value = result
            newName.value = reSet
        }
        function to() {
            folderName.value = false
            emit("getRename", folderName.value)
        }
        function editName() {
            documents.editDocument({
                dirId: reid - 0,
                name: newName.value,
            }).then(res => {
                if (res.code === 200) {
                    props.getAllTop()
                    ElMessage({
                        message: '重命名成功',
                        type: "success"
                    })
                    folderName.value = false
                    emit("getRename", folderName.value)
                }
            })
        }
        onMounted(() => {
            inlineList()
        })
        return {
            folderName,
            newName,
            inlineList,
            backToFile: to,
            sureFolderName:editName,
        }
    }
}
</script>

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