<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/include.inc.jsp" %>
<%@include file="/WEB-INF/jsp/commons/jstl_message_tld.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<base href="<%=basePath%>">
    <meta charset="UTF-8">
    <title><fmt:message key="app.name" bundle="${commonBundle}"/></title>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
	<link href="https://res.wx.qq.com/open/libs/weui/1.1.2/weui-for-work.min.css" rel="stylesheet" type="text/css"/>
	<link href="resource/css/weixin-work/public.css" rel="stylesheet" type="text/css"/>
</head>
<body ontouchstart>
	<div class="page__bd" style="margin-bottom: 50px;">
		
		<div id="task-list-box">
			<div class="weui-form-preview">
				<div class="weui-form-preview__hd" style="text-align: center;">
					<div class="weui-form-preview__item">
						<label class="weui-form-preview__label" style="float: none;">分享文件详情</label>
						<em class="weui-form-preview__value taskname"></em>
					</div>
				</div>
				<div class="weui-form-preview__bd" style="line-height: 2.3;">
					<div class="weui-form-preview__item">
						<label class="weui-form-preview__label">文件</label>
						<span class="weui-form-preview__value filename"></span>
					</div>	
					<div class="weui-form-preview__item">
						<label class="weui-form-preview__label">大小</label>
						<span class="weui-form-preview__value filesize"></span>
					</div>
					<div class="weui-form-preview__item">
						<label class="weui-form-preview__label">分享人</label>
						<span class="weui-form-preview__value shareuser"></span>
					</div>
					<div class="weui-form-preview__item">
						<label class="weui-form-preview__label">分享时间</label>
						<span class="weui-form-preview__value sharetime"></span>
					</div>
				</div>
				<div class="weui-form-preview__ft btn-parent" >
					<a id="previewfile" class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:;">预览文件</a>
				</div>
			</div>
		
		</div>

	</div><!-- .page__bd -->

<script type="text/javascript" src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript" src="resource/js/weixin-work/public.js"></script>
<script type="text/javascript">
var id = -1;
var data = null;
$(function(){
	getJsConfig();
	id = getQueryString('id');
	if(!isBlank(id)){
		getList();
	}
	
    // 预览数据库查出来的附件(基于企业微信APP)
    $('#previewfile').click(function(){
    	var filename = data.wdName+'.'+data.wdExt;
    	var filesize = data.wdSize;
    	var fileurl = 'shareFiles/inline.do?newName='+data.wdNewName+'&name='+data.wdName+'.'+data.wdExt+'&ext='+data.wdExt;
    	if(!isWeiXin()){
    	//	toastMsg('该浏览器暂不支持查看'); return;
    		window.open(fileurl, "_blank");
    		return;
    	}
    	
    	wx.checkJsApi({
    	    jsApiList: ['previewFile'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
    	    success: function(res) {
    	        // 以键值对的形式返回，可用的api值true，不可用为false
    	        // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
    	        if(res.checkResult.previewFile){
    	        	// 企业微信预览文件用后台生成的地址无法打开，改用直接用文件地址
    	        	fileurl = getSysBaseUrl()+'/upfiles/personalFiles/'+data.wdNewName+'.'+data.wdExt;
    	        	wx.previewFile({
    	        	    url: fileurl, 						// 需要预览文件的地址(必填，可以使用相对路径)
    	        	    name: filename, 	// 需要预览文件的文件名(不填的话取url的最后部分)
    	        	 	size: filesize						// 需要预览文件的字节大小(必填)
    	    		});
    	        }else{
    	        //	toastMsg('该浏览器暂不支持查看');
    	        	window.location.href = fileurl;
    	        	return;
    	        }
    	    }
    	});
    });
});

function getList(){
	showLoading();
	$.post('shareFiles/loadById.do', {id:id}, function(result){
		hideLoading();
		data = JSON.parse(JSON.stringify(result).trim());
		if(data.id>0){
			var filename = data.wdName;
			if(filename.length>30)filename=filename.substr(0, 10)+"..."+filename.substr(filename.length-10, filename.length);
			filename=filename+'.'+data.wdExt;
			$('.filename').text(filename);
			$('.filesize').text(bytesToSize(data.wdSize));
			$('.shareuser').text(trimToEmpty(data.wdUserId));
			$('.sharetime').text(formatDate(data.wdCreateTime));
		}
	}, 'json');
}

function bytesToSize(bytes) {
    if (!bytes || bytes === 0) return '0 B';
    var k = 1024,
		sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
  		i = Math.floor(Math.log(bytes) / Math.log(k));
 
   return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
}

</script>
</body>
</html>