import React from 'react';
import { Spin, Form, Button, InputNumber, message } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';

const FormItem = Form.Item;

const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 10 },
};

const EvaluateStep6 = Form.create({})(React.createClass({
  getInitialState() {
    return {
      loading: false,
      initialData: {}
    };
  },
  componentWillMount() {
    this.state.initialData = this.props.data || {};
  },
  next() {
    if (this.state.loading) {
      return;
    }
    this.props.form.validateFields((err, values) => {
      if (!err) {
        this.setState({
          loading: true
        })
        $.ajax({
          url: globalConfig.context + '/api/user/evaluate/step6',
          method: 'post',
          data: {
            id: this.props.id,
            taxRate: values.taxRate
          }
        }).done(function (res) {
          if (res.error && res.error.length) {
            message.error(res.error[0].message)
          } else {
            if (this.props.next) {
              this.props.next(values);
            }
          }
        }.bind(this)).fail(function () {
          this.setState({
            loading: false
          })
        }.bind(this));
      }
    });
  },
  prev() {
    if (this.props.prev) {
      this.props.prev();
    }
  },
  stringify(val) {
    return val && String(val)
  },
  render() {
    let { loading, initialData } = this.state;
    const { getFieldDecorator } = this.props.form;
    return (
      <Spin spinning={loading}>
        <div style={{ marginBottom: 10 }}>提示:一般企业所得税率为25%,如果应用该项技术的公司有所得税优惠政策,按照优惠政策填列。如为高新技术企业请填15%</div>
        <Form className="steps-form">
          <FormItem label="预估企业所得税率(%)" {...formItemLayout}>
            {getFieldDecorator('taxRate', {
              initialValue: this.stringify(initialData.taxRate),
              rules: [{ required: true, message: '请输入预估企业所得税率!' }, {
                validator: (rule, value, callback) => {
                  if (!Number(value)) {
                    callback('请输入预估企业所得税率!');
                  } else {
                    callback();
                  }
                },
              }]
            })(
              <InputNumber min={0} placeholder="请输入预估企业所得税率" />
              )}
          </FormItem>
        </Form>
        <div className="steps-action">
          <Button type="primary" onClick={() => this.next()}>保存,下一步</Button>
          <Button style={{ marginLeft: 8 }} onClick={() => this.prev()}>上一步</Button>
        </div>
      </Spin>
    )
  },
}));

export default EvaluateStep6;