var intSendNum = 0;//     WU_FILE_0
var m_strSelHisSendFileid = "";
var m_strSelHisSendFilename = "";
//选择发送图片消息
function SelectImageMsg() {
    $("#HidUploadContentLength").val("");
    var strLocalImgUrlJson = $.parseJSON(talkObj.selectpicmsgfile());
    if (strLocalImgUrlJson[3].length > 0) {
        ShowError("myPageAlert", strLocalImgUrlJson[3]);
        return;
    }
    var strImagePath = strLocalImgUrlJson[0];
    if (strImagePath.length > 0) {
        $("#spSendImageMsg").html(strImagePath);
        $("#imgSendImageMsg").attr("src", "data:image/png;base64," + strLocalImgUrlJson[1]);
        $("#HidUploadContentLength").val(strLocalImgUrlJson[2]);
        $("#sendImageModel").modal("show");
        $("#spSendqqcutshowMsg").hide();
        $("#spSendImageMsg").show();
    }
}


//选择发送文件消息
function Selectfile() {
    $("#HidUploadContentLength").val("");
    var strLocalfileUrlJson = talkObj.selectpicmsgfiles();
    //if (strLocalfileUrlJson[3].length > 0) {
    //    ShowError("myPageAlert", strLocalfileUrlJson[3]);
    //    return;
    //}
    //var strImagePath = strLocalfileUrlJson[0];
    if (strLocalfileUrlJson.length > 0) {
    	$("#spSendfileMsg").html(strLocalfileUrlJson);
        //$("#imgSendfileMsg").attr("src", "data:image/png;base64," + strLocalfileUrlJson[1]);
        //$("#HidUploadContentLength").val(strLocalfileUrlJson[2]);
        $("#sendfileModel").modal("show");
       
    }
}

//确认发送图片消息(上传到服务端)
function SendImageMsg(){
    var strImageUrl = $("#spSendImageMsg").html();
    var objhidDocUin = $("#hidDocUin");
    if (strImageUrl.length == 0) {
        return;
    }
    var strParaJson = $.toJSON({
        uin: objhidDocUin.val(),
        file: strImageUrl
    });

    var vcontentlength = $("#HidUploadContentLength").val();
    if (vcontentlength == 0) {
        ShowMsg("myPageAlert", "所发送文件没有任何内容！");
        $("#HidUploadContentLength").val("");
    } else {
        talkObj.sendimgmessage(strParaJson);
    }
    $("#spSendImageMsg").html("");
    $("#imgSendImageMsg").attr("src", "");
    $("#sendImageModel").modal("hide");
}

//发送文件(上传到服务端)
function SendfileMsg() {

	var strImageUrl = $("#spSendfileMsg").html();
	var objhidDocUin = $("#hidDocUin");
	if (strImageUrl.length == 0) {
		return;
	}
	var strParaJson = $.toJSON({
		uin: objhidDocUin.val(),
		file: strImageUrl
	});

	var vcontentlength = $("#HidUploadContentLength").val();


	talkObj.sendimgmessage(strParaJson);
	$("#spSendImageMsg").html("");
	$("#imgSendImageMsg").attr("src", "");
	$("#sendImageModel").modal("hide");
}

//文件上传到服务端回调
function AsyncSendImgMessageResult(jsonResult) {
  
    if (jsonResult.success) {
        //发送消息
        if ($("#divMsg" + jsonResult.msgid).length > 0) {
            $("#divMsg" + jsonResult.msgid).remove();
        }
       // $("#divTalkContent").append(MakeNewMsgHtml(jsonResult));
        document.getElementById("divTalkContent").scrollTop = document.getElementById("divTalkContent").scrollHeight;
        SendImgMsg(jsonResult.msgid, jsonResult.filename, jsonResult.filename);//发送图片消息
    } else {
        ShowError("myPageAlert", "发送文件信息异常：" + jsonResult.error);
    }

}
//发送文件
function SendImgMsg(msgid, localfile, filename) {
    
    var objhidDocUin = $("#hidDocUin");
    var objhidDocWechatKey = $("#hidDocWechatKey");
    var objhidDocUserName = $("#hidDocUserName");
    var objhidDocPid = $("#hidDocPid");
    var strParaJson = $.toJSON({
        wechatkey: objhidDocWechatKey.val(),
        uin: objhidDocUin.val(),
        username: objhidDocUserName.val(),
        pid: objhidDocPid.val(),
        fileid: "WU_FILE_" + intSendNum,
        filename: filename,
        msgid: msgid,
        content: filename
    });
    talkObj.sendmedia(strParaJson);
    intSendNum++;
}

