import React from 'react';
import { connect } from 'dva';
import { Button, Popconfirm, Row, Col, Form, Input, Select, InputNumber} from 'antd';
import {
  extendRule,
  initFilter,
  extendInclude, initQueryFilter
} from "../../../utils/common";
import {message} from "antd/lib/index";
import storage from "../../../utils/storage";
const Option = Select.Option;
const { TextArea } = Input;

class SpecialReceiveShowPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {},

      remark:'',
      InStorageCODE:'',
      wareHouse:[],
      wareHouse_ID:'',
      wareHouseLocation:[],
      wareHouseLocation_ID:'',
      InStorageNumber:'',

    };
  };

  componentDidMount() {
    this.getInStorageCODE();
    this.loadData();
    this.LoadWareHouseData();
  }

  componentWillReceiveProps(NextProps) {
    // const reload=NextProps.editPage[NextProps.formId+'_reload'];
    // if (reload ) {
    //   this.props.form.resetFields();
    //   this.loadData();
    //   this.props.dispatch({
    //     type: 'editPage/save',
    //     payload: {
    //       [this.props.formId + '_reload']: false
    //     },
    //   });
    // };

  }

  loadData=()=> {

    const {data,formId,login,dispatch}=this.props;
    const {id,ID}=data?data:{};
    const tmpId=id?id:ID;
    if(tmpId){
      const json = initFilter(this.props.login.OrgId);
      extendRule(json, 'ID', 1, tmpId);
      extendInclude(json,'Nav_CheckRecord.Nav_User');
      extendInclude(json,'Nav_CheckRecord.Nav_Product');
      extendInclude(json,'Nav_CheckRecord.Nav_InMaterialDetail.Nav_InMaterial.Nav_Provider');
      this.props.dispatch({
        type: 'QCSpecialReceive/getOrder',
        payload: json,
        onComplete: (ret) => {
          if(ret){
            this.setState({
              data: ret,
              InStorageNumber:ret.SPECIAL_RECEIVE_QTY,
            });
          }
        }
      });
    };
  };

  LoadWareHouseData=()=> { //获取库位的数据
    const orgId = storage('lacal').getItem('webOrgId').val;
    let json = initQueryFilter(orgId, 1, 100, "CREATE_TIME", 0);
    extendRule(json, "ENABLE_STATUS", 1, 0);//过滤启用状态
    this.props.dispatch({
      type: 'FMWareHouse/OrderEntities',
      payload: json,
      onComplete: (ret) => {

        this.setState({
          wareHouse: ret
        });
      }
    });
  };
  getInStorageCODE=()=> {
    const json = initFilter(this.props.login.OrgId);
    json["Count"]=1;
    json["CodeType"]=3504;
    this.props.dispatch({
      type: 'codeRule/GenSystemCode',
      payload: json,
      onComplete: (ret) => {
        if(ret){

          this.setState({
            InStorageCODE: ret,
          });
        }
      }
    });
  };
  selectChange=(e)=> {//
      const orgId = storage('lacal').getItem('webOrgId').val;

      let json = initQueryFilter(orgId, 1, 100, "CREATE_TIME", 0);
      extendRule(json, "ENABLE_STATUS", 1, 0);//过滤启用状态
      extendRule(json, "WAREHOUSE_ID", 1, e);//过滤启用状态

      this.props.dispatch({
        type: 'WHLocation/OrderEntities',
        payload: json,
        onComplete: (ret) => {
          this.setState({
            wareHouseLocation: ret
          });
        }
      });

    this.setState({
      wareHouse_ID:e,
    });
  };
  LocationselectChange=(e)=> {
    this.setState({
      wareHouseLocation_ID:e,
    });
  };
  numberChange=(e)=> {

    this.setState({
      InStorageNumber:e,
    });
  };
  textchange=(e)=> {

    this.setState({
      remark:e.target.value,
    });
  };

  batchNumberchange=(e)=> {

    this.setState({
      batchNumber:e.target.value,
    });
  };

  handleSubmit=(e)=> {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err) => {
      if (!err) {
        const {login,dispatch}=this.props;
        const json=initFilter(
          login.OrgId,
          this.state.InStorageCODE,
          '',
          0,
          1,
          this.state.wareHouse_ID,
          this.state.wareHouseLocation_ID,
          this.state.remark,
          this.state.data.Nav_CheckRecord.Nav_Product.ID,
          this.state.InStorageNumber,
          login.userId
        );
        // json["COUNT"]=1;
         json["Parameter7"]=this.state.batchNumber;
        dispatch({
          type: 'QCSpecialReceive/FullInStorage',
          payload:json,
          onComplete: (ret) => {
            if(ret) {
              message.success('执行成功');
              //loadData();
            }
          }
        })

      }
    });


  };

  render() {
    const {form, location, dispatch, match,loading} = this.props;
    const { Nav_CheckRecord,SPECIAL_RESULT } = this.state.data;
    var receiveWay;
    if(SPECIAL_RESULT==0)
    {
      receiveWay="让步接受";
    }else {
      receiveWay="降价处理";
    }
    const {getFieldValue, getFieldDecorator, validateFields, validateFieldsAndScroll, setFieldsValue} = form;
    const formItemLayout = {

      labelCol: {span: 6},
      wrapperCol: {span: 14},
    };
    const formItemLayout1 = {

      labelCol: {span: 3},
      wrapperCol: {span: 18},
    };
    return (
      <div>
        <Form onSubmit={this.handleSubmit}>
          <Row>
            <Col span={12}>
              <Form.Item
                label={'入库单号'}
                {...formItemLayout}
              >
                {getFieldDecorator('CODE', {
                  initialValue:  this.state.InStorageCODE,

                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'特采单号'}
                {...formItemLayout}
              >
                {getFieldDecorator('CODE', {
                  initialValue: this.state.data.CODE,

                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'检验单号'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_CheckRecord.CODE', {
                  initialValue: (this.state.data.Nav_CheckRecord==null?'':this.state.data.Nav_CheckRecord.CODE),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'来料单号'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_InMaterial.CODE', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord.Nav_InMaterialDetail.Nav_InMaterial==null?'':Nav_CheckRecord.Nav_InMaterialDetail.Nav_InMaterial.CODE)
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'供应商'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_Provider.NAME', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord.Nav_InMaterialDetail.Nav_InMaterial.Nav_Provider==null?'':Nav_CheckRecord.Nav_InMaterialDetail.Nav_InMaterial.Nav_Provider.NAME)
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'材料名称'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_Product.NAME', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord.Nav_Product==null?'':Nav_CheckRecord.Nav_Product.NAME)
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'材料编号'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_Product.CODE', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord.Nav_Product==null?'':Nav_CheckRecord.Nav_Product.CODE)
                ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'特采方式'}
                {...formItemLayout}
              >
                {getFieldDecorator('SPECIAL_RESULT', {
                  initialValue: (receiveWay),
                })(<Input disabled={true}/>)}

              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'特采数量'}
                {...formItemLayout}
              >
                {getFieldDecorator('SPECIAL_RECEIVE_QTY', {
                  initialValue: (
                    this.state.data==null?'':this.state.data.SPECIAL_RECEIVE_QTY
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'入库数'}
                {...formItemLayout}
              > {getFieldDecorator('INQTY', {
                initialValue:this.state.data.SPECIAL_RECEIVE_QTY,
                rules: [ {
                  required: true, message: '请输入入库数量！',
                }],
              })(
                <InputNumber
                  style={{width:"100%"}}
                  min={1}
                  max={this.state.data.SPECIAL_RECEIVE_QTY}
                  //defaultValue={3}
                  onChange={this.numberChange}
                />
              )}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'仓库'}
                {...formItemLayout}
              >
                {getFieldDecorator('wareHouse', {
                  rules: [ {
                    required: true, message: '请选择仓库！',
                  }],
                })(
                  <Select
                    showSearch
                    style={{ width: 200 }}
                    placeholder='选择仓库'
                    optionFilterProp="children"
                    onChange={this.selectChange}
                    disabled={this.props.data.isPermit}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  >
                    {//map 循环 填充下拉项目
                      this.state.wareHouse.map(item => {
                        return <Option key={item.ID} value={item.ID}>{item.NAME}</Option>
                      })
                    }
                  </Select>
                )}
              </Form.Item>
            </Col>

            <Col span={12}>
              <Form.Item
                label={'库位'}
                {...formItemLayout}
              >
                {getFieldDecorator('Location', {
                  rules: [ {
                    required: true, message: '请选择库位！',
                  }],
                })(
                  <Select
                    showSearch
                    style={{ width: 200 }}
                    placeholder='选择库位'
                    optionFilterProp="children"
                    onChange={this.LocationselectChange}
                    disabled={this.props.data.isPermit}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  >
                    {//map 循环 填充下拉项目
                      this.state.wareHouseLocation.map(item => {
                        return <Option key={item.ID} value={item.ID}>{item.NAME}</Option>
                      })
                    }
                  </Select>
                )}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'批次号'}
                {...formItemLayout}
              >
                {getFieldDecorator('BATCH_NUMBER', {

                })(<Input onChange={this.batchNumberchange} />)}
              </Form.Item>
            </Col>
            <Col span={24}>
              <Form.Item
                label={'备注'}
                {...formItemLayout1}
              >
                {getFieldDecorator('REMARK', {

                })(<TextArea rows={4} onChange={this.textchange}  />)}
              </Form.Item>
            </Col>
          </Row>
          <Row style={{textAlign: 'right'}}>
            <Form.Item >
              <Popconfirm title="是否确定送审?" onConfirm={this.handleSubmit}>
                <Button type={'primary'}>入库</Button>
              </Popconfirm>

            </Form.Item>
          </Row>
        </Form>
      </div>
    );
  }

}

SpecialReceiveShowPage.propTypes = {
};

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