import React from 'react';
import { Button, Icon, Switch, Tooltip, message } from 'antd';
import { WeaTools, WeaDialog, WeaSearchGroup, WeaFormItem, WeaInput, WeaBrowser } from 'ecCom';

import './style/';

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

    fn ;  //保存事件

    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.getSettingData = this.getSettingData.bind(this);
        this.setMulitLangPermission = this.setMulitLangPermission.bind(this);
        this.onChange = this.onChange.bind(this);
        this.onSave = this.onSave.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onShow = this.onShow.bind(this);
    }
    componentDidMount(){
        const { fn } = this.props;
        this.fn = fn;
    }

    render() {
        const { data } = this.state;
        const {
            canEdit = false, enableMultiLang = false,
            oaaddress, licenseRemind, remindUserNames, remindDays, picturePath,
            filesystem, filesystembackup, filesystembackuptime, needzip, isaesencrypt,
        } = data;

        return (
            <WeaDialog
              visible={this.state.visible}
              title="系统设置"
              icon="wevicon-wea-system-setting"
              iconBgcolor="#a7adb5"
              style={{ width: 720, height: 520 }}
              zIndex={100}
              hasScroll={true}
              buttons={this.getButtons()}
              onCancel={this.onCancel}
            >
                <WeaSearchGroup title="全局设置" needTigger={true} showGroup={true}>
                    <WeaFormItem label="OA访问地址" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput value={oaaddress} disabled={!canEdit} onChange={value => this.onChange({ oaaddress: value })} />
                    </WeaFormItem>
                    {
                        canEdit ?
                            <WeaFormItem label="授权信息到期提醒" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                                <Switch checked={licenseRemind == '1'} onChange={checked => this.onChange({ licenseRemind: checked ? '1' : '0' })} />
                            </WeaFormItem> : ''
                    }
                    {
                        canEdit && licenseRemind == '1' ?
                            <WeaFormItem label="提醒对象" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                                <WeaBrowser
                                  type={17}
                                  title="人力资源"
                                  isSingle={false}
                                  replaceDatas={remindUserNames}
                                  onChange={(ids, names, datas) => this.onChange({ remindUsers: ids, remindUserNames: datas })}
                                />
                            </WeaFormItem> : ''
                    }
                    {
                        canEdit && licenseRemind == '1' ?
                            <WeaFormItem label="提前提醒天数" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                                <WeaInput value={remindDays} onChange={value => this.onChange({ remindDays: value })} />
                            </WeaFormItem> : ''
                    }
                </WeaSearchGroup>
                <WeaSearchGroup title="文件存储设置" needTigger={true} showGroup={true}>
                    <WeaFormItem label="图片存放目录" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput
                          helpfulTip={canEdit ? '设置文件目录路径不能含有中文，不填写将默认存放在系统程序目录filesystem下' : ''}
                          value={picturePath}
                          disabled={!canEdit}
                          onChange={value => this.onChange({ picturePath: value })}
                        />
                    </WeaFormItem>
                    <WeaFormItem label="文件存放目录" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput
                          helpfulTip={canEdit ? '设置文件目录路径不能含有中文，不填写将默认存放在系统程序目录filesystem下' : ''}
                          value={filesystem}
                          disabled={!canEdit}
                          onChange={value => this.onChange({ filesystem: value })}
                        />

                    </WeaFormItem>
                    <WeaFormItem label="文件备份目录" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput
                          helpfulTip={canEdit ? '设置文件目录路径不能含有中文，不填写将默认存放在系统程序目录filesystem下' : ''}
                          value={filesystembackup}
                          disabled={!canEdit}
                          onChange={value => this.onChange({ filesystembackup: value })}
                        />
                    </WeaFormItem>
                    <WeaFormItem label="文件备份周期" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <WeaInput
                          helpfulTip={canEdit ? '设置为0或空表示不进行备份，默认备份周期是一小时（60分钟），大于0且小于等于60分钟则按照60分钟备份，大于60分钟即按照设置的时间进行备份' : ''}
                          value={filesystembackuptime}
                          disabled={!canEdit}
                          onChange={value => this.onChange({ filesystembackuptime: value })}
                        />
                    </WeaFormItem>
                    <WeaFormItem label="压缩存储" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <Switch checked={needzip == '1'} disabled={!canEdit} onChange={checked => this.onChange({ needzip: checked ? '1' : '0' })} />
                    </WeaFormItem>
                    <WeaFormItem label="加密存储" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                        <Switch checked={isaesencrypt == '1'} disabled={!canEdit} onChange={checked => this.onChange({ isaesencrypt: checked ? '1' : '0' })} />
                    </WeaFormItem>
                </WeaSearchGroup>
                {
                    enableMultiLang ?
                        <WeaSearchGroup title="多语言设置" needTigger={true} showGroup={true}>
                            <WeaFormItem label="授权设置" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
                                <div style={{ width: '80%', height: '30px', lineHeight: '30px', paddingLeft: '10px', borderBottom: '1px solid #e2e2e2' }}>
                                    <span style={{ color: '#00a9ff', cursor: 'pointer' }} onClick={this.setMulitLangPermission}>设置</span>
                                </div>
                            </WeaFormItem>
                        </WeaSearchGroup> : ''
                }
            </WeaDialog>
        );
    }

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

        let buttons = [];
        if (data.canEdit) {
            buttons.push(<Button type="primary" onClick={this.onSave}>保存</Button>);
        }
        return buttons;
    }

    getSettingData() {
        WeaTools.callApi('/api/portal/systemInfo/getSettingData', 'GET', {}).then((result) => {
            const { data } = result;
            this.setState({ data });
        });
    }

    setMulitLangPermission() {
        const dialog = new window.top.Dialog();
        dialog.currentWindow = window;
        dialog.Height = 650;
        dialog.Width = 600;
        dialog.Title = '多语言授权设置';
        dialog.URL = '/docs/tabs/DocCommonTab.jsp?_fromURL=80&isdialog=1';
        dialog.show();
    }

    onChange(obj) {
        const { data } = this.state;
        this.setState({ data: { ...data, ...obj } });
    }

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

        WeaTools.callApi('/api/portal/systemInfo/saveSettingData', 'POST', {
            ...data,
        }).then((result) => {
            typeof (this.fn)==='function'&&this.fn(data.oaaddress);
            this.onCancel();
        });
    }

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

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

export default WeaSystemSetting;
