/**
 * 
 *自备药审核
 */
import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Tabs,
    DatePicker, Button, Pagination, Icon, Divider, message, Modal,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import ShowRefusedModal from './component/ShowRefusedModal';
import moment from 'moment';
import store from 'store';
import './style/index.less';

const { RangePicker } = DatePicker;
const Option = Select.Option;
const TabPane = Tabs.TabPane;

class CheckBringMedicinePage extends React.Component {
    constructor(props) {
        super(props)
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        var date = new Date();
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 20,
            userData: userData,
            btnCode: '',
            btnStatus: 'primary',
            admStatusCode: 'A',
            admStatus: true,
            levelCode: 'BX',
            levelStatus: true,
            wardStatus: true,
            wardSPCode: 'BX',
            medGroupSPCode: 'BX',
            groupStatus: true,
            conditionCode: 'BX',
            defaultValue: '1',
            defaultDate: true,
            dateTo: moment(date).format(dayFormat),
            dateFrom: moment(new Date(date.getTime() - 144 * 60 * 60 * 1000)).format(dayFormat)
        }
    }

    componentDidMount() {
        this.initData()
        this.loadData(1)
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 100 + 'px'
    }

    initData() {
        this.columns = [
            {
                title: '序号',
                dataIndex: 'index',
                width: 80,
                align: 'center',
                render: (text, data, index) => <span style={{ width: 50 }}>{index + 1}</span>
            }, {
                title: '代码',
                dataIndex: 'code',
                width: 80,
                align: 'center',
            },
            {
                title: '描述',
                dataIndex: 'descripts',
                width: 120,
                align: 'center',
            },
            {
                title: '药品规格',
                dataIndex: 'spec',
                width: 120,
                align: 'center',
            },
            {
                title: '患者姓名',
                dataIndex: 'patName',
                width: 120,
                align: 'center',
            },
            {
                title: '病区',
                dataIndex: 'wardDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '申请人',
                dataIndex: 'userName',
                width: 120,
                align: 'center',
            },
            {
                title: '审核人',
                dataIndex: 'verifyUserName',
                width: 120,
                align: 'center',
            },
            {
                title: '频次',
                dataIndex: 'freqDesc',
                width: 80,
                align: 'center',
            },
            {
                title: '用法',
                dataIndex: 'usageDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '疗程数',
                dataIndex: 'durQty',
                width: 80,
                align: 'center',
            },
            {
                title: '疗程单位',
                dataIndex: 'durationDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '计价单位',
                dataIndex: 'unitDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '默认数量',
                dataIndex: 'dose',
                width: 80,
                align: 'center',
            },
            {
                title: '等效数量',
                dataIndex: 'doseQty',
                width: 80,
                align: 'center',
            },
            {
                title: '等效单位',
                dataIndex: 'doseUnitDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '药品剂型',
                dataIndex: 'drugFormDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '药品批号',
                dataIndex: 'drugBatchNo',
                width: 120,
                align: 'center',
            },
            {
                title: '药品子类',
                dataIndex: 'subCatDesc',
                width: 120,
                align: 'center',
            },
            {
                title: '状态',
                dataIndex: 'status',
                width: 120,
                align: 'center',
            },
            {
                title: '拒绝原因',
                dataIndex: 'note',
                width: 120,
                align: 'center',
            },
            {
                title: '申请日期',
                dataIndex: 'createDate',
                width: 120,
                align: 'center',
            },
            {
                title: '申请时间',
                dataIndex: 'createTime',
                width: 120,
                align: 'center',
            },
            {
                title: '审核日期',
                dataIndex: 'verifyDate',
                width: 120,
                align: 'center',
            },
            {
                title: '审核时间',
                dataIndex: 'verifyTime',
                width: 120,
                align: 'center',
            },
        ]
    }

    loadData(page, pageSize, e) {
        var params = {
            params: [{
                amdID: this.state.patientData ? this.state.patientData.admID : '',
                wardID: this.state.wardID,
                statusID: this.state.statusID,
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: page
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060015",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.result.data,
                        total: res.result.totalCount
                    })
                }
            }
        });
    }

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

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

    showRefusedModal() {
        if(Util.isEmpty(this.state.ids)){
            message.error('请选择你需要操作的数据！')
            return
        }
        this.setState({
            isShowRefusedModal: true
        })
    }

    //隐藏弹出框
    hideModal(name) {
        this.setState({
            [name]: false,//将所有参数置为false
            ids:[]
        });
    }

    onSelectChange(ids) {
        this.setState({
            ids
        })
    }

    onSubmit(status, data) {
        var params = {
            params: [{
                userID: this.state.userData.userID,
                id: this.state.ids,
                statusID: status == 'check' ? '2' : '3',
                note: status == 'check' ? '' : data
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060016",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('操作成功！')
                    this.loadData(1)
                    this.hideModal('isShowRefusedModal')
                }
            }
        });
    }

    render() {
        var { ids } = this.state;
        const rowSelection = {
            ids,
            onChange: this.onSelectChange.bind(this),
            selectedRowKeys: ids,
        }
        return <div className='hospital-patient-body'>
            <ShowRefusedModal isShow={this.state.isShowRefusedModal}
                onSubmit={this.onSubmit.bind(this)}
                onHide={this.hideModal.bind(this, 'isShowRefusedModal')}>
            </ShowRefusedModal>
            <Card>
                <Row gutter={40}>
                    <Col span={3} style={{float:'right'}}>
                        <Button type='primary' onClick={this.onSubmit.bind(this, 'check')}>审核</Button>
                        <Button type='danger' style={{ marginLeft: 10 }} onClick={this.showRefusedModal.bind(this)}>拒绝</Button>
                    </Col>
                </Row>
            </Card>
            <div style={{ marginTop: 10, height: store.getState().tableHeight.y + 'px' }}>
                <Table columns={this.columns} dataSource={this.state.dataSource} style={{ marginTop: 10 }}
                    pagination={false}
                    rowSelection={rowSelection}
                    scroll={{ x: 3000 }} bordered
                    rowKey={(data) => {
                        return data.id
                    }}></Table>
                {this.state.dataSource.length > 9 ? <Pagination
                    size="small"
                    showQuickJumper
                    showSizeChanger
                    defaultCurrent={1}
                    current={this.state.page}
                    total={this.state.total}
                    pageSize={this.state.pageSize}
                    onShowSizeChange={this.changePageSize.bind(this)}
                    onChange={this.onChange.bind(this)}
                    style={{ textAlign: "right", marginTop: '13px' }}
                /> : null}
            </div>
        </div>
    }

}
export default CheckBringMedicinePage;