import { Button } from 'antd'
import { WeaCheckbox, WeaFormItem, WeaHelpfulTip, WeaRightMenu, WeaTop } from 'ecCom'
import { inject, observer } from 'mobx-react'
import { i18n } from '../../public/i18n'
import '../../style/serialNumber.less'
import NumberComposition from './NumberComposition'
import NumberFlowRule from './NumberFlowRule'
import { StartReservedNumberSet } from './StartReservedNumberSet'
import {Authority}  from '../../public/valhalla/components/index.js'

@inject('deptNumberSettingStore')
@observer
export default class DeptNumberSet extends React.Component {
    componentWillReceiveProps(nextProps) {
        if (this.props.location.key != nextProps.location.key) {
            const { deptNumberSettingStore: { init } } = this.props
            init()
        }
    }
    componentDidMount() {
        const { deptNumberSettingStore: { init } } = this.props
        init()
    }

    render() {
        const { deptNumberSettingStore } = this.props
        const { dropMenuDatas, hasNumberComposition, hasNumberFlowRule, hasStartReservedNumberSet, handleSave,authorityStore } = deptNumberSettingStore
        return (
            <Authority store={authorityStore}>
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@u2uaal`} datas={dropMenuDatas} >
                    <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@3noldt`}
                        showDropIcon
                        title={i18n.label.deptNumberSet()}
                        icon={<i class='icon-coms-hrm' />}
                        iconBgcolor='#217346'
                        buttons={[<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@djec9t`} type='primary' onClick={handleSave}>{i18n.button.save()}</Button>]}
                        dropMenuDatas={dropMenuDatas}
                    >
                        <GenerationSwitch ecId={`${this && this.props && this.props.ecId || ''}_GenerationSwitch@k6romu`} store={deptNumberSettingStore}/>
                        {hasNumberComposition && <NumberComposition ecId={`${this && this.props && this.props.ecId || ''}_NumberComposition@r9r1va`} store={deptNumberSettingStore}/>}
                        {hasNumberFlowRule && <NumberFlowRule ecId={`${this && this.props && this.props.ecId || ''}_NumberFlowRule@9gz5b3`} store={deptNumberSettingStore}/>}
                        {hasStartReservedNumberSet && <StartReservedNumberSet ecId={`${this && this.props && this.props.ecId || ''}_StartReservedNumberSet@bk7zph`} store={deptNumberSettingStore}/>}
                    </WeaTop>
                </WeaRightMenu>
            </Authority>
        );
    }
}

@observer
class GenerationSwitch extends React.Component {
    render() {
        const { store: { generationSwitchChecked, handleGenerationSwitchChange } } = this.props
        const helpfulTip = (
            <div>{['531678', '531679', '531680', '531681', '531682'].map(n => <p>{i18n.label[n]()}</p>)}</div>
        )
        return (
            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@t5jpvl`} 
                label={i18n.label.deptNumberAutoGenerate()}
                ratio1to2
                className='generationSwitch'
            >
                <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@671eeo`} display='switch' value={generationSwitchChecked} onChange={handleGenerationSwitchChange}/>
                <WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@on6oqi`} title={helpfulTip} />
            </WeaFormItem>
        );
    }
}