<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>国网任务书</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<form class="layui-form" action="" lay-filter="formDemo">
    <div class="layuimini-form" style="padding-bottom: 100px">
        <div class="layui-btn-container" style="margin-bottom: -10px">
            <button type="button" class="layui-btn layui-btn-sm upload" lay-filter="upLoad"
                    lay-data="{fileType:'gwtask'}">
                <i class="layui-icon layui-icon-export" style="font-size: 10px;"></i>上传
            </button>
            <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-filter="deleteFile">
                <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
            </button>
        </div>

        <input type="hidden" id="cjr" name="cjr" class="layui-input" th:value="${userName}">
        <table class="layui-hide" id="currentTableId" name="currentTableId" lay-filter="currentTableFilte"
               style="padding-top: 0px;margin-top: 0px"></table>

    </div>
</form>

<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/lib/xm-select-v1.2.2/xm-select.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>

<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    layui.use(['form', 'table', 'laydate', 'upload'], function () {
        var form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            layer = layui.layer,
            upload = layui.upload;

        //日期
        laydate.render({
            elem: '#csrq',
            value: new Date()
        });

        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'prjGwtask/list',
            method: 'post',
            // toolbar: '#toolbarDemo',
            // defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: "checkbox", align: 'center'},
                    {type: 'numbers', title: '序号', align: 'center', width: '5%'},
                    {field: 'wjm', title: '任务书名称', width: '55%'},
                    {field: 'scr', title: '上传人', width: '15%'},
                    {field: 'scrq', title: '上传日期', width: '20%'}
                ]
            ],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'grid',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            },
            done: function (res, curr, count) { // done为数据渲染完的回调
            }
        });

        //文件上传
        upload.render({
            elem: '.upload' //绑定元素
            , url: AjaxUtil.ctx + 'fileupload/upload'
            , methd: 'post'
            , accept: 'file'
            , data: {}
            , done: function (res) {
                // debugger;
                if (0 === res.code) {
                    currTable.reload();
                } else
                    layer.alert("上传国网任务书出现错误！");
            }
            , error: function () {
                layer.alert("上传国网任务书出现错误！");
            }
            , before: function (res) {
                let fileType = this.fileType;
                this.data.fileType = fileType;
            }
        });
        $("button[lay-filter='deleteFile']").click(function () {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的国网任务书！", 1000);
                return;
            }
            layer.confirm('确定批量删除所选择国网任务书吗？', function (index) {
                var ids = [];
                for (var i = 0, len = data.length; i < len; i++) {
                    ids.push(data[i].id);
                }
                layer.close(index);
                AjaxUtil.post({
                    url: AjaxUtil.ctx + "prjGwtask/batchDelete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (res) {
                        if (res.code === 0) {
                            Message.success(1500, res.message, function () {
                                currTable.reload();
                            });
                        } else {
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                    }
                });
            });
            //刷新表格
            // currTable.reload();
        });

        //双击（附件）
        table.on('rowDouble(currentTableFilte)', function (obj) {
            // console.log(obj);
            // window.open(obj.data.weburl);
            window.open(obj.data.weburl, '_blank', 'toolbar=no,location=no,directories=no,status=no,menubar=no');
        });
    });
</script>
</body>
</html>