/**
 * 
 * 公共医嘱项列表
 */
import React from 'react';
import {
    Table, Input, Select, Button, Tooltip,
} from 'antd';
import { Util } from 'tools/index';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination/index';
import { PAGE_NUM_MORE_STATUS } from 'tools/statusConstants';
import store from 'store'
import '../style/index.less';

const Option = Select.Option;
class CommonOrderListPage extends React.Component {
    constructor(props) {
        super(props)
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 10,
            userData: userData,
            size: 'small',
            loading: false
        }
    }

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

    loadSelectData() {
        var params = {
            params: [{
                logonHospID: this.state.userData.hospID,
                logonGroupID: this.state.userData.groupID,
                subCatFlag: 'Y',
                statusFlag: 'Y'
            }]
        }
        $http.post('urlDeault', this, {
            code: "03010037",
            data: params,
            success: function (res) {
                var status = res.result.status
                var statusCode = ''
                if (!Util.isEmpty(status)) {
                    for (var i = 0; i < status.length; i++) {
                        if (status[i].defaultFlag == 'Y') {
                            statusCode = status[i].id
                            break
                        }
                    }
                }
                if (res.errorCode == '0') {
                    this.setState({
                        subCat: res.result.subCat,
                        status: res.result.status,
                        statusCode: statusCode
                    }, () => {
                        this.loadData(1)
                    })
                }
            }
        });
    }

    initData() {
        this.columns = [
            {
                title: '代码',
                dataIndex: 'medCode',
                width: 100,
                align: 'center',
            },
            {
                title: '描述',
                dataIndex: 'medDesc',
                width: 280,
                align: 'left',
            },
            {
                title: '简称',
                dataIndex: 'medShortDesc',
                width: 280,
                align: 'left',
            },
            {
                title: '医嘱子类',
                dataIndex: 'subCatDesc',
                width: 100,
                align: 'center',
            },
            {
                title: '状态',
                dataIndex: 'statusDesc',
                width: 80,
                align: 'center',
            },
            // {
            //     title: '操作',
            //     dataIndex: '5',
            //     width: 80,
            //     align: 'center',
            // },
        ]
    }

    loadData(page, pageSize, e) {
        this.setState({ loading: true });
        var params = {
            params: [{
                subCatID: this.state.subCatID,
                unionValue: this.state.unionValue,
                statusCode: this.state.statusCode,
            }],
            pagination: [{
                pageSize: this.state.size == 'small' ? this.state.pageSize : PAGE_NUM_MORE_STATUS[0].code,
                currentPage: page
            }]

        }
        $http.post('urlDeault', this, {
            code: "05100169",
            data: params,
            success: function (res) {
                this.setState({ loading: false });
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.data,
                        total: res.total,
                        search: e,
                        loading: false
                    })
                }
            },
            error: function () {
                this.setState({ loading: false });
            }
        });
    }

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

    //点击行，渲染当前行样式
    onClickRow = (record) => {
        return {
            onClick: () => {
                this.setState({
                    medID: record.medID,
                }, () => {
                    this.loadDetailData(record.medID)
                    //将行数据传给父，供其他组件使用
                    var { loadCommonMedId } = this.props;
                    loadCommonMedId && loadCommonMedId(record)
                })
            },
            onDoubleClick: () => {
            }
        };
    }

    clearInput() {
        this.setState({
            medID: '',
            record: ''
        })
    }

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

    loadDetailData(id) {
        var params = {
            params: [{ medID: id }]
        }
        $http.post('urlDeault', this, {
            code: "02020012",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    var { loadDetailMsg } = this.props;
                    loadDetailMsg && loadDetailMsg(res.result)
                }
            }
        });
    }

    //分页切换
    onChange(e) {
        this.setState({ page: e }, () => {
            this.loadData(e);
        });
    }

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

    render() {
        var { subCat, status, loading } = this.state;
        //医嘱子类
        var subCatOption = [];
        subCat && subCat.map((item, index) => {
            subCatOption.push(<Option key={index} value={item.id} code={item.descriptsSPCode} name={item.descripts}>
                <Tooltip title={item.descripts}>{item.descripts}</Tooltip></Option>)
        })

        var statusOption = [];
        status && status.map((item, index) => {
            statusOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        return <div className='common-order-list-body table-body-height'>
            <div style={{ display: 'flex' }}>
                <Input placeholder='输入代码/描述' style={{ width: 180 }} onChange={Util.commonInputChange.bind(this, 'unionValue')}
                    onKeyDown={this.onKeyDownLoadData.bind(this)} />
                <Select
                    placeholder='请选择医嘱子类'
                    style={{ width: 150, marginLeft: 10 }}
                    allowClear
                    showSearch dropdownMatchSelectWidth={false}
                    filterOption={(input, option) =>
                        option.props.code.toLowerCase().indexOf(input.toLowerCase()) >= 0 ||
                        option.props.name.toLowerCase().indexOf(input.toLowerCase()) >= 0
                    }
                    onChange={Util.handleSelectChange.bind(this, 'subCatID')}>
                    {subCatOption}
                </Select>
                <Select placeholder='请选择状态' allowClear style={{ width: 150, marginLeft: 10 }} value={this.state.statusCode}
                    onChange={Util.handleSelectChange.bind(this, 'statusCode')}>
                    {statusOption}
                </Select>
                <Button type='primary' style={{ marginLeft: 10 }} onClick={this.loadData.bind(this, '1')}>查询</Button>
            </div>
            <Table
                loading={loading}
                columns={this.columns}
                dataSource={this.state.dataSource}
                style={{ marginTop: 10, height: store.getState().tableHeight.y + 52 + 'px' }}
                loadData={this.loadData.bind(this)}
                onRow={this.onClickRow}
                rowClassName={this.setRowClassName}
                rowKey={(data) => {
                    return data.medID
                }}
                scroll={{ y: (store.getState().tableHeight.y), x: 2000 }}
                pagination={false}>
            </Table>
            <CommonPagination size={this.state.size} current={this.state.page} total={this.state.total} search={this.state.search}
                onShowSizeChange={this.changePageSize.bind(this)} complete={false} dataSource={this.state.dataSource}
                loading={this.state.loading} onChange={this.onChange.bind(this)} />
        </div>
    }
}

export default CommonOrderListPage;