//上传材料div--fengrui 2017-12-19

var prefix = "/Ytj/GsYw/";
//材料类别(根据行业大类获得)
function getcllist() {
    $('#clscPaibutton').hide();
    var hyid = $("#HY1").val();
    var url=prefix+"GetCailiao";
    var data={"hyid":hyid};
    $.ajax({
        url: url,
        type: "get",
        dataType: 'json',
        data: data,
        success: function (data) {
            if (data != "" && data != null) {
               // alert("所需材料个数：" + data.Table.length);
                if (data.length > 0) {
                    var cltypeHtml = "";
                    var clname = data[0].name;
                    //alert("所需材料：" + clname);
                    if (clname != "") {
                        var strs = []; //定义一数组
                        strs = clname.split(","); //字符分割
                        var cllistdiv = '';
                        for (i = 0; i < strs.length ; i++) {
                            cltypeHtml += '<a class="box" href="#">' + (i + 1) + '.' + strs[i] + '</a>';
                            cllistdiv += '<div class="secondInfo" style="display: none;">';
                            cllistdiv += '  <p>已有【<span id="clscRight'+i+'_clnum">0</span>】个材料扫描件</p>';
                            cllistdiv += '  <div class="bannerBox" id="clscRight'+i+'"></div>';
                            cllistdiv += '</div>';
                        }
                        if ($("#cltype").html() == "")
                            $("#cltype").html(cltypeHtml);
                        if ($("#clContainer").html() == "")
                            $("#clContainer").html(cllistdiv);

                        $(".container1>div").hide();
                        if (strs.length > 0) {
                            cl_openidA4photos(2);//打开身份证摄像头

                            $("#cltype>a:eq(0)").attr("class", "book");//默认展开第一个
                            $(".container1>div:eq(0)").show();
                            voicezdyStr('请在扫描区域扫描身份证正反面');

                            setTimeout(function () {
                                $('#clscPaibutton').attr("onclick", "javascript:cl_takePhotos('" + strs[0] + "',0);");
                                $('#clscPaibutton').val("拍照");
                                $("#clscPaibutton").show();
                            }, 2000);
                        }
                    }
                    $(".navContent a").click(function () {
                        $("#clscPaibutton").hide();
                        $("#cltype>a").attr("class", "box");//默认都不选中

                        var n = $(".navContent a").index(this);
                        $(".navContent a").index(this);
                        $(".container1>div").hide();
                        $(".container1>div:eq(" + n + ")").show();
                        $("#cltype>a:eq(" + n + ")").attr("class", "book");//选中样式
                        var clname = $(".navContent a:eq(" + n + ")").html();
                        clname = clname.substring(clname.indexOf(".") + 1, clname.length);
                        //stopCamear();
                        checkLeave();//关闭摄像头
                        if (n == 0) {
                            cl_openidA4photos(2);//打开身份证摄像头
                            setTimeout(function () {

                                $('#clscPaibutton').attr("onclick", "javascript:cl_takePhotos('" + clname + "'," + n + ");");
                                $('#clscPaibutton').val("拍照");
                                $("#clscPaibutton").show();
                            }, 2000);
                        }
                        else {
                            cl_openidA4photos(1);//打开身份证摄像头
                            setTimeout(function () {
                                $('#clscPaibutton').attr("onclick", "javascript:cl_takePhotos('" + clname + "'," + n + ");");
                                $('#clscPaibutton').val("拍照");
                                $("#clscPaibutton").show();
                            }, 2000);
                        }
                    })
                }
                else {
                    $("#cltype").html("无配置材料");
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            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 layout = { 'openType': opentype, 'layoutX': 700, 'layoutY': 315, 'layoutWidth': 300, 'layoutHeight': 490, 'choiceCamera': '1' };
    var jsonlayout = JSON.stringify(layout);
    send_command(
        "openCamera",
        jsonlayout,
        function (receivedData) {
            show_ca();
        });
}

//材料上传--拍照
function cl_takePhotos(clname, cltypeindex) {//材料名称，材料index
    //addPhoto_cl(clname, cltypeindex, "../../Content/images/chuan.png");
    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);
            }
            else {
                //alert("材料拍照失败");
                voicezdyStr('材料拍照失败');
            }
        });
}
function addPhoto_cl(clname, cltypeindex, base64) {
    var maxid = 0;
    var cldivNum = $("#clscRight" + cltypeindex).children('div').length;//材料扫描件个数
    //alert("子元素div个数："+$("#clscRight" + cltypeindex).children('div').length);
    if (cldivNum == 0)
        maxid += 1;
    else {
        var lastid = $("#clscRight" + cltypeindex).children("div:first-child").attr("id"); //clscRight0_0
        lastid = lastid.substring(lastid.indexOf("_") + 1, lastid.length);
        maxid = parseInt(lastid) + 1;
    }
    var cldivid = "clscRight" + cltypeindex + "_" + maxid;
    var cldiv = '<div style="position:relative;" class="firstBanner" id="' + cldivid + '"><img src="' + base64 +
        '"/><img style="position:absolute;left:350px;top:0px;width:40px;height:40px;z-index:100;" src="/YtjCommon/img/guan.png" onclick="delCL1(\''
        + clname + '\',\'' + maxid + '\',\'' + cldivid + '\')" /><a onclick="delimg(this)" hidden="hidden">点击此处删除图片</a><img style="width:40px;height:40px;" src="/YtjCommon/img/loading.gif"/></div>';
    alert("添加img")
    $("#clscRight" + cltypeindex).prepend(cldiv);
    $("#clscRight" + cltypeindex + "_clnum").html(cldivNum+1);
    addphotoSjk(clname, maxid, base64,cldivid);
    if ((cldivNum + 1) >= 2)
        $("#clscPaibutton").hide();
}
var lock=true;
//保存拍照材料到数据库
function addphotoSjk(clname, maxid, base64,cldivid) {
    if(lock){
        lock=false
    var formData = new FormData();

    formData.append("bsid", $("#bsid").val());
    formData.append("clname", clname);
    formData.append("clindex", maxid);
    formData.append("clsrc", base64);
    formData.append("xgflg", $("#xgflg").val());
var url=prefix+"SendCailiao";
    //ajax 提交form
    $.ajax({
        url: url,
        type: "POST",
        data: formData,
        dataType: "text",
        processData: false,        // 告诉jQuery不要去处理发送的数据
        contentType: false,        // 告诉jQuery不要去设置Content-Type请求头

        success: function (data) {
            lock=true
            if (data=='1') {
                $("#" + cldivid).children("img:last-child").attr("src", "/YtjCommon/img/normal.png"); //clscRight0_0
            }
            else {
                $("#" + cldivid).children("img:last-child").attr("src", "/YtjCommon/img/del.png"); //clscRight0_0
            }
        },
        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);
             lock=true
            return xhr;
        }
    });
    }
}

