<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>bug列表</title>
    <%@include file="/views/common.jsp" %>
    <style type="text/css">
        .layui-table-cell {
            height: 32px;
            line-height: 32px;
        }

        .layui-table td, .layui-table th, .layui-table-header, .layui-table-page, .layui-table-tool, .layui-table-total, .layui-table-view {
            border-color: #6666;
            font-size: 16px;
        }
    </style>
</head>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container demoTable">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle-fine"
                                                                  style="font-size:20px;font-weight:bold"></i> 提交问题
        </button>
    </div>
</script>
<body>
<br>
<form class="layui-form">
    <div class="layui-inline">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-inline">
            <input type="text" id="realname" name="realname" placeholder="请输入真实姓名"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" id="searchBtn" lay-submit
                    lay-filter="formDemo" style="margin-left: 15px">
                <i class="layui-icon layui-icon-search"></i> 查询
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="bugTable" lay-data="bugTable" lay-filter="bugTable"></table>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        {{#if(role_list.indexOf(1) != -1){}}
        <a class="layui-btn layui-btn-xs" lay-event="edit">回复</a>
        {{#}}}
    </div>
</script>

<script>
    let role_list = [${sessionScope.USER_SESSION.role}];

    function showImages(imagesText, index = 0) {
        if (imagesText == null || imagesText == '') {
            return;
        }
        let list = imagesText.split(',');
        if (list.length == 0) {
            return;
        }
        let data = [];
        for (let i = 0; i < list.length; i++) {
            data.push({
                "alt": "图片" + i,
                "src": list[i],
                "ptid": i
            });
        }
        layer.photos({
            photos: {
                "title": "Photos Demo",
                "start": index,
                "data": data
            }
        });
    }

    layui.use(['element', 'table', 'laydate', 'form'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var laydate = layui.laydate;
        var element = layui.element;
        var form = layui.form;

        // 生成表格
        table.render({
            elem: '#bugTable',
            url: '../../../getBugs',
            toolbar: '#toolbarDemo',
            title: '系统问题表',// 导出文件名
            id: 'bugTableAll',
            // 开启分页
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip', 'limit']
            },
            /*request : {
                'limitName' : 'pageSize' // 分页每页条数默认字段改为pageSize
            },*/
            where: {
                username: '',
                realname: ''
            },
            cellMinWidth: 80, // 全局定义常规单元格的最小宽度，layui 2.2.1 新增
            cols: [[{
                field: 'createBy',
                width: 150,
                title: '提交者'
            }, {
                field: 'bugRemark',
                title: '问题'
            }, {
                field: 'images',
                title: '图片',
                templet: function (d) {
                    let html = ""
                    let images = d.images;
                    let list = !images ? [] : images?.split(',');
                    list.forEach((item, index) => {
                        html += '<img src="' + item + '" style="width: 20px;height: 20px;" onclick="showImages(\'' + images + '\',' + index + ')">'
                    })
                    return html;
                }
            }, {
                field: 'answer',
                title: '回复'
            }, {
                field: 'createDate',
                title: '创建时间',
                width: 180,
                templet: function (d) {
                    return d.createDate != null ? layui.util.toDateString(d.createDate, "yyyy-MM-dd HH:mm:ss") : "";
                }
            }, {fixed: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#toolDemo'}]],
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.list.length,//解析数据长度
                    "data": res.list//解析数据列表
                };
            }
        });

        //点击查询按钮，重载表格
        $('#searchBtn').on('click', function () {
            table.reload('bugTableAll', {
                method: 'post',
                where: {
                    realname: $("#realname").val()
                },
                page: {
                    curr: 1
                }
            });
            return false;
        });
        // 监听头部工作栏的 新增用户
        table.on('toolbar(bugTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2,
                        title: "提交问题",
                        fix: false, //不固定
                        maxmin: true,
                        area: ['45%', '70%'],
                        content: './addBug.jsp',
                    });
                    break;
            }
            ;
        });
        table.on('tool(bugTable)', function (option) {
            console.log(option)
            switch (option.event) {
                case 'edit':
                    layer.prompt({
                        title: '请输入回复内容',
                        formType: 2,
                        value: option.data.answer
                    }, function (value, index, elem) {
                        console.log(value, index, elem)
                        if (value === '') return elem.focus();

                        $.ajax({
                            url: '../../../updateBug',
                            dataType: 'json',
                            data: {
                                id: option.data.id,
                                answer: value
                            },
                            type: 'post',
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.msg('回复成功', {
                                        icon: 6,
                                        offset: "auto",
                                        time: 2000
                                    })
                                    table.reload("bugTableAll");
                                    layer.close(index);
                                }
                            }
                        })
                        // 关闭 prompt
                    });
                    break;
            }
        })
    });

</script>

</body>
</html>