//显示语音录制
function ShowSound() {
    var objhidDocWechatKey = $("#hidDocWechatKey");
 
    var objhidDocUserName = $("#hidDocUserName");
    var objhidDocUin = $("#hidDocUin");
    var objhidDocPid = $("#hidDocPid");
    var strParaJson = $.toJSON({
        username: objhidDocUserName.val(),
        uin: objhidDocUin.val(),
        content: "",
        wechatkey: objhidDocWechatKey.val(),
        pid: objhidDocPid.val(),
        fileid: "WU_FILE_" + intSendNum,
    });
    talkObj.showsoundform(strParaJson);
    intSendNum++;
}


//处理QQ截图数据，复制文件，复制图片（类型type  图片 1，小文件 2，大文件 3）
function AsyncPasteImgTextResult(jsonResult) {
    if (jsonResult.success) {
        //文件名为空，复制的文本内容
        if (jsonResult.filename == "") {
            return;
        }
        if (jsonResult.type == "1") {
            //图片复制
            if (jsonResult != null) {
                var jsonResultfilename = $.parseJSON(jsonResult.filename);
                if (jsonResultfilename[3].length > 0) {
                    ShowError("myPageAlert", jsonResultfilename[3]);
                    return;
                }
                var strImagePath = jsonResultfilename[0];
                if (strImagePath.length > 0) {
                    $("#spSendqqcutshowMsg").show();
                    $("#spSendqqcutshowMsg").html("截图图片或黏贴图片");
                    $("#spSendImageMsg").hide();
                    $("#spSendImageMsg").html(strImagePath);
                    $("#imgSendImageMsg").attr("src", "data:image/png;base64," + jsonResultfilename[1]);
                    $("#HidUploadContentLength").val(jsonResultfilename[2]);
                    $("#sendImageModel").modal("show");
                }
            }
        } else if (jsonResult.type == "2") {
            //小文件复制
            var strLocalfileUrlJson = jsonResult.filename;
            if (strLocalfileUrlJson.length > 0) {
                $("#spSendfileMsg").html(strLocalfileUrlJson);
                $("#sendfileModel").modal("show");
            }
        } else {
            //大文件文件复制
            var strLocalfileUrlJson = jsonResult.filename;
            if (strLocalfileUrlJson.length > 0) {
                $("#bigfilemodal").modal("hide");
                $("#spSendBigfileMsg").html(strLocalfileUrlJson);
                $("#sendbigfileModel").modal("show");
            }
        }
    } else {
        ShowError("myPageAlert", "发送失败:" + jsonResult.error);
    }
}

//发送大文件
function SendBigfileMsg() {
	var objhidDocUin = $("#hidDocUin");
	var objhidDocWechatKey = $("#hidDocWechatKey");
	var objhidDocUserName = $("#hidDocUserName");
	var onjfilebigname = $("#spSendBigfileMsg").html();
	var objhidDocPid = $("#hidDocPid");
	var strParaJson = $.toJSON({
		isbatch: m_IsBatch,
		wechatkey: objhidDocWechatKey.val(),
		uin: objhidDocUin.val(),
		username: objhidDocUserName.val(),
		pid: objhidDocPid.val(),
		content: onjfilebigname,
		filename:onjfilebigname
	});
	talkObj.sendbigmedia(strParaJson);
	intSendNum++;
}

