/*
 * Creator:     ML
 * CreatDate:   2019-12-11
 * descripts：  处方规则--新增/编辑弹窗
 * */
import React from 'react';
import moment from 'moment';
import { Form, Row, Col, Input, Select, DatePicker, Button, Modal,message,Checkbox } from 'antd';
import { $http } from '../../../containers/config/https';
import store from '../../../store';
const FormItem = Form.Item;
const Option = Select.Option;
const fromItemLayout = {
    wrapperCol: {span:17} , 
    labelCol: {span:7} 
}
const PrescriptionRulesModal = Form.create()(
    class PrescriptionRulesModal extends React.Component {
        cacheOriginData = {};
        constructor() {
            super()
            this.state = {
                bigTypeArr:[],
                visible:false,
                Info:{}
            }
        }
        componentDidMount() {
            this.getBigClass()
            this.props.onRef(this)
        }
        showModal=(mes,row)=>{
            this.props.form.resetFields()
            this.setState({
                visible:true,
                mes,
                Info:row,
            })
            if(mes){

            }
        }
        handleCancel=()=>{
            this.setState({
                visible:false,
                mes:""
            })
        }
        handleOk=()=>{
            this.props.form.validateFields((err, values) => {
                if(!err){
                    let data = {}
                    data = {
                     params: [{
                        ID:this.state.Info&&this.state.Info.ID?this.state.Info.ID:"",
                        userID:sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID:"",
                        typeCode:values.typeCode || "",
                        typeDesc:values.typeDesc || "",
                        priority:values.priority || "",
                        count:values.count || "",
                        backGround:values.backGround || "",
                        prescNoRule:values.prescNoRule || "",
                        prescTypeName:values.prescTypeName || "",
                        printTemplate:values.printTemplate || "",
                        age:values.age || "",
                        ENDesc:values.ENDesc || "",
                        startDate:this.props.form.getFieldValue('startTime')?moment(this.props.form.getFieldValue('startTime')).format('YYYY-MM-DD'):"",
                        stopDate:this.props.form.getFieldValue('overTime')?moment(this.props.form.getFieldValue('overTime')).format('YYYY-MM-DD'):"",
                     }],
                 } 
                 $http.post('urlS', this, {
                     code: '03050152',
                     data: data,
                     success: function (res) {
                         if (+res.errorCode === 0) {
                            this.setState({
                                visible:false,
                                mes:""
                            })
                            if(this.state.Info&&this.state.Info.ID){
                                message.success("修改成功")
                                this.props.handleFind()
                            }else{
                                message.success("添加成功")
                                this.props.getLast()
                            }
                         }
                     }
                 })
                }
            })
            
        }

//获取大类下拉框
getBigClass=()=>{
            let data = {}
               data = {
                params: [{
                }],
            } 
            $http.post('urlS', this, {
                code: '05100228',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0 &&res.result.Data) {
                        this.setState({
                            bigTypeArr:res.result.Data
                        })
                    }
                }
            })
}

   
        render() {
            const { getFieldDecorator } = this.props.form;
            let { Info} = this.state
              
            return (
                <div>
                    <Form>
                    <Modal 
                      key='2'
                      width='999px'
                      visible={this.state.visible}
                      title={this.state.mes=="1"?"编辑处方规则":"添加处方规则"}
                      onOk={this.handleOk}
                      onCancel={this.handleCancel}
                      footer={[
                        <Button key="submitc" type="primary" onClick={this.handleCancel}>取消</Button>,
                        <Button key="submits" type="primary"  onClick={this.handleOk}>确定</Button>,
                      ]}>
                      <div>
                      <Form>
                        <Row >
                        <Col span={8}>
                                <FormItem label='代码' {...fromItemLayout}>
                                {getFieldDecorator('typeCode', {
                                    initialValue: Info&&Info.typeCode?Info.typeCode:undefined,
                                    rules: [{ required: true, message: '请输入代码' }],
                                })(
                                  <Input  style={{width:"100%"}}/>
                                )}
                                </FormItem>
                               
                        </Col>
                        <Col span={8}>
                                <FormItem label='描述' {...fromItemLayout}>
                                {getFieldDecorator('typeDesc', {
                                    initialValue: Info&&Info.typeDesc?Info.typeDesc:undefined,
                                    rules: [{ required: true, message: '请输入描述' }],
                                })(
                                  <Input  style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='优先级' {...fromItemLayout}>
                                {getFieldDecorator('priority', {
                                    initialValue: Info&&Info.priority?Info.priority:undefined,
                                    rules: [{ required: true, message: '请输入优先级' }],
                                    getValueFromEvent: (event) => {
                                        return event.target.value.replace(/\D/g, '')
                                    },
                                })(
                                  <Input  style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        </Row>
                        <Row>
                        <Col span={8}>
                                <FormItem label='处方数' {...fromItemLayout}>
                                {getFieldDecorator('count', {
                                    initialValue: Info&&Info.count?Info.count:undefined,
                                    rules: [{ required: true, message: '请输入处方数' }],
                                    getValueFromEvent: (event) => {
                                        return event.target.value.replace(/\D/g, '')
                                    },
                                })(
                                  <Input  style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='处方号规则' {...fromItemLayout}>
                                {getFieldDecorator('prescNoRule', {
                                    initialValue: Info&&Info.prescNoRule?Info.prescNoRule:undefined,
                                    rules: [{ required: true, message: '请输入处方号规则' }],
                                })(
                                  <Input />
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='显示名称' {...fromItemLayout}>
                                {getFieldDecorator('prescTypeName', {
                                    initialValue: Info&&Info.prescTypeName?Info.prescTypeName:undefined,
                                    rules: [{ required: true, message: '请输入显示名称' }],
                                })(
                                  <Input style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        </Row>
                        <Row >
                        <Col span={8}>
                                <FormItem label='打印模板' {...fromItemLayout}>
                                {getFieldDecorator('printTemplate', {
                                    initialValue: Info&&Info.printTemplate?Info.printTemplate:undefined,
                                    rules: [{ required: true, message: '请输入打印模板' }],
                                })(
                                  <Input style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='年龄' {...fromItemLayout}>
                                {getFieldDecorator('age', {
                                    initialValue: Info&&Info.age?Info.age:undefined,
                                    rules: [{ required: false, message: '' }],
                                    getValueFromEvent: (event) => {
                                        return event.target.value.replace(/\D/g, '')
                                    },
                                })(
                                  <Input style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='背景颜色' {...fromItemLayout}>
                                {getFieldDecorator('backGround', {
                                    initialValue: Info&&Info.backGround?Info.backGround:undefined,
                                    rules: [{ required: true, message: '请输入背景颜色' }],
                                })(
                                  <Input style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        </Row>
                        <Row>
                        <Col span={8}>
                                <FormItem label='英文描述' {...fromItemLayout}>
                                {getFieldDecorator('ENDesc', {
                                    initialValue:Info&&Info.ENDesc?Info.ENDesc:undefined,
                                    rules: [{ required: false, message: '' }],
                                })(
                                  <Input style={{width:"100%"}}/>
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='生效日期' {...fromItemLayout}>
                                {getFieldDecorator('startTime', {
                                    initialValue:Info&&Info.startTime?Info.startTime:moment(),
                                    rules: [{ required: true, message: '请选择生效日期' }],
                                })(
                                  <DatePicker style={{width:'100%'}}  disabledDate={(curDate) => {
                                    return curDate && curDate > moment().endOf('day');
                                  }} />
                                )}
                                </FormItem>
                        </Col>
                        <Col span={8}>
                                <FormItem label='失效日期' {...fromItemLayout}>
                                {getFieldDecorator('overTime', {
                                    initialValue: Info&&Info.overTime?Info.overTime:undefined,
                                    rules: [{ required: false, message: '' }],
                                })(
                                  <DatePicker style={{width:'100%'}}  disabledDate={(curDate) => {
                                    return curDate < moment(this.props.form.getFieldValue('startTime')).format('YYYY-MM-DD');
                                  }} />
                                )}
                                </FormItem>
                        </Col>
                        </Row>
                        </Form>
                      </div>
                    </Modal>
                    </Form>
                </div>
            )
        }
    })
export default PrescriptionRulesModal
