// 其他信息
import React from 'react';
import {InputItem, Picker, List, DatePicker, Toast} from 'antd-mobile';
import { createForm } from 'rc-form';
import Moment from 'moment';
import AddressSelectionControl from 'pages/common/component/AddressSelectionControl.jsx';

class OtherInformation extends React.Component{
  constructor() {
    super();
    this.state = {
      userInfo: {},
      verificationLocation: false
    }
  }

  componentDidMount = () => {
    this.props.onRef(this);
  }

  componentWillUnmount() {
    this.setState = (state, callback) => {
      return;
    };
  }

  // 获取form表单信息
  getFormData = () => {
    let {userInfo} = this.state;
    let obj = {}
    this.props.form.validateFields((error, values) => {
      if (!error) {
        obj = {...values};
        obj.patSexID = obj.patSexID&&obj.patSexID.length > 0 ? obj.patSexID[0] : "";
        // obj.patAddress = obj.patAddress;
        obj.patBirthDate = Moment(obj.patBirthDate).format("YYYY-MM-DD");
        obj.patNationID = obj.patNationID&&obj.patNationID.length > 0 ? obj.patNationID[0] : "";
        obj.location = userInfo&&userInfo.address ? userInfo.address : "";
        obj.patProvinceID = userInfo&&userInfo.provID ? userInfo.provID : "";
        obj.patCityID = userInfo&&userInfo.cityID ? userInfo.cityID : "";
        obj.patAreaID = userInfo&&userInfo.countryID ? userInfo.countryID : "";
        obj.patVillageID = userInfo&&userInfo.regionID ? userInfo.regionID : "";
        obj.errorCode = 0;
      } else {
        obj.errorCode = -100;
      }
    });
    return obj
  }

  // 验证表单信息
  handleVerification = (e) => {
    let {userInfo} = this.state;
    this.setState(() => {
      let obj = this.props.form.getFieldsValue();
      if (typeof(obj.patSexID) === 'object'&&obj.patBirthDate&&typeof(obj.patNationID) === 'object'&&userInfo&&userInfo.location) {
        this.props.modifyBindCardButtoStatus(false); // 如果姓名、身份证号以及手机号不为空是可操作下一步
      } else {
        this.props.modifyBindCardButtoStatus(true);
      }
    })
  }

  // 点击显示错误信息
  onErrorClick = (e) => {
    e.stopPropagation();
    Toast.info("请完善所在地区。", 3, () => {}, false)
  }

  // 给地址选框赋值
  addressBoxAssignment = (isClose) => {
    // 获取城市描述
    let cityData = this.cityChild.exportCitySelectInfo("ID");
    let {userInfo} = this.state;
    userInfo.location = cityData&&cityData.addressDesc ? cityData.addressDesc : "";
    this.setState({userInfo: {...userInfo, ...cityData}, verificationLocation: false}, () => {
      this.cityChild.handleClose(isClose); // 关闭城市弹窗
    })
  }

  cityRef = ref => {
    this.cityChild = ref;
  }

  // 打开地址选择弹窗
  showAddressModal = () => {
    let cityData = this.cityChild.exportCitySelectInfo("");
    let {userInfo} = this.state;
    this.cityChild.handleShowModal(cityData&&cityData.provData&&cityData.provData.value ? cityData : userInfo);
  }

  // 根据身份证号获取性别年龄以及出生日期
  getAnalysisIdCard = (card, num) => {
    if (num === 1) {
      //获取出生日期
      let birth = card.substring(6, 10) + "-" + card.substring(10, 12) + "-" + card.substring(12, 14);
      return new Date(birth);
    }
    if (num === 2) {
      //获取性别
      if (parseInt(card.substr(16, 1)) % 2 === 1) {
        //男
        return ['1'];
      } else {
        //女
        return ['2'];
      }
    }
    if (num === 3) {
      //获取年龄
      var myDate = new Date();
      var month = myDate.getMonth() + 1;
      var day = myDate.getDate();
      var age = myDate.getFullYear() - card.substring(6, 10) - 1;
      if (((card.substring(10, 12) < month ) || (card.substring(10, 12) === month)) && (card.substring(12, 14) <= day)) {
        age++;
      }
      return age;
    }
  }

  render() {
    let {verificationLocation, userInfo} = this.state;
    const { getFieldProps, getFieldError } = this.props.form;
    let {sex, nation} = this.props.selectData;
    return (
      <div>
        <form>
          <Picker data={sex} cols={1}
            error={!!getFieldError('patSexID')}
            onErrorClick={() => {
              Toast.info(getFieldError('patSexID').join('、'), 3, () => {}, false);
            }}
            title="选择性别"
            {...getFieldProps('patSexID', {
              onChange: this.handleVerification,
              initialValue: this.props.patIDCardNo&&typeof(this.props.patIDCardNo) === "string" ? this.getAnalysisIdCard(this.props.patIDCardNo, 2) : undefined,
              rules: [
                { required: true, message: '请选择' },
              ],
            })}
          >
            <List.Item arrow="horizontal">性别</List.Item>
          </Picker>
          <DatePicker
            mode="date"
            minDate={new Date(1900, 1, 1, 0, 0, 0)}
            {...getFieldProps('patBirthDate', {
              onChange: this.handleVerification,
              initialValue: this.props.patIDCardNo&&typeof(this.props.patIDCardNo) === "string" ? this.getAnalysisIdCard(this.props.patIDCardNo, 1) : undefined,
              rules: [
                { required: true, message: '请选择' },
              ],
            })}
          >
            <List.Item arrow="horizontal">出生日期</List.Item>
          </DatePicker>
          <Picker data={nation} cols={1}
            title="选择民族"
            {...getFieldProps('patNationID', {
              onChange: this.handleVerification,
              initialValue: nation&&nation.length > 0 ? [nation[0].value] : undefined,
              rules: [
                { required: true, message: '请选择' },
              ],
            })}
          >
            <List.Item arrow="horizontal">民族</List.Item>
          </Picker>
          <List.Item onClick={this.showAddressModal} className={verificationLocation ? "am-input-error list-address ellipsis" : "list-address ellipsis"} arrow="horizontal">
            <span className="am-input-label am-input-label-5" style={{float: "left"}}>
              所在地区
            </span>
            <span style={{color: "#888"}}>{userInfo&&userInfo.location ? userInfo.location : ""}</span>
            <div className="am-input-error-extra" onClick={(e) => this.onErrorClick(e)}></div>
          </List.Item>
          <InputItem
            error={!!getFieldError('patAddress')}
            onErrorClick={() => {
              Toast.info(getFieldError('patAddress').join('、'), 3, () => {}, false);
            }}
            {...getFieldProps('patAddress', {
              onChange: this.handleVerification,
              initialValue: undefined,
              rules: [
                { required: true, message: '请输入' },
              ],
            })}
          >详细地址</InputItem>
        </form>
        <AddressSelectionControl onRef={this.cityRef} addressBoxAssignment={this.addressBoxAssignment} />
      </div>
    )
  }
};
const OtherInformationForm = createForm()(OtherInformation);
export default OtherInformationForm;
