<?php
use \common\libs\MyLib;
?>
<?php $this->beginBlock('header_css'); ?>
<link href="/assets/css/plugins/iCheck/custom.css" rel="stylesheet">
<?php $this->endBlock(); ?>
<input id="serverInfo" type="hidden" value="ws://127.0.0.1:8800/" size="40"/>
<div id="txt-e" style=" color: red; font-size: 18px; display: none;"></div>
<div id="txt-ok" style=" color: green; font-size: 18px; display: none;"></div>
<input id="sendMsg" type="hidden" value='{"cmd":"LINK"}' style="width: 500px;" >
<input id="recMsg" type="hidden" value='' style="width: 500px;" >

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>呼叫电话</h5>
        </div>
        <div class="ibox-content">
            <div class="row">
                <div class="col-sm-12">
                    <form role="form" class="form-horizontal data-from" id="theFrm">
                        <input type="hidden" name="id" value="<?=$user_info->id?>">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">呼叫电话</label>
                            <div class="col-sm-5">
                                <input type="text" name="phone" id="phone" value="" class="form-control" autocomplete="off">
                            </div>
                        </div>
                        <div>
                            <div class="row">
                                <div class="col-sm-5 col-sm-offset-3">
                                    <button class="btn btn-primary btn-call" type="button">
                                        <strong>呼叫</strong>
                                    </button>
                                    <button class="btn btn-primary btn-stop" type="button">
                                        <strong>挂断</strong>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php $this->beginBlock('footer_js'); ?>
<script type="text/javascript" language="javascript">
    $(function () {
        //提交按钮
        $(".btn-call").click(function(){
            var phone = $('#phone').val();
            sendMsg('ATD',phone);
        });
        $(".btn-stop").click(function(){
            sendMsg('ATH');
        });
        wsInit();
    })
