import React from 'react';
import { Button, Icon, message } from 'antd';
import { WeaTools, WeaDialog, WeaFormItem, WeaInput, WeaUpload } from 'ecCom';

import LicenseMessage from './LicenseMessage';

class LicenseSubmit extends React.Component {
    state = { visible: false, isShowMsg: false, isChangeCode: false };

    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.getLicenseCode = this.getLicenseCode.bind(this);
        this.onChange = this.onChange.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
        this.onSubmitCode = this.onSubmitCode.bind(this);
        this.onBack = this.onBack.bind(this);
        this.onCancel = this.onCancel.bind(this);
    }

    render() {
        const { code = '', code_old = '', code_new = '', code_rep = '' } = this.state;

        let displayComp = (
            <div className="wea-license-group">
                <WeaFormItem label="验证码" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                    <WeaInput type="password" value={code} style={{ width: '270px' }} viewAttr={3} onChange={value => this.setState({ code: value })} />
                    <span style={{ marginLeft: '20px', color: '#00a9ff', cursor: 'pointer' }} onClick={() => this.setState({ isChangeCode: true })}>更改验证码</span>
                </WeaFormItem>
                <WeaFormItem label="license文件" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                    <WeaUpload
                      ref="WeaUpload"
                      uploadId="LicenseOperation"
                      uploadUrl="/api/system/license/LicenseOperation"
                      uploadParams={{ code }}
                      category="string"
                      clearWhenReset={false}
                      multiSelection={false}
                      maxFilesNumber={1}
                      showClearAll={false}
                      autoUpload={false}
                      limitType="license"
                      viewAttr={3}
                      onChange={this.onChange}
                    />
                </WeaFormItem>
                <WeaFormItem label="识别码" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }} underline>
                    <WeaInput value={this.state.licenseCode} viewAttr={1} />
                </WeaFormItem>
                <div className="wea-license-tip-msg">
                    <Icon type="exclamation-circle" />
                    <span style={{ marginLeft: '10px' }}>提示：将公司名称及识别码提交给软件供应商，以获取License。</span>
                </div>
            </div>
        );

        if (this.state.isShowMsg) {
            displayComp = <LicenseMessage message={this.state.message} />;
        }

        if (this.state.isChangeCode) {
            displayComp = (
                <div className="wea-license-group">
                    <WeaFormItem label="旧验证码" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput type="password" value={code_old} viewAttr={3} onChange={value => this.setState({ code_old: value })} />
                    </WeaFormItem>
                    <WeaFormItem label="新验证码" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput type="password" value={code_new} viewAttr={3} onChange={value => this.setState({ code_new: value })} />
                    </WeaFormItem>
                    <WeaFormItem label="确认新验证码" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput type="password" value={code_rep} viewAttr={3} onChange={value => this.setState({ code_rep: value })} />
                    </WeaFormItem>
                </div>
            );
        }

        return (
            <WeaDialog
              visible={this.state.visible}
              title="License提交"
              icon="wevicon-wea-license"
              iconBgcolor="#a7adb5"
              style={{ width: 600, height: 310 }}
              zIndex={101}
              hasScroll={true}
              buttons={this.getButtons()}
              onCancel={this.onCancel}
            >
                {displayComp}
            </WeaDialog>
        );
    }

    getButtons() {
        const { isShowMsg, isChangeCode } = this.state;

        let buttons = [];
        if (isChangeCode) {
            buttons.push(<Button type="primary" onClick={this.onSubmitCode}>提交</Button>);
            buttons.push(<Button type="primary" onClick={this.onBack}>返回</Button>);
        } else {
            if (isShowMsg) {
                buttons.push(<Button type="primary" onClick={this.onBack}>返回</Button>);
            } else {
                buttons.push(<Button type="primary" onClick={this.onSubmit}>提交</Button>);
            }
            buttons.push(<Button type="primary" onClick={this.onCancel}>关闭</Button>);
        }
        return buttons;
    }

    getLicenseCode() {
        WeaTools.callApi('/api/system/license/InLicense', 'POST', {}).then((data) => {
            this.setState({ ...data });
        });
    }

    onChange(ids, list, args) {
        const _message = args[0].message;
        this.setState({ isShowMsg: true, message: _message });
    }

    onSubmit() {
        const { code = '' } = this.state;
        if (code.trim() != '') {
            this.refs.WeaUpload.doUpload();
        } else {
            message.warning('必要信息不完整！');
        }
    }

    onSubmitCode() {
        const { code_old = '', code_new = '', code_rep = '' } = this.state;

        if (code_old.trim() != '' && code_new.trim() != '' && code_rep.trim() != '') {
            if (code_new === code_rep) {
                WeaTools.callApi('/api/system/license/CodeOperation', 'POST', {
                    passwordold: code_old,
                    passwordnew: code_new,
                }).then((data) => {
                    const _message = data.message;
                    if (_message == '1') {
                        message.error('旧的验证码不正确！');
                    } else {
                        message.success(_message);
                        this.onBack();
                    }
                });
            } else {
                message.warning('两次输入的新验证码不同！');
            }
        } else {
            message.warning('必要信息不完整！');
        }
    }

    onBack() {
        this.setState({ isShowMsg: false, isChangeCode: false, message: '', code: '', code_old: '', code_new: '', code_rep: '' });
    }

    onCancel() {
        this.setState({ visible: false, isShowMsg: false, isChangeCode: false, message: '', code: '', code_old: '', code_new: '', code_rep: '' });
    }

    onShow() {
        this.setState({ visible: true });
        this.getLicenseCode();
    }
}

export default LicenseSubmit;
