/**
 * JYW
 * 叫号
 */
// 登录
import React, { Component } from 'react';
import { callNumberSocket } from 'tools/callNumber.js';
import { message } from 'antd';
import { $http } from '../../containers/config/https';

class callNumber extends Component {
    constructor(props) {
        super(props);
        this.state = {
            keywords: '',
            form: '',
            bindInfo: "",
        };
        this.sendMessage = this.sendMessage.bind(this);
        this.handlerKeywords = this.handlerKeywords.bind(this);
    }
    componentDidMount() {
        this.getBindInfo();
    }
    componentWillUnmount() {
        callNumberSocket.colse();
    }
    getBindInfo = (dataID) => {
        let userData=window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data = {
            params: [{
                dataID: dataID,
                hospID: userData.hospID,
                purCode:"purCode",
            }]
        }
        $http.post('urlDeault', this, {
            code: "04150007",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    callNumberSocket.init(res.result.serverIP, res.result.serverPort);
                    this.setState({
                        bindInfo: res.result,
                    })
                }
            }
        })
    }

    /** 发送消息 */
    sendMessage = () => {
        if (this.state.bindInfo == "") {
            message.warn("未关联电子屏，不能呼叫!");
            return;
        }
        let msg = {};
        let callMsg = [];
        let callInfo = {}
        callInfo.patName = "贾元伟";
        callInfo.patNo = "01";
        callInfo.status = "CI";
        callInfo.statusDesc = "呼叫中";
        callMsg.push(callInfo);
        callInfo = {}
        callInfo.patName = "熊英豪";
        callInfo.patNo = "02";
        callInfo.status = "CW";
        callInfo.statusDesc = "呼叫等待";
        callMsg.push(callInfo);
        callInfo = {}
        callInfo.patName = "李刚";
        callInfo.patNo = "03";
        callInfo.status = "CW";
        callInfo.statusDesc = "呼叫等待";
        callMsg.push(callInfo);
        callInfo = {}
        callInfo.patName = "梁家权";
        callInfo.patNo = "04";
        callInfo.status = "CW";
        callInfo.statusDesc = "呼叫等待";
        callMsg.push(callInfo);

        msg.callMsg = callMsg;
        msg.readMsg = this.state.keywords;
        let ip = this.state.bindInfo.IP;

        callNumberSocket.sendMsg(msg, ip)
    }

    handlerKeywords = (e) => {
        this.setState({
            keywords: e.target.value,
        })
    }

    render() {
        return (
            <div style={{ marginTop: 100 }}>
                消息:
                <input
                    className="keywords"
                    name="keywords"
                    value={this.state.keywords}
                    onChange={this.handlerKeywords}
                    type="text"
                />
                <br />
                <button onClick={this.sendMessage} className="send-btn">发送</button>
            </div>
        );
    }
}

export default callNumber