<!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" style="display: flex; align-items: center; gap: 10px;">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 40px;">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="xm" id="xm" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width: 40px;">学历</label>
                                <div class="layui-input-inline" style="width: 130px;">
                                    <select name="xl" id="xl" lay-search>
                                        <option value="">请选择</option>
                                        <option th:each="item:${xlList}" th:text="${item.name}" th:value="${item.name}"></option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 80px;">身份证号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sfzh" id="sfzh" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-inline">
                                <label class="layui-form-label" style="width: 40px;">状态</label>
                                <div class="layui-input-inline" style="width: 130px;">
                                    <select name="zt" id="zt">
                                        <option value="">[请选择]</option>
                                        <option value="0">可用</option>
                                        <option value="1">禁用</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-inline" style="margin-left: 0px;">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</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 data-add-btn" lay-event="add" style="background-color: #00a2d4">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>新增
                </button>
                <button class="layui-btn layui-btn-sm" lay-event="export" style="background-color: #2f363c" >
                    <i class="layui-icon layui-icon-export" style="font-size: 10px;"></i>批量导出
                </button>
                <button class="layui-btn layui-btn-sm" lay-event="download"  style="background-color: purple">
                    <i class="layui-icon layui-icon-download-circle" style="font-size: 10px;"></i>模板下载
                </button>
                <button type="button" class="layui-btn layui-btn-sm" id="test3">
                    <i class="layui-icon"></i>批量导入
                </button>
            </div>

        </script>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="optBtn">
            <button class="layui-btn layui-btn-sm" style="background-color: #1E9FFF" lay-event="update">编辑</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delFlag">删除</button>
        </script>

        <script type="text/html" id="statusTemps">
            <!-- “ d ”表示当前行数据对象 -->
            {{# if(d.zt === 0 ){ }}
            <button class="layui-btn layui-btn-sm" style="background-color: #5FB878">可用</button>
            {{# }else{ }}
            <button class="layui-btn layui-btn-sm layui-btn-warm">禁用</button>
            {{# } }}
        </script>
    </div>
</div>
<script>
    // 获取所有需要去除空格的输入框
    const inputs = [
        document.getElementById('xm'),
        document.getElementById('sfzh'),
    ];
    // 为每个输入框添加事件监听
    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','upload'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer,
         upload = layui.upload;
        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'lsUser/all',
            method: 'get',
            where: {sortName: 'ID', sortOrder: 'asc'},
            toolbar: '#toolbarDemo',
            // defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: "radio", width: '3%', fixed: 'left',title:'😊'},
                    {type: 'numbers', align: 'center', title: '序号'},
                    {field: 'xm', width: '7%', title: '姓名', align: 'left'},
                    {field: 'xb', width: '5%', title: '性别', align: 'left'},
                    {field: 'phone', width: '10%', title: '电话', align: 'left'},
                    {field: 'sfzh', width: '10%', title: '身份证号', align: 'left'},
                    {field: 'xl', width: '10%', title: '学历', align: 'left'},
                    {field: 'byxy', width: '10%', title: '毕业院校', align: 'left'},
                    {field: 'zy', width: '10%', title: '专业', align: 'left'},
                    {field: 'zq', width: '10%', title: '聘用周期(年)', align: 'center'},
                    {field: 'qpsj', width: '10%', title: '起聘时间', align: 'center'},
                    {field: 'dqsj', width: '15%', title: '合同到期时间', align: 'center'},
                    {field: 'gznr', width: '20%', title: '工作内容', align: 'left'},
                    {field: 'zt', width: '5%', title: '状态', templet: '#statusTemps', fixed: 'right'},
                    {title: '操作', fixed: 'right', width: '10%', toolbar: "#optBtn", 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));
            }
        });
        //指定允许上传的文件类型
        upload.render({
            elem: '#test3'
            , url: AjaxUtil.ctx + 'lsUser/file'
            , method: 'post'
            , accept: 'file' //普通文件
            , done: function (res) {
                console.log(res);
                console.log(res.code);
                if (res.code === 43) {
                    layer.msg(res.message);
                }else {
                    if (res.data && Array.isArray(res.data) && res.data.length > 0) {
                        // 安全访问数组元素或对象属性
                        layer.msg(res.message);
                        table.reload('currentTableId');
                    }
                    console.log(res);
                    // 将 JSON 数据转换成 HTML 表格格式
                    var tableHtml = '<table class="layui-table"><thead><tr>' +
                        '<th>姓名</th>' +
                        '<th>身份证号</th>' +
                        '<th>起聘时间</th>' +
                        '<th>到期时间</th>' +
                        '<th>原因</th>' +
                        '</tr></thead><tbody>';
                    for(var i = 0; i < res.data.length; i++) {
                        var xm = res.data[i].xm || 'N/A';
                        var sfzh = res.data[i].sfzh || 'N/A';
                        var qpsj = res.data[i].qpsj || 'N/A';
                        var dqsj = res.data[i].dqsj || 'N/A';
                        var xmfzr = res.data[i].xmfzr || 'N/A';

                        tableHtml += '<tr>' +
                            '<td>' + xm + '</td>' +
                            '<td>' + sfzh + '</td>' +
                            '<td>' + qpsj + '</td>' +
                            '<td>' + dqsj + '</td>' +
                            '<td>' + xmfzr + '</td>' +
                            '</tr>';
                    };
                    tableHtml += '</tbody></table>';
                    layer.open({
                        type: 1,
                        title: '问题数据', // 弹框标题
                        content: tableHtml, // 弹框内容为生成的表格 HTML
                        area: ['1000px', '600px'] // 设置弹框大小
                    });
                    table.reload('currentTableId');
                }
            },

    });
        // 监听搜索操作
        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 () {
            // 表格重载
            table.reload('currentTableId', {
                data: data,  // 更新后的数据源
                page: false, // 不分页处理
                limit: data.length // 限制数据条数，取数据源长度
            }, true);
        });


        // 监听行工具事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新建人员',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    area: ['1000px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'lsUser/forAdd',
                    success: function (res) {

                    }
                });
            } 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: ['1000px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'lsUser/forView/' + data[0].id,
                        end: function (index) {
                            layer.close(index);
                        }
                    });
                }
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要删除的人员！", 1000);
                    return;
                }
                layer.confirm('确定删除人员吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "lsUser/batchDelete",
                        contentType: "application/json",
                        data: JSON.stringify(data),
                        success: function (res) {
                            if (res.code === 0) {
                                Message.success(1500, res.message);
                                table.reload('currentTableId');
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });

                // layer.alert(JSON.stringify(data));
            }else if(obj.event ==='export'){  // 监听导出按钮

                AjaxUtil.get({
                    url:AjaxUtil.ctx +'lsUser/getAll',
                    success: function(res){
                        table.exportFile('currentTableId',res.data,'xlsx');
                    },
                    error:function (error) {
                        Message.error(error,2000)

                    }
                });

            }else if (obj.event === 'download'){
                window.location.href = 'http://10.208.114.107:8082/202407/Template.xlsx';
            }
        });

        // 监听行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            console.log(obj);

            var data = obj.data;
            if (obj.event === 'view') {
                var index = layer.open({
                    title: '浏览人员',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['1000px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'lsUser/forView/' + data.id,
                    end: function (index) {
                        layer.close(index);
                    }
                });
            } else if (obj.event === 'update') {
                console.log(data.id);
                var index = layer.open({
                    title: '编辑人员',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    area: ['1000px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'lsUser/up/' + data.id,
                    end: function (index) {

                    }
                });
            } else if (obj.event === 'delFlag') {
                layer.confirm('确定删除该人员吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "lsUser/delete/" + data.id,
                        success: function (res) {
                            if (res.code === 0) {
                                layer.msg(res.message);
                                table.reload('currentTableId');
                            } else {
                                layer.msg(res.message);
                                table.reload('currentTableId');
                            }
                        },
                        error: function (error) {

                            layer.alert('服务器睡着了', {icon: 5});
                            console.log(error);
                            table.reload('currentTableId');
                        }
                    });
                });
            }
            return false;
        });


    });

</script>
</body>
</html>