import React from 'react';
import { connect } from 'dva';
import { Button, Row, Col, Form, Input,Select,Checkbox,Modal,Popconfirm,Spin ,Table,Tabs,message,InputNumber} from 'antd';
import { ConfirmButton } from '@woowalker/feui'
import {  extend,  extendRule,  initFilter,  initQueryFilter,  getOnlyPropertyData,  extendInclude} from "../../../utils/common";
import ProcessPrintPage from '../WO/ProcessPrintPage';
import ReactToPrint from "react-to-print";
const Option = Select.Option;




class EMOrderFlowCardPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
      maxQty:0,
      emType:0,
    };
  };

  componentDidMount() {
    this.loadData();
  }

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

  loadData=()=> {
    const {data}=this.props;
    const {id}=data?data:{};
    if(id){
      const json = initFilter(this.props.login.OrgId,id);
      extendRule(json, 'ID', 1, id);
      extendInclude(json,'Nav_MrpOrderWork.Nav_MrpOrderEmProposal');
      extendInclude(json,'Nav_EmCofirmUser');
      this.props.dispatch({
        type: 'EMemOrder/getEmOrder',
        payload: json,
        onComplete: (ret) => {
          if(ret){
            let maxQty=0;
            if(ret.Nav_MrpOrderWork&&ret.Nav_MrpOrderWork.Nav_MrpOrderEmProposal)
              maxQty=ret.Nav_MrpOrderWork.Nav_MrpOrderEmProposal.FEED_QTY;
            if(ret.PRE_FEED_QTY)
              maxQty=ret.PRE_FEED_QTY;
            let confirmUserId=ret.EM_CONFIRM_USER_ID;
            let confirmUserName=ret.Nav_EmCofirmUser==null?'':ret.Nav_EmCofirmUser.NAME;
            if(!confirmUserId){
              confirmUserId=this.props.login.userId;
              confirmUserName=this.props.login.user?this.props.login.user.NAME:null;
            }

            this.setState({
              data:ret,
              emType:ret.EM_ORDER_TYPE,
              confirmUserId,
              confirmUserName,
              maxQty
            },()=>{
              this.handleGetFlowCard();
            });
          }
        }
      });
    }
    else{
      this.handleGetFlowCard();
    }
  };

  handleGetFlowCard=()=>{
    const {data}=this.props;
    let {getIds}=data?data:{};
    const ids= getIds();
    if(ids) {
      const json = initFilter(this.props.login.OrgId, ids,'',0,1,this.state.maxQty,this.state.confirmUserId, this.state.emType);
      this.props.dispatch({
        type: 'EMemOrder/getFlowCardPrint',
        payload: json,
        onComplete: (ret) => {
          if (ret) {
            if(this.componentRef) {
              this.componentRef.updateData(ret);
            }
          }
        }
      });
      this.setState({
        ids,
      });
    }
  };

  handleConfirm=()=>{
    if(!this.state.maxQty){
      message.error('请填写每张投料数');
      return;
    }
    const {data}=this.props;
    const {id}=data?data:{};
    if(id) {
        const json = initFilter(this.props.login.OrgId, id,'',0,1,this.state.maxQty);
      this.props.dispatch({
        type: 'EMemOrder/confirmEmOrder',
        payload: json,
        onComplete: (ret) => {
          if (ret) {
            const data={...this.state.data};
            data.EM_CONFIRM_STATUS=1;
            this.setState({
              data,
            });
            message.success('执行成功');
          }
        }
      });
    }
  }

  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: 'EMemOrder/updatePrintStatus',
            payload: json,
            onComplete: (ret) => {
              if (ret) {
                if(data.close){
                  data.close();
                }
              }
            }
          });
      }
    }

    const changeMaxQty=(e)=>{
      if(e===null||e===undefined)return;
      const value=e.target&&e.target.value?e.target.value:(typeof e ==='object'?null:e);
      this.setState({
        maxQty:value,
      });
    };

    const enums=this.props.app.enums;
    const disabled=!this.state.data.ID|| this.state.data.EM_CONFIRM_STATUS==1||this.state.data.EM_CANCEL_STATUS==1||this.state.data.ORDER_STATUS==3;
    return (
      <div>

        {
          id? <div>
            <label>每张投料数</label>  <InputNumber value={this.state.maxQty} onChange={changeMaxQty} style={{marginRight:'25px'}}
                                               disabled={disabled} />
            <Select value={this.state.emType}  style={{marginRight:'25px'}} onChange={(e)=>{
              this.setState({
                emType:e
              });
            }}  disabled={disabled} >
              {
                enums&&enums.EMOrderTypeEnum&&enums.EMOrderTypeEnum.options?enums.EMOrderTypeEnum.options.map(t=>{
                  return  <Option value={t.value}>{t.label}</Option>
                }):null
              }
            </Select>

            <Button type={'primary'}  onClick={this.handleGetFlowCard} icon={'select'} style={{marginRight:'25px'}}
                    disabled={disabled}>确定</Button>
            <ConfirmButton isConfirm={true} confirmTitle="是否确定开工?" onClick={this.handleConfirm}
                           type={'danger'} icon={'check'} style={{marginRight:'25px'}}  disabled={disabled}  title={'确认开工'}/>
          </div>:null
        }

        <div style={{marginTop:'25px'}}>
            <ReactToPrint
              trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
              content={() => this.componentRef}
              onAfterPrint={()=>{onAfterPrint()}}
            />
        </div>
        <div style={{marginTop:'25px'}}>
          <ProcessPrintPage ref={el => (this.componentRef = el)} id={id} qccodeKey={'emOrder'} />
        </div>
      </div>
    );
  }

}

EMOrderFlowCardPage.propTypes = {
};

export default connect(({ login, loading,app,custom }) => ({ login, loading,app,custom }))(Form.create()(EMOrderFlowCardPage));
