const { WeaLocaleProvider, WeaDialog, WeaTools } = ecCom;
const { WeaForm } = comsMobx;
const { Button, message } = antd;
const { getLabel } = WeaLocaleProvider;

class Main extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			visible: false,
			form: new WeaForm()，
			date: new Date()
		}
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.date != nextProps.date) {
			this.init();
		}
	}

	componentDidMount() {
		this.init();
	}

	init = () => {
		this.setState({ visible: true })
		this.getForm()
	}

	getForm = () => {
		WeaTools.callApi('/api/sensitive/getSensitiveWordApproveForm', 'GET', params = {}).then(res => {
			const { conditions, status } = res;
			if (status == '1') {
				const { form } = this.state;
				conditions[0].title = '';
				form.setCondition(conditions);
				this.setState({ form })
			} else {
				message.error(res.message);
			}
		})
	}

	getButtons = () => {
		return [<Button type='primary' onClick={this.handleSubmit}>{getLabel(615, '提交')}</Button>]
	}

	handleCancel = () => {
		this.setState({ visible: false })
	}

	handleSubmit = () => {
		const { form } = this.state;
		form.validateForm().then(f => {
			if (f.isValid) {
				WeaTools.callApi('/api/sensitive/saveSensitiveWordApprove', 'POST', form.getFormParams()).then(res => {
					const { conditions, status } = res;
					if (status == '1') {
						message.success(res.message);
						this.handleCancel()
					} else {
						message.error(res.message);
					}
				})
			} else {
				f.showErrors();
				this.setState({date: new Date()})
			}
		})
	}

	render() {
		const { visible, form, date } = this.state;

		return (
			<WeaDialog
				ecId={this && this.props && this.props.ecId || ''}
				hasScroll
				moduleName='currency'
				style={{ height: 300 }}
				visible={visible}
				title={getLabel('531499', '上报敏感词')}
				buttons={this.getButtons()}
				moreBtn={{ datas: [] }}
				onCancel={this.handleCancel}
			>
				{form.render({ col: 1 })}
			</WeaDialog>
		);
	}
}

ecodeSDK.setCom('${appId}', 'ReportSensitiveWordDialog', Main);
