import React from 'react';
import { Spin, Form, Button, Select } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';
import { political, technical, market, capital, management } from './dict.js';

const FormItem = Form.Item;
const Option = Select.Option;

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

const EvaluateStep7 = 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
        })
        values.id = this.props.id;
        $.ajax({
          url: globalConfig.context + '/api/user/evaluate/step7',
          method: 'post',
          data: values
        }).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 }}>项目整体获利情况受政策、技术、市场、资金、管理等影响。</div>
        <Form className="steps-form">
          <FormItem label="政策风险" {...formItemLayout}>
            {getFieldDecorator('political', {
              rules: [{ required: true, message: '请选择政策风险!' }],
              initialValue: this.stringify(initialData.political)
            })(
              <Select placeholder="请选择政策风险">
                {political.map((it) => {
                  return <Option key={it.id} value={it.id}>{it.text}</Option>
                })}
              </Select>
              )}
          </FormItem>
          <FormItem label="技术风险" {...formItemLayout}>
            {getFieldDecorator('technical', {
              rules: [{ required: true, message: '请选择技术风险!' }],
              initialValue: this.stringify(initialData.technical)
            })(
              <Select placeholder="请选择技术风险">
                {technical.map((it) => {
                  return <Option key={it.id} value={it.id}>{it.text}</Option>
                })}
              </Select>
              )}
          </FormItem>
          <FormItem label="市场风险" {...formItemLayout}>
            {getFieldDecorator('market', {
              rules: [{ required: true, message: '请选择市场风险!' }],
              initialValue: this.stringify(initialData.market)
            })(
              <Select placeholder="请选择市场风险">
                {market.map((it) => {
                  return <Option key={it.id} value={it.id}>{it.text}</Option>
                })}
              </Select>
              )}
          </FormItem>
          <FormItem label="资金风险" {...formItemLayout}>
            {getFieldDecorator('capital', {
              rules: [{ required: true, message: '请选择资金风险!' }],
              initialValue: this.stringify(initialData.capital)
            })(
              <Select placeholder="请选择资金风险">
                {capital.map((it) => {
                  return <Option key={it.id} value={it.id}>{it.text}</Option>
                })}
              </Select>
              )}
          </FormItem>
          <FormItem label="管理风险" {...formItemLayout}>
            {getFieldDecorator('management', {
              rules: [{ required: true, message: '请选择管理风险!' }],
              initialValue: this.stringify(initialData.management)
            })(
              <Select placeholder="请选择管理风险">
                {management.map((it) => {
                  return <Option key={it.id} value={it.id}>{it.text}</Option>
                })}
              </Select>
              )}
          </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 EvaluateStep7;