import React from 'react';
import { Button, Icon } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaNewScroll, WeaTree } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class MaterialLibLeft extends React.Component {
    state = { datas: [] };

    constructor(props) {
        super(props);
        this.onSelect = this.onSelect.bind(this);
        this.onLoadData = this.onLoadData.bind(this);
        this.onAdd = this.onAdd.bind(this);
        this.onEdit = this.onEdit.bind(this);
        this.onDelete = this.onDelete.bind(this);
    }

    componentWillMount() {
        WeaTools.callApi('/api/portal/materialLib/getDirs', 'GET', {}).then((result) => {
            this.setState({ datas: result.datas || [] });
        });
    }

    render() {
        const { hasRight } = this.props;
        const { datas } = this.state;

        return (
            <div className="portal-p4e-mll">
                <div className="portal-p4e-mll-top">
                    <Icon type="bars" />
                    <span style={{ marginLeft: '5px' }}>{getLabel(0, '全部目录')}</span>
                </div>
                <div className="portal-p4e-mll-middle">
                    <WeaNewScroll height="100%">
                        <WeaTree
                          datas={datas}
                          loadData={this.onLoadData}
                          onSelect={this.onSelect}
                          onAdd={this.onAdd}
                          onEdit={this.onEdit}
                          onDelete={this.onDelete}
                        />
                    </WeaNewScroll>
                </div>
                {
                    hasRight ? (
                            <div className="portal-p4e-mll-bottom">
                                <Icon type="exclamation-circle-o" />
                                <span style={{ marginLeft: '5px' }}>{getLabel(0, '目录名称只能包含字母和数字')}</span>
                            </div>
                        ) : ''
                }
            </div>
        );
    }

    onSelect(dirs) {
        this.props.setDir(dirs[0]);
    }

    onLoadData(node) {
        return WeaTools.callApi('/api/portal/materialLib/getDirs', 'GET', { dir: node.dir });
    }

    onAdd(value, node) {
        return WeaTools.callApi('/api/portal/materialLib/addDir', 'POST', { dir: node.dir, dirname: value });
    }

    onEdit(value, node) {
        return WeaTools.callApi('/api/portal/materialLib/renameDir', 'POST', { dir: node.dir, dirname: value });
    }

    onDelete(node) {
        WeaTools.callApi('/api/portal/materialLib/deleteDir', 'POST', { dir: node.dir });
    }
}

export default MaterialLibLeft;
