/**
 * 公共组件弹框
 */
import React from 'react';
import { Modal, Table, Checkbox, Input, Select } from 'antd';
import { Util, } from 'tools/index';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import store from 'store';
import '../style/index.less';

const Option = Select.Option;

class ShowPatientListModal extends React.Component {

    constructor(props) {
        super(props);
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        this.state = {
            userData: userData,
            size: 'small',
            checked: true,
            pageSize: 10,
            page: 1
        }
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.loadColumn()
            this.loadData(1)
            this.loadBedData()
        } else if (this.props.isShow === false) {

        }
    }

    componentDidMount(){
        this.props.onRef(this)
        this.loadColumn()
        this.loadData(1)
        this.loadBedData()
    }

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

    showModal() {
        this.setState({
            isShow: true
        })
    }

    loadBedData() {
        var params = {
            params: [{
                locID: this.state.userData.locID,
                admStatusCode: this.state.admStatusCode,
                wardID: this.state.wardID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060005",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        bedData: res.data
                    })
                }
            }
        });
    }

    loadData(page, pageSize, e) {
        var params = {
            params: [{
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
                admStatusCode: this.state.admStatusCode,
                wardID: this.state.wardID,
                medGroupID: this.state.medGroupID,
                levelCode: this.state.checked ? 'SELF' : 'BX',
                conditionCode: this.state.conditionCode,
                patMessage: this.state.patMessage,
                bedID: this.state.bedID,
                medicalNo: this.state.medicalNo,
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: page
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060004",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.result.data,
                        admStatusData: res.result.admStatusData,
                        wardData: res.result.wardData,
                        medGroupData: res.result.medGroupData,
                        levelData: res.result.levelData,
                        conditionData: res.result.conditionData,
                        total: res.result.totalCount
                    })
                }
            }
        });
    }

    loadColumn() {
        var params = {
            params: [{
                compontName: 'hospitalListModal',
                type: 'C'
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    var columns = res.result.C;
                    var dynColumns = []
                    for (var i = 0; i < columns.length; i++) {
                        dynColumns.push({
                            title: columns[i].title,
                            dataIndex: columns[i].dataIndex,
                            width: parseInt(columns[i].width),
                            align: columns[i].align || 'center',
                        })
                    }
                    this.setState({
                        column: dynColumns,
                    })
                    this.props.hanlderThis.setState({
                        reload: false,
                    })
                }
            }
        });
    }


    closeModal(name) {
        // let { onHide } = this.props;
        // onHide && onHide();
        this.setState({
            isShow: false
        })
    }
    //分页切换
    onChange(e) {
        this.setState({ page: e }, () => {
            this.loadData(e);
        });
    }

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

    changeInputMsg(name, e) {
        this.setState({
            [name]: e.target.value
        })
    }

    commonCheckbox(name, e) {
        this.setState({
            [name]: e.target.checked
        }, () => {
            this.loadData(1)
        })
    }

    keyDownMsg(e) {
        if (e.keyCode == '13') {
            this.loadData(1)
        }
    }

    onColumnRef = (ref) => {
        this.columnRef = ref;
    }

    //点击行，渲染当前行样式
    onClickRow = (record) => {
        return {
            onDoubleClick: () => {
                var patientData = {}
                patientData = {
                    admID: record.admID,
                    patID: record.patID,
                }
                sessionStorage.setItem('patientData', JSON.stringify(patientData))
                this.closeModal()
                var { loadTitleData } = this.props;
                loadTitleData && loadTitleData(record)
            }
        };
    }

    //点击行，渲染当前行样式 
    setRowClassName = (record) => {
        return record.admID === this.state.admID ? 'clickRowStyle' : '';
    }

    changeBedSelect(name, e) {
        this.setState({
            [name]: e
        }, () => {
            this.loadData(1)
        })
    }

    render() {
        var { bedData } = this.state;
        var bedDataOption = []
        bedData && bedData.map((item, index) => {
            bedDataOption.push(<Option key={index} value={item.bedID}>{item.bedNo}</Option>)
        })
        return <div>
            <ColumnAuthoritysModel
                clientWidth='800px'
                compontName='hospitalListModal'
                configType="C"
                onRef={this.onColumnRef}
            />
            <Modal className='hospital-component-modal'
                title={'住院病人列表'}
                maskClosable={false}
                visible={this.state.isShow}
                onCancel={this.closeModal.bind(this)}
                footer={null}>
                <Table
                    style={{ width: '100%', "padding": "5px" }}
                    dataSource={this.state.dataSource}
                    className='patient-component-table'
                    columns={this.state.column}
                    bordered
                    pagination={false}
                    onRow={this.onClickRow}
                    rowClassName={this.setRowClassName}
                    onHeaderRow={column => {
                        return {
                            onDoubleClick: () => {
                                this.columnRef.onOpenModel();
                            }
                        };
                    }}
                    title={() => {
                        return <div>
                            人员标识：<Input style={{ width: 200 }} placeholder='患者id/姓名/身份证号/手机号码'
                                onChange={this.changeInputMsg.bind(this, 'patMessage')}
                                onKeyDown={this.keyDownMsg.bind(this)} />
                            <span style={{ marginLeft: 10 }}>床号：<Select style={{ width: 160 }} placeholder='选择床号'
                                allowClear showSearch
                                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                onChange={this.changeBedSelect.bind(this, 'bedID')}>
                                {bedDataOption}</Select></span>
                            <Checkbox checked={this.state.checked} style={{ marginLeft: 10 }}
                                onChange={this.commonCheckbox.bind(this, 'checked')} />我的患者
                        </div>
                    }}
                    scroll={{ y: store.getState().tableHeight.y - 60, x: this.state.tableWidth }}
                />
                <CommonPagination
                    size='small'
                    current={this.state.page}
                    total={this.state.total}
                    search='search'
                    onShowSizeChange={this.changePageSize.bind(this)}
                    complete={true}
                    dataSource={this.state.dataSource}
                    onChange={this.onChange.bind(this)}
                />
            </Modal></div>
    }
}

export default ShowPatientListModal;