<template>
    <div class="container">
        <div class="search-ctn">
            <searchs></searchs>
        </div>
        <div v-for="(folderListItem, folderListIndex) of currentList" :key="folderListIndex">
            <folder-list
                v-if="!currentIsFiLe"
                :list="folderListItem"
                :list-title="currentIsFirstLevel?fistLevelFolderTitile[folderListIndex]:''"
                @folderClick="intoFolder">
            </folder-list>
        </div>
        <file-list
            ref="fileList"
            :mode="'myUpload'"
            :list="currentList"
            v-if="currentIsFiLe"
            @handleClickItem="toggleShowBtn"
            @handleOpen="openFile"
            @handleShare="shareFile"
            @handleDeleteFile="handleDeleteFile"
            @updateFileName="updateFileName">
        </file-list>
    </div>
</template>

<script>
export default {
    name: 'MyUpload',
    components: {
        Searchs: () => import('./components/Searchs'),
        FolderList: () => import('./components/FolderList'),
        FileList: () => import('./components/FileList')
    },
    data () {
        return {
            currentList: [],
            currentIsFirstLevel: true, // 当前列表是不是第一层文件夹
            currentIsFiLe: false, // 当前层次是否为文档列表
            breadcrumb: [], // 面包屑
            fistLevelFolderTitile: ['个人文件夹', '学校文件夹']
        }
    },
    mounted () {
        this.getFolderList()
        this.$EventBus.$on('backLayer', this.backLayer)
    },
    methods: {
        getFolderList () {
            /* 获取文件夹列表 */
            this.$api.schoolBasedResourcesApi.GetFolderList().then(res => {
                if (res.data.Status) {
                    let data = res.data.Data
                    this.initFromBreadcrumb(data)
                }
            })
        },
        initFromBreadcrumb (data) {
            /* 通过面包屑初始化数据 */
            this.currentIsFile = false
            let breadcrumb = this.breadcrumb
            if (breadcrumb.length <= 1) {
                this.breadcrumb = this.initFirstLevelData(data)
                this.$EventBus.$emit('intoLayer', true, '校本资源')
            } else {
                let newBreadcrumb = this.initFirstLevelData(data)
                let currentList = data
                for (let i = 1; i < breadcrumb.length; i++) {
                    let hasThisFile = false
                    for (let j = 0; j < currentList.length; j++) {
                        if (currentList[j].b_id === breadcrumb[i].parent.b_id) {
                            let isFile = (!currentList[j].children || currentList[j].children.length === 0) && currentList[j].file_count > 0
                            this.currentIsFile = isFile
                            this.addCrumb(newBreadcrumb, currentList[j], isFile)
                            if (isFile) {
                                this.getFileList(currentList[j])
                            } else {
                                currentList = currentList[j].children
                            }
                            hasThisFile = true
                            break
                        }
                    }
                    if (hasThisFile === false) {
                        break
                    }
                }
                this.breadcrumb = newBreadcrumb
                this.$EventBus.$emit('intoLayer', false, this.breadcrumb[this.breadcrumb.length - 1].label)
                if (!this.currentIsFile) {
                    this.currentList = [currentList]
                }
            }
        },
        getFileList (file) {
            /* 获取文件列表 */
            const folderId = file.b_id
            const selType = file.folder_type
            let data = {
                data: { folder_id: folderId, sel_type: selType },
                PageSize: 999,
                PageNumber: 1
            }
            this.$api.schoolBasedResourcesApi.GetFileList(data).then(res => {
                if (res.data.Status) {
                    let data = res.data.Data.Data
                    data.forEach(item => {
                        item.showFunBtn = false
                    })
                    this.currentList = data
                }
            })
        },
        initFirstLevelData (data) {
            /* 初始化文件夹第一层数据结构 */
            let list = [[], []]
            for (let i = 0; i < data.length; i++) {
                if (data[i].folder_type === 1) {
                    list[0].push(data[i]) // 需注释
                    list[1].push(data[i])
                } else {
                    list[0].push(data[i])
                }
            }
            let breadcrumb = [{ label: '校本资源', listData: list, parent: { b_id: '0', parent_id: '0' }, currentIsFiLe: false }]
            this.currentList = list
            return breadcrumb
        },
        backLayer (layer) {
            if (layer === 0) {
                this.backCrumb(0)
            } else {
                this.backCrumb(this.breadcrumb.length - 2)
            }
        },
        backCrumb (index) {
            /* 点击面包屑返回前几级 */
            let breadcrumb = this.breadcrumb
            let item = breadcrumb[index]
            this.currentIsFiLe = false
            if (index < breadcrumb.length - 1 && breadcrumb.length > 1) {
                this.currentList = item.listData
                this.breadcrumb = breadcrumb.slice(0, index + 1)
                if (this.breadcrumb.length === 1) {
                    this.currentIsFirstLevel = true
                    this.$EventBus.$emit('intoLayer', true, '校本资源')
                } else {
                    this.$EventBus.$emit('intoLayer', false, item.parent.folder_name)
                }
            }
        },
        addCrumb (breadcrumb, file, currentIsFiLe) {
            /* 添加面包屑 */
            breadcrumb.push({ label: file.folder_name, listData: [file.children], parent: file, currentIsFiLe })
        },
        intoFolder (file) {
            /* 进入下一级文件夹或文件 */
            this.currentIsFirstLevel = false
            this.$EventBus.$emit('intoLayer', this.currentIsFirstLevel, file.folder_name)
            if ((file.children && file.children.length > 0 && this.currentIsFiLe === false) || (file.file_count === 0 && this.breadcrumb.length < 3)) {
                // 进入文件夹
                this.currentIsFiLe = false
                this.addCrumb(this.breadcrumb, file, this.currentIsFiLe)
                this.currentList = [file.children] || []
            } else {
                // 获取当前文件夹的文档列表
                if (this.currentIsFiLe === false) {
                    this.currentIsFiLe = true
                    this.addCrumb(this.breadcrumb, file, this.currentIsFiLe)
                    this.getFileList(file)
                }
            }
        },
        toggleShowBtn (item) {
            /* 显示功能按钮 */
            item.showFunBtn = !item.showFunBtn
        },
        openFile (item) {
            /* 打开文档 */
            // this.$router.push({
            //     path: '/education/documentDetail'
            // })
        },
        shareFile (item) {
            /* 分享文档 */
            this.$router.push({
                path: `/education/share?file_id=${item.file_info_id}`
            })
        },
        updateFileName (params) {
            /* 修改文件名称 */
            this.$api.schoolBasedResourcesApi.updateFileName(params).then(res => {
                if (res.data.Status === true) {
                    this.$vux.toast.show({
                        text: '修改名称成功'
                    })
                    this.$refs.fileList.renameDialog = false
                    this.getFolderList()
                } else {
                    this.$vux.toast.show({
                        text: res.data.ResponseError.LongMessage,
                        type: 'cancel'
                    })
                }
            })
        },
        handleDeleteFile (params) {
            /* 删除文件 */
            this.$api.schoolBasedResourcesApi.deleteFile(params).then(res => {
                if (res.data.Status === true) {
                    this.$vux.toast.show({
                        text: '删除成功'
                    })
                    this.$refs.fileList.deleteDialog = false
                    this.getFolderList()
                } else {
                    this.$vux.toast.show({
                        text: res.data.ResponseError.LongMessage,
                        type: 'cancel'
                    })
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
.container {
    .search-ctn {
        padding: 20px 15px 10px 15px;
    }
}
</style>
