/*
 * Creator:     ML
 * CreatDate:   2019-09-29
 * descripts：医嘱大类设置--右上基本信息
 * */
import React from 'react';
import moment from 'moment';
import { Form, Row, Col, Input, Select, DatePicker, Button, message } from 'antd';
import { $http } from '../../../containers/config/https';
import '../style/index.less';

const paddRightCol = {
    paddingRight: "0px"
}
const formItemLayoutType = {
    labelCol: { span: 9 },
    wrapperCol: { span: 15 }
}
const FormItem = Form.Item;
const Option = Select.Option;
const BaseInformation = Form.create()(
    class BaseInformation extends React.Component {
        cacheOriginData = {};
        constructor() {
            super()
            this.state = {
                topInfo: {}
            }
        }
        componentDidMount() {
            this.props.OnChildrefs(this);
        }
        //提交保存基本信息
        handleTopOk = () => {
            const { topInfo } = this.state
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    let data = {}
                    //修改
                    if (topInfo && topInfo.orderCatID) {
                        data = {
                            params: [{
                                orderCatID: topInfo.orderCatID || "",
                                orderCatSeqNo: values.bigSqeNo || "",
                                orderCatCode: values.bigCode || "",
                                orderCatDesc: values.bigDec || "",
                                orderType: values.bigType || "",
                                startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : "",
                                stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : "",
                                updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
                            }],

                        }
                    } else {
                        data = {
                            params: [{
                                orderCatSeqNo: values.bigSqeNo,
                                orderCatCode: values.bigCode,
                                orderCatDesc: values.bigDec,
                                orderType: values.bigType,
                                startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : "",
                                stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : "",
                                updateUserID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
                            }],

                        }
                    }

                    $http.post('urlS', this, {
                        code: '05100200',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                if (topInfo && topInfo.orderCatID) {
                                    message.success("修改成功")
                                    //刷新医嘱大类表格数据 若设为失效 左侧默显示生效状态更改为默认显示全部
                                    if ((values.dateFrom && values.dateFrom) && (values.dateTo && values.dateTo)) {
                                        if (moment(values.getDateTo).format('YYYYMMDD') < moment().format('YYYYMMDD')) {
                                            this.props.updataTable("N")
                                            return
                                        }
                                    }
                                    this.props.updataTable("Y")
                                } else {
                                    message.success("添加成功")
                                    this.setState({
                                        canSave:true
                                    })
                                    this.props.canSave(res.orderCatID)
                                    if ((values.dateFrom && values.dateFrom) && (values.dateTo && values.dateTo)) {
                                        if (moment(values.getDateTo).format('YYYYMMDD') < moment().format('YYYYMMDD')) {
                                            this.props.getLast("N")
                                            return
                                        }
                                    }
                                    this.props.getLast("Y",res.orderCatID)
                                    return
                                }
                                this.props.updataTable("Y")
                            }
                        }
                    })
                }
            })
        }
        //取消保存基本信息--回到刚进入时状态信息
        handletTopCancel = () => {
            this.setState({
                canSave:false
            })
            this.initOrig()
        }
        //切换时清空内容
        initData = (topInfo) => {
            this.props.form.resetFields()
            this.setState({
                topInfo: topInfo,
                canSave:false
            })
        }
        //取消时 返回到添加状态
        initOrig = () => {
            this.props.form.resetFields()
            this.setState({
                topInfo: {},
            })
            this.props.initOrigin()
            // this.props.initTabs()
        }

        render() {
            const { getFieldDecorator } = this.props.form;
            let { topInfo } = this.state
            let typeArr = [{
                code: "01",
                descripts: "医生开嘱",
                descriptsSPCode: "YSKZ",
                enDescripts: "",
                id: "D"
            }, {
                code: "02",
                descripts: "护士开嘱",
                descriptsSPCode: "HSKZ",
                enDescripts: "",
                id: "N"
            },
            //  {
            //     code: "03",
            //     descripts: "补录开嘱(费用)",
            //     descriptsSPCode: "BLKZ",
            //     enDescripts: "",
            //     id: "A"
            // },
             ]
            //开医嘱类型
            let typeOp = []
            typeArr && typeArr.map(item => {
                typeOp.push(<Option key={item.id} value={String(item.id)}>{item.descripts}</Option>)
            })
            return (
                <div>
                    <Form>
                        <Row gutter={60}>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='序号'>
                                    {getFieldDecorator('bigSqeNo', {
                                        initialValue: topInfo.orderCatSeqNo && topInfo.orderCatSeqNo ? topInfo.orderCatSeqNo : undefined,
                                        rules: [{ required: true, message: '请输入序号' }],
                                    })(
                                        <Input />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='代码'>
                                    {getFieldDecorator('bigCode', {
                                        initialValue: topInfo.orderCatCode && topInfo.orderCatCode ? topInfo.orderCatCode : undefined,
                                        rules: [{ required: true, message: '请输入代码' }],
                                    })(
                                        <Input />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='描述'>
                                    {getFieldDecorator('bigDec', {
                                        initialValue: topInfo.orderCatDesc && topInfo.orderCatDesc ? topInfo.orderCatDesc : undefined,
                                        rules: [{ required: true, message: '请输入描述' }],
                                    })(
                                        <Input />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem {...formItemLayoutType} label='开医嘱类型'>
                                    {getFieldDecorator('bigType', {
                                        initialValue: topInfo.orderType && topInfo.orderType!="F" ? topInfo.orderType : undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <Select
                                            allowClear
                                            onSelect={this.handleStatus}
                                        >
                                            {typeOp}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row gutter={60}>
                            <Col span={5} style={paddRightCol}>
                                <FormItem label='生效日期' {...formItemLayoutType}>
                                    {getFieldDecorator('dateFrom', {
                                        initialValue: topInfo.startDate ? moment(topInfo.startDate, "YYYY-MM-DD") : moment(),
                                        rules: [{ required: true, message: '请选择生效日期' }],
                                    })(
                                        <DatePicker style={{ width: '100%' }} placeholder={'生效日期'} disabledDate={(curDate) => {
                                            return curDate && curDate > moment().endOf('day');
                                        }} />
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5} style={paddRightCol}>
                                <FormItem label='失效日期' {...formItemLayoutType}>
                                    {getFieldDecorator('dateTo', {
                                        initialValue: topInfo.stopDate ? moment(topInfo.stopDate, "YYYY-MM-DD") : undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <DatePicker style={{ width: '100%' }} placeholder={'失效日期'} disabledDate={(curDate) => {
                                            return curDate < moment(this.props.form.getFieldValue('dateTo')).format('YYYY-MM-DD');
                                        }} />
                                    )}
                                </FormItem>
                            </Col >
                            <Col span={5} style={{ textAlign: "center", }}>
                                <Button key="submitc" type="primary" onClick={this.handletTopCancel}>取消</Button>
                            <Button key="submits" type="primary" disabled={this.state.canSave} onClick={this.handleTopOk}>保存</Button>
                            </Col>
                        </Row>
                        <Row gutter={60}>
                            <Col style={{ color: "#faad14" }}>
                                <span>{topInfo && topInfo.orderCatID ? "点击”取消“，返回到添加医嘱大类" : "请保存基本信息后，添加“接收科室”"}</span>
                            </Col>
                        </Row>
                    </Form>
                </div>
            )
        }
    })
export default BaseInformation