//  科室选择Table组件
import React from 'react';
import { Table } from 'antd';
import { $http } from '../../../containers/config/https';

class DepartmentList extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            columns: [
                {
                    title: '科室',
                    key: '1',
                    dataIndex: 'logonLocDesc',
                    align: 'center',
                },
                {
                    title: '角色',
                    key: '2',
                    dataIndex: 'logonGroupDesc',
                    align: 'center',
                }, {
                    title: '所属医院',
                    key: '3',
                    dataIndex: 'logonHospDesc',
                    align: 'center',
                },
                // {
                //     title: '机构代码',
                //     key: '4',
                //     width: 120,
                //     dataIndex: 'institution',
                //     align: 'center',
                //     render: (text, record) => {
                //         return (
                //             <Tooltip title={text}>
                //                 <span className="ellipsis" style={{ width: '100%', display: 'block' }}>{text}</span>
                //             </Tooltip>
                //         );
                //     }
                // }
            ],
            userCode: '', //科室列表
            columnData: [],
            filterVal: ''
        }
        this.props.onRef(this);
    }

    /**获取科室列表 */
    getDepartmentList = (userCode, language, userID) => {
        let data = {
            "params": [{
                userID: userID,
                userCode: userCode,
                currentLocFlag: "Y",
                language: language || '', // 国际化语言标识
            }],
            "session": [{
                "loginIp": window.location && window.location.protocol && window.location.host ? (window.location.host.split(":")[0]) : (window.location && window.location.origin ?
                    (window.location.origin.split(":")[0] + window.location.origin.split(":")[1]).split("//")[1] : "")
            }]
        };
        $http.post('urlDeault', this, {
            code: "01040043",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    let result = res.result;
                    for (var i = 0; i < result.logonLocAry.length; i++) {
                        let currentRecord = result.logonLocAry[i];
                        result.logonLocAry[i].key = i;
                        result.logonLocAry[i].searchDesc = currentRecord.logonHospDesc + '^' + (currentRecord.logonHospDescSPCode || '') + '^' + currentRecord.logonLocDesc + '^' + (currentRecord.logonLocDescSPCode || '') + '^' + currentRecord.logonGroupDesc + '^' + (currentRecord.logonGroupDescSPCode || '');
                    }
                    this.setState({
                        columnData: result.logonLocAry
                    })
                }
            }
        });
    }

    // 清除列表数据
    clearTableData = () => {
        this.setState({ columnData: [] })
    }

    // 回车检索角色数据
    handlePressEnter = (e) => {
        this.setState({ filterVal: e?.target?.value || '' })
    }

    handleFilterTableData = () => {
        let { filterVal, columnData } = this.state;
        return columnData && columnData.filter(item => (item.searchDesc).indexOf((filterVal || '')) > -1)
    }

    render() {
        return (
            <div>
                <Table
                    columns={this.state.columns}
                    dataSource={this.handleFilterTableData()}
                    pagination={false}
                    scroll={{ y: 240 }}
                    onRow={(record) => {
                        return {
                            onClick: (event) => {
                                this.props.setDepartmentValues(record)
                            },
                        };
                    }} />
            </div>
        );
    }
}

export default DepartmentList