function delimg(e) {
    alert($(e).text())
    $(e).parent().remove()
}
function delCL1(clname, clindex, cldivid) {
    jqueryAlert({
        'icon': '/YtjCommon/img/loading.gif',
        'content': '请稍后，材料删除中',
        'closeTime': 4000,
    })
    var url=prefix+"DelCailiao";
    $("#" + cldivid).children("img:last-child").attr("src", "/YtjCommon/img/loading.gif");
    var bsid = $("#bsid").val();
    var data = {"bsid":bsid,"clname":encodeURI(clname),"clindex":clindex};
    $.ajax({
        url:url,
        data:data,
        type: 'post',
        cache: false,
        success: function(data) {
            if (data == '1') {
                $("#" + cldivid).children("img:last-child").attr("src", "/YtjCommon/img/normal.png");
                $("#" + cldivid).remove();
                var cldiv = cldivid.substring(0, cldivid.indexOf('_'));
                var cldivNum = $("#" + cldiv).children('div').length;//材料扫描件个数
                $("#" + cldiv + "_clnum").html(cldivNum);
                $("#clscPaibutton").show();
            } else {
                alert("原因：" + obj.Message);
                $("#" + cldivid).children("img:last-child").attr("src", "/YtjCommon/img/warning.png");
            }
        }
    });
}

//上传材料--上一页、下一页判断
function cailiaoleve(stepid) {
    if (stepid == "5") {
        var cldivNum = $("#clContainer").children('div').length;//材料分类个数
        if (cldivNum > 0) {
            for (var k = 0; k < cldivNum; k++) {
                if ($("#clscRight" + k).children('div').length == 0) {
                    var clname = $(".navContent a:eq(" + k + ")").html();
                    clname = clname.substring(clname.indexOf(".") + 1, clname.length);
                    voicezdyStr('请在扫描区域扫描' + clname);
                    return false;
                }
                else {
                    if (k == 0 && $("#clscRight" + k).children('div').length != 2) {
                        voicezdyStr('请在扫描区域扫描身份证正反面');
                        return false;
                    }
                }
            }
        }
    }
    //stopCamear();//关闭高拍仪
    checkLeave();//关闭摄像头
    go_step(stepid);
}


//“多证合一”整合证照事项信息表
function getzhengzhaolist() {
    $.ajax({
        url: "GetZhengzhao",
        type: "get",
        dataType: 'json',
        data: "{}",
        success: function (data) {
            if (data != "" && data != null) {
                if ($("#zhengzhao").html() == "" || $("#zhengzhao").html() == "<tr><td colspan='2'>无整合证照事项</td></tr>") {
                $("#zhengzhao").html("");
                //alert("zz:"+data.Table.length);
                if (data.Table.length > 0) {
                    $.each(data.Table, function (i, item) {
                        var checkDis = "";
                        var html0 = "<tr id='zztr_" + (i + 1) + "'><td width='5%' style='text-align:center;'>";
                        if (i == 0 || i == 1) {
                            checkDis = " checked disabled='true'";
                        }
                        html0 += "<input name='zz' type='checkbox' id='zz_" + (i + 1) + "' value='" + item.ID + "' " + checkDis + "/><i class='fa fa-2x icon-checkbox'></i></td>";
                        html0 += "<td width='25%' align='left'>" + item.zzShixiang + "</td></tr>";
                        $("#zhengzhao").append(html0);
                    })
                }
                else {
                    $("#zhengzhao").html("<tr><td colspan='2'>无整合证照事项</td></tr>");
                }
                }
                //多证合一  阻止tr中的checkbox冒泡事件
                    $("#qrtjdiv table tbody tr input[type='checkbox']").click(function (e) {
                        e.stopPropagation();
                    });
                //多证合一 点击整行选中
                    $("#qrtjdiv table tbody tr").click(function () {
                        var trindex = $(this).attr("id");
                        //alert("当前行id:" + trindex);
                        trindex = trindex.substring(trindex.indexOf('_') + 1, trindex.length);
                        if (document.getElementById('zz_' + trindex).disabled != true) {
                            if (document.getElementById('zz_' + trindex).checked) {
                                document.getElementById('zz_' + trindex).checked = false;
                            }
                            else {
                                document.getElementById('zz_' + trindex).checked = true;
                            }
                        }
                    });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("数据加载失败")
        }
    });
}
