step4.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React from 'react';
  2. import { Spin, Form, Button, message, Radio, Table, InputNumber } from 'antd';
  3. import ajax from 'jquery/src/ajax/xhr.js'
  4. import $ from 'jquery/src/ajax';
  5. const RadioButton = Radio.Button;
  6. const RadioGroup = Radio.Group;
  7. const FormItem = Form.Item;
  8. const formItemLayout = {
  9. labelCol: { span: 4 },
  10. wrapperCol: { span: 10 },
  11. };
  12. const now = new Date();
  13. const EvaluateStep4 = Form.create({})(React.createClass({
  14. getInitialState() {
  15. return {
  16. loading: false,
  17. initialData: {},
  18. hasIncome: '1',
  19. dataSource: []
  20. };
  21. },
  22. componentWillMount() {
  23. let initialData = this.props.data || {}, dataSource = [];
  24. if (initialData.incomes) {
  25. dataSource = initialData.incomes;
  26. } else {
  27. let step1 = this.props.record && this.props.record[0] || {};
  28. let year = now.getFullYear();//Number(step1.benchmarkDate && step1.benchmarkDate.split('-')[0]) || now.getFullYear();
  29. for (let i = 1; i < 4; i++) {
  30. dataSource.push({
  31. year: year - i,
  32. income: '',
  33. profit: ''
  34. })
  35. }
  36. }
  37. this.state.dataSource = dataSource;
  38. this.state.initialData = initialData;
  39. this.state.hasIncome = initialData.hasIncome || '1'
  40. },
  41. next() {
  42. if (this.state.loading) {
  43. return;
  44. }
  45. this.props.form.validateFields((err, values) => {
  46. if (!err) {
  47. this.setState({
  48. loading: true
  49. })
  50. let data = {
  51. id: this.props.id,
  52. hasIncome: values.hasIncome
  53. }, val = {
  54. hasIncome: values.hasIncome
  55. }
  56. if (data.hasIncome == 2) {
  57. let incomes = [], year = 0;
  58. for (let i = 0; i < 3; i++) {
  59. year = this.state.dataSource[i].year;
  60. incomes.push({
  61. year: year,
  62. income: values['income' + year] || 0,
  63. profit: values['profit' + year] || 0
  64. })
  65. }
  66. data.incomes = JSON.stringify(incomes);
  67. val.incomes = incomes;
  68. }
  69. $.ajax({
  70. url: globalConfig.context + '/api/user/evaluate/step4',
  71. method: 'post',
  72. data: data
  73. }).done(function (res) {
  74. if (res.error && res.error.length) {
  75. message.error(res.error[0].message)
  76. } else {
  77. if (this.props.next) {
  78. this.props.next(val);
  79. }
  80. }
  81. }.bind(this)).fail(function () {
  82. this.setState({
  83. loading: false
  84. })
  85. }.bind(this));
  86. } else {
  87. for (let field in err) {
  88. if (err.hasOwnProperty(field)) {
  89. message.error(err[field].errors[0].message)
  90. break;
  91. }
  92. }
  93. }
  94. });
  95. },
  96. prev() {
  97. if (this.props.prev) {
  98. this.props.prev();
  99. }
  100. },
  101. onRadioChange(e) {
  102. this.setState({
  103. hasIncome: e.target.value,
  104. });
  105. },
  106. tableCols() {
  107. const { getFieldDecorator } = this.props.form;
  108. return [
  109. {
  110. title: '年份',
  111. dataIndex: 'year',
  112. key: 'year',
  113. render: (text, it) => {
  114. return <div>{text}</div>
  115. }
  116. },
  117. {
  118. title: '营收(元)',
  119. dataIndex: 'income',
  120. key: 'income',
  121. render: (text, it, index) => {
  122. let rules = [];
  123. if (index === 0) {
  124. rules.push({ required: index === 0, message: '请输入' + it.year + '营收情况!' })
  125. rules.push({
  126. validator: (rule, value, callback) => {
  127. if (!Number(value)) {
  128. callback('请输入' + it.year + '营收情况!' );
  129. } else {
  130. callback();
  131. }
  132. },
  133. })
  134. }
  135. return getFieldDecorator('income' + it.year, {
  136. initialValue: text || 0,
  137. rules: rules
  138. })(<InputNumber min={0} placeholder="请输入营收情况" />);
  139. }
  140. },
  141. {
  142. title: '利润率(%)',
  143. dataIndex: 'profit',
  144. key: 'profit',
  145. render: (text, it, index) => {
  146. let rules = [];
  147. if (index === 0) {
  148. rules.push({ required: index === 0, message: '请输入' + it.year + '利润率!' })
  149. rules.push({
  150. validator: (rule, value, callback) => {
  151. if (!Number(value)) {
  152. callback('请输入' + it.year + '利润率!' );
  153. } else {
  154. callback();
  155. }
  156. },
  157. })
  158. }
  159. return getFieldDecorator('profit' + it.year, {
  160. initialValue: text || 0,
  161. rules: rules
  162. })(<InputNumber min={0} placeholder="请输入利润率" />);
  163. }
  164. },
  165. ]
  166. },
  167. stringify(val) {
  168. return val && String(val)
  169. },
  170. render() {
  171. let { loading, initialData } = this.state;
  172. const { getFieldDecorator } = this.props.form;
  173. return (
  174. <Spin spinning={loading}>
  175. <div style={{ marginBottom: 10 }}>若该技术曾被应用于生产活动并产生收益,可选择【有历史收入】,若否,请选择【无历史收入】。</div>
  176. <Form className="steps-form">
  177. <FormItem label="是否有历史收入" {...formItemLayout}>
  178. {getFieldDecorator('hasIncome', {
  179. initialValue: this.stringify(initialData.hasIncome) || '1'
  180. })(
  181. <RadioGroup size="large" onChange={this.onRadioChange}>
  182. <RadioButton value="1">无历史收入</RadioButton>
  183. <RadioButton value="2">有历史收入</RadioButton>
  184. </RadioGroup>
  185. )}
  186. </FormItem>
  187. {
  188. this.state.hasIncome == 1 ?
  189. <div style={{ marginTop: 10 }}>无历史收入, 请直接进入下一步</div> :
  190. <Table pagination={false} rowKey="year" columns={this.tableCols()} dataSource={this.state.dataSource} />
  191. }
  192. </Form>
  193. <div className="steps-action">
  194. <Button type="primary" onClick={() => this.next()}>保存,下一步</Button>
  195. <Button style={{ marginLeft: 8 }} onClick={() => this.prev()}>上一步</Button>
  196. </div>
  197. </Spin>
  198. )
  199. },
  200. }));
  201. export default EvaluateStep4;