import React, { Component } from "react";
import { getAllDataList, getAllDataStringList } from '@utils/tools';
import style from "./index.less";
import PreviewBody from "../PreviewBody";
import store from '@store';
import Notify from '@commonComp/Notify';
import close from '../../common/images/icon_close.png'
import $ from 'jquery'
(function ($) {
    var opt;

    $.fn.jqprint = function (options) {
        opt = $.extend({}, $.fn.jqprint.defaults, options);

        var $element = (this instanceof $) ? this : $(this);

        if (opt.operaSupport && $.browser.opera) {
            var tab = window.open("", "jqPrint-preview");
            tab.document.open();

            var doc = tab.document;
        }
        else {
            var $iframe = $("<iframe  />");

            if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-300px", top: "-300px" }); }

            $iframe.appendTo("body");
            var doc = $iframe[0].contentWindow.document;
        }

        if (opt.importCSS) {
            if ($("link[media=print]").length > 0) {
                $("link[media=print]").each(function () {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
                });
            }
            else {
                $("link").each(function () {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
        }

        if (opt.printContainer) { doc.write($element.outer()); }
        else { $element.each(function () { doc.write($(this).html()); }); }

        doc.close();

        (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
        setTimeout(function () { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
    }

    $.fn.jqprint.defaults = {
        debug: false,
        importCSS: true,
        printContainer: true,
        operaSupport: true
    };

    $.fn.outer = function () {
        return $($('<div></div>').html(this.clone())).html();
    }
})($);

class PrintPreview extends Component {
    constructor(props) {
        super(props)
        this.onPrint = this.onPrint.bind(this);
    }
    onPrint() {
        $("#content").jqprint({
            debug: false,
            importCSS: true,
            printContainer: true,
            operaSupport: false
        });
        this.props.onClose()
    }
    surePrint(jsonData,jsonStr,whichSign){
        if(whichSign == 0){
            if(jsonData.chief.length < 1){
                Notify.info('主诉不能为空');
                return;
            }else if(jsonData.diag.length < 1){
                Notify.info('诊断不能为空');
                return;
            }else{
                this.onPrint();
                this.props.save(true)
            }
        }else{
            if(JSON.parse(jsonStr.chief).length == 1 && JSON.parse(jsonStr.chief)[0] ==''){
                Notify.info('主诉不能为空');
                return;
            }else if(JSON.parse(jsonStr.chief).length < 1){
                Notify.info('主诉不能为空');
                return;
            }else if(jsonStr.diag.trim().length < 1){
                Notify.info('诊断不能为空');
                return;
            }else{
                this.onPrint();
                this.props.save(true)
            }
        }
    }
    render() {
        const { onClose, visible, preInfo } = this.props;
        let baseList = store.getState();
        const dataJson = getAllDataList(baseList);
        const dataStr = getAllDataStringList(baseList);
        let whichSign = baseList.typeConfig.typeConfig;

        return <div className={style['container']} style={visible ? { display: 'block' } : { display: 'none' }}>
            <div className={style['shade']}></div>
            <div className={style['modal']}>
                <div className={style['close']}>打印<img src={close} onClick={onClose} /></div>
                <div id="content">
                    <PreviewBody
                        preInfo={preInfo}
                        dataJson={dataJson}
                        dataStr={dataStr}
                        show={true}
                        onClose={onClose}
                    ></PreviewBody>
                </div>

                <div onClick={()=>{this.surePrint(dataJson,dataStr,whichSign)}} style={{
                    padding: '10px 20px',
                    position: 'absolute',
                    bottom: '20px',
                    right: '20px',
                    background: '#3B9ED0',
                    color: '#fff',
                    borderRadius: '4px',
                    cursor: 'pointer'
                }}>打印</div>
            </div>
        </div>
    }
}

export default PrintPreview;
