/**
 * Created by zhouhuan on 2017/11/2.
 */
import React from 'react';
import { createForm } from 'rc-form';
import { View, ScrollView } from 'react-native';
import { List, InputItem, WhiteSpace } from 'antd-mobile';
import { connect } from 'react-redux';
import Icon from 'react-native-vector-icons/FontAwesome';
import { createAction } from '../../../utils';

@connect(({ peopleInfo }) => ({ peopleInfo }))
class PeopleDetailEditScreen extends React.Component {
  componentDidMount() {
    this.props.dispatch(createAction('peopleInfo/getPeopleDetail')(this.props.peopleInfo.peopleID));
  }
  onSubmit = () => {
    this.props.form.validateFields({ force: true }, (error, value) => {
      if (!error) {
        this.props.dispatch(createAction('peopleInfo/updateDSRDetail')({ id: this.props.peopleInfo.peopleID, singularData: value }));
      } else {
        console.log('Validation failed');
      }
    });
  };
  render() {
    const { detailData, metas } = this.props.peopleInfo;
    const { getFieldProps } = this.props.form;
    return (
      <ScrollView>
        <WhiteSpace />
        <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
          <Icon.Button
            name="id-card"
            backgroundColor="#08BBF9"
            onPress={() => {
              this.props.dispatch({ type: 'peopleInfo/DuKa' });
            }}
          >
            读卡
          </Icon.Button>
        </View>
        <WhiteSpace />
        <List>
          {
            metas.map((m) => {
              if (m.required === true) {
                return (
                  <InputItem
                    key={m.key}
                    {...getFieldProps(m.key, {
                      initialValue: detailData[m.key],
                      rules: [
                        { required: true, message: '不允许为空' },
                        { validator: this.validateNoNull },
                      ],
                    })}
                    clear
                    placeholder="请输入"
                    ref={(el) => {
                      this.customFocusInst = el;
                    }}
                  >
                    {m.label}
                  </InputItem>
                );
              } else {
                return (
                  <InputItem
                    key={m.key}
                    {...getFieldProps(m.key, {
                      initialValue: detailData[m.key],
                    })}
                    clear
                    placeholder="请输入"
                    ref={(el) => {
                      this.customFocusInst = el;
                    }}
                  >
                    {m.label}
                  </InputItem>
                );
              }
            })}
          {[1].map(() => {
            return (
              <List.Item key="saveItem">
                <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
                  <Icon.Button
                    name="save"
                    backgroundColor="#08BBF9"
                    onPress={this.onSubmit}
                  >
                    保存
                  </Icon.Button>
                </View>
              </List.Item>
            );
          })
          }
        </List>
      </ScrollView>
    );
  }
}
export default createForm()(PeopleDetailEditScreen);
