/** 
 * 添加或编辑备忘录
*/
import React from 'react'
import { Row, Col, Form, Input, Select, Modal, Switch } from 'antd';
import { $http } from 'containers/config/https';
import './CSS/bedchart.less';
import { Util } from 'tools/index';
const { Option } = Select;
const { TextArea } = Input;
const FormItem = Form.Item;
let user;

class AddMemorandum extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            patList: [],
            admID: "",
            bedID: "",
            editFlag: false,
        }
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    }

    componentDidMount() {
        this.props.onRef(this);
    }

    componentWillUnmount() {
        this.setState = (state, callback) => {
            return
        }
    }

    //加载患者列表
    loadPatList = () => {
        var params = {
            params: [{
                locID: this.props.locID
            }]
        }
        if (this.props.componentType == "Delivery") {
            params.params[0].regStatus = "REG";
            params.params[0].wardID = this.props.locID;
        }
        let code = this.props.componentType == "Delivery" ? "04110015" : "04020014"
        $http.post('urlDeault', this, {
            code: code,
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    if (this.props.componentType == "Delivery") {
                        let patList = []
                        res.result.forEach((element, index) => {
                            let patInfo = {
                                key: index,
                                admDate: element.admDate,
                                patName: element.patName,
                                admID: element.admID,
                                bedID: element.admHBBedID,
                                bedCode: element.admBedCode,
                            }
                            patList.push(patInfo)
                        })
                        this.setState({
                            patList: patList
                        })
                    } else {
                        this.setState({
                            patList: res.result
                        })
                    }
                }
            }
        });
    }

    //加载备忘信息
    loadMemorandumInfo = () => {

    }

    //新增
    onSubmit = () => {
        this.props.form.validateFields((err, values) => {
            if (!err) {
                let data = {
                    "params": [{
                        locID: this.props.locID,                //科室ID                 
                        admID: values.patInfo || '',                //就诊ID 
                        title: values.title || '',              //标题     
                        descripts: values.descripts || '',      //备忘内容
                        days: values.days || '',                //起始座位号  
                        hours: values.hours || '',              //座位号前缀                              
                        ownFlag: values.ownFlag ? "Y" : "N",    //仅个人显示标记  
                        userID: user.userID,
                    }]
                };
                $http.post('urlADS', this, {
                    code: "04020015",
                    data: data,
                    success: function (res) {
                        let errorCode = res.errorCode;
                        if (errorCode === '0') {
                            this.closeModal();
                            this.props && 'getMemoList' in this.props && this.props.getMemoList ? this.props?.getMemoList() : this.props.handelThis.getMemorandumInfo(this.props.admID, this.props.locID);
                        }
                    }
                });
            }
        });
    }

    //编辑提交
    onEditData = () => {
        this.props.form.validateFields((err, values) => {
            if (!err) {
                let data = {
                    params: [{
                        locID: this.props.locID,                //科室ID                 
                        admID: values.patInfo || '',                //就诊ID 
                        title: values.title || '',              //标题     
                        descripts: values.descripts || '',      //备忘内容
                        days: values.days || '',                //起始座位号  
                        hours: values.hours || '',              //座位号前缀                              
                        ownFlag: values.ownFlag ? "Y" : "N",    //仅个人显示标记  
                        userID: user.userID,
                        id: this.props.record.id,
                    }]
                };
                $http.post('urlADS', this, {
                    code: "04020015",
                    data: data,
                    success: function (res) {
                        let errorCode = res.errorCode;
                        if (errorCode === '0') {
                            this.closeModal();
                            this.props && 'getMemoList' in this.props && this.props.getMemoList ? this.props?.getMemoList() : this.props.handelThis.getMemorandumInfo(this.props.admID, this.props.locID);
                        }
                    }
                });
            }
        });
    }

    //新增
    showModal = () => {
        this.setState({
            visible: true,
        }, () => {
            this.loadPatList();
        })
    }

    //编辑
    editModal = () => {
        this.setState({
            visible: true,
            editFlag: true,
        }, () => {
            this.loadPatList();
        })
    }

    //关闭
    closeModal = () => {
        this.setState({
            visible: false,
            editFlag: false,
        })
    }

    ///校验数据
    checkData = (rule, value, callback) => {
        //与表单数据进行关联
        var regex = /^[1-9]\d*$/;
        if (!value) {
            // 当value值不存在的话
            callback('数据不能为空');
        } else {
            if (!regex.test(value)) {
                callback("只允许填写正整数");
            }
        }
        callback();
    }

    render() {
        let recordObj = this.props.record || {};
        if (!this.state.editFlag) {
            recordObj = {};
        }
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: { span: 24 },
                sm: { span: 6 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 14 },
            },
        };
        let formItemDayHours = {
            labelCol: { span: 1 },
            wrapperCol: { span: 23 }
        };
        let patItem = []
        this.state.patList.map((item, index) => {
            patItem.push(
                <Option className="DoctorAdviceFind-optionLeft" key={item.admID} value={item.admID}>{item.bedCode}&nbsp;&nbsp;&nbsp;&nbsp;{item.patName}&nbsp;&nbsp;&nbsp;&nbsp;{item.admDate}</Option>
            )
        })
        let admID = [];
        if (!Util.isEmpty(this.props.admID)) { admID.push(this.props.admID) }
        return (
            <Modal
                className="memorandum"
                title="备忘录"
                visible={this.state.visible}
                width="608px"
                onCancel={this.closeModal}
                onOk={this.state.editFlag ? this.onEditData : this.onSubmit}
                destroyOnClose
                maskClosable={false}
            >
                <Form layout="horizontal" {...formItemLayout}>
                    {/* 第一行 */}
                    <FormItem label="患者:">
                        {getFieldDecorator('patInfo', {
                            initialValue: recordObj.admID || admID,
                        })(
                            <Select
                                mode="multiple"
                                allowClear={true}
                                placeholder="请选择患者" >
                                {patItem}
                            </Select>
                        )}
                    </FormItem>
                    {/* 第二行 */}
                    <FormItem label="标题:">
                        {getFieldDecorator('title', {
                            initialValue: recordObj.title || '',
                            rules: [{ required: true, message: '请填写备忘标题,不超过15个字' }]
                        })(
                            <Input maxLength={15} />
                        )}
                    </FormItem>
                    {/* 第三行 */}
                    <FormItem label="备忘信息:" >
                        {getFieldDecorator('descripts', {
                            initialValue: recordObj.descripts || '',
                        })(
                            <TextArea
                                maxLength={200}
                                placeholder="备忘内容不超过200个字"
                                autosize={{ minRows: 4, maxRows: 10 }}
                                style={{
                                    height: '150',
                                }} />
                        )}
                    </FormItem>
                    {/* 第四行 */}
                    <Row style={{ lineHeight: '40px', verticalAlign: 'middle' }}>
                        <Col style={{ textAlign: 'right' }} span={6}>提醒时长:</Col>
                        <Col span={2} >
                            <FormItem label=""  {...formItemDayHours} >
                                {getFieldDecorator('days', {
                                    initialValue: recordObj.days || '',
                                })(
                                    <Input />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={1}>
                            天
                        </Col>
                        <Col span={2}  >
                            <FormItem label=""  {...formItemDayHours} >
                                {getFieldDecorator('hours', {
                                    initialValue: recordObj.time || '',
                                })(
                                    <Input />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={2}>
                            小时
                        </Col>
                        <Col span={7} style={{ color: 'red' }}>
                            剩余:{recordObj.leaveDays || ''}{recordObj.leaveDays ? "天" : ""}{recordObj.leaveTime || ''}
                        </Col>
                    </Row>
                    {/* 第五行 */}
                    <FormItem label="仅自己可见:">
                        {getFieldDecorator('ownFlag', {
                            valuePropName: 'checked',
                            initialValue: recordObj.ownFlag === "Y",
                        })(
                            <Switch checkedChildren="是" unCheckedChildren="否" className='input' />
                        )}
                    </FormItem>
                    <Col style={{ color: 'red', ineHeight: '40px', textAlign: 'center' }}>
                        注意:提醒时长为空时,默认提醒时长为24小时。
                    </Col>
                </Form>
            </Modal>
        )
    }
};

const AddMemorandumFrom = Form.create()(AddMemorandum);
export default AddMemorandumFrom;