<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('智能报告列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>文件名称：</label>
                            <input type="text" name="fileName"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="showUploadModal()" shiro:hasPermission="project:attachment:add">
                <i class="fa fa-plus"></i> 添加
            </a>
<!--            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="project:attachment:edit">
                <i class="fa fa-edit"></i> 修改
            </a>-->
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="project:attachment:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <!--<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="project:attachment:export">
                <i class="fa fa-download"></i> 导出
            </a>-->
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<!-- 文件上传模态框 -->
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="uploadModalLabel">上传智能报告</h4>
            </div>
            <div class="modal-body">
                <form id="uploadForm" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="fileInput">选择文件</label>
                        <input type="file" id="fileInput" name="file" class="form-control" required>
                        <small class="text-muted">支持PDF、Word、Excel等文档格式</small>
                    </div>
                    <div class="form-group">
                        <label for="fileDescription">文件描述</label>
                        <input type="text" id="fileDescription" name="description" class="form-control" placeholder="请输入文件描述">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="uploadFile()">上传</button>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('project:attachment:edit')}]];
    var removeFlag = [[${@permission.hasPermi('project:attachment:remove')}]];
    var prefix = ctx + "project/attachment";

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "智能报告",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'fileName',
                    title: '文件名称'
                },
                {
                    field: 'fileSize',
                    title: '文件大小',
                    formatter: function(value) {
                        return formatFileSize(value);
                    }
                },
                {
                    field: 'fileType',
                    title: '文件类型'
                },
                {
                    field: 'time',
                    title: '上传时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
/*
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
*/
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="downloadFile(\'' + row.id + '\')"><i class="fa fa-download"></i>下载</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 显示上传模态框
    function showUploadModal() {
        $('#uploadModal').modal('show');
    }

    // 修改上传函数
    function uploadFile() {
        var fileInput = document.getElementById('fileInput');
        var file = fileInput.files[0];

        // 前端验证
        if (!file) {
            $.modal.alertWarning("请先选择要上传的文件");
            return;
        }

        // 创建FormData对象
        var formData = new FormData();
        formData.append("file", file);
        formData.append("description", $('#fileDescription').val());

        // 显示加载状态
        var $uploadBtn = $('#uploadModal').find('.btn-primary');
        $uploadBtn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 上传中...');

        $.ajax({
            url: prefix + "/upload",
            type: "POST",
            data: formData,
            contentType: false,  // 必须设置为false
            processData: false,  // 必须设置为false
            cache: false,
            success: function(response) {
                if (response.code === 0) {
                    $.modal.msgSuccess(response.msg || "文件上传成功");
                    $('#uploadModal').modal('hide');
                    $('#bootstrap-table').bootstrapTable('refresh');
                    $('#uploadForm')[0].reset();
                } else {
                    $.modal.alertError(response.msg || "上传失败");
                }
            },
            error: function(xhr) {
                var errorMsg = "上传出错";
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    errorMsg = xhr.responseJSON.msg;
                }
                $.modal.alertError(errorMsg);
            },
            complete: function() {
                $uploadBtn.prop('disabled', false).html('上传');
            }
        });
    }
    // 下载文件
    function downloadFile(id) {
        window.location.href = prefix + "/download/" + id;
    }

    // 格式化文件大小
    function formatFileSize(bytes) {
        if (bytes === 0) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }
</script>
</body>
</html>