<template>
    <div class="ht-base-tree-render-demo">
        <ht-base-tree :data="data" :render="renderContent"></ht-base-tree>
    </div>
</template>

<script>
    export default {
        name: 'HtBaseTreeRenderDemo',
        data() {
            return {
                data: [
                    {
                        title: '第一小节',
                        expand: true,
                        render: (h, {root, node, data}) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h('ht-base-icon', {
                                        props: {
                                            type: 'LiRa_collapse2'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '32px'
                                    }
                                }, [
                                    h('ht-base-button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'LiRa_add',
                                            type: 'primary'
                                        }),
                                        style: {
                                            width: '64px',
                                            height: '28px'
                                        },
                                        on: {
                                            click: () => {
                                                this.append(data)
                                            }
                                        }
                                    })
                                ])
                            ]);
                        },
                        children: [
                            {
                                title: '第一小节1-0',
                                expand: true,
                                children: [
                                    {
                                        title: '小节内容-01',
                                        expand: true
                                    },
                                    {
                                        title: '小节内容-02',
                                        expand: true
                                    }
                                ]
                            },
                            {
                                title: '第一小节1-1',
                                expand: true,
                                children: [
                                    {
                                        title: '小节内容-01',
                                        expand: true
                                    },
                                    {
                                        title: '小节内容-02',
                                        expand: true
                                    }
                                ]
                            }
                        ]
                    }
                ],
                buttonProps: {
                    type: 'default',
                    size: 'small',
                }
            }
        },
        methods: {
            renderContent(h, {root, node, data}) {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h('ht-base-icon', {
                            props: {
                                type: 'LiRa_work'
                            },
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px',

                        }
                    }, [
                        h('ht-base-button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'LiRa_add'
                            }),
                            style: {
                                height: '32px',
                                width: '27px',
                                padding: '0 0',
                                marginRight: '8px'
                            },
                            on: {
                                click: () => {
                                    this.append(data)
                                }
                            }
                        }),
                        h('ht-base-button', {
                            props: Object.assign({}, this.buttonProps, {
                                icon: 'LiRa_reduce1'
                            }),
                            style: {
                                height: '32px',
                                width: '27px',
                                padding: '0 0',
                            },
                            on: {
                                click: () => {
                                    this.remove(root, node, data)
                                }
                            }
                        })
                    ])
                ]);
            },
            append(data) {
                const children = data.children || [];
                children.push({
                    title: 'appended node',
                    expand: true
                });
                this.$set(data, 'children', children);
            },
            remove(root, node, data) {
                const parentKey = root.find(el => el === node).parent;
                const parent = root.find(el => el.nodeKey === parentKey).node;
                const index = parent.children.indexOf(data);
                parent.children.splice(index, 1);
            }
        }
    }
</script>
<style>
    .ht-base-tree-render-demo {

    }
</style>

