var appServer = url_b + '/oss/token?name=b';

var Buffer = OSS.Buffer;
var currentDeleteKey = null;
var logoSize = '?x-oss-process=image/resize,w_200,h_200';
var OSS = OSS.Wrapper;
var region = 'oss-cn-shenzhen';
var STS = OSS.STS;
var lsOrderId = '';




var progress = function(p) {//上传成功的进度条
    return function (done) {
        done();
    };
  };
  var getFileURL = function(client) {
      var object = data[0].name;
      var filename = object.replace(/^.*[\\\/]/, '');
      var result = client.signatureUrl(object, {
          response: {
              'content-disposition': 'attachment; filename="' + filename + '"'
          }
      });
      // fileRequest(result);
      //  return result;
  }
  ;
/*上传图片*/
function updatePic() {
    var orderId = $("#mod-pic-orderId1").val();
    picUpload(orderId);
}
//pic上传处理
function picUpload(id) {
    lsOrderId = id ;
    applyTokenDo(uploadFile);
}
var uploadFile = function(client) {
    var picFile=document.getElementById('picFileId').files[0];
    var orderId=lsOrderId;
    var divId=document.getElementById('mod-pic-divId').value;
    var fileName = UUIDjs.create().hex;
    var fileType = getFileType(picFile.name);
    var key = "lsd/"+orderId+"/"+fileName+"."+fileType;
    return client.multipartUpload(key, picFile, {
        progress: progress
    }).then(function(res) {
        saveToDB(orderId,key,fileName,fileType,divId);
    });
};

var applyTokenDo = function(func) {
    var url = appServer;
    $.ajax({
        type: "POST",
        url: url,
        data: "",
        dataType: "jsonp",
        success: function(e) {
            var client = new OSS({
                region: region,
                accessKeyId: e.AccessKeyId,
                accessKeySecret: e.AccessKeySecret,
                stsToken: e.SecurityToken,
                bucket: bucket
            });
            return func(client);
        },
        error: function(data) {
            //alert(JSON.stringify(data));
        }
    });
};
  
  function uploadEditPic(attachType,divId){
	  document.getElementById('picform').reset();
      $("#mod-pic").modal("toggle");
      $("#mod-pic-attachType").val(attachType);
      $("#mod-pic-divId").val(divId);
      $("#mod-pic-orderId").val($('#mod-edit-orderId').text());
  }

  function uploadPic1(attachType,divId){
      document.getElementById('picform').reset();
      $("#mod-pic").modal("toggle");
      $("#mod-pic-attachType").val(attachType);
      $("#mod-pic-divId").val(divId);
      $("#mod-pic-orderId").val($('#mod-dwCheck-pass-orderId').text());
  }    

  
  //默认回调函数
  function errorCallbackDefault(data){
    //alert("enter errorCallbackDefault"+JSON.stringify(data));
  }  
  
  //向后台请求数据
  function postData(url, parameters, successCallback, errorCallback){
     $.ajax({
         type : "POST",
         url : url,
         data : parameters,
         dataType : "json",
         success : function(data) {
           successCallback(data); 
         },
         error : function(data) {
           errorCallback(data);
         }
         }); 
     }  
  //显示信息
  function showMsg(msg){
     if(msg != undefined && msg.length > 0){
        alert(msg);
     }
  }

  function loadPiCallback(data){
      if(data.success){
         var url = '${oss_bucket_a}';
         var thum = '${oss_bucket_a_url_img}';
         var size = '${oss_bucket_a_url_img_size}';
         var v = null;
         var sec = '';
         for(var i in data.attachs){
            v = data.attachs[i];
            //url = "${ServerC}/ssdbOrderPic?orderAttachId="+v.id;
            
            sec += '<div style="text-align:center;display:inline-block;vertical-align:top;" id="picDiv' + v.id + '">';
            sec += '<a href="' + url + v.path + '" target="_blank">';
            sec += '<img class="lazy" src="${contextPath}/static/img/loading.gif" data-original="' + thum + v.path + size + v.suffix.toLowerCase() + '" height="100" width="100" id="' + v.id + '"/>';
            sec += '</a><br/><a href="javascript:void(0)" onclick="deletePic(\'' + v.id + ','+data.attachType + '\')">删除</a>';
            sec += '</div>&nbsp;';
         }
         $("#div" + data.attachType).html(sec);
         setTimeout(function () {
            $("img.lazy").lazyload({
               threshold : 200
            });
           }, 200);
      }else{
         showMsg(data.message);
      }
    }
  
  function getFileType(fileName){
    var ldot = fileName.lastIndexOf('.')
    if(ldot>=0)
       return fileName.substring(ldot + 1);
    else
       return "";
  }