// 会诊统计-全院
import React, { Component } from 'react';
import { Row, Col, DatePicker, Table, Select, Button, Divider, message, Pagination } from 'antd';
import { $http } from '../../../containers/config/https';
import moment from 'moment';
import { Util } from 'tools/index';
import ConsultationStatisticDetail from '../Consultation/ConsultationStatisticsDetail';
import CommonPagination from 'components/pagination/index';
import CommonRangePicker from 'components/rangePicker/index';
import store from 'store';

// 用户数据
let userData ;
export default class ConsultationStatistics extends Component {
    constructor(props) {
        userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
        super(props);
        this.state = {
            statisticsType: 'applyLoc',         // 统计类型
            conTypeCode: '',            // 会诊类型
            statisticsTypeData: [{
                id: 'applyLoc',
                code: 'applyLoc',
                descripts: '按申请科室'
            }, {
                id: 'consultLoc',
                code: 'consultLoc',
                descripts: '按接收科室'
            }],                         // 统计类型下拉数据
            conTypeData: [],            // 会诊类型下拉数据
            tableData: [],              // 会诊统计数据
            total: 0,                  // table数据总量
            stDate: null, // 开始日期
            endDate: moment().format('YYYY-MM-DD'),                // 结束日期
            page: 1,
            pageSize:10,
            clickFlag: false,
            tableUserID: '',            // 表格人员id
            recordLocID: ''             // 行上的科室id
        }
    }

