import React from 'react';
import { connect } from 'dva';
import { Button} from 'antd';
import { initFilter } from "../../../utils/common";
import ProcessPrintPage from './ProcessPrintPage';

import ReactToPrint from "react-to-print";


class OrderFlowCardPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      columns:[],
    };
  };

  componentDidMount() {
    this.loadData();
  }

  componentWillReceiveProps(NextProps) {
    const {data}=this.props;
    let {id}=data?data:{};
    if(!id)
      id='';
    if (NextProps.custom['orderFlowCardClick'+id] ) {
      this.loadData();
      this.props.dispatch({
        type: 'custom/save',
        payload: {
          ['orderFlowCardClick'+id]:false
        },
      });
    }
  }

  loadData=()=> {
    const {data}=this.props;
    let {id}=data?data:{};
    if(!id)
      id='';
    const {getIds}=data?data:{};
    if (this.componentRef) {
      this.componentRef.updateData(null,id);
    }
    if(typeof getIds === 'function'){
      const ids= getIds();
      if(ids) {
        const json = initFilter(this.props.login.OrgId, ids);
        this.props.dispatch({
          type: 'WOOrderFlowCard/getFlowCardPrint',
          payload: json,
          onComplete: (ret) => {
            if (ret) {
              if (this.componentRef) {
                this.componentRef.updateData(ret,id);
              }
            }
          }
        });
      }
      this.setState({
        ids,
      })
    }
  };
  render() {
    const {data}=this.props;
    let {id}=data?data:{};
    if(!id)
      id='';
    const onAfterPrint=()=>{
        const {ids}=this.state;
        if(ids){
          const json = initFilter(this.props.login.OrgId,ids);
          this.props.dispatch({
            type: 'WOOrderFlowCard/updatePrintStatus',
            payload: json,
            onComplete: (ret) => {
              if (ret) {
                if(data.close){
                  data.close();
                }
              }
            }
          });
      }
    }
    return (
      <div>
        <ReactToPrint
          trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
          content={() => this.componentRef}
          onAfterPrint={()=>{onAfterPrint()}}
        />
        <div style={{marginTop:'25px'}}>
        <ProcessPrintPage ref={el => (this.componentRef = el)}  id={id} qccodeKey={'pdtOrder'} />
        </div>
      </div>
    );
  }

}

OrderFlowCardPage.propTypes = {
};

export default connect(({ login, loading,app,custom }) => ({ login, loading,app,custom }))(OrderFlowCardPage);
