import React from 'react'
import { Input, Icon, Button, Popconfirm } from 'antd'
import { connect } from 'dva'
import './header.less'
import SettingBox from './settingBox';

class Header extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visibleConfirm: false,
            visibleSettingBox: false
        }
    }

    handleVisibleChange = (visible) => {
        this.setState({ visibleConfirm: visible });
    }

    render() {
        const { dashboardDesigner, dispatch } = this.props;
        return (
            <div className='dashboarddesigner-header'>
                <div className='header-item toolbar-back'>
                    <Popconfirm
                        placement="bottomLeft"
                        title="离开前保存修改吗？"
                        visible={this.state.visibleConfirm}
                        onVisibleChange={this.handleVisibleChange}
                        onConfirm={async () => {
                            this.setState({
                                visibleConfirm: false
                            });
                            try {
                                await dispatch({ type: 'dashboardDesigner/saveWithThumbnail' });
                            }catch(e){
                                return e
                            }                            
                            dispatch({ type: 'main/redirect', path: '/dashboard' });
                        }}
                        onCancel={() => {
                            this.setState({
                                visibleConfirm: false
                            });
                            dispatch({ type: 'main/redirect', path: '/dashboard' });
                        }}
                        okText="保存"
                        cancelText="不保存"
                    >
                        <Button onClick={() => {
                            console.log(dashboardDesigner.dirty);
                            if(!dashboardDesigner.dirty) {
                                dispatch({ type: 'main/redirect', path: '/dashboard' });
                            }
                        }}>
                            <Icon type='left' />返回
                        </Button>
                    </Popconfirm>
                    <Button onClick={() => {
                        if(dashboardDesigner.code && dashboardDesigner.code !== -1) {
                            dispatch({ type: 'dashboardDesigner/saveWithThumbnail' });
                        }else {
                            dispatch({ type: 'dashboard/remoteAdd' });
                        }
                    }}><Icon type='save' />保存</Button>
                </div>
                <div className='header-item toolbar-title'>
                    <Input
                        className='input-title'
                        value={dashboardDesigner.name}
                        addonAfter={<Icon type="edit" 
                            onClick={(e) => {
                                const input = e.currentTarget.parentElement.parentElement.getElementsByTagName('input')[0];
                                input && input.focus()
                            }}
                        />}
                        onChange={(e) => {
                            dispatch({ type: 'dashboardDesigner/setField', name: 'name', value: e.target.value });
                        }}
                    />
                </div>
                <div className='header-item toolbar-buttons'>
                    <Button onClick={() => this.setState({ visibleSettingBox: true})}>属性设置</Button>
                    <SettingBox 
                        visibleSettingBox={this.state.visibleSettingBox} 
                        hideBox={() => this.setState({visibleSettingBox:false})}
                    />
                </div>
            </div>
        );
    }
}

function mapStateToProps(state) {
    const dashboardDesigner = state.present.dashboardDesigner;
    return { dashboardDesigner }
}
export default connect(mapStateToProps)(Header);