/**
 * 
 * 确认医嘱错误
 * 信息弹框
 */

import React, { Component } from 'react';
import { Form, Modal, Table, message } from 'antd';
import { Util } from 'tools/index';
import { $http } from 'containers/config/https';
import '../style/index.less';

class ShowConfirmModal extends Component {

    constructor(props) {
        super(props);
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        this.state = {
            code: [],
            confirmData: [],
            errorData: [],
            userData: userData
        }
    }

    static getDerivedStateFromProps(nextProp, nextState) {
        const { confirmData, errorData, code } = nextProp;
        // 当传入的type发生变化的时候，更新state
        if ((confirmData !== nextState.confirmData) || errorData !== nextState.errorData) {

            if (!Util.isEmpty(confirmData)) {
                for (var i = 0; i < confirmData.length; i++) {
                    code.push(confirmData[i].medID + '&&' + confirmData[i].indexId)
                }
            }
            return {
                confirmData,
                errorData,
                code
            };
        }
        // 否则，对于state不进行任何操作
        return null;
    }

    componentDidMount() {
        this.initData()
        this.loadCommonModal()
    }

    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }

    loadCommonModal() {
        let data = {
            params: [{
                compontName: "ApplicationForm",
                type: "C",
                reactCode: ['ApplicationForm']
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN
                    }, () => {
                        this.initData()
                    })
                }
            }
        })
    }

    //国际化
    setMessage(name, message) {
        return this.state.localeProvider && this.state.localeProvider[name] ? this.state.localeProvider[name][message] : ''
    }

    initData() {
        this.columns = [
            {
                title: this.setMessage('ApplicationFormrtr', 'descripts'),
                dataIndex: 'ordDesc',
                width: 180,
                align: 'left',
            }, {
                title: this.setMessage('ApplicationFormbhtt', 'descripts'),
                dataIndex: 'errorMessage',
                width: 120,
                align: 'left',
            },
        ]
    }

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

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

    changeRegistrTime(date, dateStr) {
        this.setState({
            admDateFrom: dateStr[0],
            admDateTo: dateStr[1]
        })
    }

    onSelectChange(code, data) {
        this.setState({
            code,
            data
        })
    }

    //提交
    onSubmit() {
        var { code } = this.state;
        if (Util.isEmpty(this.state.code)) {
            message.error(this.setMessage('ApplicationFormfg', 'descripts'))
            return
        }
        var source = [];
        if (!Util.isEmpty(this.props.adviceDrData)) {
            var adviceDrData = this.props.adviceDrData;
            for (var i = 0; i < adviceDrData.length; i++) {
                for (var j = 0; j < code.length; j++) {
                    if (adviceDrData[i].defaultData.insuFlag == 'Y' && code[j] == (adviceDrData[i].defaultData.medID + '&&' + adviceDrData[i].indexId)) {
                        source.push(adviceDrData[i])
                    }
                }
            }
        }
        let { onSubmit } = this.props;
        this.cleanInput();
        onSubmit && onSubmit(this.getSelectedRowData(), source);
    }

    // 获取当前选中的列表数据
    getSelectedRowData = () => {
        let { code, confirmData } = this.state;
        let selectedRowData = [];
        for (var i = 0; i < confirmData.length; i++) {
            if (code.indexOf(confirmData[i].medID + '&&' + confirmData[i].indexId) !== -1) {
                selectedRowData.push(confirmData[i])
            }
        }
        return selectedRowData;
    }

    render() {
        var { code, confirmData, errorData } = this.state;
        const rowSelection = {
            code,
            onChange: this.onSelectChange.bind(this),
            selectedRowKeys: code,
            getCheckboxProps: record => ({
                disabled: record.disabled === 'Y',
            }),
        }
        //讲确认弹框与错误弹框数据合并
        var dataSource = []
        if (confirmData || errorData) {
            dataSource = confirmData.concat(errorData)
        }

        return <div>
            <Modal className='confirm-advice-modal'
                title={this.setMessage('ApplicationFormbfgrt', 'descripts')}
                maskClosable={false}
                visible={this.props.isShow}
                okText={this.state.userData.language == 'EN' ? 'Submit' : '确定'}
                cancelText={this.state.userData.language == 'EN' ? 'Cancel' : '取消'}
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <div>
                    <Table columns={this.columns}
                        dataSource={dataSource}
                        rowSelection={rowSelection}
                        rowKey={(data) => {
                            return data.medID + '&&' + data.indexId
                        }}
                        pagination={false}></Table>
                </div>
            </Modal></div>
    }
}

export default Form.create()(ShowConfirmModal);