/**
 * 
 * 公共医嘱项列表
 */
import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Pagination, Tabs,
    DatePicker, Button, Popconfirm, Icon, Divider, message, Modal,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import store from 'store'
import moment from 'moment';
import '../style/index.less';

const { RangePicker } = DatePicker;
const Search = Input.Search;
const Option = Select.Option;
const TabPane = Tabs.TabPane;
const confirm = Modal.confirm;

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,
        }
    }

    componentDidMount() {
        this.loadData(1)
        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) {
                if (res.errorCode == '0') {
                    this.setState({
                        subCat: res.result.subCat,
                        status: res.result.status,
                    })
                }
            }
        });
    }

    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: '5',
            //     width: 80,
            //     align: 'center',
            // },
        ]
    }

    loadData(page, pageSize, e) {
        var params = {
            params:[{
                value: this.state.unionValue,
                medSubCatID: this.state.subCatID,
                statusCode: this.state.statusCode
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: page
            }]
        
        }
        $http.post('urlDeault', this, {
            code: "03010062",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.rows,
                        total: res.total
                    });
                }
            }
        });
    }

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

    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);
        });
    }

    render() {
        var { subCat, status } = this.state;

        //医嘱子类
        var subCatOption = [];
        subCat && subCat.map((item, index) => {
            subCatOption.push(<Option key={index} value={item.id}>{item.descripts}</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'>
            <div style={{ display: 'flex' }}>
                <Input placeholder='输入代码/描述' onKeyDown={(e)=>e.keyCode==13?this.loadData.call(this,'1'):''} style={{ width: 180 }} onChange={Util.commonInputChange.bind(this,'unionValue')}/>
                <Select placeholder='请选择医嘱子类' style={{width:150,marginLeft:10}} onChange={Util.handleSelectChange.bind(this,'subCatID')}>
                    {subCatOption}
                </Select>
                <Select placeholder='请选择状态' style={{width:150,marginLeft:10}}onChange={Util.handleSelectChange.bind(this,'statusCode')}>
                    {statusOption}
                </Select>
                <Button type='primary' style={{marginLeft:10}} onClick={this.loadData.bind(this,'1')}>查询</Button>
            </div>
            <Table 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={{x:800}}
                pagination={false}
                footer={()=><div></div>}
                />

            <Pagination
                size="small"
                showQuickJumper
                showSizeChanger
                defaultCurrent={1}
                current={this.state.page}
                total={this.state.total}
                onShowSizeChange={this.changePageSize.bind(this)}
                onChange={this.onChange.bind(this)}
                style={{ textAlign: "right", marginTop: '13px' }}
            />
        </div>
    }

}
export default CommonOrderListPage;