import React from 'react';
import { connect } from 'dva';
import moment from 'moment';
import { Button,Popconfirm,Row, Col, Form, Input,Select,Checkbox,Modal,Transfer,Spin ,Table,DatePicker,InputNumber} from 'antd';
import {
  extend,
  extendRule,
  initFilter,
  guid,
  extendGroupRule,
  initFilterGroup,
  extendInclude
} from "../../../utils/common";
import {message} from "antd/lib/index";

const FormItem = Form.Item;
const Option = Select.Option;
const { TextArea } = Input;

class SpecialReceiveShowPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
      specialResult:'',
      receiveNumber:'0',
      remark:''
     
    };
  };
  
  componentDidMount() {
    this.loadData();
  }
  
  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){
            //console.log(ret)
            this.setState({
              data: ret,
            });
          }
        }
      });
    }
  };
  selectChange=(e)=> {
    
    this.setState({
      specialResult:e,
    });
  };
  numberChange=(e)=> {
    this.setState({
      receiveNumber:e,
    });
  };
  textchange=(e)=> {
    
    this.setState({
      remark:e.target.value,
    });
  };
  handleSubmit=(e)=> {
  
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err) => {
      if (!err) {
        const {login,dispatch,data}=this.props;
        
        const json=initFilter(
          login.OrgId,
          this.state.data.ID,
          '',
          0,
          1,
          this.state.specialResult,
          this.state.receiveNumber,
          login.userId,
          this.state.remark
        );
  
        dispatch({
          type: 'QCSpecialReceive/SendFlow',
          payload:json,
          onComplete: (ret) => {
            if(ret) {
              message.success('执行成功');
              if(data.close){
                data.close();
              }
            }
          }
        })
       
      }
    });
    
   
  };
  
  render() {
    const {form, location, dispatch, match,loading} = this.props;
    const { Nav_CheckRecord } = this.state.data;
   
    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.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('QTY', {
                  initialValue: (
                    this.state.data==null?'':this.state.data.QTY
                ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'不良率'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_CheckRecord.BAD_RATE', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord==null?'':Nav_CheckRecord.BAD_RATE)
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'检验时间'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_CheckRecord.END_TIME', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord==null?'':Nav_CheckRecord.END_TIME)
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'检验人'}
                {...formItemLayout}
              >
                {getFieldDecorator('Nav_User.NAME', {
                  initialValue: (
                    Nav_CheckRecord && (Nav_CheckRecord.Nav_User==null?'':Nav_CheckRecord.Nav_User.NAME)
                  ),
                })(<Input disabled={true}/>)}
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'特采方式'}
                {...formItemLayout}
              >
                {getFieldDecorator('SPECIAL_RESULT', {
                  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}
                  >
                    <Option value="0">让步接受</Option>
                    <Option value="1">降价处理</Option>
                  </Select>
                )}
                
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item
                label={'特采数量'}
                {...formItemLayout}
              > {getFieldDecorator('SPECIAL_RECEIVE_QTY', {
                rules: [ {
                  required: true, message: '请输入特采数量！',
                }],
              })(
                <InputNumber
                  min={1}
                  max={this.state.data.QTY}
                  disabled={this.props.data.isPermit}
                  onChange={this.numberChange}
                />
              )}
              
              </Form.Item>
            </Col>
            
            <Col span={24}>
              <Form.Item
                label={'备注'}
                {...formItemLayout1}
              >
                {getFieldDecorator('REMARK', {
                  initialValue: this.state.data.REMARK,
                })(<TextArea rows={4} onChange={this.textchange} disabled={this.props.data.isPermit} />)}
              </Form.Item>
            </Col>
          </Row>
          <Row style={{textAlign: 'right'}}>
            <Form.Item >
              <Popconfirm title="是否确定送审?" onConfirm={this.handleSubmit}>
                <Button type={'primary'}>送审</Button>
              </Popconfirm>
              
              {/*<Button*/}
                {/*type="primary"*/}
                {/*htmlType="submit"*/}
              {/*>*/}
                {/*送审*/}
              {/*</Button>*/}
            </Form.Item>
          </Row>
        </Form>
      </div>
    );
  }
  
}

SpecialReceiveShowPage.propTypes = {
};

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