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: '#informationTable',
        url: '../../../getInformations',
        toolbar: '#toolbarDemo',
        title: '用户表',// 导出文件名
        id: 'informationTableAll',
        // 开启分页
        // page	: true,
        page: {
            layout: ['count', 'prev', 'page', 'next', 'skip', 'limit']
        },
        limits: [10, 30, 50, 80, 100, 999],
        /*request : {
            'limitName' : 'pageSize' // 分页每页条数默认字段改为pageSize
        },*/
        where: {
            type: '1',content: ''
        },
        cellMinWidth: 80, // 全局定义常规单元格的最小宽度，layui 2.2.1 新增
        cols: [[{
            type: 'numbers',
            title: '序号',
            width: 50,
            fixed: "left"
        }, {
            field: 'content',
            title: '内容',
        }, {
            field: 'type',
            align: 'center',
            width: 150,
            title: '类型'
        }, {
            field: 'createBy',
            align: 'center',
            width: 150,
            title: '创建人'
        }, {
            field: 'createDate',
            title: '创建时间',
            width: 220,
            align: 'center',
            templet: function (d) {
                return d.createDate != null ? layui.util.toDateString(d.createDate, "yyyy-MM-dd HH:mm:ss") : "";
            }
        }, {
            fixed: 'right',
            title: '操作',
            align: 'center',
            width: 150,
            toolbar: '#barDemo'
        }]],
        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": 0, //解析接口状态
                "msg": "", //解析提示文本
                "count": res.data.list.total,//解析数据长度
                "data": res.data.list.list//解析数据列表
            };
        }
    });

    //点击查询按钮，重载表格
    $('#searchBtn').on('click', function () {
        table.reload('informationTableAll', {
            method: 'get',
            where: {
                content: $("#content").val()
            },
            page: {
                curr: 1
            }
        });
        return false;
    });

    // 监听头部工作栏的 新增
    table.on('toolbar(informationTable)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                layer.open({
                    type: 2,
                    title: "添加问题",
                    fix: false, //不固定
                    maxmin: true,
                    skin: 'layui-layer-molv',
                    area: ['45%', '70%'],
                    content: './addInformation.jsp',
                });
                break;
        }
        ;
    });

    function previewImage(textImage) {


    }

    // 监听 修改
    table.on('tool(informationTable)', function (obj) {
        var data1 = obj.data;
        console.log(data1)
        if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: "修改问题",
                area: ['45%', '70%'],
                skin: 'layui-layer-molv',
                content: './updateInformation.jsp',
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; // 得到iframe页的窗口对象
                    body.find('#id').val(data1.id);
                    body.find('#contentUpdate').val(data1.content);
                    body.find('#type').val(data1.type);

                    body.find('#images').val(data1.attachment);
                    let textImage = data1.attachment;
                    if (textImage) {

                        let textImageList = textImage == "" ? [] : textImage.split(",");
                        let html = "";
                        textImageList.forEach((item, index) => {
                            if (item != "" && (item.indexOf(".mp4") > -1 || item.indexOf(".MP4") > -1)) {
                                html += "<video src='" + item + "' class='layui-upload-img' style='width: 50px;height: 50px;margin-right: 5px;' onclick='delImage(\"" + index + "\")'/>";
                            } else if (item != "") {
                                html += "<img src='" + item + "' class='layui-upload-img' style='width: 50px;height: 50px;margin-right: 5px;' onclick='delImage(\"" + index + "\")'/>";
                            }
                        })
                        body.find("#preview").empty().append(html);
                    }
                    layui.form.render();
                }
            });
        } else if (obj.event === 'del') {
            layer.confirm('确定要删除该数据么？', {icon: 3, title: "提示"}, function (index) {
                $.ajax({
                    url: "../../../deleteInformation",
                    data: {'id': data1.id},
                    type: "post",
                    //dataType:"json",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg('删除成功！', {icon: 6, offset: "auto", time: 1000});//提示框
                        } else {
                            layer.msg('删除失败！', {icon: 5, offset: "auto", time: 1000});//提示框
                        }
                        setTimeout(function () {
                            location.reload();//重新加载页面
                        }, 1100);
                    }
                });
                return false;
            });
        }
    })
});
