import React from 'react';
import { Form, Row, Col, Input, Select, DatePicker, Button, Modal, message, Divider, Tooltip, Icon, } from 'antd';
import moment from 'moment';
import fetchJsonp from 'fetch-jsonp';
import { $http } from '../../containers/config/https';
const userData=sessionStorage.userData?JSON.parse(sessionStorage.userData):{}
class ReadCard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            cardTypeObj:[]
        };
    }
    componentWillMount() {
        const hospID=userData.hospID
        let data = {
            params: [{
                hospID:hospID
            }]
        }
        $http.post("urlS",this, {
            code: "01040059",
            data: data,
            success: function (res) { 
                if (+res.errorCode === 0) {
                   
                   this.setState({
                        cardTypeObj:res.cardType
                    })
                } else {
                    message.error('失败：' + res.errorMessage);
                }
            }
        })  
    }
    ReadCard=(cardType,mode,callback)=>{
        var cardTypeObj=this.state.cardTypeObj
        var dllPath=""
        var className=""
        var funcName=""
        for(var i=0;i<cardTypeObj.length;i++){
            var itemObj=cardTypeObj[i]
            if(itemObj.id===cardType){
                var equiData=itemObj.equiData
                dllPath=equiData.dllPath
                className=equiData.className
                for(var j=0;j<equiData.func.length;j++){
                    var funcItem=equiData.func[j]
                    if(funcItem.funType===mode){
                        funcName= funcItem.funDesc
                    }
                }
            }
        }
        if((dllPath==="")||(className==="")||(funcName==="")){
            message.error("读卡设备配置不正确,不能读卡");
            return
        }
        let data = 'input={"inputDir":"'+dllPath+'","clsName":"'+className+'","funcName":"'+funcName+'"}'
        let that = this
        fetchJsonp('http://127.0.0.1:62534/XYSWCFService/DynamicLoadingDLL?' + '&' + data, {
            jsonpCallback: 'JsonCallback',
        })
        .then(function (response) {
            return response.json()
        }).then(function (json) {
            //返回卡号
            json=JSON.parse(json)
            if(json.errorCode==="0"){
                message.success('读卡成功');
                var cardNo=json.cardNo
                if(mode==="RI"){
                    callback(json); 
                    return 
                }
                that.GetPatInfoByCardNo(cardType,cardNo,callback)

                //callback(cardNo);
            }else{
                message.error(json.errorMessage);
            }
           
            return cardNo
                    
        }).catch(function (ex) {
            that.setState({
                visibleIp: true
            })
            return false
        })
        
    }
    GetPatInfoByCardNo=(cardTypeID,cardNo,callback)=>{
        const hospID=userData.hospID
        let data = {
            params: [{
                cardTypeID:cardTypeID,
                cardNo:cardNo
            }]
        }
        var cardObj={}
        cardObj.cardTypeID=cardTypeID
        cardObj.cardNo=cardNo
        $http.post("urlS",this, {
            code: "03040004",
            data: data,
            success: function (res) { 
                if (+res.errorCode === 0) {
                    cardObj.patInfo=res.data
                    //console.log(JSON.stringify(cardObj))
                    callback(cardObj)
                    
                } else {
                   // console.log(JSON.stringify(cardObj))
                    callback(cardObj)
                    //message.error('失败：' + res.errorMessage);
                }
            }
        }) 
    }
    
    render() {
        return (
            <div></div>
        );
    }
}

export default ReadCard;