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

import './style/';
import LicenseSubmit from './LicenseSubmit';

class WeaLicense extends React.Component {
    state = { visible: false, data: {} };

    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.getLicenseInfo = this.getLicenseInfo.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onShow = this.onShow.bind(this);
    }

    render() {
        const { data } = this.state;
        const { companyName, licenseCode, hrmNum, scCount, expireDate, concurrentFlag, onlineusercount, licensenum, unusedlice } = data;

        const colProps = {
            labelCol: { span: 8 },
            wrapperCol: { span: 16 },
        };

        return (
            <WeaDialog
              visible={this.state.visible}
              title="授权信息"
              icon="wevicon-wea-license"
              iconBgcolor="#a7adb5"
              style={{ width: 620, height: 440 }}
              zIndex={100}
              hasScroll={true}
              buttons={this.getButtons()}
              onCancel={this.onCancel}
            >
                <LicenseSubmit ref="LicenseSubmit" />
                <WeaSearchGroup title="基本信息" needTigger={true} showGroup={true} center size="large">
                    <WeaFormItem label="授权用户" {...colProps} underline>
                        <WeaInput value={companyName} viewAttr={1} />
                    </WeaFormItem>
                    <WeaFormItem label="标识码" {...colProps} underline>
                        <WeaInput value={licenseCode} viewAttr={1} />
                    </WeaFormItem>
                    <WeaFormItem label="用户数" {...colProps} underline>
                        <WeaInput value={hrmNum} viewAttr={1} />
                    </WeaFormItem>
                    <WeaFormItem label="分部数" {...colProps} underline>
                        <WeaInput value={scCount} viewAttr={1} />
                    </WeaFormItem>
                    <WeaFormItem label="结束日期" {...colProps} underline>
                        <WeaInput value={expireDate} viewAttr={1} />
                    </WeaFormItem>
                </WeaSearchGroup>
                <WeaSearchGroup title="license使用情况" needTigger={true} showGroup={true} center>
                    <WeaFormItem label="已使用数量" {...colProps} underline>
                        <WeaInput value={concurrentFlag == '1' ? onlineusercount : licensenum} viewAttr={1} />
                    </WeaFormItem>
                    <WeaFormItem label="未使用数量" {...colProps} underline>
                        <WeaInput value={unusedlice} viewAttr={1} />
                    </WeaFormItem>
                </WeaSearchGroup>
            </WeaDialog>
        );
    }

    getButtons() {
        const { data } = this.state;

        let buttons = [];
        if (data.canEdit) {
            buttons.push(<Button type="primary" onClick={this.onSubmit}>提交License</Button>);
        }
        return buttons;
    }

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

    onSubmit() {
        this.refs.LicenseSubmit.onShow();
    }

    onCancel() {
        this.setState({ visible: false });
    }

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

export default WeaLicense;
