/**
 * 
 * 诊断证明书
 */
import React from 'react';
import { Input, Button, message, Checkbox } from 'antd';
import { Util } from 'tools/index';
import { $http } from 'containers/config/https';
import { $loadTemplate } from '../../lodop/Common/Print.js';
import '../style/index.less';

const { TextArea } = Input;

class CertificateDiagnosisPage extends React.Component {
    constructor(props) {
        super(props)
        var recordData = JSON.parse(sessionStorage.getItem('recordData'));
        var patientData = JSON.parse(sessionStorage.getItem('patientData'));
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 10,
            recordData: recordData,
            patientData: patientData,
            userData: userData,
            isOpen: true,
            diagnosisValue: [], // 临床诊断选择
            // current: 0
        }
    }

    componentDidMount() {
        this.loadData()
    }

    loadData() {
        var params = {
            params: [{
                hospID: this.state.userData.hospID,
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
                admID: this.state.patientData && this.state.patientData.admID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050231",
            data: params,
            success: function (res) {
                let dataSource = res?.result || {};
                let admDiagnosisArr = dataSource?.admDiagnosisArr || [];
                let diagnosisValue = [];
                /**
                * @ByShizi: 打印时需选择诊断打印
                * 需求 #756
                * Create 2022-07-28
                */
                for (var i = 0; i < admDiagnosisArr.length; i++) {
                    if (admDiagnosisArr[i].defaultFlag === 'Y') {
                        diagnosisValue.push(admDiagnosisArr[i].diagnoseID);
                    }
                }
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource,
                        diagnosisValue,
                        patAddress: !Util.isEmpty(res.result) ? res.result.patAddress : '',
                        admAdvise: !Util.isEmpty(res.result) && res.result.admAdvise ? res.result.admAdvise : '特此证明！',
                        certificateID: !Util.isEmpty(res.result) ? res.result.certificateID : '',
                    })
                }
            }
        });
    }

    handleSaveDiagnosis() {
        if (Util.isEmpty(this.state.admAdvise)) {
            message.error('建议不能为空，请重新输入！')
            return
        }
        var params = {
            params: [{
                userID: this.state.userData.userID,
                admID: this.state.patientData && this.state.patientData.admID,
                patAddress: this.state.patAddress,
                admDiagnosis: !Util.isEmpty(this.state.dataSource) && this.state.dataSource.admDiagnosis,
                admAdvise: this.state.admAdvise,
                certificateID: this.state.certificateID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050230",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('操作成功', this.state.userData && this.state.userData.messageTime)
                }
            }
        });
    }

    handlePrint() {
        if (Util.isEmpty(this.state.admAdvise)) {
            message.error('建议不能为空，请重新输入！')
            return
        }
        let { dataSource, diagnosisValue } = this.state;
        let admDiagnosisArr = dataSource?.admDiagnosisArr || [];
        let admDiagnosisStr = '';
        admDiagnosisArr = admDiagnosisArr && admDiagnosisArr.filter(item => diagnosisValue.indexOf(item.diagnoseID) > -1);
        for (var i = 0; i < admDiagnosisArr.length; i++) {
            admDiagnosisStr += ((i + 1) + '.' + admDiagnosisArr[i].diagDesc + (admDiagnosisArr[i].eyePartDesc ? ('(' + admDiagnosisArr[i].eyePartDesc + ')') : '') + ';')
        }
        var params = {
            params: [{
                hospID: this.state.userData.hospID,
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
                admID: this.state.patientData && this.state.patientData.admID,
                admDiagnosisStr
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050232",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    // message.success('操作成功')
                    //   if (!Util.isEmpty(res.result)) {
                    //     $loadTemplate(res.result[0].template, res.result[0].printStr);
                    //   }
                    var print = res.result;
                    if (!Util.isEmpty(print)) {
                        for (var i = 0; i < print.length; i++) {
                            $loadTemplate(print[i].template, print[i].printStr);
                        }
                    }
                    message.success('打印成功！', this.state.userData && this.state.userData.messageTime)
                }
            }
        });
    }

    // 诊断
    handleDiagnosisChange = (checkedValues) => {
        this.setState({ diagnosisValue: checkedValues })
    }

    render() {
        let { diagnosisValue, dataSource } = this.state;
        return <div className='medical-record-body' style={{ width: '100%', height: '100%' }}>
            <div style={{ width: '70%', marginLeft: '15%', textAlign: 'center' }}>
                <div>
                    <h2>{!Util.isEmpty(dataSource) && dataSource.hospDesc || '--'}</h2>
                    <h3>{!Util.isEmpty(dataSource) && dataSource.title || '--'}</h3>
                </div>

                <div>
                    <span><span className='diagnosis-label'>姓名：</span>
                        <span>{!Util.isEmpty(dataSource) && dataSource.patName || '--'}</span></span>
                    <span className='diagnosis-span'><span className='diagnosis-label'>性别：</span>
                        <span>{!Util.isEmpty(dataSource) && dataSource.patSexDesc || '--'}</span></span>
                    <span className='diagnosis-span'><span className='diagnosis-label'>年龄：</span>
                        <span>{!Util.isEmpty(dataSource) && dataSource.patAge || '--'}</span></span>
                    <span className='diagnosis-span'><span className='diagnosis-label'>登记号：</span>
                        <span>{!Util.isEmpty(dataSource) && dataSource.patNo || '--'}</span></span>
                </div>

                <div style={{ marginTop: 10, textAlign: 'right' }}>
                    工作单位（住址）：<Input style={{ width: '80%' }} placeholder='请输入工作单位（住址）'
                        value={this.state.patAddress} onChange={Util.commonInputChange.bind(this, 'patAddress')} />
                </div>
                <div style={{ marginTop: 10, display: 'flex', paddingLeft: '11%' }}>
                    <div style={{ width: '10%', textAlign: 'right' }}>临床诊断：</div>
                    {/* <TextArea rows={4} disabled style={{ width: '90%' }} value={!Util.isEmpty(dataSource) && dataSource.admDiagnosis} /> */}
                    <Checkbox.Group style={{ width: '90%', textAlign: 'left' }} value={diagnosisValue} onChange={this.handleDiagnosisChange}>
                        {dataSource.admDiagnosisArr && dataSource.admDiagnosisArr.map((item, index) => {
                            return (
                                <div style={{ lineHeight: '28px' }}>
                                    <Checkbox key={index} value={item.diagnoseID}>
                                        {(item && item.diagnoseTypeDesc ? (item.diagnoseTypeDesc + '：') : '') + item.diagDesc + (item.eyePartDesc ? '(' + item.eyePartDesc + ')' : '')}
                                    </Checkbox>
                                </div>
                            )
                        })}
                    </Checkbox.Group>
                </div>

                <div style={{ marginTop: 10, textAlign: 'right', display: 'flex', paddingLeft: '11%' }}>
                    <div style={{ width: '10%' }}>
                        <span style={{ color: '#f5222d', fontFamily: 'SimSun' }}>*</span>建议：
                    </div>
                    <TextArea rows={4} style={{ width: '90%' }} placeholder='请输入建议'
                        value={this.state.admAdvise} onChange={Util.commonInputChange.bind(this, 'admAdvise')} />
                </div>

                <div style={{ marginTop: 10, textAlign: 'right' }}>
                    <span><span>科室：</span><span className='diagnosis-label'>
                        {!Util.isEmpty(dataSource) && dataSource.admLocDesc || '--'}</span></span>
                    {/* <span className='diagnosis-span'><span>医生签名：</span><span className='diagnosis-label'>
                        {!Util.isEmpty(dataSource) && dataSource.doctorName || '--'}</span></span>
                    <span className='diagnosis-span'><span>记录日期：</span><span className='diagnosis-label'>
                        {!Util.isEmpty(dataSource) && dataSource.receiveDate || '--'}</span></span> */}
                </div>

                <div style={{ position: 'absolute', left: '45%', bottom: 10 }}>
                    <Button onClick={this.handlePrint.bind(this)}>打印</Button>
                    <Button style={{ marginLeft: 10 }} type='primary' onClick={this.handleSaveDiagnosis.bind(this)}>保存</Button>
                </div>
            </div>
        </div>
    }
};

export default CertificateDiagnosisPage;