/*
 * Creator:     ML
 * CreatDate:   2020-02-28
 * descripts：  患者分类配置 添加/编辑
 * */
import React from 'react';
import { Form, Row, Col, Input, Select, Button, DatePicker, Modal, Table, Icon, Pagination, message, Divider, Tooltip, Checkbox,Switch} from 'antd';
import moment from 'moment';
import { $http } from '../../../containers/config/https';

const FormItem = Form.Item;
const paddRightCol = {
    paddingRight: "0px"
}
const formItemLayout = { // label 和input宽度
    labelCol: { span: 9 },
    wrapperCol: { span: 15 },
}
const PatientCatModal = Form.create()(
    class PatientCatModal extends React.Component {
        constructor(props) {
            super(props)
            this.state = {
                visible: false,
                baseParms: {},
                edit: -1,
                params: {},
                id: "",
                defaultFlag:false
            }
        }
        componentDidMount() {
            this.props.OnChildrefs(this);
        }
        //国际化
        setMessage(name, message) {
            return this.props.localeProvider ? this.props.localeProvider[name][message] : ''
        }
        showModal = (record, num) => {
            this.setState({
                visible: true,
            })
            if (record && num && num == "1") {
                let arr = []
                this.setState({
                    edit: num,
                    params: record,
                    id: record.ID,
                    defaultFlag: record.pregnantFlag=="Y"?true:false,
                })
            }
        }
      
        handleCancel = () => {
            this.props.form.resetFields();
            this.setState({ visible: false, params: {}, edit: -1, id: "" });
        }
        //添加/编辑
        handleCreat = () => {
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    let data = {}
                    data = {
                        params: [{
                            updateUserID: JSON.parse(sessionStorage.userData).userID || '',
                            ID: this.state.id && this.state.id ? this.state.id : "",//修改
                            code: values.code || "",
                            descripts: values.descripts || "",
                            eNDesc: values.eNDesc || "",
                            note: values.note || "",
                            ageFrom: values.ageFrom || "",
                            ageTo: values.ageTo || "",
                            weightFrom: values.weightFrom || "",
                            weightTo: values.weightTo || "",
                            pregnantFlag:this.state.defaultFlag == true ? "Y" : "N",
                            startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : undefined,//生效日期
                            stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : undefined,//失效日期
                        }]
                    }
                    $http.post('urlS', this, {
                        code: '03050196',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                if (this.state.edit == "1") {
                                    message.success(this.props.localeProvider && this.props.localeProvider.PatientCatUpdate? this.setMessage('PatientCatUpdate', 'descripts') : '更新成功');
                                    this.props.getNewData();
                                } else {
                                    message.success(this.props.localeProvider && this.props.localeProvider.PatientCatAddS? this.setMessage('PatientCatAddS', 'descripts') : '添加成功');
                                    this.props.getNewData(res.Id);
                                }
                                this.setState({
                                    visible: false,
                                    edit: -1,
                                    params: {},
                                    id: ""
                                })
                                this.props.form.resetFields();
                            }
                        }
                    })
                }
            })
        }
        onChangeSwith = (v) => {
            this.setState({ defaultFlag: v })
        }
        render() {
            const { getFieldDecorator, } = this.props.form;
            const { params } = this.state
            //医院
            let hosOp = []
            this.state.hospArr && this.state.hospArr.map((item) => {
                hosOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })
            //费用类别
            let feeOp = []
            this.state.feeArr && this.state.feeArr.map((item) => {
                feeOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })


            return (
                <div style={{ padding: '4px 2px' }} className="bedDom">
                    <Modal
                        className='findPatients'
                        width='1000px'
                        visible={this.state.visible}
                        title={this.state.edit == "1" ? this.props.localeProvider && this.props.localeProvider.PatientCatEditBasicModal? this.setMessage('PatientCatEditBasicModal', 'descripts') : "编辑患者分类" : this.props.localeProvider && this.props.localeProvider.PatientCatAddBasicModal? this.setMessage('PatientCatAddBasicModal', 'descripts') : "添加患者分类"}
                        onOk={this.handleOk}
                        onCancel={this.handleCancel}
                        footer={[
                            <Button key="submit" type="primary" loading={this.setState.loading} disabled={this.state.rowId == ""} onClick={this.handleCreat}>{this.props.localeProvider && this.props.localeProvider.PatientCatOk? this.setMessage('PatientCatOk', 'descripts') : "确定"}</Button>,
                            <Button key="cancel" type="primary" loading={this.setState.loading} onClick={this.handleCancel}>{this.props.localeProvider && this.props.localeProvider.PatientCatCancel? this.setMessage('PatientCatCancel', 'descripts') : "取消"}</Button>,
                        ]}>
                        <div className="department-header">
                            <Form>
                                <Row gutter={60} style={{ margin: '0' }}>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem {...formItemLayout} label={this.props.localeProvider && this.props.localeProvider.PatientCatCode? this.setMessage('PatientCatCode', 'descripts') : "代码"}>
                                            {getFieldDecorator('code', {
                                                initialValue: params && params.code ? String(params.code) : undefined,
                                                rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.PatientCatCode? this.setMessage('PatientCatCode', 'message') : "请输入代码" }],
                                            })(
                                               <Input></Input>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem {...formItemLayout} label={this.props.localeProvider && this.props.localeProvider.PatientCatDescripts? this.setMessage('PatientCatDescripts', 'descripts') : "描述" }>
                                            {getFieldDecorator('descripts', {
                                                initialValue: params && params.descripts ? String(params.descripts) : undefined,
                                                rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.PatientCatDescripts? this.setMessage('PatientCatDescripts', 'message') : "请输入描述" }],
                                            })(
                                                <Input></Input>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatEnglishDescription? this.setMessage('PatientCatEnglishDescription', 'descripts') : "英文描述" } {...formItemLayout}>
                                            {getFieldDecorator('eNDesc', {
                                                initialValue: params && params.eNDesc ? params.eNDesc : undefined,
                                                rules: [{ required: false, message: '' }],
                                                
                                            })(
                                                <Input allowClear />
                                            )}
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row gutter={60} style={{ margin: '0' }}>
                                <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatNote? this.setMessage('PatientCatNote', 'descripts') : "备注" } {...formItemLayout}>
                                            {getFieldDecorator('note', {
                                                initialValue: params && params.note ? params.note : undefined,
                                                rules: [{ required: false, message: '' }],
                                                
                                            })(
                                                <Input allowClear />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatNote? this.setMessage('PatientCatNote', 'descripts') : "开始年龄" } {...formItemLayout}>
                                            {getFieldDecorator('ageFrom', {
                                                initialValue: params && params.ageFrom ? params.ageFrom : undefined,
                                                rules: [{ required: false, message: '' }],
                                                
                                            })(
                                                <Input allowClear type="number" min={0}/>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatNote? this.setMessage('PatientCatNote', 'descripts') : "结束年龄" } {...formItemLayout}>
                                            {getFieldDecorator('ageTo', {
                                                initialValue: params && params.ageTo ? params.ageTo : undefined,
                                                rules: [{ required: false, message: '' }],
                                                
                                            })(
                                                <Input allowClear type="number" min={0}/>
                                            )}
                                        </FormItem>
                                    </Col>
                                   
                                </Row>
                                <Row gutter={60} style={{ margin: '0' }}>
                                <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatNote? this.setMessage('PatientCatNote', 'descripts') : "开始体重(kg)" } {...formItemLayout}>
                                            {getFieldDecorator('weightFrom', {
                                                initialValue: params && params.weightFrom ? params.weightFrom : undefined,
                                                rules: [{ required: false, message: '' }],
                                                
                                            })(
                                                <Input allowClear type="number" min={0}/>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatNote? this.setMessage('PatientCatNote', 'descripts') : "结束体重(kg)" } {...formItemLayout}>
                                            {getFieldDecorator('weightTo', {
                                                initialValue: params && params.weightTo ? params.weightTo : undefined,
                                                rules: [{ required: false, message: '' }],
                                                
                                            })(
                                                <Input allowClear type="number" min={0}/>
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} className="patNameDom">
                                <FormItem {...formItemLayout} label='孕妇'>
                                    {getFieldDecorator('structAddress', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: '', },
                                        ],
                                    })(
                                        <Switch checkedChildren="是" checked={this.state.defaultFlag} unCheckedChildren="否" onChange={(v) => { this.onChangeSwith(v) }} />
                                    )}
                                </FormItem>
                            </Col>
                                </Row>
                                <Row gutter={60} style={{ margin: '0' }}>
                                  <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatStartData? this.setMessage('PatientCatStartData', 'descripts') : "生效日期" } {...formItemLayout}>
                                            {getFieldDecorator('dateFrom', {
                                                initialValue: params.startDate ? moment(params.startDate, "YYYY-MM-DD") : moment(),
                                                rules: [{ required: true, message: this.props.localeProvider && this.props.localeProvider.PatientCatStartData? this.setMessage('PatientCatStartData', 'message') : '请选择生效日期' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} disabledDate={(curDate) => {
                                                    return curDate && curDate > moment().endOf('day');
                                                }} />
                                            )}
                                        </FormItem>
                                    </Col>
                                    <Col span={8} style={paddRightCol}>
                                        <FormItem label={this.props.localeProvider && this.props.localeProvider.PatientCatStopData? this.setMessage('PatientCatStopData', 'descripts') : "失效日期" } {...formItemLayout}>
                                            {getFieldDecorator('dateTo', {
                                                initialValue: params.stopDate ? moment(params.stopDate, "YYYY-MM-DD") : undefined,
                                                rules: [{ required: false, message: '' }],
                                            })(
                                                <DatePicker style={{ width: '100%' }} />
                                            )}
                                        </FormItem>
                                    </Col>`
                                </Row>
                               
                            </Form>
                        </div>
                    </Modal>
                </div>
            )
        }
    })
export default PatientCatModal    