<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>企业文件</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 上方搜索栏 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索</legend>
            <div class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <!-- 搜索【工作流名】和【权限字符】-->
                    <div class="layui-inline">
                        <label class="layui-form-label">关键词</label>
                        <div class="layui-input-inline">
                            <input type="text" id="fileName" name="fileName" autocomplete="off" class="layui-input" placeholder="请输入关键词"/>
                        </div>
                    </div>
                    <!-- 搜索按钮 -->
                    <div class="layui-inline">
                        <button type="button" class="layui-btn layui-btn-primary layui-border-green btn-role-action" data-type="search">
                            <i class="layui-icon layui-icon-search"></i> 搜 索
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary layui-border-box btn-role-action" data-type="refresh">
                            <i class="layui-icon layui-icon-refresh"></i> 清 空
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
        <!-- 表格 -->

        <table class="layui-hide" id="table-data" lay-filter="table-data"></table>
    </div>
</div>

<script type="text/html" id="toolbar-table">
    <div class="layui-btn-container">
        <button style="background-color: #154d96;" class="layui-btn layui-btn-sm layuimini-btn-primary" lay-event="btn-refresh"><i class="fa fa-refresh"></i></button>
    </div>


</script>


<!-- 【行内按钮】表格每行末尾的操作按钮-->
<script type="text/html" id="table-tool">


    {{# if(d.fileFolder == 1) { }}
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="btn-preview">预览</a>

    {{# if(d.roleDown == 1) { }}
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="btn-down">下载</a>
    {{# } }}
    {{# } }}

</script>
<!--引入JS文件-->
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/jquery-1.10.2.min.js" charset="utf-8"></script>
<script>
    let fileParentId = 0;
    layui.use(['table','dropdown', 'util', 'layer',], function () {
        let form = layui.form,
            dropdown = layui.dropdown,
            $ = layui.jquery,
            table = layui.table;

        let currPage = 1



        let table_data = table.render({
            even: true,
            elem: '#table-data',
            url: '/cloud/lately/list',
            where:{fileParentId:fileParentId},
            response: {statusCode: 200},
            toolbar: '#toolbar-table',
            defaultToolbar: ['filter'],
            title: '企业文件',
            height: 'full-200',
            cols: [
                [
                    {type: "checkbox", fixed: 'left',width: 50},
                    {field: 'fileImage', title: '', fixed: 'left',width: 60,align: 'center',
                        templet: function(d){
                            return '<img src="' + d.fileImage + '" style="width: 25px; height: 25px;" />';
                        }
                    },
                    {field: 'fileName', title: '文件名称', fixed: 'left'},
                    {field: 'fileFormat', title: '文件格式',width: 150},
                    {field: 'fileSize', title: '文件大小',width: 150, templet: function (d) {
                            let html = "";
                            if (d.fileSize === null) {
                                html = ""
                            }else{
                                html = d.fileSize + ` KB`
                            }
                            return html
                        }
                    },

                    {field: 'creatorName', title: '创建人',width: 150},
                    {fixed: 'right', title: '操作', align: 'center', toolbar: '#table-tool', width: 250}
                ]
            ],
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip', 'refresh'], // 自定义分页布局
                curr: 1, // 初始当前页
                limits: [30, 50, 100,500,1000], // 自定义每页显示条数选择项
                limit: 30 // 默认每页显示条数
            },
            done: function (res, curr, count) {

                getRole()
                if (res.data.length === 0 && curr > 1) {
                    table_data.reload({page: {curr: curr - 1}})
                }
                currPage = curr
            }
        });
        let active = {
            search: function () {
                table_data.reload({
                    page: {curr: 1},
                    where: {
                        fileName: $('#fileName').val(),
                        fileParentId: fileParentId
                    }
                });
            },
            refresh: function () {
                table_data.reload({
                    page: {curr: 1},
                    where: {fileName: ""}
                });
                $('#fileName').val('')
            }
        };
        $('.btn-role-action').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('toolbar(table-data)', function (obj) {
            let that = this;
            switch (obj.event) {
                case "btn-refresh":
                    table_data.reload()
                    break;
            }
        });

        table.on('rowDouble(table-data)', function(obj){
            let data = obj.data;
            lately(data.id,data.fileOss);
        });


        table.on('tool(table-data)', function (obj) {
            let that = this;
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                case "btn-preview":
                    lately(data.id,data.fileOss);
                    break;
                case "btn-down":
                    let a = document.createElement('a');  // 创建下载链接
                    a.href = data.fileOss;
                    a.target = '_blank';  // 新开页下载
                    a.download = data.fileName + data.fileFormat; // 下载文件名
                    document.body.appendChild(a);  // 添加dom元素
                    a.click();  //  点击下载
                    document.body.removeChild(a); // 下载后移除元素
                    window.URL.revokeObjectURL(data.fileOss);
                    a.remove();
                    break;
            }
        });


    });

    function getRole(){
        $.ajax({
            type:"get",
            data:{},
            dataType:"json",
            url:"/cloud/roleUser/getRole",
            success:function(data){
                if (data.code === 200) {

                    if(data.data.roleUpload==0){
                        $("#btn-group").css("display","none")
                    }
                }
            }
        })
    }
    function lately(id,url){
        let dataSet = {
            "fileId" : id
        }
        $.post("/cloud/lately/addSave", dataSet, function (data) {
            if (data.code === 200) {
                window.open('http://onlinefile.lingtao8.com/onlinePreview?url='+encodeURIComponent(base64Encode(url)));
            }else{
                window.open('http://onlinefile.lingtao8.com/onlinePreview?url='+encodeURIComponent(base64Encode(url)));
            }
        });
    }

    function base64Encode(str) {
        // 将字符串转换为UTF-8编码的字节序列，然后进行Base64编码
        return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
            return String.fromCharCode('0x' + p1);
        }));
    }
</script>
</body>
</html>
