/**
 * 阿里云
 */

var Buffer = OSS.Buffer;
var currentDeleteKey = null;
var currentDeleteId = null;
var currentDeleteType = null;
var OSS = OSS.Wrapper;
var STS = OSS.STS;

var addPic = function(client) {
	var file = document.getElementById('picFileId').files[0];
	var key = 'ssrz/order/';
	var date=new Date;
	key += date.getFullYear() + '/' + (date.getMonth()+1) + '/' + $('#mod-applicant-orderId').val() + '/';
	var nId = UUIDjs.create().hex;
	key += nId + '.' + getFileType(file.name);
	var attachType = $('#mod-pic-attachType').val();
	return client.multipartUpload(key, file, {
        progress: progress
    }).then(function(res) {
    	addPicSuccessCallBack(res, file, key, attachType, nId);
    });
};

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) {
            console.log(data);
        }
    });
};

var deletePic = function(key,id,type) {
	currentDeleteKey = key;
	currentDeleteId = id;
	currentDeleteType = type;
	return applyTokenDo(deletePicFromOss);
};

var deletePicFromOss = function(client) {
	return client.delete(currentDeleteKey, {
        progress: progress
    }).then(function(res) {
    	deletePicSuccessCallBack(res, currentDeleteKey, currentDeleteId, currentDeleteType);
    });
};

var progress = function(p) {//上传成功的进度条
	return function (done) {
		done();
	};
};

function addPicSuccessCallBack(res, file, path, attachType, id){
	var ldot = file.name.lastIndexOf('.');
	var name = '';
	var suffix = '';
	if(ldot>=0){
		name = file.name.substring(0, ldot);
		suffix = file.name.substring(ldot + 1);
	}
	var html = '<div style="text-align:center;display:inline-block;vertical-align:top;" id="mod-pic-' + id + '">';
	html += '<a href="' + logoUrl + '/' + path + '" target="_blank">';
	html += '<img class="lazy" src="' + contextPath +'/static/img/loading.gif" data-original="' + logoThum  + '/' + path + logoSize + '" height="100" width="100" />';
	html += '</a><br/><a href="javascript:void(0)" onclick="deletePic(\'' + path + '\',\'' + null + '\',\'' + attachType + '\')">删除</a>';
	html += '</div>&nbsp;';
	var divId = 'mod-applicant-div';
	if(attachType == 5){
		divId = 'mod-caseInfo-div';
		picFileBxs[path] = ['add', attachType, name, path , suffix];
	}else{
		picFileInfo[path] = ['add', attachType, name, path , suffix];
	}
	$("#" + divId + attachType).append(html);
	$("#mod-pic").modal("toggle");
	setTimeout(function () {
		$("img.lazy").lazyload({
			threshold : 200
		});
	}, 200);
}

function deletePicSuccessCallBack(res, key, id, type){
	var fileInfo = null;
	if(type == '5'){
		fileInfo = picFileBxs[key];
	}else{
		fileInfo = picFileInfo[key];
	}
	if(fileInfo != undefined){
		delete picFileInfo[key];
	}
	if(id != null){
		if(type == '5'){
			picFileBxs[key] = ['del', id];
		}else{
			picFileInfo[key] = ['del', id];
		}
	}
	$("#mod-pic-" + key.substring(key.lastIndexOf('/') + 1, key.lastIndexOf('.'))).remove();
}

function getFileType(fileName){
	var ldot = fileName.lastIndexOf('.')
	if(ldot>=0)
		return fileName.substring(ldot + 1);
	else
		return "";
}
