import React from 'react';
import { connect } from 'dva';
import { Button, Row, Col, Form, Input, Spin, Select, Icon, Table, InputNumber, DatePicker, message} from 'antd';
import {
  extend,
  extendRule,
  initFilter,
  initQueryFilter,
  getOnlyPropertyData,
  extendInclude,
  getDataFieldValue,
  setDataFieldValue,
  empty,
  guid
} from "../../../utils/common";
import moment from 'moment';

import FormModal from '../../FormModal';

import  DropDownPagination from '../../common/DropDownPaginationEx';
import EditComponent from "../../../baseComponents/EditComponent";
import login from "../../../models/login";

const Option = Select.Option;

//生产预投料
class PdtOrderOverPickingPage extends EditComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
      // btns:[],
      // checked:true,
      // isEnoughWorkQty:true,
    };
  };

  toOverPicking=()=>{
    const { dispatch,form , } = this.props;
    const { validateFieldsAndScroll } = form;
    validateFieldsAndScroll((errors, values) => {
      if (errors) return;
      const pdtOrderId = this.getFieldValue('PDT_ORDER_ID');
      const overFeedQty = this.getFieldValue('OVER_FEED_QTY');
      let remark = this.getFieldValue('REMARK');
      if(!remark){
        remark='';
      }
      const json = initFilter(this.props.login.OrgId,(pdtOrderId+":"+overFeedQty+":"+remark));
      this.setState({ btnLoading: true });
      const editConfig= this.getEditConfig();
      dispatch({
        type: 'app/getDataByPost',
        payload: json,
        url: editConfig.saveApi,
        onComplete: (ret) => {
          if(ret) {
            message.success('执行成功');
            this.setState({
              data:{}
            });
            this.addOtherLog({
              formCode:this.props.formCode,
              title:"用户【"+this.props.login.user.NAME+ "】使用生产订单生产"+this.getFieldValue('Nav_PdtOrder.PDT_CODE')+'进行超领',
              data:'ID:'+pdtOrderId+',投入数'+overFeedQty,
            })
            if(this.props.data&&this.props.data.close){
              this.props.data.close(1);
            }
          }
          setTimeout(() => { this.setState({ btnLoading: false })}, 2000)
        }
      })
    });
  };

  onAfterHeadControlChange(params){
    super.onAfterHeadControlChange(params);
    const {value, colConfig,thisRef} = params ? params : {};
    if (colConfig.field==='OVER_FEED_QTY') {
      //this.handleFieldChangeEvent(value, colConfig.column);
      const pcs=thisRef.getFieldValue('Nav_PdtOrder.Nav_EmOrder.PCS');
      if(value&&pcs){
        thisRef.setFieldValue('OUT_PCS',pcs*value);
      }
      else{
        thisRef.setFieldValue('OUT_PCS',0);
      }
    }

  }

  onAfterHeadDropDownSelect(params){
    super.onAfterHeadDropDownSelect(params);
    const {value,record,colConfig,thisRef}=params?params:{};
    if(colConfig.field==='Nav_PdtOrder.PDT_CODE') {
      const overFeedQty = thisRef.getFieldValue('OVER_FEED_QTY');
      const pcs = thisRef.getFieldValue('Nav_PdtOrder.Nav_EmOrder.PCS');
      if (overFeedQty && pcs) {
        thisRef.setFieldValue('OUT_PCS', pcs * overFeedQty);
      }
      else {
        thisRef.setFieldValue('OUT_PCS', 0);
      }
    }
  }


  onRenderBtn(params){
    const {btn}=params?params:{};
    if(btn.BTN_TYPE === 1){
      return <Button key={btn.ID} type={'primary'} icon={'save'} loading={this.state.btnLoading}
              onClick={() => this.toOverPicking()}>{btn.LABEL}</Button>;
    }
    return super.onRenderBtn(params);
  }

}


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