//********************************************发送大文件**********************************************************
//绑定历史发送的文件
function BindHisSendFile() {
	talkObj.gethisfilelist();
	//$(".talk-zhezhao").show();
	//$("#iLoadFriendsTurn").show();

}
//历史发送过的文件
function AsyncGetHisFileListResult(jsonResult) {
	var strContent = "";
	strContent += "<li onclick=\"SelectBigfile()\"><i class=\"icon iconfont icon-add\"></i></li>";
	if (jsonResult.success) {
		for (var i = 0; i < jsonResult.result.list.length; i++) {
			var jsonitem = jsonResult.result.list[i];
			var filesize = "";
			if (jsonitem.filegsize.length > 0) {
				if (Number(jsonitem.filegsize) > 1024) {
					filesize = (Number(jsonitem.filegsize) / 1024 / 1024).toFixed(2) + "M";
				} else {
					filesize = (Number(jsonitem.filegsize) / 1024).toFixed(2) + "KB";
				}
			}
				//= jsonitem.filegsize.length >0 ? (Number(jsonitem.filegsize) / 1024 / 1024).toFixed(4) + "M" : "";
			strContent += "<li id=\"liSelectThisBigFile" + jsonitem.fileid + "\"  style=\"cursor:pointer;\" onclick=\"clickLiSelectThisBigFile('" + jsonitem.fileid + "','" + jsonitem.filename + "')\">"
			strContent += "<img src=\"../img/file.png\" height=\"60\" class=\"pull-left\"><div>"
			strContent += "<h2>" + jsonitem.filename + "</h2>"
			strContent += "<p>" + jsonitem.uploadtime + "</p>"
			strContent += "<p>" + filesize + "<span class=\"pull-right\" onclick=\"DeleteHisFile('" + jsonitem.fileid + "')\"><i class=\"icon iconfont icon-delete1\"></i></span></p>"
			strContent += "</div>"
			strContent += "</li>"
		}
	} else {
		ShowError("myPageAlert", "获取历史发送过的文件异常：" + jsonResult.error);
	}
	$(".bigfilelilst").html(strContent);
	$("#bigfilemodal").modal("show");
	//$(".talk-zhezhao").hide();
	//$("#iLoadFriendsTurn").hide();

}

//选择发送大文件
function SelectBigfile() {
	var strLocalfileUrlJson = talkObj.selectpicmsgfiles();
	if (strLocalfileUrlJson.length > 0) {
		$("#bigfilemodal").modal("hide");
		$("#spSendBigfileMsg").html(strLocalfileUrlJson);

		$("#sendbigfileModel").modal("show");
	}
}

//选择历史发送过的大文件
function clickLiSelectThisBigFile(fileid,filename) {
	m_strSelHisSendFileid = fileid;
	m_strSelHisSendFilename = filename;
	$(".bigfilelilst li").each(function () {
		$(this).removeClass("active");
	});
	$("#liSelectThisBigFile" + fileid).addClass("active");
}
//删除历史上传过的文件
function DeleteHisFile(fileid) {
	var strParaJson = $.toJSON({
		fileid: fileid,
	});
	talkObj.deletefile(strParaJson);
}
//删除历史上传过的文件回调
function AsyncDeleteFileResult(jsonResult) {
	if (!jsonResult.success) {
		ShowError("myPageAlert", "获取历史发送过的文件异常：" + jsonResult.error);
	} else {
		BindHisSendFile();
	}

	m_strSelHisSendFileid = "";
	m_strSelHisSendFilename = "";
}
//发送历史发送过的文件
function BtnSendHisFile() {

	if (m_strSelHisSendFileid.length < 1 || m_strSelHisSendFilename.length < 1) {
		ShowMsg("myPageAlert", "没有选中任何文件！");
		return;
	}
	var objhidDocUin = $("#hidDocUin");
	var objhidDocWechatKey = $("#hidDocWechatKey");
	var objhidDocUserName = $("#hidDocUserName");
	var onjfilebigname = $("#spSendBigfileMsg").html();
	var objhidDocPid = $("#hidDocPid");
	var strParaJson = $.toJSON({
		fileid: m_strSelHisSendFileid,
		filename: m_strSelHisSendFilename,
		isbatch: m_IsBatch,
		wechatkey: objhidDocWechatKey.val(),
		uin: objhidDocUin.val(),
		username: objhidDocUserName.val(),
		pid: objhidDocPid.val(),
		content: onjfilebigname,
		msgtype:1,
	});
	$("#bigfilemodal").modal("hide");
	talkObj.sendhisbigmedia(strParaJson);
	
}

//取消发送
function BtnCancleSendHisFile() {
	m_strSelHisSendFileid = "";
	m_strSelHisSendFilename = "";
}

//********************************************发送大文件**********************************************************

function Delfile(uin, msgid, filename) {
	var strpid = $("#hidDocPid").val();

	var strParaJson = $.toJSON({
		uin: uin,
		pid: strpid,
		msgid: msgid,
		suffixname: filename
	});
	talkObj.deletetalkfile(strParaJson);
}

function AsyncDeleteTalkFileResult(jsonResult) {
	if (jsonResult.success) {
		ShowMsg("myPageAlert", "操作成功！");
		$("#divMsg" + jsonResult.msgid).remove();
	} else {
		ShowError("myPageAlert", "操作失败：" + jsonResult.error);
	}
}


