{include file="public/header" /}
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" id="LAY-user">
            <div>
				<span class="layui-breadcrumb" lay-separator="/"></span>	
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-Alist" lay-filter="LAY-Alist"></table>
			<!--操作模板-->
            <script type="text/html" id="opeBar">
			{{# if(d.fileType !== '文件夹'){ }}
				{{# if(d.fileType.match(/(m3u8|mp4|ts|mkv|avi|mov|rmvb|webm|flv)$/i)){ }}
				<a class="layui-btn layui-btn-xs layui-btn-primary" title="播放预览" lay-event="look"><i class="layui-icon layui-icon-play"></i></a>				
				{{# }else if(d.fileType.match(/(jpg|jpeg|gif|png)$/i)){ }}
				<a class="layui-btn layui-btn-xs layui-btn-primary" title="预览图片" lay-event="look"><i class="layui-icon layui-icon-picture"></i></a>
				{{# }else{ }}
				<a class="layui-btn layui-btn-xs layui-btn-primary" title="下载" lay-event="look"><i class="layui-icon layui-icon-download-circle"></i></a>
				{{# } }}
				<a class="layui-btn layui-btn-xs layui-btn-normal" title="复制链接" lay-event="copy"><i class="layui-icon layui-icon-link"></i></a>
			{{# }else{ }}
				<a class="layui-btn layui-btn-xs layui-btn-normal" title="打开文件夹" lay-event="Folder">打开文件夹</a>
			{{# } }}
            </script>
			<!--批量操作模板-->
			<script type="text/html" id="toolBar">
				<a class="layui-btn layui-btn-xs " lay-event="copyAll" title="批量复制数据"><i class="fa fa-copy"></i> 批量复制</a>
				<a class="layui-btn layui-btn-normal layui-btn-xs " title="一键发布视频到苹果cms" lay-event="fabu"><i class="layui-icon layui-icon-release"></i>一键发布</a>
				<a class="layui-btn layui-btn-xs " lay-event="m3u8" title="根据切片文件生成m3u8播放地址"><i class="fa fa-play-circle-o"></i> 生成m3u8链接</a>
				<a class="layui-btn layui-btn-normal layui-btn-xs " title="设置定时任务,定时更新发布数据" lay-event="timing"><i class="layui-icon layui-icon-log"></i>定时发布</a>
			</script>
        </div>
    </div>
</div>
<!--复制-->
{$pan_data}
{include file="public/footer" /}
{include file="public/copy" /}
<script>
	var fileId = "{$param.fileId}";
	var root_path = '{$param.root}';
	var driver = '{$param.driver}';
	var domain = '{$param.fileId|http_type}';
    layui.use(['element','table','form'], function () {
        var $ = layui.$, form = layui.form, element = layui.element, table = layui.table;
		var device = layui.device();
		var localTest = layui.data('Alist');
		var api_url = localTest.api[0].url;
		var token = localTest.api[0].token;
		var load = layer.load();
        var panList = table.render({
            elem: '#LAY-Alist'
			, toolbar: '#toolBar'
			, defaultToolbar: ''
			, url: '{:url("panlist")}'
			, where: {api_url: api_url, token:token, root:root_path, fileId:maopan.fileId, driver:driver}
			, method: 'post'
            , page: true
            , even: false
            , cellMinWidth: 150
			, height: 'full-120'
            , limits: [100]
            , limit: "100"
            , id: 'LAY-table'
			, scrollPos: 'fixed' 
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'fileName', minWidth: 300, title: '文件名', templet: function (d){
					if(d.is_dir){
						return '<a href="?fileId='+ d.root_path + encodeURIComponent(d.path) +'&root='+d.root_path+'&driver='+driver+'&id='+maopan.account_id+'" id="fileId-'+d.fileId+'" style="color:blue;">'+d.fileName+'</a>';
					}else{
						return d.fileName;
					}
				}}
                , {field: 'fileType', width: 100, title: '文件类型', align: 'center'}
				, {field: 'fileSize', width: 110, title: '文件大小',  align: 'center',templet: function (d){
					if(d.is_dir){
						return d.fabu ? '<font color="green" title="最后发布时间：'+layui.util.toDateString(d.fabu*1000)+'">已发布</font>' : (d.fileSize ? bytesToSize(d.fileSize) : '');
					}else{
						return d.fileSize ? bytesToSize(d.fileSize) : '';
					}
				}}
                , {field: 'last_time', width: 180, title: '修改时间', align: 'center'}				
                , {fixed: 'right', width: 150, title: '操作', align: 'center', toolbar: '#opeBar'}
            ]]
			,done: function (res, curr, count) {
				layer.close(load);
				function menuLink(path){
					if(path=='/根目录' || path=='/'){
						return root_path;
					}
					var menu = fileId.split(path);
					return encodeURIComponent(fileId.replace(menu[1],''));
				}
				html = '';
				if(typeof res.path !== "undefined"){
					for(var i=0;i<res.path.length;i++){
						html += res.path[i] ? '<a href="?fileId=' + menuLink("/"+res.path[i]) + '&root='+root_path+'&driver='+driver+'&id='+maopan.account_id+'">'+res.path[i]+'</a>':'';
					}
					maopan.filePath = maopan.fileId;
				}
				$('.layui-breadcrumb').html(html);
				element.render('breadcrumb');
            }
        });
		
		
		//监听操作
		table.on('tool(LAY-Alist)', function(obj) {
			var layEvent = obj.event,
				data = obj.data;
			var account_id = maopan.account_id ? maopan.account_id : data.userId;
			var download_url = domain + '/d/file/' + data.fileId + '/' + encodeURIComponent(data.fileName);
			var play_url = maopan.domain_url + '/v/alist/' + data.code;
			if(driver.match(/(Aliyundrive|AliyundriveOpen)$/i) && data.fileType.match(/(mp4|mkv|flv|avi|rmvb|rm)$/i)){
				var play_url = play_url + '?preview=video';
				var download_url = maopan.domain_url + '/d/alist/' + data.code + '/preview.m3u8';
			}
			if (layEvent === 'look') {
				if (data.fileType == '文件夹' || data.fileType == '') {
					layer.msg('此类型文件无法预览', {icon: 2,anim: 6,shade:0.5});
					return false;
				}else{
					if(data.fileType.match(/(mp4|mkv)$/i)){
						wk.layer_show('预览播放：' + data.fileName + '',play_url,'70%','70%');
					}
					else if(data.fileType.match(/(gif|jpg|jpeg|png|tiff|bmp|svg|ico|swf|webp)$/i)){
						var json = {"id":"Alist-Preview","data":[{'src':download_url,'thumb':download_url,'alt':data.fileName}]};
						wk.PicPreview(json);
					}
					else{
						window.open('javascript:window.name;', '<script>location.replace("'+download_url+'")<\/script>');
					}
				}
			}
			else if(layEvent === 'copy'){
				if(driver.match(/(Aliyundrive|AliyundriveOpen)$/i) && data.fileType.match(/(mp4|mkv|flv|avi|rmvb|rm)$/i)){
					name = data.fileName.substring(0, data.fileName.lastIndexOf("."));
					var file_link = name + '$' + domain + '/d/file/' + data.fileId + '/' + encodeURIComponent(data.fileName);
					multiple(file_link,name + '$' + download_url,name + '$' + play_url);
				}else if(data.fileType.match(/(m3u8|mp4|ts|mkv)$/i)){
					showlink(download_url,play_url);
				}else{
					copyText(download_url);
				}
			}else if(layEvent === 'Folder')
			{
				var open_url = '?fileId=' + root_path + encodeURIComponent(data.path) + '&root='+ root_path + '&driver=' + driver + '&id='+maopan.account_id;
				window.location.href = open_url;
			}
		});

		//监听工具栏
		table.on('toolbar(LAY-Alist)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			var data = checkStatus.data;
			if (data.length == 0) {
                layer.msg('请至少勾选一组数据',{icon:2});
                return false;
            }
			switch (obj.event) {
				case 'copyAll':
					var url = '';
					var play_url = '';
					var playLink = '';
					var preview = false;
					data.forEach(function(v,nums) {
						if (v.fileType != '文件夹') {
							link = domain + '/d/file/' + v.fileId + '/' + encodeURIComponent(v.fileName)+ "\n";
							name = v.fileName.substring(0, v.fileName.lastIndexOf("."));
							url += name + '$' + link;
							if(v.fileType.match(/(mp4|mp3|ts|mkv)$/i)){
								if(driver.match(/(Aliyundrive|AliyundriveOpen)$/i)){
									preview = true;
									playLink += name + '$' + maopan.domain_url + '/v/alist/' + v.code + "?preview=video\n";
									play_url += name + '$' + maopan.domain_url + '/d/alist/' + v.code + "/preview.m3u8\n";
								}else{
									playLink += name + '$' + maopan.domain_url + '/v/alist/' + v.code + "\n";
									play_url += name + '$' + link;
								}
							}else{
								play_url += name + '$' + link;
							}
                        }
					});
					if(url){
						if(preview){
							multiple(url,play_url,playLink);
						}else{
							multiple(url,play_url,playLink);
						}
					}else {
                        layer.alert('只能复制文件，不能复制文件夹', {icon: 2});
                    }
				break;
				case 'fabu':
					PubData(data);
				break;
				case 'm3u8':
					data.id = maopan.account_id;
					M3u8Link(data,maopan.panType);
				break;	
				case 'timing':
					data.timing = 1;
					PubData(data);
				break;	
			}
		});		
		
    });
	
</script>
</body>
</html>