/*
* 打印
* */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button, Card, Row, Col, message, DatePicker, Checkbox, TimePicker } from 'antd';
import { Util, dayFormat, hourFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import '../courierStyle/index.less';
import moment from 'moment';

const Option = Select.Option;


class ShowPrintListModal extends Component {

    constructor(props) {
        var date = new Date()
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        super(props);
        this.state = {
            userData: userData,
            startDate: moment(date).format(dayFormat),
            startTime: moment(date).format(hourFormat),
            endTime: moment(date).format(hourFormat),
            recipeList: false,
            printList: true,
            label: false,
            EMR: false,
            distr: false
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                adressMsg: nextProp.adressMsg
            }, () => {
                this.loadCounrierOrder()
                this.loadPayWay()
                this.loadLocalMessage()
                this.getRain()
            })
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    loadLocalMessage() {
        let data = {
            params: [{
                // compontName: "QueryCourierOrderPage",
                type: "C",
                reactCode: ['ShowPrintListModal']
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (res.errorCode == '0') {
                    this.setState({
                        localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN
                    })
                }
            }
        })

    }

    //国际化
    setMessage(name, message) {
        return this.state.localeProvider && this.state.localeProvider[name] ? this.state.localeProvider[name][message] : ''
    }

    loadPayWay() {
        var params = {
            params: [{
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03030034",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        payWay: res.result,
                        payWayID: !Util.isEmpty(res.result) ? res.result[0].id : ''
                    })
                }
            }
        });
    }

    loadCounrierOrder() {
        var params = {
            params: [{
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03030035",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        counrierOrder: res.result,
                        expressServiceID: !Util.isEmpty(res.result) ? res.result[0].id : ''
                    })
                }
            }
        });
    }

    closeModal() {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.setState({
            expressID: '',
            expressPayID: '',
            printList: true,
            sendStartDate: '',
            sendStartTime: '',
            expressType: '',
            tempRange: '',
            recipeList: false,
            label: false,
            EMR: false,
            distr: false
        })
    }

    changeCheckBox(name, e) {
        this.setState({
            [name]: e.target.checked
        })
    }

    changeStartDate(name, e) {
        this.setState({
            [name]: e ? moment(e).format(dayFormat) : ''
        })
    }

    changeStartTime(name, e) {
        this.setState({
            [name]: e ? moment(e).format(hourFormat) : ''
        })
    }

    commonInputChange(name, e) {
        var value = e.target.value
        var reg = /[^0-9]/g
        if (reg.test(value)) {
            // message.error('只允许输入正整数')
            return
        }
        this.setState({
            [name]: e.target.value
        })
    }

    onSubmit() {
        var data = {
            expressID: this.state.expressServiceID,
            expressPayID: this.state.payWayID,
            printList: this.state.printList,
            sendStartDate: this.state.startDate,
            sendStartTime: this.state.startTime,
            expressType: this.state.expressType,
            tempRangeID: this.state.tempRange,
            recipeList: this.state.recipeList,
            parcelQuantity: this.state.parcelQuantity,
            label: this.state.label,
            EMR: this.state.EMR,
            distr: this.state.distr,
        }
        var { onSubmit } = this.props;
        onSubmit && onSubmit(data)
    }
    //获取温控条件下拉框
    getRain = () => {
        let data = {
            params: [{}],
        }
        $http.post('urlS', this, {
            code: '03010112',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        rainData: res.result
                    })
                } else {

                }
            }
        })
    }
    render() {
        var date = new Date()
        //快递服务
        let counrierOrderOption = []
        this.state.counrierOrder && this.state.counrierOrder.map((item, index) => {
            counrierOrderOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })

        //付款方式
        let payWayOption = []
        this.state.payWay && this.state.payWay.map((item, index) => {
            payWayOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        //获取温控条件
        let rainOp = []
        this.state.rainData && this.state.rainData.map(item => {
            rainOp.push(<Option key={item.id} title={item.descripts} role={item.descripts + '^' + item.descriptsSPCode} value={String(item.id)}>{item.descripts}</Option>)
        })
        return <div>
            <Modal className='courier-modal'
                title={this.setMessage('ShowPrintListModalLogistics', 'descripts')}
                maskClosable={false}
                visible={this.props.isShow}
                onOk={this.onSubmit.bind(this)}
                onCancel={this.closeModal.bind(this)}>
                <div>
                    <Card>
                        <div>
                            <span>{this.setMessage('ShowPrintListModalReceiving', 'descripts')}<span>{!Util.isEmpty(this.state.adressMsg) ? this.state.adressMsg.receiveName : '--'}</span>&nbsp;&nbsp;
                                <span>{!Util.isEmpty(this.state.adressMsg) ? this.state.adressMsg.receiveMobile : '--'}</span>
                            </span>
                        </div>
                        <div style={{ marginTop: 5 }}>{!Util.isEmpty(this.state.adressMsg) ? this.state.adressMsg.receiveAddress : '--'}</div>
                    </Card>

                    <Card style={{ marginTop: 5 }}>
                        <Row>
                            <Col span={10}>
                                {this.setMessage('ShowPrintListModalcompany', 'descripts')}
                                <Select placeholder={this.setMessage('ShowPrintListModalPlease', 'descripts')} style={{ width: '70%' }} value={this.state.expressServiceID}
                                    onChange={Util.handleSelectChange.bind(this, 'expressServiceID')}>{counrierOrderOption}</Select>
                            </Col>
                            <Col span={10}>
                                {this.setMessage('ShowPrintListModalPayWay', 'descripts')}
                                <Select placeholder={this.setMessage('ShowPrintListModalmethod', 'descripts')} style={{ width: '70%' }} value={this.state.payWayID}
                                    onChange={Util.handleSelectChange.bind(this, 'payWayID')}>{payWayOption}</Select>
                            </Col>
                            <Col span={10} style={{ marginTop: 5 }}>
                                {this.setMessage('ShowPrintListModalCategory', 'descripts')}
                                <Select placeholder={this.setMessage('ShowPrintListModalproduct', 'descripts')} style={{ width: '70%' }}></Select>
                            </Col>
                            <Col span={this.state.userData && this.state.userData.language == 'EN' ? 12 : 10} style={{ marginTop: 5 }}>
                                {this.setMessage('ShowPrintListModalconditions', 'descripts')}
                                {/* <Input placeholder={this.setMessage('ShowPrintListModalconditionss', 'descripts')} style={{ width: '70%' }} value={this.state.tempRange}
                                    onChange={Util.commonInputChange.bind(this, 'tempRange')} /> */}
                                <Select placeholder={this.setMessage('ShowPrintListModalconditions', 'descripts')} 
                                style={{ width: '70%' }} value={this.state.tempRange} allowClear
                                    onChange={Util.handleSelectChange.bind(this, 'tempRange')}>{rainOp}</Select>
                            </Col>
                            <Col span={10} style={{ marginTop: 5 }}>
                                {this.setMessage('ShowPrintListModalpackages', 'descripts')}
                                <Input placeholder={this.setMessage('ShowPrintListModalnumber', 'descripts')} style={{ width: '70%' }} value={this.state.parcelQuantity}
                                    onChange={this.commonInputChange.bind(this, 'parcelQuantity')} />
                            </Col>
                            <Col span={20} style={{ marginTop: 5 }}>
                                {this.setMessage('ShowPrintListModalpick', 'descripts')}
                                <DatePicker value={this.state.startDate ? moment(this.state.startDate, dayFormat) : ''} format={dayFormat}
                                    onChange={this.changeStartDate.bind(this, 'startDate')} />
                                <TimePicker style={{ marginLeft: 5 }} value={this.state.startTime ? moment(this.state.startTime, hourFormat) : ''} format={hourFormat}
                                    onChange={this.changeStartTime.bind(this, 'startTime')} />
                                {/* <TimePicker style={{ marginLeft: 5 }} value={this.state.endTime ? moment(this.state.endTime, hourFormat) : ''} format={hourFormat}
                                    onChange={this.changeStartTime.bind(this, 'endTime')} /> */}
                            </Col>
                        </Row>
                    </Card>
                    <Card style={{ marginTop: 5 }}>
                        <Row>
                            <div><Checkbox disabled={true} /><span style={{ marginLeft: 5 }}>{this.setMessage('ShowPrintListModalInvoice', 'descripts')}</span></div>
                            <div style={{ marginTop: 5 }}><Checkbox checked={this.state.recipeList}
                                onChange={this.changeCheckBox.bind(this, 'recipeList')} /><span style={{ marginLeft: 5 }}>{this.setMessage('ShowPrintListModalprescription', 'descripts')}</span></div>
                            <div style={{ marginTop: 5 }}><Checkbox checked={this.state.printList}
                                onChange={this.changeCheckBox.bind(this, 'printList')} /><span style={{ marginLeft: 5 }}>{this.setMessage('ShowPrintListModaldelivery', 'descripts')}</span></div>
                            <div style={{ marginTop: 5 }}><Checkbox checked={this.state.label}
                                onChange={this.changeCheckBox.bind(this, 'label')} /><span style={{ marginLeft: 5 }}>{this.setMessage('ShowPrintListModallabel', 'descripts')}</span></div>
                            <div style={{ marginTop: 5 }}><Checkbox checked={this.state.EMR}
                                onChange={this.changeCheckBox.bind(this, 'EMR')} /><span style={{ marginLeft: 5 }}>打印病历</span></div>
                            <div style={{ marginTop: 5 }}><Checkbox checked={this.state.distr}
                                onChange={this.changeCheckBox.bind(this, 'distr')} /><span style={{ marginLeft: 5 }}>打印诊断证明</span></div>
                        </Row>
                    </Card>
                </div>
            </Modal></div>
    }
}

export default Form.create()(ShowPrintListModal);