/**
 * 
 * 检验告知单
 */
import React from 'react';
import { Tabs, Row, Col, Icon, Divider, Checkbox, Button, Table, Card, Tooltip, } from 'antd';
import { $http } from 'containers/config/https';
import { $loadTemplate } from '../../lodop/Common/Print.js';
import '../style/index.less';
import { Util } from 'tools/index';

class InspectionNoticePage extends React.Component {
    constructor(porps) {
        super(porps);
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        var patientData = JSON.parse(sessionStorage.getItem('patientData'));
        this.state = {
            userData: userData,
            patientData: patientData
        }

    }

    componentDidMount() {
        this.loadData()
    }

    //初始化数据
    loadData() {
        let data = {
            params: [{
                admID: this.state.patientData ? this.state.patientData.admID : ''
            }]
        }
        $http.post('urlS', this, {
            code: '03050265',
            data: data,
            success: function (res) {
                if (res.errorCode == '0') {
                    var source = res.result;
                    if (!Util.isEmpty(source)) {
                        var checked = true
                        var data = source.ordInfo
                        if (!Util.isEmpty(data)) {
                            for (var i = 0; i < data.length; i++) {
                                if (data[i].defaultFlag !== 'Y') {
                                    checked = false
                                    break;
                                }
                            }
                        }else{
                            checked = false
                        }
                    }
                    this.setState({
                        dataSource: res.result,
                        checked: checked
                    })
                }
            }
        })
    }

    //打印
    handlePrintMsg() {
        var { dataSource } = this.state;
        var ids = []
        if (!Util.isEmpty(dataSource)) {
            var source = dataSource.ordInfo;
            if (!Util.isEmpty(source)) {
                for (var i = 0; i < source.length; i++) {
                    if (source[i].defaultFlag == 'Y') {
                        ids.push(source[i].ordID)
                    }
                }
            }
        }
        var data = {
            params: [{
                admID: this.state.patientData ? this.state.patientData.admID : '',
                ordID: ids
            }]
        }

        $http.post('urlADS', this, {
            code: "03050266",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    let dataArr = res.result
                    dataArr && dataArr.map((item, i) => {
                        $loadTemplate(item.template, item.printStr);
                    })
                }
            }
        });
    }

    //渲染动态数据
    loadInspectionDetails() {
        var sourceOption = []
        var { dataSource } = this.state;
        if (!Util.isEmpty(dataSource)) {
            var source = dataSource.ordInfo
            source && source.map((item, index) => {
                sourceOption.push(<div key={index}>
                    <Col span={13} style={{ marginTop: 10 }}><Checkbox style={{ marginRight: 5 }} onChange={this.handleChangeCheck.bind(this, item)}
                        checked={item.defaultFlag == 'Y' ? true : false} /><Tooltip title={item.ordDesc}>
                            <span className='inspection-class-style'>{item.ordDesc}</span></Tooltip></Col>
                    <Col span={4} style={{ marginTop: 10 }}>{item.ordSpecimen}</Col>
                    <Col span={3} style={{ marginTop: 10 }}>{item.totalAmt}</Col>
                    <Col span={4} style={{ marginTop: 10 }}>{item.relocDesc}</Col>
                </div>)
            })
        }
        return sourceOption
    }

    handleChangeCheck(item, e) {
        var { dataSource } = this.state;
        var flag = ''
        item['defaultFlag'] = e.target.checked == true ? 'Y' : 'N';
        if (!Util.isEmpty(dataSource)) {
            var data = dataSource.ordInfo
            if (!Util.isEmpty(data)) {
                for (var i = 0; i < data.length; i++) {
                    if (data[i].defaultFlag !== 'Y') {
                        flag = false
                        break
                    } else {
                        flag = true
                    }
                }
            }
        }

        this.setState({ checked: flag })
    }

    selectAll(e) {
        var { dataSource } = this.state;
        if (!Util.isEmpty(dataSource)) {
            var data = dataSource.ordInfo
            if (!Util.isEmpty(data)) {
                for (var i = 0; i < data.length; i++) {
                    if (e.target.checked) {
                        data[i]['defaultFlag'] = 'Y'
                    } else {
                        data[i]['defaultFlag'] = 'N'
                    }
                }
            }
        }
        this.setState({ checked: e.target.checked })
    }

    render() {
        var { dataSource } = this.state;
        return (
            <div className="userInfoDom" style={{ width: "100% !important" }}>
                <div>
                    <Card style={{ width: '80%' }} title={<h3 style={{ textAlign: 'center' }}>检验告知单</h3>}
                        extra={<Button type='primary' size='small' disabled={!Util.isEmpty(dataSource) && !Util.isEmpty(dataSource.ordInfo) ? false : true} onClick={this.handlePrintMsg.bind(this)}>打印</Button>}>
                        <div>
                            <Row style={{ marginTop: 5 }}>
                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">人员标识：</span>{!Util.isEmpty(dataSource) ? dataSource.patNo : '--'}</span>
                                </Col>
                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">卡号：</span>{!Util.isEmpty(dataSource) ? dataSource.cardNo : '--'}</span>
                                </Col>
                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">姓名：</span>{!Util.isEmpty(dataSource) ? dataSource.patName : '--'}</span>
                                </Col>

                            </Row>

                            <Row style={{ marginTop: 5 }}>
                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">性别：</span>{!Util.isEmpty(dataSource) ? dataSource.patSex : '--'}</span>
                                </Col>

                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">年龄：</span>{!Util.isEmpty(dataSource) ? dataSource.patAge : '--'}</span>
                                </Col>

                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">费别：</span>{!Util.isEmpty(dataSource) ? dataSource.admReason : '--'}</span>
                                </Col>
                            </Row>

                            <Row style={{ marginTop: 5 }}>
                                <Col span={8}>
                                    <span className="marginR">
                                        <span className="middTitle">就诊科室：</span>{!Util.isEmpty(dataSource) ? dataSource.admlocDesc : '--'}</span>
                                </Col>
                            </Row>
                        </div>
                        <Divider type='horizontal' />

                        <div style={{overflow: "hidden"}}>
                            <Col span={13} style={{ fontSize: '16px', color: '#666666', fontWeight: 'bold' }}>
                                <Checkbox style={{ marginRight: 5 }} checked={this.state.checked} onChange={this.selectAll.bind(this)} />检验项目</Col>
                            <Col span={4} style={{ fontSize: '16px', color: '#666666', fontWeight: 'bold' }}>标本</Col>
                            <Col span={3} style={{ fontSize: '16px', color: '#666666', fontWeight: 'bold' }}>金额</Col>
                            <Col span={4} style={{ fontSize: '16px', color: '#666666', fontWeight: 'bold' }}>执行科室</Col>
                        </div>

                        <div>
                            {!Util.isEmpty(dataSource) && !Util.isEmpty(dataSource.ordInfo) ? (
                                <div style={{height: "56vh", overflow: "auto"}}>{this.loadInspectionDetails()}</div>
                            ) :
                                <div style={{ height: 200, textAlign: 'center', lineHeight: '200px' }}>暂无打印数据~~</div>}
                        </div>
                    </Card>
                </div>
                <div>

                </div>
            </div>
        )
    }
}
export default InspectionNoticePage