<template>
    <div class="menus">
        <el-row :gutter="20">
            <el-col :span="12">
                <div class="menu-tree">
                    <el-tree
                        :data="classList"
                        :render-content="renderContent"
                        :highlight-current="true"
                        :expand-on-click-node="true"
                        node-key="id"
                        v-loading="loading"
                        accordion
                        draggable
                        :allow-drop="allowdrop"
                        @node-click="nodeClick"
                        :default-expanded-keys="expandKeys"
                        :default-checked-keys="expandKeys"
                    >
                    </el-tree>
                    <el-button type="text" @click="addRootMenu" style="margin-left: 24px">添加 </el-button>
                    <el-button type="text" @click="submit" style="margin-left: 24px">保存 </el-button>
                </div>
            </el-col>
            <transition name="el-fade-in">
                <el-col :span="12" v-if="dialogVisible">
                    <div class="menu-tree">
                        <div style="font-weight: bold; padding: 10px 0">{{ menu.id ? '编辑分类' : '新增分类' }}</div>
                        <el-form :model="menu" ref="form" label-position="top">
                            <el-form-item
                                label="分类名"
                                prop="name"
                                :rules="[{ required: true, message: '请填写分类名', trigger: 'blur' }]"
                            >
                                <el-input v-model="menu.name"></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer">
                            <el-button @click="dialogVisible = false">取消 </el-button>
                            <el-button type="primary" @click="addMenu" :loading="loading">保存 </el-button>
                        </div>
                    </div>
                </el-col>
            </transition>
        </el-row>
    </div>
</template>
<script>
export default {
    created() {
        this.getData();
        this.$http.get('/authority/all').then(res => {
            //this.authorities = res.filter(i => i.name === 'ROLE_ADMIN' || i.name === 'ROLE_OPERATOR');
            this.authorities = res || [];
        });
    },
    data() {
        return {
            dialogVisible: false,
            curr: null,
            loading: false,
            classList: [],
            menu: {
                name: '',
                parentId: ''
            },
            parent: null,
            currentRef: null,
            edit: false,
            icon: '',
            projectId: this.$store.state.projectId,
            // categories: [],
            category: null,
            expandKeys: [],
            authorities: []
        };
    },
    methods: {
        addRootMenu() {
            this.menu = {
                name: '',
                parentId: '0',
                projectId: this.projectId
            };
            this.parent = null;
            this.dialogVisible = true;
        },
        showAddDialog(node, data) {
            this.edit = false;
            this.parent = node.data;
            this.menu = {
                parentId: data.id,
                name: '',
                projectId: data.projectId
            };
            this.dialogVisible = true;
        },
        allowdrop(draggingNode, dropNode, type) {
            if (draggingNode.data.level === dropNode.data.level) {
                if (draggingNode.data.parentId === dropNode.data.parentId) {
                    return type === 'prev' || type === 'next';
                } else {
                    return false;
                }
            } else {
                // 不同级进行处理
                return false;
            }
        },
        showEditDialog(node, data) {
            this.edit = true;
            this.currentRef = node.data;
            this.menu = {
                ...data
            };
            this.dialogVisible = true;
        },
        addMenu() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true;
                    let menu = {
                        ...this.menu
                    };
                    delete menu.children;
                    this.$http
                        .post('/collectionCategory/save', menu, { body: 'json' })
                        .then(res => {
                            this.loading = false;
                            this.$message.success('添加成功');
                            this.dialogVisible = false;
                            this.getData();
                        })
                        .catch(e => {
                            console.log(e);
                            this.loading = false;
                            this.$message.error(e.error);
                        });
                }
            });
        },
        remove(node, data) {
            this.$confirm('确定删除分类？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
            })
                .then(() => {
                    return this.$http.post(
                        '/collectionCategory/delete',
                        {
                            ...data,
                            children: null
                        },
                        { body: 'json' }
                    );
                })
                .then(res => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    this.loading = false;
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error(e.error);
                    }
                });
        },
        getData() {
            this.$http
                .post('/collectionCategory/categories', { projectId: this.projectId }, { body: 'json' })
                .then(res => {
                    this.classList = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        },
        renderContent(h, { node, data, store }) {
            console.log(data, 'data');
            return (
                <span
                    class={
                        this.menu.id == data.id || (this.menu.parent == data.id && !this.menu.id)
                            ? 'custom-tree-node selected'
                            : 'custom-tree-node'
                    }
                >
                    <span>{data.name}</span>
                    <span class="url">{data.path}</span>
                    <span class="opt">
                        <el-button
                            type="text"
                            on-click={e => {
                                this.showEditDialog(node, data), e.stopPropagation();
                            }}
                            icon="el-icon-edit"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            on-click={e => {
                                this.showAddDialog(node, data), e.stopPropagation();
                            }}
                            icon="el-icon-plus"
                        >
                            添加
                        </el-button>
                        <el-button
                            type="text"
                            on-click={e => {
                                this.remove(node, data), e.stopPropagation();
                            }}
                            icon="el-icon-delete"
                        >
                            删除
                        </el-button>
                    </span>
                </span>
            );
        },
        submit() {
            this.saving = true;
            this.$http
                .post('/collectionCategory/batchUpdate', this.classList, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    //this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        nodeClick(data, node, el) {
            if (this.expandKeys[0] != data.id) {
                this.expandKeys = [data.id];
            }
        }
    },
    watch: {
        '$store.state.projectId'(val) {
            console.log(111, val);
            this.projectId = val;
            this.getData();
        }
    }
};
</script>
<style lang="less" scoped>
.menus {
    padding: 20px;
}
.menu-tree {
    border-radius: 4px;
    background: white;
    margin-top: 20px;
    padding: 10px;
}
/deep/ .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
    line-height: 40px;
    height: 40px;
    .url {
        flex-grow: 1;
        text-align: right;
        margin-right: 20px;
        color: #999;
    }
    .opt {
        opacity: 0;
    }
    &.selected {
        border: 2px solid @prim;
        border-radius: 4px;
        padding: 0 10px;
        box-sizing: border-box;
        .opt {
            opacity: 1;
        }
    }
    &:hover {
        .opt {
            opacity: 1;
        }
    }
}
/deep/ .menu-tree {
    .el-tree-node__content {
        height: 40px;
        line-height: 40px;
    }
}

/deep/ .available-icons {
    color: @prim;
    text-decoration: none;
    &:hover {
        color: @prim;
        text-decoration: none;
    }
}
</style>
