/**
 * 
 * 医保、自费确认弹框
 */

import React, { Component } from 'react';
import { Form, Modal, Table, Radio } from 'antd';
import { Util } from 'tools/index';
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,
            dataSource: []
        }
    }

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

    loadData() {
        if (!Util.isEmpty(this.props.insuSource)) {
            var insuSource = this.props.insuSource;
            for (var i = 0; i < insuSource.length; i++) {
                this.state.dataSource.push({
                    ordDesc: insuSource[i].defaultData.ordDesc,
                    insuMessage: insuSource[i].defaultData.insuMessage,
                    noINSUFlag: insuSource[i].defaultData.noINSUFlag,
                })
            }
            this.setState({})
        }
    }

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

    initData() {
        this.columns = [
            {
                title: '医嘱名称',
                dataIndex: 'ordDesc',
                width: 180,
                align: 'center',
            }, {
                title: '确认信息',
                dataIndex: 'insuMessage',
                width: 120,
                align: 'center',
            }, {
                title: '是否医保',
                dataIndex: 'noINSUFlag',
                width: 100,
                align: 'center',
                render: (text, data, index) => {
                    return (
                        <Radio.Group value={data.noINSUFlag ? 'Y' : 'N'} onChange={(e) => this.handleChangeCheckbox('noINSUFlag', data, index, e.target.value)}>
                            <Radio value="Y">医保</Radio>
                            <Radio value="N">自费</Radio>
                        </Radio.Group>
                    )
                    // return <Checkbox checked={data.noINSUFlag} onChange={this.handleChangeCheckbox.bind(this, 'noINSUFlag', data, index)} />
                }
            }
        ]
    }

    handleChangeCheckbox = (name, data, index, e) => {
        this.state.dataSource[index][name] = e === 'Y' ? true : false;
        this.setState({});
    }

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

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

    //提交
    onSubmit() {
        this.closeModal()
    }

    render() {
        return <div>
            <Modal className='confirm-advice-modal'
                title={'医保确认'}
                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={this.state.dataSource}
                        bordered
                        pagination={false}></Table>
                </div>
            </Modal></div>
    }
}

export default Form.create()(ShowConfirmModal);