import React from 'react';
import { connect } from 'dva';
import { Button, Row, Col, Form, message,Select,Table,InputNumber} from 'antd';
import {   extendRule,  initFilter,   getOnlyPropertyData,empty,getPropertyData,extendFilterGroup, guid, getDataFieldValue} from "../../../utils/common";
import EditComponent from '../../../baseComponents/EditComponent';
import Item from 'antd/lib/list/Item';
 

class AllocateReturnProductPage extends EditComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
    };
  };

  onBeforeEdit(params){
    super.onBeforeEdit(params);
    const { isNew, } = params ? params : {};
    if(isNew){
      const json=initFilter(this.props.login.OrgId,'','CREATE_TIME',1);
      extendRule(json,'CODE',1,'F901');
      this.props.dispatch({
        type: 'app/getDataByPost',
        payload: json,
        url:'FM/Warehouse/Get',
        onComplete: (ret) => {
          if (ret) {
            this.saveHeadRecord({
              ALLOCATE_WAREHOUSE_ID:ret.ID,
              Nav_AllocateWarehouse:ret,
            });
          }
        }
      });

      const json1=initFilter(this.props.login.OrgId,'','CREATE_TIME',1);
      extendRule(json1,'CODE',1,'F902');
      this.props.dispatch({
        type: 'app/getDataByPost',
        payload: json1,
        url:'FM/Warehouse/Get',
        onComplete: (ret) => {
          if (ret) {
            this.saveHeadRecord({
              WAREHOUSE_ID:ret.ID,
              Nav_Warehouse:ret,
            });
          }
        }
      });
    }
  }
 

  onAfterHeadDropDownSelect(params){
    super.onAfterHeadDropDownSelect(params);
    const{colConfig}=params; 
    if(colConfig.field==='Nav_AllocateWarehouse.NAME'){ 
      if(this.state.data.ALLOCATE_WAREHOUSE_ID){
        const json=initFilter(this.props.login.OrgId,'','CREATE_TIME',1);
        extendRule(json,'Nav_Warehouse.ID',1,this.state.data.ALLOCATE_WAREHOUSE_ID);
          this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url:'WH/Location/Get',
            onComplete: (ret) => {
              if (ret) {
                const data={...this.state.data};
                if(data.Nav_Details){
                  data.Nav_Details.forEach(t=>{
                    t.ALLOCATE_LOCATION_ID=ret.ID;
                  });
                }
                this.setState({data});
              }
            }
          });
      }
    }
  }

  onSave(closeType,clearType){
    let { dispatch, onSave, form , onCancel,id ,formCode,data} = this.props;
    if(!onSave&&data){
      onSave=data.onSave;
    }
    if(!onCancel&&data){
      onCancel=data.onCancel;
    }
    const { validateFieldsAndScroll } = form;

    validateFieldsAndScroll((errors, values) => {
      if (errors) return;
      const tmpStateData={...this.state.data};
      let updateData=getOnlyPropertyData(tmpStateData);
 

      let tmpIsError=false;
      // 从表数据
      if (this.state.subTable&&this.state.subTable.length) {
        this.state.subTable.map(t1=>{
          const tempSubData=tmpStateData[t1.NAV_PROPERTY];
          let subTableNewData = []; // 从表数据
          if(tempSubData&&tempSubData.length) {
            tempSubData.forEach((k, n) => {
              if(t1.editColumns&&t1.editColumns.length){
                t1.editColumns.forEach((n1,i1)=>{
                  if(n1.REQUIRED&&n1.IS_DEFAULT) {//必填字段检查
                    const v = getDataFieldValue(k,(n1.CONTROL_TYPE===9&&n1.INPUT_ID_FIELD?n1.INPUT_ID_FIELD: n1.FIELD_NAME), null);
                    if (empty(v) && !tmpIsError) {
                      message.error('序号'+(n+1)+': 请填写'+ n1.LABEL );
                      tmpIsError = true;
                    }
                  }
                });
              }
              let detail=subTableNewData.filter(t=>t.PRODUCT_ID===k.PRODUCT_ID&&t.LOCATION_ID===k.LOCATION_ID)[0];
              if(!detail){
                detail={
                  ALLOCATE_ORDER_ID:updateData.ID,
                  PRODUCT_ID:k.PRODUCT_ID,
                  LOCATION_ID:k.LOCATION_ID,
                  ID:guid(),
                  QTY:0,
                  ORG_ID:this.props.login.OrgId,
                  Nav_Inventorys:[],
                };
                subTableNewData.push(detail);
              }
              detail.QTY+=k.QTY;
              let tempK=getOnlyPropertyData(k);
              detail.Nav_Inventorys.push({
                ...tempK,
                ID:guid(),ALLOCATE_ORDER_DETAIL_ID:detail.ID,
                INVENTORY_ALLOCATE_TYPE:1,
                LOCATION_ID:k.ALLOCATE_LOCATION_ID
              }); 
            }); 
          } 
          if(t1.IS_DETAIL_REQUIRED&&subTableNewData.length){
            message.error('至少需要输入一笔记录');
            tmpIsError=true;
          }
          updateData[t1.NAV_PROPERTY]=subTableNewData;
        });
      }
      if(tmpIsError)return;
      const isError=this.handleBeforeSaveEvent(updateData);
      if(isError) return;
      this.setState({ btnLoading: true }) 
      const editConfig= this.getEditConfig();
      dispatch({
        type: 'app/getDataByPost',
        payload: updateData,
        url: editConfig.saveApi,
        onComplete: (ret) => {
          if(ret) {
            message.success('执行成功');
            if (typeof onSave === 'function') {
              onSave(this.state.data);
            }
            if(closeType===1&&id||//仅编辑时关闭
              closeType===2&&!id||//仅新增时关闭
              closeType!==1&&closeType!==2||//全部都关闭
              closeType===undefined){
              onCancel && onCancel();
            }
            else{
              if(!clearType||clearType===0) {
                this.props.form.resetFields();
                this.onLoadData();//重新渲染
              }
            }
            //添加日志
            if(id){
              this.addEditLog({
                formCode,
                ids:updateData.ID,
                codes:updateData.CODE,
                extData:JSON.stringify(updateData)
              }); 
            }
            else{
              this.addAddLog({
                formCode,
                ids:updateData.ID,
                codes:updateData.CODE,
                extData:JSON.stringify(updateData)
              });
            }
          }
          setTimeout(() => { this.setState({ btnLoading: false })}, 2000)
        }
      })
    })
  }


  onAfterAddRow(params){
    super.onAfterAddRow(params);
    if(this.state.data.ALLOCATE_WAREHOUSE_ID){
      const{record,editCode}=params;
      const json=initFilter(this.props.login.OrgId,'','CREATE_TIME',1);
      extendRule(json,'Nav_Warehouse.ID',1,this.state.data.ALLOCATE_WAREHOUSE_ID);
      this.props.dispatch({
        type: 'app/getDataByPost',
        payload: json,
        url:'WH/Location/Get',
        onComplete: (ret) => {
          if (ret) {
            record.ALLOCATE_LOCATION_ID=ret.ID;
            this.saveRowRecord({editCode,record});
          }
        }
      });
    }
  }

}
 

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