/**
 * Created by zhouhuan on 2017/11/9.
 */
import React from 'react';
import { View, ScrollView, Text } from 'react-native';
import { List, InputItem, Button, Toast, WhiteSpace } from 'antd-mobile';
import { createForm } from 'rc-form';
import { connect } from 'react-redux';
import { createAction } from '../../../utils/index';

@connect(({ HouseDetail, ListHouse }) => ({ HouseDetail, ListHouse }))
class EditHouseDetail extends React.Component {
  componentDidMount() {
    const { dispatch, ListHouse } = this.props;
    dispatch(createAction('HouseDetail/getHouseDetail')(ListHouse.houseID));
  }
  onSubmit = () => {
    this.props.form.validateFields({ force: true }, (error, value) => {
      if (!error) {
        this.props.dispatch(createAction('HouseDetail/updateHouseDetail')({ id: this.props.ListHouse.houseID, singularData: value }));
      } else {
        Toast.show('Validation failed');
      }
    });
  };
  onReset = () => {
    this.props.form.resetFields();
  };
  render() {
    console.log(this.props.HouseDetail);
    const { singularData, metas } = this.props.HouseDetail;
    const { getFieldProps } = this.props.form;
    const list = metas.map(({ key, label }) => {
      const data = singularData[key];
      return (
        <InputItem
          {...getFieldProps(key, {
            initialValue: data,
          })}
          clear
          ref={(el) => { this.customFocusInst = el; }}
        >
          {label}
        </InputItem>
      );
    });
    return (
      <View>
        <ScrollView>
          <List>
            {list}
            <List.Item>
              <View style={{ flexDirection: 'row' }}>
                <Button type="primary"onClick={this.onSubmit}>保存</Button>
              </View>
            </List.Item>
          </List>
        </ScrollView>
      </View>
    );
  }
}
export default createForm()(EditHouseDetail);
