/**
 * Created by baoyinghai on 11/17/16.
 */
import React from 'react';
import moment from 'moment';
import { VIEW, EDIT } from '../../constant';
import { TimePicker } from '../../../../../components/mxa';

class MyTimePicker extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      value: this.props.value
    };
  }

  componentWillReceiveProps(next) {
    if (next.value !== this.state.value) {
      this.state.value = next.value;
    }
  }

  _changeTime2Moment = value => {
    return value ? moment(new Date(value)) : null;
  }

  _changeMoment2Time = value => {
    return value.toDate().getTime();
  }

  _onChange = target => {
    const times = this._changeMoment2Time(target);
    this.setState({ value: times });
    this.props.onChange && this.props.onChange(times);
  }

  render() {
    return (
      <TimePicker disabled={this.props.disabled} value={this._changeTime2Moment(this.state.value)} onChange={this._onChange} />
    );
  }

}

const compRender = config => {
  const { record, props } = config;
  return (
    <MyTimePicker disabled={record.isReadonly || (props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly)} />
  );
};

/* eslint-disable */
class StaticDisplay extends React.Component {
  render() {
    const value = this.props.value ? moment(new Date(this.props.value)) : null;
    return (
      <span>{value && value.format('YYYY-MM-DD')}</span>
    );
  }
}

export default {
  [VIEW]: () => (<StaticDisplay />),
  [EDIT]: compRender
};
