/**
 * Image Types
 */
import React from 'react';
import { Button } from 'antd';
import { connect } from 'react-redux';
import styles from './styles.less';
import FilterGroup from './components/FilterGroup';
import List from './components/List';
import DataDetails from './components/DataDetails';
import pureRender from 'common/pureRender';
import { createStructuredSelector } from 'reselect';
import types from './types';
import {
  listLoadAction,
  changePageAction,
  changePageSizeAction
} from './actions';

export class Container extends React.Component { 
  constructor(props) {
    super(props);
    debugger;
    console.log(props);
    const typeCreator = types[this.props.params.type] || types.nil;
    this.state = {
      type: typeCreator(this),
      ids:[]
    }

    /**
     * 翻页和分页
     * 
     */
    this.changePage = (current) => {
      this.props.dispatch(changePageAction(this.state.type, current));
    }

    this.changePageSize = (pageSize) => {
      this.props.dispatch(changePageSizeAction(this.state.type, pageSize))
    }
  }

  componentWillUpdate(nextProps, nextState) {
    if (nextProps.params.type !== this.props.params.type) {
      const typeCreator = types[nextProps.params.type] || types.nil;

      this.setState({
        type: typeCreator(this),
      });

      setTimeout(() => {
        this.resetPage();
        this.reload();
      }, 10);
    }
  }

  componentDidMount(){
    this.resetPage();
    this.reload();
  }

  getSelectedIds=(ids)=>{
    this.setState({
      ids:ids
    })
  }

  reload = () => {
    if (this.state.type.name !== 'notfound') {
      const type = this.state.type;
      this.props.dispatch(listLoadAction(type))
    }
  }

  resetPage = () => {
    this.props.dispatch({
      type: 'Image/changePage',
      payload: {
        currentPage: 1
      }
    })
  }

  render() {
    const type = this.state.type || {};
    if (type.name === 'notfound') {
      console.log(type);
      console.log(type.name);
      console.log(this.props);
      return (
        <div className={`idcos`}>
          <div className={styles.createInstance}>
            <type.component content={`您访问的 ${this.props.params.type} 页面不存在`}/>
          </div>
        </div>
      );
    }
    return (
      <div className={`idcos ${styles.component}`}>
        <h4 className="panel-title">云网络 / {type.name}</h4>
        <FilterGroup {...this.props} ids={this.state.ids} type={type}/>
        <List
          {...this.props}
          type={type}
          getSelectedIds={this.getSelectedIds}
          reload={this.reload}
          changePage={this.changePage}
          changePageSize={this.changePageSize}
          />
        <h1 className="details-title">详细信息</h1>
        <div className="details-box">
            <Button type="primary" size="large" className="details-bt">
               基本信息
            </Button>
            <DataDetails data={this.props.data.clickRowData ? this.props.data.clickRowData.results : {}} showTable={this.props.data.clickRowData ? {'display':'block'} :{'display':'none'}}/>
        </div>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    data: state.get('Image').toJS()
  }
}

function mapDispatchToProps(dispatch) {
  return {
    dispatch
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(pureRender(Container));
