

/**
 * 住院证列表
 */

import React from 'react'
import {
    Card, Col, Icon, Table, Row, Checkbox, Input, Divider, Popconfirm, message, Button
} from 'antd';
import store from '../../../store';
import { Util, dayFormat } from 'tools/index';
import ColumnAuthoritysModel from '../../columnAuthority/ColumnAuthoritysModel'
import { $http } from 'containers/config/https';
import '../style/index.less';
import { $loadTemplate } from '../../lodop/Common/Print.js';

var userData;
var recordData;
class HospitalizationList extends React.Component {
    constructor(props) {
        userData = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        recordData = JSON.parse(sessionStorage.getItem('patientData'));
        if (!recordData) {
            recordData = JSON.parse(sessionStorage.getItem('recordData'));
        }
        super(props);
        this.state = {
            column: [],
            clickRow: {},
            tableData: [],
            EN: {},
            CN: {},
            editAdmin:{},
            tableWidth:0,
        }
        this.columnOperation = [
            {
                title: this.display('operation'),
                dataIndex: 'operation',
                align: "center",
                width: 130,
                render: (text, record) => (
                    <span>
                        {(record.editFlag === "Y") && (!this.state.editAdmin.id) ? <span className='span' onClick={(e) => this.onEditAdmin(record,e)}>{this.display('edit')}</span> :
                            (<span style={{ color: '#cccccc' }}>{this.display('edit')}</span>)}
                        <Divider type="vertical" />
                        {
                            (record.editFlag === "Y") && (!this.state.editAdmin.id) ? (

                                <Popconfirm title={this.display('certain')} onConfirm={() => this.onCancelAdmin(record)} >
                                    <span className='span'>{this.display('void')}</span>
                                </Popconfirm>
                            ) : <span style={{ color: '#cccccc' }}>{this.display('void')}</span>
                        }

                    </span>
                ),
            }
        ]
    }
    componentDidMount() {
        this.props.onRef(this);
        this.loadColumn();
        this.onloadPatAdminData();
    }
    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }
    componentWillReceiveProps(nextProps) {
    }
    onloadPatAdminData = () => {
        let data = {
            params: [{
                admID: recordData.admID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050050",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        tableData: res.result,
                        editAdmin:{},
                    })
                }
            }
        })
    }
    //编辑住院证
    onEditAdmin = (record,e) => {
        if (e) { e.stopPropagation(); }
        this.setState({
            editAdmin:record
        },()=>{
            this.props.addchild.resetData(record);
            this.props.addchild.loadBedList(record.wardID);
        })
    }
    //作废住院证
    onCancelAdmin = (record, e) => {
        let data = {
            params: [{
                id: record.id,
                userID: userData.userID,
                status: "C",
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050051",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    message.success(this.display('abolished'));
                    this.onloadPatAdminData();
                }
            }
        })
    }
    //查询历史住院证
    loadColumn = () => {
        let data = {
            params: [{
                reactCode: ['HospitalizationList'],
                compontName: "HospitalizationList",
                type: "C",
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    let column = res.result.C
                    for (let i = 0; i < column.length; i++) {
                        if (userData.language == 'EN') {
                            column[i].title = column[i].enDescripts
                        }
                    }
                    let tableWidth=res.totalWidth.split("px")[0]+130;
                    this.setState({
                        tableWidth:tableWidth,
                        column: column,
                        EN: res.result.language.EN,
                        CN: res.result.language.CN
                    })
                }
            }
        })
    }
    // 中英文切换
    display(name) {
        return userData.language != 'EN' ? (this.state.CN['HospitalizationList' + name] && this.state.CN['HospitalizationList' + name]['descripts'] ? this.state.CN['HospitalizationList' + name]['descripts'] : '') : (this.state.CN['HospitalizationList' + name] && this.state.EN['HospitalizationList' + name]['descripts'] ? this.state.EN['HospitalizationList' + name]['descripts'] : '')
    }
    //点击历史住院证行
    onRowClick = (record) => {
        if (record.id === this.state.clickRow.id) {
            this.setState({
                clickRow: {},
            },() => this.props.addchild.resetData({}))
        } else {
            this.setState({
                clickRow: record,
            },() => 
            this.props.addchild.resetData(record),
            this.props.addchild.loadBedList(record.wardID)
            )
        }
    }
    // 打印住院证
    print = () => {
        if (Util.isEmpty(this.state.clickRow)) {
            message.error("请选择一条住院记录");
        } else {
            let data = {};
            data = {
                params: [{
                    adminID: this.state.clickRow.id
                }]
            }
            let code = '03050224'
            let templateCode = 'Inpatientcard'
            $http.post('urlS', this, {
                code: code,
                data: data,
                success: function (res) {
                    if (res.errorCode === '0') {
                        //加载模板
                        $loadTemplate(templateCode, res.result);
                    }
                }
            })
        }
    }
    //渲染历史住院证行
    setRowClassName = (row) => {
        return row.id === this.state.clickRow.id ? 'clickRowStyle' : '';
    }
    //组件权限编辑器
    onColumnRef = (ref) => {
        this.columnRef = ref;
    }
    render() {
        this.columnOperation[0].title=this.display('operation');
        let columns = this.state.column.concat(this.columnOperation);
        return (
            <div className="appointmentList" style={{ border: '1px solid #cccccc', height: '100%' }}>
                <ColumnAuthoritysModel
                    clientWidth='800px'
                    compontName="HospitalizationList"
                    configType="C"
                    onRef={this.onColumnRef}
                    reloadData={this.loadColumn}
                />
                <div style={{ height: '36px', lineHeight: '36px', background: "linear-gradient(0deg,rgba(238,238,238,0.8) 0%,rgba(238,238,238,0.5) 50%,rgba(238,238,238,0.1) 100%)", color: '409EFF', paddingLeft: "10px" }}>
                    <span className='scheduling-title-border'></span>
                    <span style={{ marginLeft: 7, fontWeight: 'bold', color: '#409EFF', }}>{this.display('history')}</span>
                    <Button style={{float:'right'}}  type='primary' icon='printer' size='small' onClick={this.print}>{this.display('print')}</Button>
                </div>
                <div style={{ height: 'calc(100% - 36px)' }}>
                    <Table
                        style={{ height: '100%', padding: '10px', textAlign: 'left' }}
                        scroll={{ y: 'calc(100% - 45px)',x:this.state.tableWidth }}
                        bordered
                        columns={columns}
                        onRow={(record, index) => ({ onClick: (e) => { this.onRowClick(record, index, e) } })}
                        rowClassName={this.setRowClassName}
                        rowKey={(data) => {
                            return data.id
                        }}
                        pagination={false}
                        dataSource={this.state.tableData}
                        onHeaderRow={column => {
                            return {
                                onDoubleClick: () => {
                                    this.columnRef.onOpenModel();
                                }
                            };
                        }}
                    >
                    </Table>
                </div>
            </div>
        )
    }
}
export default HospitalizationList;