
stsAccessKeyId = ""
stsAccessKeySecret = ""
stsToken = ""
var checkpoint_temp;
var requestUrl;
var returnUrl;
var str;
var buckName='newxgccpm'

function multipartUploadWithSts(storeAs, file,attachmentId,attachmentFlag,uploadPath,divId,size, cpt) {
    console.log("-----------------------start");
    // let BosClient = baidubce.sdk.BosClient
    //     // let config = {
    //     //     endpoint: 'http://su.bcebos.com',
    //     //     credentials: {
    //     //         ak: '9915286aab2949949b489e8ab3f70a09',           //您的AK
    //     //         sk: '069036bdb93c4bc59c4995ccc543243e'       //您的SK
    //     //     }
    //     // }
    //     // let client = new BosClient(config);
        multitest(storeAs, file,attachmentId,attachmentFlag,uploadPath,divId,size, cpt);
};

function multitest (storeAs, file,attachmentId,attachmentFlag,uploadPath,divId, size,cpt) {
    // var buffer = new Buffer('123')
    var formdata = new FormData();
    formdata.append("storeAs",storeAs);
    formdata.append("file",file);
    var names =  storeAs.split("/");
    var name = names[names.length-1];
    $.ajax({
        type:'post',
        url:realPath+"/a/bos/upload",
        data:formdata,
        contentType: false,
        processData: false,
        // progress: function* (percent, cpt) {
        //     console.log(cpt);
        //     console.log('Progress: ' + percent);
        //     var p = Math.floor(percent*100);
        //     $("#jindutiao"+divId).attr("style","width: "+p+"%");
        //     $("#jindutiao"+divId).attr("aria-valuenow",p+"%");
        //     $("#baifenbi"+divId).html(p+"%");
        //     $("#fileName"+divId).html(name);
        // }
        success:function (data1) {
            if(data1.code==='1'){
                returnUrl = realPath+"/a/sys/workattachment/saveAttachment";
                var p = Math.floor(1*100);
                $("#jindutiao"+divId).attr("style","width: "+p+"%");
                $("#jindutiao"+divId).attr("aria-valuenow",p+"%");
                $("#baifenbi"+divId).html(p+"%");
                $("#fileName"+divId).html(name);
                $.ajax({
                    type:'post',
                    url:returnUrl,
                    data:{
                        "attachmentName":file['name'],
                        "attachmentId":attachmentId,
                        "attachmentFlag":attachmentFlag,
                        "url":data1.url
                    },
                    success:function(data){
                        // console.log('ajax requst : ' + data.status);
                        console.log('url : ' + data.url);
                        if(data.status=="Success"){
                            parent.layer.msg("上传成功！",{icon:1});
                            var lenght =$(".trIdAdds").length;
                            if (size != 0){
                                lenght += size;
                            }
                            var addFile = "addFile"+divId;
                            var str = '<tr class="trIdAdds">'+
                                '<td class="text-center">' ;
                            if (isContains("jpg,png,gif,bmp,jpeg",data.type)){
                                str = str +'<img src="'+data1.url+'" width="50" height="50" onclick="openDialogView(\'预览\',\''+realPath+ '/a/sys/picturepreview/picturePreview?url=' +data1.url+ '\',\'90%\',\'90%\')" alt="'+data.attachmentName+'"/>';
                            }else if(isContains("pdf",data.type)){
                                str = str + '<a class="attention-info" href="javascript:void(0)" onclick="preview(\'预览\',\''+data1.url+'\',\'90%\',\'90%\',\'1\')">'+data.attachmentName+'</a>';
                            }else {
                                str = str + '<a class="attention-info" href="javascript:void(0)" onclick="preview(\'预览\',\''+data1.url+'\',\'90%\',\'90%\')">'+data.attachmentName+'</a>';
                            }
                            str = str +'</td>'+
                                '<td >'+data.createByName+'</td>'+
                                '<td >'+data.createDate+
                                '<input name="workAttachments['+lenght+'].id" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value=""/>'+
                                '<input name="workAttachments['+lenght+'].attachmentId" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.attachmentId+'"/>'+
                                '<input name="workAttachments['+lenght+'].url" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data1.url+'"/>'+
                                '<input name="workAttachments['+lenght+'].attachmentName" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.attachmentName+'"/>'+
                                '<input name="workAttachments['+lenght+'].createBy.id" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.createBy+'"/>'+
                                '<input name="workAttachments['+lenght+'].type" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.type+'"/>'+
                                '<input name="workAttachments['+lenght+'].attachmentFlag" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.attachmentFlag+'"/>'+
                                '</td>'+
                                '<td class="op-td">'+
                                '<div class="op-btn-box">' +
                                // '<a href="javascript:location.href=\''+realPath+'/a/workfullmanage/workFullManage/downLoadAttach?file=\'+encodeURIComponent(\''+data.url+'\');" class="op-btn op-btn-download"><i class="fa fa-download"></i>&nbsp;下载</a>'+
                                '<a href="'+data1.url+'" class="op-btn op-btn-download"><i class="fa fa-download"></i>&nbsp;下载</a>'+
                                '<a href="javascript:void(0)" onclick="deleteFileFromAliyun(this,\''+realPath+'/a/sys/workattachment/deleteFileFromAliyun?url='+data.url+'&id='+data.id+'&type=2\',\''+addFile+'\',\''+divId+'\')" class="op-btn op-btn-delete" ><i class="fa fa-trash"></i>&nbsp;删除</a>'+
                                '</div>' +
                                '</td>'+
                                '</tr>';
                            console.log("file:"+divId);
                            console.log("str="+str);
                            $("#file"+divId).append(str);
                            // $("img").not(".upload_ico").each(function () {
                            //     var num = 1 ;
                            //     $(this).live("click",function () {
                            //         var flag = num%2;
                            //         if(flag == 1){
                            //             $(this).css("width","90%");
                            //             $(this).css("height","90%");
                            //             num++;
                            //         }else if(flag ==0){
                            //             $(this).css("width","50px");
                            //             $(this).css("height","50px");
                            //             num++;
                            //         }
                            //     })
                            // });
                        }else {
                            parent.layer.msg("上传失败！",{icon:2});
                        }
                    }
                })
            }else {
                parent.layer.msg("上传失败！",{icon:2});
            }
        }
    })
    // var names =  storeAs.split("/");
    // var name = names[names.length-1];
    // client.putObjectFromString(buckName,file, '/1/1'
    // ).then(function (result) {
    //     console.log("uploadPath:"+uploadPath);
    //     console.log("rusult:"+result);
    //     console.log("end-------result:");
    //     returnUrl = realPath+"/a/sys/workattachment/saveAttachment";
    //     $.ajax({
    //         type:'post',
    //         url:returnUrl,
    //         data:{
    //             "attachmentName":file['name'],
    //             "attachmentId":attachmentId,
    //             "attachmentFlag":attachmentFlag,
    //             "url":uploadPath
    //         },
    //         success:function(data){
    //             console.log('ajax requst : ' + data.status);
    //             if(data.status=="Success"){
    //                 parent.layer.msg("上传成功！",{icon:1});
    //                 var lenght =$(".trIdAdds").length;
    //                 if (size != 0){
    //                     lenght += size;
    //                 }
    //                 var addFile = "addFile"+divId;
    //                 var str = '<tr class="trIdAdds">'+
    //                     '<td class="text-center">' ;
    //                 if (isContains("jpg,png,gif,bmp,jpeg",data.type)){
    //                     str = str +'<img src="'+data.url+'" width="50" height="50" onclick="openDialogView(\'预览\',\''+realPath+ '/a/sys/picturepreview/picturePreview?url=' +data.url+ '\',\'90%\',\'90%\')" alt="'+data.attachmentName+'"/>';
    //                 }else if(isContains("pdf",data.type)){
    //                     str = str + '<a class="attention-info" href="javascript:void(0)" onclick="preview(\'预览\',\''+data.url+'\',\'90%\',\'90%\',\'1\')">'+data.attachmentName+'</a>';
    //                 }else {
    //                     str = str + '<a class="attention-info" href="javascript:void(0)" onclick="preview(\'预览\',\''+data.url+'\',\'90%\',\'90%\')">'+data.attachmentName+'</a>';
    //                 }
    //                 str = str +'</td>'+
    //                     '<td >'+data.createByName+'</td>'+
    //                     '<td >'+data.createDate+
    //                     '<input name="workAttachments['+lenght+'].id" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value=""/>'+
    //                     '<input name="workAttachments['+lenght+'].attachmentId" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.attachmentId+'"/>'+
    //                     '<input name="workAttachments['+lenght+'].url" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.url+'"/>'+
    //                     '<input name="workAttachments['+lenght+'].attachmentName" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.attachmentName+'"/>'+
    //                     '<input name="workAttachments['+lenght+'].createBy.id" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.createBy+'"/>'+
    //                     '<input name="workAttachments['+lenght+'].type" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.type+'"/>'+
    //                     '<input name="workAttachments['+lenght+'].attachmentFlag" type="hidden" readonly="readonly" maxlength="20" style="width:140px;" class="form-control" value="'+data.attachmentFlag+'"/>'+
    //                     '</td>'+
    //                     '<td class="op-td">'+
    //                     '<div class="op-btn-box">' +
    //                     '<a href="javascript:location.href=\''+realPath+'/a/workfullmanage/workFullManage/downLoadAttach?file=\'+encodeURIComponent(\''+data.url+'\');" class="op-btn op-btn-download"><i class="fa fa-download"></i>&nbsp;下载</a>'+
    //                     '<a href="javascript:void(0)" onclick="deleteFileFromAliyun(this,\''+realPath+'/a/sys/workattachment/deleteFileFromAliyun?url='+encodeURIComponent(data.url)+'&id='+data.id+'&type=2\',\''+addFile+'\',\''+divId+'\')" class="op-btn op-btn-delete" ><i class="fa fa-trash"></i>&nbsp;删除</a>'+
    //                     '</div>' +
    //                     '</td>'+
    //                     '</tr>';
    //                 console.log("file:"+divId);
    //                 console.log("str="+str);
    //                 $("#file"+divId).append(str);
    //                 // $("img").not(".upload_ico").each(function () {
    //                 //     var num = 1 ;
    //                 //     $(this).live("click",function () {
    //                 //         var flag = num%2;
    //                 //         if(flag == 1){
    //                 //             $(this).css("width","90%");
    //                 //             $(this).css("height","90%");
    //                 //             num++;
    //                 //         }else if(flag ==0){
    //                 //             $(this).css("width","50px");
    //                 //             $(this).css("height","50px");
    //                 //             num++;
    //                 //         }
    //                 //     })
    //                 // });
    //             }else {
    //                 parent.layer.msg("上传失败！",{icon:2});
    //             }
    //         }
    //     })
    // }).catch(function (err) {
    //     console.log("err------ err:");
    //     console.log(err);
    //     multipartUploadWithSts(storeAs, file,attachmentId,attachmentFlag,uploadPath, size,checkpoint_temp);
    // });
};

function isContains(str, substr) {
    return str.indexOf(substr) >= 0;
}

function deleteFileFromAliyun(obj,deleteUrl,addFile,divId){
    console.log("---------------------------------:");
    console.log(divId);
    console.log("---------------------------------");
    if (divId==null || divId== undefined || divId == ''){
        divId = "_attachment";
    }
    var fileDiv = divId.substr(1,divId.length)+"_file";
    console.log("---------------------------------:");
    console.log(fileDiv);
    console.log("---------------------------------");
    $("#"+fileDiv).val("");
    $.ajax({
        type:"post",
        url:deleteUrl,
        success:function(data){
            if(data.str){
                parent.layer.msg('删除成功',{icon:1});
                $(obj).parent().parent().parent().remove();
                $("#"+addFile).hide();
            }else {
                parent.layer.msg('删除失败',{icon:2});
            }
        }
    });
}