function logger(msg) {
    document.getElementById("logger").innerHTML =  msg;
}


function addPhoto(base64) {
    var picLayout = document.getElementById("pic-layout");
    var li = document.createElement("li");
    var img = document.createElement("img");
    img.setAttribute("src", base64);
    li.appendChild(img);
    picLayout.appendChild(li);
}


//离开页面关闭高拍仪
function checkLeave() {
    send_command(
        "closeCamera",
        function (data) {
            // logger(data);
    });
}

//打开高拍仪、设置相对位置、窗口大小
function A3takePhotos() {
    logger('打开A3拍照');
    var layout={'openType':0,'layoutX': 50,'layoutY': 100,'layoutWidth': 690,'layoutHeight': 550,'choiceCamera':'1'};
    var jsonlayout=JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            logger(receivedData);
        });
}

function A4takePhotos() {
    logger('打开A4拍照');
    var layout={'openType':1,'layoutX': 50,'layoutY': 100,'layoutWidth': 690,'layoutHeight': 550,'choiceCamera':'1'};
    var jsonlayout=JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            logger(receivedData);
        });
}

function IdtakePhotos() {
    logger('打开身份证拍照');
    var layout={'openType':2,'layoutX': 50,'layoutY': 100,'layoutWidth': 690,'layoutHeight': 550,'choiceCamera':'1'};
    var jsonlayout=JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            logger(receivedData);
        });
}

//扫描二维码
function takeQr() {
    logger('扫描二维码');

    var layout={'openType':5,'layoutX': 50,'layoutY': 100,'layoutWidth': 690,'layoutHeight': 550,'choiceCamera':'1'};

    var jsonlayout=JSON.stringify(layout);
    send_command(
        "scanQR",
        jsonlayout,
        function (receivedData) {
            logger(receivedData);
        });
}

//高拍仪
function camear1() {
    logger('高拍仪1');

    var layout={'openType':0,'layoutX': 50,'layoutY': 100,'layoutWidth': 690,'layoutHeight': 550,'choiceCamera':'1'};

    var jsonlayout=JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            logger(receivedData);
        });
}
function camear2_old() {
    logger('高拍仪2');

    var layout={'openType':0,'layoutX': 50,'layoutY': 100,'layoutWidth': 690,'layoutHeight': 550,'choiceCamera':'0'};

    var jsonlayout=JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            logger(receivedData);
        });
}




//关闭高拍仪
function stopCamear() {
    closeCamera();
}

function facerRcognition() {
    logger('获取拍摄的第一张和第二张,进行人脸识别');

    var base= [];
    var i=0;
    var lis=$('#pic-layout li img');
    lis.each(function () {
        base[i]=$(this).attr('src');
        i=i+1;
    });

    var strs;
    strs = base[0]+'**'+base[1];
    // strs = base[0];
    save_info(strs);
    delete strs;

    closeCamera();

    window.location.href = 'faceRecognition.html';
}

//关闭相机
function closeCameras() {
    logger('关闭');

    var base= [];
    var i=0;
    var lis=$('#pic-layout li img');
    lis.each(function () {
        base[i]=$(this).attr('src');
        i=i+1;
    });

    // logger(base[0]);

    var strs = [];
    for(var j=0;j<base.length;j++){
        if(base.length===1){
            strs = base[0];
        }else{
            str ="*"+base[j];
            strs += str;
        }
    }
    //删除变量
    delete base;

    //保存数据
    save_info(strs);
    strss='"'+strs+'"';

    delete strs;

    //把空格转换@;
    arrstr=strss.replace(/\s+/g,'@');

    delete strss;


    $.ajax({
        url:'./././upload.php',
        type:'post',
        dataType:'json',
        data:'img='+arrstr,
        success:function () {
            // alert('成功');
        },
        error:function () {
            // alert('错误');
        }
    });

    closeCamera();

    window.location.href = 'sweepattachment.html';
}


