// 库存请求制单-依据《消耗及补货标准》
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Modal, message, Input } from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';
import '../style/index.less';

const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:7 },
    wrapperCol: { span: 17 },
};

const RequestConsume = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                SelectData: {},
                visible: false,
                ChineseEnglishSize: {}, //中英翻译
            };
        }

        componentDidMount() {
            this.props.onRef(this);
            // 初始化数据
            this.getTacitlyApproveData();
        }

        //清空表格内容
        handleReset = () => {
            this.props.form.resetFields();
        };

        // 获取界面默认值
        getTacitlyApproveData() {
            let data = {
                params: []
            };
            // 获取下拉数据
            $http.post('urlS', this, {
                code: "02020251",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({SelectData: res})
                    }
                }
            })
        }

        // 保存
        getWarehouseList=()=>{
            this.props.form.validateFields((err , val)=>{
                let obj = JSON.parse(JSON.stringify(val));
                if(err == null) {
                    let data = {
                        params: [{
                            frLocID: obj.frLocDr ? obj.frLocDr : undefined,
                            toLocID: obj.toLocDr ? obj.toLocDr : undefined ,
                            startDate:  obj.startDate ? Moment(obj.startDate).format('YYYY-MM-DD')  : undefined,
                            endDate:  obj.startDate ? Moment(obj.endDate).format('YYYY-MM-DD')  : undefined,
                            useDays:  obj.useDays ? obj.useDays : undefined,
                            stkCatID: obj.stkCatDr ? obj.stkCatDr : undefined,

                }],
                    };
                    $http.post('urlS', this, {
                        code: "02020249",
                        data: data,
                        success: function (res) {
                            if(res.errorCode == 0){
                                message.success(res.errorMessage);
                                this.props.handleThis.getEchoData(res.result.main.stkReqID);
                                this.handleCancel();
                            }
                        }
                    });
                }
            })

        };

        // 关闭
        handleCancel = (e) => {
            this.setState({
                visible: false,
            });
        };
        // 打开弹框
        showModal = (val) => {
            this.setState({
                visible: true,
                ChineseEnglishSize: val,
            });
        };
        render() {
            const { getFieldDecorator } = this.props.form;

            //下拉列表
            let loc = [];
            this.state.SelectData.loc && this.state.SelectData.loc.map((item, i) => {
                loc.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            let {ChineseEnglishSize} = this.state;
            return (
                <Modal
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    width='30%'
                    footer={null}
                    className="RequestConsume"
                >
                    <Row>
                        <Card
                            size="small"
                            title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.StockRequestFormbaseOnCon ? ChineseEnglishSize.StockRequestFormbaseOnCon.descripts:'辅助请求-依据《消耗及补货标准》'}</div>}
                        >
                            <Col >
                                <Form>
                                    <Row>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormtoLocDr ? ChineseEnglishSize.StockRequestFormtoLocDr.descripts:'请求部门'}>
                                                {getFieldDecorator('toLocDr',{
                                                    initialValue: this.state.userInfo.locID ? this.state.userInfo.locID : undefined,
                                                    rules:[{ required : ChineseEnglishSize.StockRequestFormtoLocDr ? ChineseEnglishSize.StockRequestFormtoLocDr.required =='Y'? true:false: false, message: ChineseEnglishSize.StockRequestFormtoLocDr ? ChineseEnglishSize.StockRequestFormtoLocDr.message:'请求部门不能为空'}]
                                                })(
                                                    <Select allowClear={true}>
                                                        {loc}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormfrLocDr ? ChineseEnglishSize.StockRequestFormfrLocDr.descripts:'供给部门'}>
                                                {getFieldDecorator('frLocDr',{
                                                    initialValue: undefined,
                                                    rules:[{ required : ChineseEnglishSize.StockRequestFormfrLocDr ? ChineseEnglishSize.StockRequestFormfrLocDr.required =='Y'? true:false: false, message: ChineseEnglishSize.StockRequestFormfrLocDr ? ChineseEnglishSize.StockRequestFormfrLocDr.message:'供给部门不能为空'}]
                                                })(
                                                    <Select showSearch allowClear placeholder={ChineseEnglishSize.StockRequestFormselectSupplyDept ? ChineseEnglishSize.StockRequestFormselectSupplyDept.descripts:'请选择供给部门'} >
                                                        {loc}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormstartDate ? ChineseEnglishSize.StockRequestFormstartDate.descripts:'起始时间'}>
                                                {getFieldDecorator('startDate',{
                                                    initialValue: undefined,
                                                    rules:[{ required : true, message: ChineseEnglishSize.StockRequestFormstartDate ? ChineseEnglishSize.StockRequestFormstartDate.message:'起始时间不能为空'}]
                                                })(
                                                    <DatePicker  style={{width: '100%'}} placeholder={ChineseEnglishSize.StockRequestFormSelectDate ? ChineseEnglishSize.StockRequestFormSelectDate.descripts:'请选择起始日期'} />
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormendDate ? ChineseEnglishSize.StockRequestFormendDate.descripts:'结束时间'}>
                                                {getFieldDecorator('endDate',{
                                                    initialValue: undefined,
                                                    rules:[{ required : true, message: ChineseEnglishSize.StockRequestFormendDate ? ChineseEnglishSize.StockRequestFormendDate.message:'结束时间不能为空'}]
                                                })(
                                                    <DatePicker style={{width: '100%'}} placeholder={ChineseEnglishSize.StockRequestFormSelectDate ? ChineseEnglishSize.StockRequestFormSelectDate.descripts:'请选择截止日期'} />
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormstkCatID ? ChineseEnglishSize.StockRequestFormstkCatID.descripts:'库存大类'}>
                                                {getFieldDecorator('stkCat',{
                                                    initialValue: undefined,
                                                    rules:[{ required : false, message: '库存大类不能为空'}]
                                                })(
                                                    <Select showSearch allowClear placeholder={ChineseEnglishSize.StockRequestFormstkCatID ? ChineseEnglishSize.StockRequestFormstkCatID.descripts:'请选择库存大类'} >
                                                        {this.state.SelectData.stkCat && this.state.SelectData.stkCat.map(item => {
                                                            return <Option key={item.id} value={item.id} >{item.descripts}</Option>
                                                        })}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormstkType ? ChineseEnglishSize.StockRequestFormstkType.descripts:'库存分类'}>
                                                {getFieldDecorator('stkSubCat',{
                                                    initialValue: undefined,
                                                    rules:[{ required : false, message: '库存分类不能为空'}]
                                                })(
                                                    <Select showSearch allowClear placeholder={ChineseEnglishSize.StockRequestFormselectInvenClass ? ChineseEnglishSize.StockRequestFormselectInvenClass.descripts:'请选择库存分类'} >
                                                        {this.state.SelectData.stkSubCat && this.state.SelectData.stkSubCat.map(item => {
                                                            return <Option key={item.id} value={item.id} >{item.descripts}</Option>
                                                        })}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.StockRequestFormuseDays ? ChineseEnglishSize.StockRequestFormuseDays.descripts:'参考天数'}>
                                                {getFieldDecorator('useDays',{
                                                    initialValue:  undefined,
                                                    rules:[{ required : ChineseEnglishSize.StockRequestFormuseDays ? ChineseEnglishSize.StockRequestFormuseDays.required =='Y'? true:false: false, message: ChineseEnglishSize.StockRequestFormfrLoc ? ChineseEnglishSize.StockRequestFormfrLoc.message:'参考天数不能为空'}]
                                                })(
                                                    <Input placeholder={ChineseEnglishSize.StockRequestFormuseDaysTips ? ChineseEnglishSize.StockRequestFormuseDaysTips.descripts:'请输入参考天数'}/>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row style={{marginTop: '4px', textAlign: 'center'}} className="buttonLeft">
                                        <Button onClick={this.getWarehouseList}>{ChineseEnglishSize.StockRequestFormsave ? ChineseEnglishSize.StockRequestFormsave.descripts:'保存'}</Button>
                                        <Button style={{marginLeft: '90px'}} onClick={this.handleReset}>{ChineseEnglishSize.StockRequestFormReset ? ChineseEnglishSize.StockRequestFormReset.descripts:'清空'}</Button>
                                    </Row>
                                </Form>
                            </Col>
                        </Card>
                    </Row>
                </Modal>
            )
        }
    }
);
export default RequestConsume;