/**
 * Created by baoyinghai on 1/10/17.
 */
import React from 'react';
import { autobind } from 'core-decorators';
import detailCreator from './main';
import SwitchContainer from './switchContainer';
import { VIEW, EDIT } from './constant';
import { Row, Col, Icon, Button } from '../../../components/mxa';


export default class Detail extends React.Component {

  /* eslint-disable */
  static defaultProps = {
    model: EDIT
  };

  /* eslint-disable */
  static propTypes = {
    displayType: React.PropTypes.oneOf([VIEW, EDIT]),
    inject: React.PropTypes.object,
    createRules: React.PropTypes.func, // 自定义表单校验 // record
    beforeSubmit: React.PropTypes.func, // 表单提交之前 // callback
    afterSubmit: React.PropTypes.func, // 表单提交之后  //  err
    dataSource: React.PropTypes.object.isRequired,
    renderAnalyser: React.PropTypes.func, // 自定义渲染标单项 // componentName
    renderButtonsSelf: React.PropTypes.func, // 自定义渲染按钮
  };

  @autobind
  confirmModel() {
    return this.props.dataSource.displayType || this.props.displayType || (this.props.params && this.props.params.displayType);
    return undefined;
  }

  constructor(props) {
    super(props);
    // 检查buttons中是否有保存按钮
    //
    this.state = {
      model:  this.confirmModel() || this.props.model,
    };
    // this.originalOnBeforeUnload = null;
  }

  componentWillMount() {
    // this.originalOnBeforeUnload = window.onbeforeunload;
    // window.onbeforeunload = function (e) {
    //   return '关闭提示'; // 这个提示无效
    // };
  }

  componentWillUnmount() {
    // window.onbeforeunload =  this.originalOnBeforeUnload;
  }

  @autobind
  getFormFieldsValue() {
    return this.refs.main.getFieldsValue();
  }

  @autobind
  changeState() {
    if (this.state.model === VIEW) {
      this.setState({ model: EDIT });
    } else {
      this.setState({ model: VIEW });
    }
  }

  render() {
    const model = this.state.model;
    const mainProps = { ...this.props, model };
    const Main = detailCreator();
    return (
      <Main { ...mainProps } ref="main" changeState={this.changeState} />
    );
  }

}

