/**
 * Created by baoyinghai on 11/10/16.
 */
import React from 'react';
import { autobind } from 'core-decorators';
import detailCreator from './main';
import SwitchContainer from './switchContainer';
import { VIEW, EDIT } from './constant';
import DetailBody from './detailBody';
import { Row, Col, Icon, Button } from '../../../components/mxa';
import { scrollAble, scrollDisable } from '../../utils/ScrollHelper';
import { removeMask } from '../../utils/LoadingMaskHelper';
import TitleBar from '../info/titleBar';

export default class ListDetail extends React.Component {

  constructor(props) {
    super(props);
    // 检查buttons中是否有保存按钮
    //
    this.state = {
      globalBtns: [],
      main: null,
      topButtons: null,
      dataSource: this.props.dataSource
    };
  }

  componentWillMount() {
    this.setState({ main: (<DetailBody {...this.props} dataSource={this.state.dataSource} createTopButtons={this.createTopButtons} replaceMain={this.replaceMain} />) });
  }

  @autobind
  replaceMain(actionLink, p = {}, ...argv) {
    let param = p;
    if (argv && argv[1]) {
      param = Object.assign({}, param, argv[1]);
    }
    this.props.exec(() => {
      return this.props.fetch(actionLink, { ...this.props.params, param }).then(response => {
        // savePageData(response);
        response.detailResult = Object.assign({}, response.detailResult, param);
        this.setState({ dataSource: response });
      });
    });
  }

  _closeSelectDropDown = () => {
    const tags = document.querySelectorAll('.mx-select-dropdown.mx-select-dropdown--single.mx-select-dropdown-placement-bottomLeft');
    if (tags) {
      tags.forEach(item => {
        item.className += ' mx-select-dropdown-hidden';
      });
    }
  };

  componentDidMount() {
    if (this.props.query.s === '1') {
      scrollAble();
    }
    removeMask();
  }


  componentWillUnmount() {

  }

  shouldComponentUpdate(nextProps, nextState) {
    if (nextProps.notShouldUpdate) {
      return false;
    }
    return true;
  }

  @autobind
  createTopButtons(data) {
    this.setState({ topButtons: [...this.state.globalBtns, ...data] });
  }

  _getTitleDesc = () => {
    return '';
  };


  getFormFieldsValue = () => {
    /* eslint-disable */
    return this.refs.detailBody.getFormFieldsValue();
  }

  render() {
    const detail = (<DetailBody ref="detailBody" {...this.props} dataSource={this.state.dataSource} replaceMain={this.replaceMain} />);
    // info template will delete query of props;
    return detail;
  }

}

