<!DOCTYPE html>
<html lang="en" 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">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息(<b>收起-展开</b>)</legend>
            <div id="search-div" style="margin: 5px 10px -10px 0px">
                <form class="layui-form" action="" lay-filter="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 30px">名称</label>
                            <div class="layui-input-inline" style="width: 200px">
                                <input type="text" id="MC" name="MC" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px">单位全称</label>
                            <div class="layui-input-inline" style="width: 300px">
                                <input type="text" id="DWQC" name="DWQC" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn"><i
                                    class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>新建
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">
                    <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="view">
                    <i class="layui-icon layui-icon-file" style="font-size: 10px;"></i>查看
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">
                    <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-view" lay-event="view">
                <i class="layui-icon layui-icon-set" style="font-size: 10px;"></i>查看
            </a>
            <a class="layui-btn layui-btn-xs data-count-update" lay-event="update">
                <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
            </a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delFlag">
                <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
            </a>
        </script>
    </div>
</div>

<script>
    // 获取所有需要去除空格的输入框
    const inputs = [
        document.getElementById('MC'),
        document.getElementById('DWQC')
    ];
    // 为每个输入框添加事件监听
    inputs.forEach(input => {
        input.addEventListener('input', function() {
            this.value = this.value.replace(/\s+/g, ''); // 去除空格
        });
    });
</script>

<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="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    var updateflag = 0;
    layui.use(['form', 'table', 'treeTable'], function () {
        var form = layui.form,
            table = layui.table;

        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'baseCompany/list',
            method: 'get',
            where: {sortName: 'ID', sortOrder: 'asc'},
            toolbar: '#toolbarDemo',
            // defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: "checkbox", fixed: 'left'},
                    {type: 'numbers', align: 'center',title: '序号'},
                    {field: 'mc', width: '20%', title: '名称', align: 'left'},
                    {field: 'dwqc', width: '25%', title: '单位全称', align: 'left'},
                    {field: 'szd', width: '25%', title: '所在地', align: 'left'},
                    {field: 'xm', width: '8%', title: '联系人', align: 'center'},
                    // {field: 'px', width: '8%', title: '排序', align: 'center'},
                    {field: 'lxmc', width: '8%', title: '类型', align: 'center'}
                    // {title: '操作', toolbar: '#currentTableBar', align: 'center'}
                ]
            ],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            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为数据渲染完的回调
                // 测试打印出后端返回的权限列表
                let permissionSet = [[${permissionSet}]];
                console.log(JSON.stringify(permissionSet));
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1 // 页码从1开始
                },
                where: form.val("search-form")
            });
            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            form.val("search-form", {
                "MC": '',
                "DWQC": ''
            });
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1
                },
                where: form.val("search-form")
            });
            return false;
        });

        // 监听行工具事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新建单位',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    area: ['880px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'baseCompany/forAdd',
                    end: function (index) {
                        // 重载表格
                        if (updateflag == 1)
                            currTable.reload();
                        updateflag = 0;
                    }
                });
            } else if (obj.event == 'edit') {
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要编辑的单位！", 1500);
                } else if (data.length > 1) {
                    Message.warning("请选择一条记录进行编辑操作！", 1500);
                } else {
                    var index = layer.open({
                        title: '编辑单位',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['880px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'baseCompany/forUpdate/' + data[0].id,
                        end: function (index) {
                            // 重载表格
                            if (updateflag == 1)
                                currTable.reload();
                            updateflag = 0;
                        }
                    });
                }
            } else if (obj.event == 'view') {
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要查看的单位！", 1500);
                } else if (data.length > 1) {
                    Message.warning("请选择一条记录进行查看！", 1500);
                } else {
                    var index = layer.open({
                        title: '浏览单位',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['880px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'baseCompany/forView/' + data[0].id
                    });
                }
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要删除的单位！", 1500);
                    return;
                }
                layer.confirm('确定批量删除单位吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "baseCompany/batchDelete",
                        contentType: "application/json",
                        data: JSON.stringify(data),
                        success: function (res) {
                            if (res.code === 0) {
                                Message.success(1500, res.message, function () {
                                    // 重载表格
                                    currTable.reload();
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
            }
        });

        // 监听行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'view') {
                var index = layer.open({
                    title: '查看单位',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['880px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'baseCompany/forView/' + data.id,
                    end: function (index) {
                        layer.close(index);
                    }
                });
            } else if (obj.event === 'update') {
                var index = layer.open({
                    title: '编辑单位',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['880px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'baseCompany/forUpdate/' + data.id,
                    end: function () {
                        // 重载表格
                        if (updateflag == 1)
                            currTable.reload();
                        updateflag = 0;
                        layer.close(index);
                    }
                });
            } else if (obj.event === 'delFlag') {
                layer.confirm('确定删除该单位吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.get({
                        url: AjaxUtil.ctx + "baseCompany/delete/" + data.id,
                        success: function (res) {
                            if (res.code === 0) {
                                Message.success(1500, res.message, function () {
                                    // 重载表格
                                    currTable.reload();
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
            }
            return false;
        });

        //双击
        table.on('rowDouble(currentTableFilter)', function (obj) {
            data = obj.data;
            var index = layer.open({
                title: '查看单位',
                type: 2,
                shade: 0.2,
                maxmin: true,
                area: ['880px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'baseCompany/forView/' + data.id,
                end: function (index) {
                    layer.close(index);
                }
            });
        });
    });

    function setUpdateFlag() {
        updateflag = 1;
    }
</script>
</body>
</html>