import React from 'react';
import { connect } from 'dva';
import {Row, Col, Form, Input, Select, Icon, Table, InputNumber} from 'antd';
import {extendRule,  initFilter, extendInclude, getDataFieldValue} from "../../../utils/common";

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

const Option = Select.Option;

//查看依据工程方案MRP单
class ShowFeedProposalPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
      btns:[],
      checked:true,
    };
  };

  componentDidMount() {
    this.props.form.resetFields();
    this.loadData();
  }

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

  loadData=()=> {
    const {data,formId,login,dispatch}=this.props;
    const {id}=data?data:{};

    if(id){
      const json = initFilter(this.props.login.OrgId);
      extendRule(json, 'ID', 1, id);
      extendInclude(json,'Nav_Product.Nav_Mould');
      extendInclude(json,'Nav_EmProposals.Nav_Product');
      extendInclude(json,'Nav_EmProposals.Nav_Inventorys.Nav_Location.Nav_Warehouse');
      extendInclude(json,'Nav_EmProposal');
      this.props.dispatch({
        type: 'SOMrpEdit/getMrp',
        payload: json,
        onComplete: (ret) => {
          if(ret){
            if(ret.Nav_EmProposals){
              ret.Nav_EmProposals.sort((x, y) => (x.PROPOSAL_TYPE > y.PROPOSAL_TYPE) ? 1 : -1)
            }
            this.setState({
              data: ret
            });
          }
        }
      });
    }
    else{
      this.setState({
        data:{}
      });
    }

  };



  render() {
    const {form, location, dispatch, match,loading} = this.props;
    const {getFieldValue, getFieldDecorator, validateFields, validateFieldsAndScroll, setFieldsValue} = form;
    const enums=this.props.app.enums;


    const renderProposal=(field,record)=>{
      if(record.PROPOSAL_TYPE==0) {
        if (field == 'SPLIT_QTY' || field == 'SET' || field == 'SPLIT_QTY' || field == 'FEED_QTY' || field == 'PCS' ||
          field == 'OUTPUT_PCS_QTY'||field=='FEED_AREA_QTY') {
          return;
        }
        if(field=='ShowInventory'){
          const editFormData= {
            pageCode:'FeedProposalPage',
            record: record,
            onSave: (record)=>{record.CurrInventoryQty=record.QTY; this.saveRecord(record);},
            isShow:true,
            isPermit:false,
            stateData:this.state.data.Nav_EmProposals,
          }
          return<>
            <InputNumber value={record.INVENTORY_QTY}  disabled={true} style={{ width: '80%' }}/>
            <FormModal
              formCode={'WH022'}
              data={editFormData}>
              <Icon type="edit" style={{ marginLeft: 8, fontSize: 20 }} />
            </FormModal></>
        }
        return getDataFieldValue(record, field);
      }

      if(field=='INVENTORY_QTY'||field=='Nav_Product.INVENTORY_MIN_LIMIT_NUMBER'||field=='PURCHASE_QTY'||field=='ShowInventory')
        return;
      return getDataFieldValue(record,field);
    }

    const tableColumns=[
      {title: '', dataIndex: '', key: '',width:100,  render: (text, record, index)=>{return renderProposal('DESCRIPTION',record)}},
      {title: '长度', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('LENGTH',record)}},
      {title: '宽度', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('WIDTH',record)}},
      {title: 'PCS/拼版', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('PCS',record)}},
      {title: 'SET/拼版', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('SET',record)}},
      {title: '可切割数', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('SPLIT_QTY',record)}},
      {title: '投料数量', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('FEED_QTY',record)}},
      {title: '投料面积', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('FEED_AREA_QTY',record)}},
      {title: '产出PCS', dataIndex: '', key: '', render: (text, record, index)=>{return renderProposal('OUTPUT_PCS_QTY',record)}},
    ]

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


    return (
      <div>
          <Form>
            <Row>
              <Col span={12}>
                <Form.Item
                  label={'产品名称'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('PRODUCT_NAME', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.NAME),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'产品编号'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('PRODUCT_CODE', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.CODE),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'交货方式'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('PRODUCT_SPLIT_QTY', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.SPLIT_QTY),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'单PCS尺寸'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('PRODUCT_PCS_SIZE', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.PCS_SIZE),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'拼版尺寸'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('PRODUCT_SPLIT_SIZE', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.SPLIT_SIZE),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'产品损耗率'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('ProductLossRaitoStr', {
                    initialValue:(this.state.data.Nav_Product==null?'':((this.state.data.Nav_Product.LOSS_RATIO*100).toFixed(2)+'%')),
                  })(<Input  disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'模具'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('Nav_Mould.NAME', {
                    initialValue: (this.state.data.Nav_Product==null||this.state.data.Nav_Product.Nav_Mould==null?'':this.state.data.Nav_Product.Nav_Mould.NAME),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'板厚'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('THICKNESS', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.THICKNESS),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'铜箔厚度'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('METAL_THICKNESS', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.METAL_THICKNESS),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'阻焊油墨'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('ZH_PRINTING_INK', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.ZH_PRINTING_INK),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'文字油墨'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('PRINTING_INK', {
                    initialValue: (this.state.data.Nav_Product==null?'':this.state.data.Nav_Product.PRINTING_INK),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'总数量'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('QTY', {
                    initialValue: this.state.data.QTY,
                  })(<Input  disabled={true}/>)}
                </Form.Item>
              </Col>




              <Col span={12}>
                <Form.Item
                  label={<label><span style={{color:'red'}}>*</span>下单数量</label> }
                  {...formItemLayout}
                >
                  <InputNumber value={this.state.data.WORK_QTY}  disabled={true}/>
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={'外部生产订单交期'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('DELIVERY_TMIE', {
                    initialValue: (this.state.data.DELIVERY_TMIE?this.state.data.DELIVERY_TMIE.toString().substring(0,10):''),
                  })(<Input  disabled={true}/>)}
                </Form.Item>
              </Col>
              <Col span={12}>
                <Form.Item
                  label={<label><span style={{color:'red'}}>*</span>生产交期</label> }
                  {...formItemLayout}
                >
                  {getFieldDecorator('PDT_ORDER_DUE_TIME', {
                    initialValue: (this.state.data.PDT_ORDER_DUE_TIME?this.state.data.PDT_ORDER_DUE_TIME.toString().substring(0,10):''),
                  })(<Input  disabled={true}/>)}

                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'紧急程度'}
                  {...formItemLayout}
                >
                  <Select  value={this.state.data.EMERGENCY_DEGREE_TYPE} disabled={true}>
                    {
                      enums&&enums.WOPdtOrderEmergencyDegreeTypeEnum&&enums.WOPdtOrderEmergencyDegreeTypeEnum.options?enums.WOPdtOrderEmergencyDegreeTypeEnum.options.map(t=>{
                        return  <Option value={t.value}>{t.label}</Option>
                      }):null
                    }
                  </Select>
                </Form.Item>
              </Col>

              <Col span={12}>
                <Form.Item
                  label={'工程方案'}
                  {...formItemLayout}
                >
                  {getFieldDecorator('Nav_EmProposal', {
                    initialValue: (this.state.data.Nav_EmProposal==null?'':this.state.data.Nav_EmProposal.NAME),
                  })(<Input disabled={true}/>)}
                </Form.Item>
              </Col>

            </Row>

            <Row style={{marginTop:'20px'}}>
              <Table
                rowKey="ID"
                pagination={false}
                dataSource={this.state.data.Nav_EmProposals}
                columns={tableColumns}
              />
            </Row>


          </Form>
      </div>
    );
  }

}

ShowFeedProposalPage.propTypes = {
};

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