    componentDidMount() {
        this.initDefaultValue();
        this.getConTypeData();
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
    }
    onRef = (ref) => {
        this.child = ref;
    }
    //查询默认数据
    initDefaultValue = () => {
        let data = {
            params: [{
                defaultAppObj: {
                    Consultation: ['StatisticsStDate', 'StatisticsEndDate', 'StatisticsType'],
                }
            }]
        }
        $http.post('urlS', this, {
            code: "04110023",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        // defaultValueObj: res.result,
                        stDate:res.result.StatisticsStDate,
                        endDate:res.result.StatisticsEndDate,
                        statisticsType:res.result.StatisticsType,
                    }, () => {
                        this.getConStatisticsInfo();
                    }
                    )
                }
            }
        })
    }


    // 获取会诊类型下拉框
    getConTypeData = () => {
        let data = {
            "params": [{
                hospID: userData && userData.hospID ? userData.hospID : '', // 登录医院id       
                locID: userData.locID,
                groupID: userData.groupID,
                userID: userData.userID,
                type: 'apply'
            }]
            
        };
        $http.post('urlDeault', this, {
            code: "04090011",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        conTypeData: res.result.conType
                    })
                }
            },
            error: function (error) {
                message.error('失败：' + error);
            }
        });
    }
    // 查询统计信息
    getConStatisticsInfo = () => {
        // console.log("------",this.state.statisticsType,this.columns,"=====",arr)
        this.setState({
            clickFlag: true
        })
        let data = {
            "params": [{
                hospID: userData && userData.hospID ? userData.hospID : '', // 登录医院id       
                // locID: userData.locID,
                // groupID: userData.groupID,
                // userID: userData.userID,
                type: this.state.statisticsType,
                conTypeCode: this.state.conTypeCode,
                startDate: moment(this.state.stDate).format('YYYY-MM-DD'),
                stopDate: moment(this.state.endDate).format('YYYY-MM-DD')
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: this.state.page,
            }]
        };
        $http.post('urlDeault', this, {
            code: "04090021",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    let tableData = res.result.Data;
                    for (let i = 0; i < tableData.length - 1; i++) {
                        tableData[i].key = i
                    }
                    this.setState({
                        tableData,
                        total: res.result.TotalCount,
                        clickFlag: false
                    })
                } else {
                    this.setState({
                        clickFlag: false
                    })
                }
            },
            error: function (error) {
                message.error('失败：' + error);
                this.setState({
                    clickFlag: false
                })
            }
        });
    }
    // 获取明细信息
    getConsultationDetail = (record) => {
        this.setState({
            tableUserID: record.userID,
            recordLocID: record.locID
        }, () => { this.child.showModal() })
    }
    // 翻页
    onChange = (page, pageSize) => {
        this.setState({ page: page, pageSize: this.state.pageSize}, () => {
            // 重新调用查询方法
            this.getConStatisticsInfo();
        });
    };

    onShowSizeChange = (current, size) => {
        this.setState({ page: current, pageSize: size }, () => {
            // 重新调用查询方法
            this.onChange(current, size);
        })
    };
    // 显示分页总条数
    showTotal(total) {
        return `共 ${total} 条`;
    }
    render() {
        const formItemDate = {
            xs: { span: 24 },
            sm: { span: 12 },
            md: { span: 10 },
            lg: { span: 6 }
        };
        const columns = [
            {
                key: 1,
                title: this.state.statisticsType === 'applyLoc' ? '申请科室' : '接收科室',
                dataIndex: 'locDesc',
                align: "center",
            }, {
                key: 2,
                title: '会诊职称',
                dataIndex: 'titleDesc',
                align: "center",
            }, {
                key: 3,
                title: this.state.statisticsType === 'applyLoc' ? '申请医生' : '会诊医师',
                dataIndex: 'userDesc',
                align: "center",
            }, {
                kye: 4,
                title: '会诊次数',
                dataIndex: 'conItemNumber',
                align: "center",
                render: (text, record) => {
                    return (<a onClick={() => this.getConsultationDetail(record)}>{text}</a>)
                }
            }, {
                key: 5,
                title: '会诊费用',
                dataIndex: 'consultAmount',
                align: "center",
            }
        ]

        const params = {
            startDate: moment(this.state.stDate).format("YYYY-MM-DD"),
            stopDate: moment(this.state.endDate).format("YYYY-MM-DD"),
            type: this.state.statisticsType,
            conApplyUserID: this.state.statisticsType === 'applyLoc' ? this.state.tableUserID : '',
            conItemUserID: this.state.statisticsType === 'applyLoc' ? '' : this.state.tableUserID,
            conTypeCode: this.state.conTypeCode,
            recordLocID: this.state.recordLocID
        }
        return (
            <div style = {{overflow: 'hidden'}}>
                <div style={{ padding: '20px 0 0' ,height: 'calc(100% - 56px)'}}>
                    <Row type="flex" justify='center'>
                        <Col {...formItemDate}> 
                            <CommonRangePicker
                                width={300}
                                label = '日期:'
                                startDate = {moment()}
                                endDate = {moment()}
                                format = {'YYYY-MM-DD'}
                                startChange = {(date) => this.setState({ stDate: date })}
                                endChange = {(date) => this.setState({ endDate: date })}
                            />
                        </Col>                        
                        <Col span={4}>
                            统计类型：
                            <Select
                                allowClear
                                style={{ width: '70%' }}
                                value={this.state.statisticsType}
                                onChange={(id) => { this.setState({ statisticsType: id },() => this.getConStatisticsInfo()) }}
                            >
                                {this.state.statisticsTypeData.map(data => (<Select.Option key={data.code} value={data.code}>{data.descripts}</Select.Option>))}
                            </Select>
                        </Col>
                        <Col span={4}>
                            会诊类型：
                            <Select
                                allowClear
                                style={{ width: '70%' }}
                                onChange={(id) => { this.setState({ conTypeCode: id }) }}
                            >
                                {this.state.conTypeData.map(data => (<Select.Option key={data.id} value={data.code}>{data.descripts}</Select.Option>))}
                            </Select>
                        </Col>
                        <Col span={2}>
                            <Button type='primary' onClick={this.getConStatisticsInfo}>查询</Button>
                        </Col>
                    </Row>
                </div>
                <br />
                <div>
                    <Table
                        columns={columns}
                        dataSource={this.state.tableData}
                        pagination={false}
                        bordered
                        rowKey={(row) => {
                            return row.ordID
                        }}
                    />
                    <CommonPagination 
                        size='small'
                        current={this.state.page} 
                        total={this.state.total} 
                        search='search'
                        onShowSizeChange={this.onShowSizeChange.bind(this)} 
                        complete={true} 
                        dataSource={this.state.tableData}
                        onChange={this.onChange.bind(this)} 
                        scroll={{ y: store.getState().tableHeight.y + 'px' }}
                    />
              
                </div>
                <ConsultationStatisticDetail
                    onRef={this.onRef}
                    params={params}
                >
                </ConsultationStatisticDetail>
            </div>
        )
    }
}