</script>
<script>
    var ws;
    function wsInit(val){
        //重连先关闭
        if(val=='reConn'){
            ws.close();
        }
        document.getElementById("txt-ok").innerText="";
        document.getElementById("txt-e").innerText="";
        if("WebSocket" in window) {
            var serverInfo = document.getElementById("serverInfo").value;
            // 打开一个 web socket，全局共用一个
            ws = new WebSocket(serverInfo);

            console.log("已连接"+serverInfo);
            ws.onopen = function() {
                setTimeout(function(){
                    sendMsg('link')
                },100);
                showResult('ok');
            };
            ws.onmessage = function(evt) {
                document.getElementById("recMsg").value=evt.data;
                var data = JSON.parse(evt.data);
                //console.log(data);
                switch(data['cmd']) {
                    // 服务端ping客户端
                    case 'USB':
                        ws.send('{"cmd":"USB","connected":"true","success":"true","message":"成功"}');
                        break;
                    case 'CORG':
                        ws.send('{"cmd":"CORG","number":"10010","success":"true","message":"成功"}');
                        break;
                    case 'CALLING':
                        ws.send('{"cmd":"CALLING","number":"10010","success":"true","message":"成功"}');
                        break;
                    case 'CBEGIN':
                        ws.send('{"cmd":"CBEGIN","success":"true","message":"成功"}');
                        break;
                    case 'ALERT':
                        ws.send('{"cmd":"ALERT","success":"true","message":"成功"}');
                        break;
                    case 'CEND':
                        ws.send('{"cmd":"CEND","success":"true","message":"成功"}');
                        break;

                }

            };
            //出现错误
            ws.onerror = function(evt){
                //console.log(evt);
            }
            //连接断开
            ws.onclose = function(evt){
                //console.log(evt)
                showResult('error');
            }
        } else {
            // 浏览器不支持 WebSocket
            parent.layer.msg("您的浏览器不支持 WebSocket!");
        }
    }

    function showResult(val){
        var serverInfo = document.getElementById("serverInfo").value;
        var okText = serverInfo + "连接成功";
        var errText = serverInfo + "连接失败,请检查window服务、IP和端口安装并配置正确";
        if(val=='ok'){
            document.getElementById("txt-ok").innerText = okText;
            document.getElementById("txt-ok").style.display="none";
            document.getElementById("txt-e").style.display="none";
        }else{
            //console.log(errText)
            document.getElementById("txt-e").innerText = errText;
            document.getElementById("txt-ok").style.display="none";
            document.getElementById("txt-e").style.display="none";
        }
    }
    function reConn(){
        ws = new WebSocket(serverInfo);
    }
    function sendMsg(val,phone){

        // Web Socket 已连接上，使用 send() 方法发送数据
        if(ws === undefined) {
            parent.layer.msg('控件无效，请重新安装');
            return;
        }
        var state=ws.readyState;

        console.log("readyState:"+state);
        if(state!=1){
            document.getElementById("txt-e").style.display="block";
            //location.reload();
            return;
        }else{
            document.getElementById("txt-e").style.display="none";
        }

        if(val=="link"){
            val = '{"cmd":"LINK"}';
        }
        /*if(val=="USB"){
            val = '{"cmd":"USB","connected":"true","success":"true","message":"成功"}';
        }*/
        if(val=="ATD"){
            function getRadioLValue(haoma){
                var obj = document.getElementsByName(haoma);
                for(i = 0;i<obj.length;i++){
                    if(obj[i].checked){
                        return obj[i].value;
                    }
                }
                return "undefined";
            }


//            var sendhaoma = getRadioLValue("sendhaoma");
            var sendhaoma = 0;
            var number_one = phone;


            $.get('/phone/ax-out',{'phone':number_one,'user_id':'<?=$user_info->id?>'},function(obj){

                if(obj.success) {

                    var number = obj.msg;

                    val = '{"cmd":"ATD","number":"'+number+'","hidden":"'+sendhaoma+'"}';
                    document.getElementById("sendMsg").value=val;
                    ws.send(val);
                    console.log("数据已发送");
                } else {
                    parent.layer.msg(obj.msg);
                }
            },'json');

        }
        if(val=="ATH"){
            val = '{"cmd":"ATH"}';
        }
        if(val=="ATA"){
            val = '{"cmd":"ATA"}';
        }
        if(val=="READIMEI"){
            val = '{"cmd":"READIMEI"}';
        }
        if(val=="READVER"){
            val = '{"cmd":"READVER"}';
        }
        if(val=="READSTATUS"){
            val = '{"cmd":"READSTATUS"}';
        }
        if(val=="startrecord"){
            function getLocalTime(nS) {
                return new Date(parseInt(nS) * 1000).Format("yyyyMMddhhmmss");
            }
            Date.prototype.Format = function(fmt) {//author: meizz
                var o = {
                    "M+" : this.getMonth() + 1, //月份
                    "d+" : this.getDate(), //日
                    "h+" : this.getHours(), //小时
                    "m+" : this.getMinutes(), //分
                    "s+" : this.getSeconds(), //秒
                    "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
                    "S" : this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt))
                    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt))
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }
            //E:\\qtproject\\PhoneService\\kaerHjSDK\\recordfile\\20180514.mp3
            var time= Math.round(new Date() / 1000) ;
            var times =(getLocalTime(time));
            console.log(time);
            var filename= "E:/qtproject/PhoneService/kaerHjSDK/recordfile/"+times+".mp3";
            val = '{"cmd":"startrecord","filename":"'+filename+'"}';
            //alert(filename);
        }
        if(val=="stoprecord"){
            val = '{"cmd":"stoprecord"}';
        }
        if(val=="HIDENUMBER"){
            function getRadioValue(laidian){
                var obj = document.getElementsByName(laidian);
                for(i = 0;i<obj.length;i++){
                    if(obj[i].checked){
                        return obj[i].value;
                    }
                }
                return "undefined";
            }
            function getRadioLValue(haoma){
                var obj = document.getElementsByName(haoma);
                for(i = 0;i<obj.length;i++){
                    if(obj[i].checked){
                        return obj[i].value;
                    }
                }
                return "undefined";
            }
            var type=getRadioValue('laidian');
            var hidden=getRadioLValue('haoma');
            //console.log(type);
            //console.log('号码'+hidden);
            val = '{"cmd":"HIDENUMBER","type":"'+type+'","hidden":"'+hidden+'"}';
        }
        if(val=="GETNUMBERHIDEN"){
            function getRadioDqValue(duqvhj){
                var obj = document.getElementsByName(duqvhj);
                for(i = 0;i<obj.length;i++){
                    if(obj[i].checked){
                        return obj[i].value;
                    }
                }
                return "undefined";
            }
            var type=getRadioDqValue('duqvhj');
            val = '{"cmd":"GETNUMBERHIDEN","type":"'+type+'"}';
        }
        if(val=="SETRECORDVOICE"){
            var voice= document.getElementById("voice").value;
            val = '{"cmd":"SETRECORDVOICE","voice":"'+voice+'"}';
        }
        if(val=="SETMODE"){
            function getRadioHpValue(headsetProfile){
                var obj = document.getElementsByName(headsetProfile);
                for(i = 0;i<obj.length;i++){
                    if(obj[i].checked){
                        return obj[i].value;
                    }
                }
                return "undefined";
            }
            var mode=getRadioHpValue('headsetProfile');
            val = '{"cmd":"SETMODE","mode":"'+mode+'"}';
        }
        if(val=="GETMODE"){
            val = '{"cmd":"GETMODE"}';
        }
        if(val=="SETUOC"){
            function getRadioHpValue(headsetProfileRadio){
                var obj = document.getElementsByName(headsetProfileRadio);
                for(i = 0;i<obj.length;i++){
                    if(obj[i].checked){
                        return obj[i].value;
                    }
                }
                return "undefined";
            }
            var mode=getRadioHpValue('headsetProfileRadio');
            val = '{"cmd":"SETUOC","UOC":"'+mode+'"}';
        }

        if(val=="GETUOC"){
            val = '{"cmd":"GETUOC"}';
        }
        if(val=="UPPARAM"){//设置通话记录传平台的参数
            var upparamurl = document.getElementById("upparamurl").value;//坐席工号
            var agent = document.getElementById("agent").value;//坐席工号
            var company =document.getElementById("company").value;//计费账号
            var thismobile =document.getElementById("thismobile").value;//本机号码
            val = '{"cmd":"UPPARAM","url":"'+upparamurl+'","agent":"'+agent+'","company":"'+company+'","thismobile":"'+thismobile+'"}';
        }
        document.getElementById("sendMsg").value=val;
        ws.send(val);
        console.log("数据已发送");
    }


</script>
<?php $this->endBlock('footer_js'); ?>
