/**
 * 停医嘱
 */

import React, { Component } from 'react';
import { Form, Select, Modal, DatePicker, Table, Pagination, message } from 'antd';
import { Util, dateFormat, timeFormat, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;
const FormItem = Form.Item;
const { RangePicker } = DatePicker;

class ShowStopAdviceModal extends Component {

    constructor(props) {
        super(props);
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            total: 0,
            page: 1,
            pageSize: 10,
            dataSource: [],
            userData: userData
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.loadStopReason()
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        this.initData()
    }

    initData() {
        this.columns = [
            {
                title: '医嘱开始时间',
                dataIndex: 'ordStartDate',
                width: 120,
                align: 'center',
            }, {
                title: '组',
                dataIndex: 'ordLinkSign',
                width: 50,
                align: 'center',
                render: (text, data) => {
                    return <span style={{ color: 'blue' }}>{data.ordLinkSign}</span>
                }
            }, {
                title: '医嘱内容',
                dataIndex: 'statusDesc',
                width: 200,
                align: 'center',
                render: (text, data, index) => {
                    return <span >
                        <span>{data.ordEMFlag == 'Y' ? '【加急】;' : ''}</span>
                        <span>{data.ordSkinTestFlag == 'Y' ? <span style={{ color: '#F5222D' }}>【{'皮试'}】</span> : ''}</span>
                        <span>{data.ordSkinTestWayDesc ? <span>{data.ordSkinTestWayDesc}</span> : ''}</span>
                        <span>{data.ordNote ? `【${data.ordNote}】;` : ''}</span>
                        <span style={{ color: '#1999db' }}>{data.ordMedItemDesc ? data.ordMedItemDesc : data.ordDesc}</span>
                        <span>{data.ordDurQty ? '【' + data.ordDurQty : ''}{data.ordDurationDesc ? data.ordDurationDesc + '】' : ''}</span>
                        <span>{data.ordQty ? '【' + data.ordQty : ''}{data.ordQtyUomDesc ? data.ordQtyUomDesc + '】' : ''}</span>
                        <span>{data.ordUsageDesc ? '【' + data.ordUsageDesc + '】' : ''}</span>
                        <span>{(data.ordFrequenceCode ? data.ordFrequenceCode :
                            (data.ordFrequenceDesc ? data.ordFrequenceDesc : undefined)) || data.ordFreqDesc}</span>
                        {/* <span>{data.ordDurQty}{data.ordDurationDesc}</span> */}
                        {/* <span>{data.ordRecLocDesc}</span> */}
                    </span>
                }
            }, {
                title: '不能停止原因',
                dataIndex: 'execMessage',
                width: 120,
                align: 'center',
            },
        ]
    }

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

    //清空输入框
    cleanInput() {
        this.setState({
            code: [],
            reasonID: undefined
        })
    }

    changeRegistrTime(e) {
        this.setState({
            stopDate: moment(e).format(dayFormat),
            stopTime: moment(e).format(timeFormat)
        })
    }

    //分页切换
    onChange(e) {
        this.setState({ page: e }, () => {
            this.loadData(e);
        });
    }

    //分页切换
    changePageSize(current, pageSize) {
        this.setState({ pageSize: pageSize, page: current }, () => {
            this.loadData(current, pageSize);
        });
    }

    loadStopReason() {
        let data = {
            params: [{
                language: this.state.userData.language
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060027",
            data: data,
            success: (res) => {
                if (res.errorCode == '0') {
                    var stopReason = res.result;
                    var reasonID;
                    if (!Util.isEmpty(stopReason)) {
                        for (var i = 0; i < stopReason.length; i++) {
                            if (stopReason[i].defaultFlag == 'Y') {
                                reasonID = stopReason[i].id
                                break;
                            }
                        }
                    }
                    this.setState({
                        stopReason: res.result,
                        reasonID: reasonID
                    })
                }
            }
        })
    }

    //提交
    onSubmit() {
        let { onSubmit } = this.props;
        var stopDate = '', stopTime = '';
        var date = new Date();
        if (this.state.stopDate) {
            stopDate = this.state.stopDate;
            stopTime = this.state.stopTime;
        } else {
            stopDate = moment(date).format(dayFormat)
            stopTime = moment(date).format(timeFormat)
        }
        if (Util.isEmpty(this.state.reasonID)) {
            message.error('停医嘱原因不能为空！')
            return
        }
        onSubmit && onSubmit(stopDate, stopTime, this.state.reasonID);
    }

    commonSelectOption(name, e) {
        this.setState({
            [name]: e
        })
    }

    render() {
        var date = new Date();
        var ordStopReasonOption = []
        this.state.stopReason && this.state.stopReason.map((item, index) => {
            ordStopReasonOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        return <div>
            <Modal className='confirm-advice-modal'
                title={'停止医嘱'}
                maskClosable={false}
                visible={this.props.isShow}
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <Table columns={this.columns}
                    dataSource={this.props.modalSource}
                    bordered
                    title={() => {
                        return <div style={{ display: 'flex' }}>
                            <div>指定停止时间：<DatePicker showTime defaultValue={moment(date, dateFormat)}
                                onChange={this.changeRegistrTime.bind(this)} /></div>
                            <div style={{ marginLeft: 5 }}>停医嘱原因：<Select style={{ width: '180px' }} allowClear showSearch
                                placeholder='请选择停医嘱原因' value={this.state.reasonID}
                                onChange={this.commonSelectOption.bind(this, 'reasonID')}>{ordStopReasonOption}</Select></div>
                            <div style={{ marginLeft: '15%' }}>共执行{this.props.modalSource && this.props.modalSource.length || 0}次</div>
                        </div>
                    }}
                    rowKey={(data) => {
                        return data.ordID
                    }}
                    pagination={false}></Table>
                {/* <Pagination
                    size="small"
                    showQuickJumper
                    showSizeChanger
                    defaultCurrent={1}
                    current={this.state.page}
                    total={this.state.total}
                    pageSize={this.state.pageSize}
                    onShowSizeChange={this.changePageSize.bind(this)}
                    onChange={this.onChange.bind(this)}
                    style={{ textAlign: "right", marginTop: '13px' }}
                /> */}
            </Modal></div >
    }
}

export default Form.create()(ShowStopAdviceModal);