function rzdbCamera(flag) {
    if (flag == 0) {
        //打开摄像头
        camear2();
    }
    else if (flag == 1) {
        //拍照
        takePhotos();
        stopCamear();
    }
}
function camear2() {
    var layout = { 'openType': 0, 'layoutX': 750, 'layoutY': 350, 'layoutWidth': 400, 'layoutHeight': 400, 'choiceCamera': 0 };
    var jsonlayout = JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            show_ca();
            setTimeout(function () {
                $('#opencamearbutton').attr("onclick", "javascript:rzdbCamera(1);");
                $('#opencamearbutton').val("拍照");
            }, 2000);
        });
}
function addPhoto_new(base64) {
    $("#rzdb_pai").attr('src', base64);//认证对比-前置摄像头拍照
    $("#paizhao").val(base64);
}
//开始拍照
function takePhotos() {
    send_command(
        "takePhoto",
        function (receivedData) {
            var photoBean = JSON.parse(receivedData);
            if (photoBean.status == 100) {
                var base64 = "data:image/jpeg;base64," + photoBean.imgStr;
                addPhoto_new(base64);
                $('#rzdb_pai').attr('width', "280px");
                $('#rzdb_pai').attr('heihgt', "180px");
                $('#opencamearbutton').attr("onclick", "javascript:rzdbCamera(0);");
                $('#opencamearbutton').val("重新拍照");
                rszb();//自动进行对比
            }
            else {
                alert("拍照失败");
            }
        });
}



//材料上传
//材料上传--拍照
function cl_openidA4photos(opentype) {
    //alert("摄像头类型：" + opentype);
    //openType:2-身份证，1-A4
    var layout = { 'openType': opentype, 'layoutX': 200, 'layoutY': 315, 'layoutWidth': 750, 'layoutHeight': 490, 'choiceCamera': '1' };
    var jsonlayout = JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            show_ca();
            //alert("打开摄像头返回数据："+receivedData);
            //logger(receivedData);
        });
}
function cl_takePhotos(clname, cltypeindex) {//材料名称，材料index
    send_command(
        "takePhoto",
        function (receivedData) {
            var photoBean = JSON.parse(receivedData);
            if (photoBean.status == 100) {
                var base64 = "data:image/jpeg;base64," + photoBean.imgStr;
                addPhoto_cl(clname, cltypeindex, base64);
                //$('#rzdb_pai').attr('width', "280px");
                //$('#rzdb_pai').attr('heihgt', "180px");
                //$('#opencamearbutton').attr("onclick", "javascript:rzdbCamera(0);");
                //$('#opencamearbutton').val("重新拍照");
            }
            else {
                alert("材料拍照失败");
            }
        });
}
function addPhoto_cl(clname, cltypeindex, base64) {
    picLayout = document.getElementById("clscRight" + cltypeindex);

    //alert("clscRight0子元素最后一个：" + $("#clscRight0").children("div:last-child").attr("id"));
    var lastid = $("#clscRight" + cltypeindex).children("div:last-child").attr("id"); //clscRight0_0
    lastid = lastid.substring(lastid.indexOf("clscRight" + cltypeindex) + 2, lastid.length);
    maxid = parseInt(lastid) + 1;
    var div = document.createElement("div");
    div.className = "firstBanner";
    div.id = "clscRight" + cltypeindex + "_" + maxid;
    var img = document.createElement("img");
    img.setAttribute("src", base64);
    div.appendChild(img);
    picLayout.appendChild(div);

    addphotoSjk(clname, maxid, base64);
}

//保存拍照材料到数据库
function addphotoSjk(clname, maxid, base64) {
    var formData = new FormData();

    formData.append("bsid", $("#bsid").val());
    formData.append("clname", clname);
    formData.append("clindex", maxid);
    formData.append("clsrc", base64);
    //formData.append("fileNum", arrPicNum);
    //for (var k = 0; k < lis.length; k++) {
    //    formData.append("file_" + k, convertBase64UrlToBlob(arrPath[k]), "YTJ-" + sxID + "-" + userID + "-" + KeyValue + "_" + k + "_" + new Date().getTime() + ".png");
    //}
    //ajax 提交form
    $.ajax({
        url: 'SendCailiao',
        type: "POST",
        data: formData,
        dataType: "text",
        processData: false,        // 告诉jQuery不要去处理发送的数据
        contentType: false,        // 告诉jQuery不要去设置Content-Type请求头

        success: function (data) {
            //document.getElementById('light').style.display = 'none';
            //document.getElementById('fade').style.display = 'none';

            var obj = $.parseJSON(data);
            //closeCameras(Row, sxID, userID);
        },
        xhr: function () {            //在jquery函数中直接使用ajax的XMLHttpRequest对象
            var xhr = new XMLHttpRequest();

            xhr.upload.addEventListener("progress", function (evt) {
                if (evt.lengthComputable) {
                    var percentComplete = Math.round(evt.loaded * 100 / evt.total);
                    console.log("正在提交." + percentComplete.toString() + '%');        //在控制台打印上传进度
                }
            }, false);

            return xhr;
        }
    });
}


