<?php
use \common\libs\MyLib;
?>
<?php $this->beginBlock('header_css'); ?>
    <link href="/assets/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
<?php $this->endBlock(); ?>
    <input id="serverInfo" type="hidden" value="ws://127.0.0.1:8800/" size="40"/>
    <br>
    <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 class="ibox-tools">
                    <a class="btn btn-primary btn-xs edit-btn" data-id="0" href="javascript:void(0);" onclick="opDlg()">
                        <i class="fa fa-history"></i> 操作记录
                    </a>
                    <a class="btn btn-primary btn-xs history-btn" data-id="0" href="javascript:void(0);" onclick="history.go(-1)">
                        <i class="fa fa-backward"></i> 返回
                    </a>
                </div>
            </div>
            <div class="ibox-content" style="position: relative;">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-1 control-label">车牌号</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->car_no?></label>
                        </div>
                        <label class="col-sm-1 control-label">车架号</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->car_frame_no?></label>
                        </div>
                        <label class="col-sm-1 control-label">发动机号</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->engine_no?></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">车主</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->car_man?></label>
                        </div>
                        <label class="col-sm-1 control-label">联系电话</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->car_man_phone?></label>
                            <a class="btn btn-primary btn-bitbucket" onclick="sendMsg('ATD','<?=$car_info->car_man_phone?>')">
                                <i class="fa fa-phone"></i>
                            </a>
                            <a class="btn btn-danger btn-bitbucket" onclick="sendMsg('ATH')">
                                <i class="fa fa-close"></i>
                            </a>
                        </div>
                        <label class="col-sm-1 control-label">证件号码</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->car_man_number?></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">厂牌型号</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->factory_model?></label>
                        </div>
                        <label class="col-sm-1 control-label">初登日期</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->register_date?></label>
                        </div>
                        <label class="col-sm-1 control-label">座位数</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->seats?></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">车辆类型</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->carType?$car_info->carType->name:''?></label>
                        </div>
                        <label class="col-sm-1 control-label">运营性质</label>
                        <div class="col-sm-3">
                            <label class="form-control-static"><?=$car_info->carUse?$car_info->carUse->name:''?></label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label">备注</label>
                        <div class="col-sm-11">
                            <label class="form-control-static"><?=$car_info->remark?></label>
                        </div>
                    </div>
                </form>
                <div id="qrcode" style="
                    position: absolute;
                    right: 0;
                    bottom: 0;
                    border: 2px solid #000;
                    padding: 5px;
                    width: 142px;
                    height: 142px;
                "></div>
            </div>
        </div>
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>联系人</h5>
                <div class="ibox-tools">
                    <a class="btn btn-primary btn-xs edit-btn" data-id="0" href="javascript:void(0);" onclick="linkmanDlg()">
                        <i class="fa fa-user"></i> 添加新联系人
                    </a>
                </div>
            </div>
            <div class="ibox-content">
                <form class="form-horizontal">
                    <?php foreach($link_men as $link) { ?>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">姓名</label>
                            <div class="col-sm-2">
                                <label class="form-control-static"><?=$link->name?></label>
                            </div>
                            <label class="col-sm-1 control-label">联系电话</label>
                            <div class="col-sm-2">
                                <label class="form-control-static"><?=$link->phone?></label>
                                <a class="btn btn-primary btn-bitbucket" onclick="sendMsg('ATD','<?=$link->phone?>')">
                                    <i class="fa fa-phone"></i>
                                </a>
                                <a class="btn btn-danger btn-bitbucket" onclick="sendMsg('ATH')">
                                    <i class="fa fa-close"></i>
                                </a>
                            </div>
                            <label class="col-sm-1 control-label"><?=$link->id_type?></label>
                            <div class="col-sm-2">
                                <label class="form-control-static"><?=$link->id_number?></label>
                            </div>
                            <label class="col-sm-1 control-label">关系</label>
                            <div class="col-sm-2">
                                <label class="form-control-static"><?=$link->type?></label>
                            </div>
                        </div>
                    <?php } ?>
                </form>
            </div>
        </div>
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>保险业务</h5>
                <div class="ibox-tools">
                    <a class="btn btn-primary btn-xs edit-btn" data-id="0" href="javascript:void(0);" onclick="insurerDlg()">
                        <i class="fa fa-plus"></i> 投保
                    </a>
                </div>
            </div>
            <div class="ibox-content">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-1 control-label">商业止保</label>
                        <div class="col-sm-3">
                            <label class="form-control-static text-danger"><?=$car_info->insurer1_date?></label>
                        </div>
                        <label class="col-sm-1 control-label">交强止保</label>
                        <div class="col-sm-3">
                            <label class="form-control-static text-danger"><?=$car_info->insurer2_date?></label>
                        </div>
                        <label class="col-sm-1 control-label">保险公司</label>
                        <div class="col-sm-3">
                            <label class="form-control-static text-danger"><?=$car_info->company?></label>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>预约记录</h5>
                <div class="ibox-tools">
                    <a class="btn btn-primary btn-xs edit-btn" data-id="0" href="javascript:void(0);" onclick="invalidDlg()">
                        <i class="fa fa-ban"></i> 无效数据
                    </a>
                    <a class="btn btn-primary btn-xs edit-btn" data-id="0" href="javascript:void(0);" onclick="appointmentDlg()">
                        <i class="fa fa-calendar"></i> 预约
                    </a>
                    <a class="btn btn-primary btn-xs edit-btn" data-id="0" href="javascript:void(0);" onclick="refreshList()">
                        <i class="fa fa-refresh"></i> 刷新
                    </a>
                </div>
            </div>
            <div class="ibox-content">
                <div class="row">
                    <div class="col-sm-12">
                        <table id="listTable">
                            <thead>
                            <tr>
                                <th data-field="pdate">预约日期</th>
                                <th data-field="ptime">预约时间</th>
                                <th data-field="remark">预约备注</th>
                                <th data-field="user">业务员</th>
                                <th data-field="updated_at">操作时间</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php $this->beginBlock('footer_js'); ?>
    <script src="/js/jquery.qrcode.min.js"></script>
    <script src="/assets/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
    <script src="/assets/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
    <script src="/assets/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
    <script>
        var o = {};
        function insurerDlg() {
            title = '投保';
            layer_show(title, '/car/order-add?car_id=<?=$car_info->id?>');
        }
        function invalidDlg() {
            title = '无效数据';
            layer_show(title, '/car/invalid-car?car_id=<?=$car_info->id?>');
        }
        function appointmentDlg() {
            title = '添加预约';
            layer_show(title, '/appointment/add?car_id=<?=$car_info->id?>');
        }
        function linkmanDlg() {
            title = '添加联系人';
            layer_show(title, '/car/linkman-add?car_id=<?=$car_info->id?>');
        }
        function opDlg() {
            title = '操作记录';
            layer_show(title, '/car/history?car_id=<?=$car_info->id?>');
        }
        function refreshList() {
            $('#listTable').bootstrapTable('refresh');
        }
        function search() {
            $('#listTable').bootstrapTable('destroy');
            $('#listTable').bootstrapTable({
                url: "/appointment/history-json",
                pagination: true,
                sidePagination: 'server',
                multipleSelectRow: true,
                queryParams: function(params) {
                    o['car_id'] = '<?=$car_info->id?>';
                    o['limit'] = params['limit'];
                    o['offset'] = params['offset'];
                    return o;
                }
            });
        }
        $(function() {
            $('#qrcode').qrcode({
                text: window.location.origin + '/phone.html?car_id=<?=$car_info->id?>&user_id=<?=$user_id?>',
                width: 128,
                height: 128,
            });
            search();
            wsInit();
            $.get('/car/bihu-search?id=<?=$car_info->id?>',function(obj) {
                console.log(obj);
            }, 'json');
        });
    </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() 方法发送数据
            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_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(); ?>