// 采购计划制单-依据消耗
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Modal, Table, Switch, message, Pagination, Input, Checkbox, Icon } from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';
import store from 'store';
import '../style/index.less';

const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:7 },
    wrapperCol: { span: 17 },
};
const formItemSwitch2 = {    // label 和input宽度
    labelCol: { span:10 },
    wrapperCol: { span: 12 },
};
const AccordingToConsume = 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();
        }
        componentWillReceiveProps(props){
            this.setState({ChineseEnglishSize: props.ChineseEnglishSize})
        }
        //清空表格内容
        handleReset = () => {
            this.props.form.resetFields();
        };

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

        // 获取药品列表数据
        getWarehouseList=()=>{
            this.setState({loading:true})
            this.props.form.validateFields((err , val)=>{
                let obj = JSON.parse(JSON.stringify(val));
                if(err == null){
                    // let qStkCatDr = this.props.form.getFieldValue('stkCatDr');
                    let data = {
                        params: [{
                            locID: obj.locDr ? obj.locDr : undefined,
                            useLocID: obj.useLoc ? obj.useLoc : undefined,
                            startDate: Moment(obj.startDate).format('YYYY-MM-DD'),
                            endDate: Moment(obj.endDate).format('YYYY-MM-DD'),
                            useDays: obj.useDays ? obj.useDays : undefined,
                            busType: obj.busType ? obj.busType : undefined,
                            stkCatID: obj.stkCatDr ? obj.stkCatDr : undefined,
                            stkSubCatID: obj.stkSubCatDr ? obj.stkSubCatDr : undefined,
                            PBFlag: obj.PBFlag ? obj.PBFlag : undefined,
                            allConsume: obj.allConsume ? "Y" : "N"
                         }],
                    };
                    $http.post('urlS', this, {
                        code: "02020244",
                        data: data,
                        success: function (res) {
                            if(res.errorCode == 0){
                                message.success(res.errorMessage);
                                this.props.handleThis.getEchoData(res.result.main.PPID);
                                this.handleCancel();
                            }
                        }
                    });
                }
            })

        };

        // 关闭
        handleCancel = (e) => {
            this.setState({
                visible: false,
            });
        };
        // 确定
        handleOk = (e) => {
            this.handleCancel()
        };
        // 打开弹框
        showModal = () => {
            this.setState({
                visible: true,
            });
        };
        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, userInfo}=this.state;
            return (
                <Modal
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    width='30%'
                    footer={null}
                    className="AccordingToConsume"
                >
                    <Row>
                        <Card
                            size="small"
                            title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.purchasePlanDetailListPurchasePlanPopoup ? ChineseEnglishSize.purchasePlanDetailListPurchasePlanPopoup.descripts:'采购计划制单-依据消耗'}</div>}
                        >
                            <Col >
                                    <Col>
                                        <Form>
                                            <Row >
                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListPurchaseLoc ? ChineseEnglishSize.purchasePlanDetailListPurchaseLoc.descripts:'采购部门'}>
                                                        {getFieldDecorator('locDr',{
                                                            initialValue: this.state.userInfo.locID ? this.state.userInfo.locID : undefined,
                                                            rules:[{ required : ChineseEnglishSize.purchasePlanDetailListloc ? ChineseEnglishSize.purchasePlanDetailListloc.required =='Y'? true:false: false, message: ChineseEnglishSize.purchasePlanDetailListloc ? ChineseEnglishSize.purchasePlanDetailListloc.message:'采购部门不能为空'}]
                                                        })(
                                                            <Select allowClear={true}>
                                                                {loc}
                                                            </Select>
                                                        ) }
                                                    </FormItem>
                                                </Col>

                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListStartD ? ChineseEnglishSize.purchasePlanDetailListStartD.descripts:'起始时间'}>
                                                        {getFieldDecorator('startDate',{
                                                            initialValue: undefined,
                                                            rules:[{ required : true, message: ChineseEnglishSize.purchasePlanDetailListStartD ? ChineseEnglishSize.purchasePlanDetailListStartD.message:'起始时间不能为空'}]
                                                        })(
                                                            <DatePicker style={{width: '100%'}}   />
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                            </Row>
                                            <Row>
                                                <Col span={12}>
                                                    <FormItem  {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListdeadline ? ChineseEnglishSize.purchasePlanDetailListdeadline.descripts:'截止时间'}>
                                                        {getFieldDecorator('endDate',{
                                                            initialValue: undefined,
                                                            rules:[{ required : true, message: ChineseEnglishSize.purchasePlanDetailListdeadline ? ChineseEnglishSize.purchasePlanDetailListdeadline.message:'截止时间不能为空'}]
                                                        })(
                                                            <DatePicker style={{width: '100%'}}  />
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                                <Col span={12}>
                                                    <Form.Item label={ChineseEnglishSize.purchasePlanDetailListstkSubCatDr ? ChineseEnglishSize.purchasePlanDetailListstkSubCatDr.descripts:'库存子类'} {...formItemLayout}>
                                                        {getFieldDecorator('stkSubCatDr', {
                                                            initialValue:undefined,
                                                            rules: [{ required: false, message: '库存分类不能为空' }]
                                                        })(
                                                            <Select showSearch allowClear placeholder="请选择库存分类" >
                                                                {this.state.SelectData.stkSubCat && this.state.SelectData.stkSubCat.map(item => {
                                                                    return <Option key={item.id} value={item.id} >{item.descripts}</Option>
                                                                })}
                                                            </Select>
                                                        )}
                                                    </Form.Item>
                                                </Col>
                                            </Row>
                                            <Row>
                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListConsumptionLoc ? ChineseEnglishSize.purchasePlanDetailListConsumptionLoc.descripts:'消耗科室'}>
                                                        {getFieldDecorator('useLoc',{
                                                            initialValue: undefined,
                                                            rules:[{ required : ChineseEnglishSize.purchasePlanDetailListConsumptionLoc ? ChineseEnglishSize.purchasePlanDetailListConsumptionLoc.required =='Y'? true:false: false, message: ChineseEnglishSize.purchasePlanDetailListConsumptionLoc ? ChineseEnglishSize.purchasePlanDetailListConsumptionLoc.message:'消耗科室不能为空'}]
                                                        })(
                                                            <Select allowClear={true}>
                                                                {loc}
                                                            </Select>
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListMedicationDays ? ChineseEnglishSize.purchasePlanDetailListMedicationDays.descripts:'用药天数'}>
                                                        {getFieldDecorator('useDays',{
                                                            initialValue: undefined,
                                                            rules:[{ required : ChineseEnglishSize.purchasePlanDetailListMedicationDays ? ChineseEnglishSize.purchasePlanDetailListMedicationDays.required =='Y'? true:false: false, message: ChineseEnglishSize.purchasePlanDetailListMedicationDays ? ChineseEnglishSize.purchasePlanDetailListMedicationDays.message:'用药天数不能为空'}]
                                                        })(
                                                            <Input />
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                            </Row>
                                            <Row>
                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListpurchasingType ? ChineseEnglishSize.purchasePlanDetailListpurchasingType.descripts:'采购方式'}>
                                                        {getFieldDecorator('operTypeDr',{
                                                            initialValue:  undefined,
                                                            rules:[{ required : false, message: '采购方式不能为空'}]
                                                        })(
                                                            <Select showSearch allowClear placeholder="请选择采购方式" >
                                                                {this.state.SelectData.operType && this.state.SelectData.operType.map(item => {
                                                                    return <Option key={item.id} value={item.id} >{item.descripts}</Option>
                                                                })}
                                                            </Select>
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListstkCat ? ChineseEnglishSize.purchasePlanDetailListstkCat.descripts:'库存大类'}>
                                                        {getFieldDecorator('stkCatDr',{
                                                            initialValue: undefined,
                                                            rules:[{ required : false, message: '库存大类不能为空'}]
                                                        })(
                                                            <Select showSearch allowClear placeholder="请选择库存大类" >
                                                                {this.state.SelectData.stkCat && this.state.SelectData.stkCat.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.purchasePlanDetailListTender ? ChineseEnglishSize.purchasePlanDetailListTender.descripts:'招标'}>
                                                        {getFieldDecorator('PBFlag',{
                                                            initialValue: undefined,
                                                            rules:[{ required : false, message: '招标不能为空'}]
                                                        })(
                                                            <Select showSearch allowClear placeholder="请选择库存分类" >
                                                                {this.state.SelectData.PBFlag && this.state.SelectData.PBFlag.map(item => {
                                                                    return <Option key={item.id} value={item.id} >{item.descripts}</Option>
                                                                })}
                                                            </Select>
                                                        ) }
                                                    </FormItem>
                                                </Col> */}
                                                <Col span={12}>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.purchasePlanDetailListbusinessType ? ChineseEnglishSize.purchasePlanDetailListbusinessType.descripts:'业务类型'}>
                                                        {getFieldDecorator('busType',{
                                                            initialValue: undefined,
                                                            rules:[{ required : ChineseEnglishSize.purchasePlanDetailListloc ? ChineseEnglishSize.purchasePlanDetailListloc.required =='Y'? true:false: false, message: ChineseEnglishSize.purchasePlanDetailListloc ? ChineseEnglishSize.purchasePlanDetailListloc.message:'业务类型不能为空'}]
                                                        })(
                                                            <Select
                                                                mode="multiple"
                                                                style={{ width: '100%' }}
                                                            >
                                                                {this.state.SelectData.busType && this.state.SelectData.busType.map((item, i) => {
                                                                    return <Option value={item.id} key={i}>{item.descripts}</Option>
                                                                })}
                                                            </Select>
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                                <Col span={12}>
                                                    <Form.Item {...formItemSwitch2} label={ChineseEnglishSize.purchasePlanDetailListhospitalConsumption ? ChineseEnglishSize.purchasePlanDetailListhospitalConsumption.descripts:'根据全院消耗'}>
                                                        {getFieldDecorator('allConsume',{
                                                            valuePropName: 'checked',
                                                            initialValue: false,
                                                            rules:[{required: false}]
                                                        })(
                                                            <Switch checkedChildren={userInfo.language == 'EN' ? "yes" : "是"} unCheckedChildren={userInfo.language == 'EN' ? "no" : "否"} />
                                                        ) }
                                                    </Form.Item>
                                                </Col>
                                            </Row>
                                         </Form>
                                    </Col>

                                    <Row style={{marginTop: '4px', textAlign: 'center'}} className="buttonLeft">
                                        <Button type="primary" onClick={this.getWarehouseList}>{ChineseEnglishSize.purchasePlanDetailListsave ? ChineseEnglishSize.purchasePlanDetailListsave.descripts:'保存'}</Button>
                                        <Button style={{marginLeft: '90px'}} onClick={this.handleReset}>{ChineseEnglishSize.purchasePlanDetailListReset ? ChineseEnglishSize.purchasePlanDetailListReset.descripts:'重置'}</Button>
                                    </Row>
                            </Col>
                        </Card>
                    </Row>
                </Modal>
            )
        }
    }
);
export default AccordingToConsume;