layui.config({
    base: '/assets/lib/jplus/treeTable/'
}).use(['form', 'layer', 'table', 'laytpl', 'treeTable'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        treeTable = layui.treeTable;
    //用户列表
    var tableIns = table.render({
        elem: '#list',
        url: '/cms/book/contents/queryPage',
        method: 'POST',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        headers: {token: ''},
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 15,
        id: "listTable",
        request: {
            pageName: "pageNo",//重新定义当前页码参数名称
            limitName: "pageRows"//重新定义每页大小参数名称
        },
        response: {
            statusName: 'code', //数据状态的字段名称，默认：code
            statusCode: 200, //成功的状态码，默认：0
            msgName: 'msg',  //状态信息的字段名称，默认：msg
            countName: 'totalRows',  //数据总数的字段名称，默认：count
            dataName: 'data' //数据列表的字段名称，默认：data
        },
        where: {
            //请求的参数写在where
        },
        parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg, //解析提示文本
                "totalRows": res.data.totalRows, //解析数据长度
                "data": res.data.dataList, //解析数据列表
            };
        },
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'bookInfoId', title: '关联图书', minWidth: 100, align: "center"},
            {field: 'chapterLevel', title: '章节层级', minWidth: 100, align: "center"},
            {field: 'chapterNumber', title: '章节编号', minWidth: 100, align: "center"},
            {field: 'chapterTitle', title: '章节标题', minWidth: 100, align: "center"},
            {field: 'contentType', title: '内容类型', minWidth: 100, align: "center"},
            {field: 'wordCount', title: '字数统计', align: 'center'},

            {field: 'isPublic', title: '是否公开', minWidth: 100, align: "center"},
            /*{field: 'userAvatar', title: '用户头像', minWidth:100, align:"center",templet:function(d){
                    return "<img src='"+d.userAvatar+"' style='width: 30px;height: 30px'>";
            }},*/
            {field: 'version', title: '内容版本', minWidth: 100, align: "center"},
            {field: 'parentId', title: '父级章节', minWidth: 100, align: "center"},
            {field: 'coverImage', title: '封面图片路径', minWidth: 100, align: "center"},
            {
                field: 'createTime', title: '创建时间', minWidth: 100, align: "center", templet: function (d) {
                    return showTime(d.createTime);
                }
            },
            {
                field: 'updateTime', title: '修改时间', minWidth: 100, align: "center", templet: function (d) {
                    return showTime(d.updateTime);
                }
            },
            {title: '操作', minWidth: 200, templet: '#listBar', fixed: "right", align: "center", toolBar: '#listBar'}
        ]]
    });
    //时间转换函数
    function showTime(tempDate) {
        var d = new Date(tempDate);
        var year = d.getFullYear();
        var month = d.getMonth();
        month++;
        var day = d.getDate();
        var hours = d.getHours();

        var minutes = d.getMinutes();
        var seconds = d.getSeconds();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        hours = hours < 10 ? "0" + hours : hours;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;


        var time = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        return time;
    }
    $(".search_btn").click(function () {
        const searchInput = $("#searchInput").val();
        table.reload("listTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                contentsName: searchInput,
                contentType: $("#contentType").val(),
                isPublic: $("#isPublic").val(),
                version: $("#version").val(),
                createBeginTime: $("#createTime").val(),
                bookInfoId: $("#bookInfoId").val(),
            }
        })
    })
    $(".add_btn").click(function () {
        edit();
    })
    // 添加/修改
    function edit(edit) {
        let title = "添加章节";
        if (edit != null) {
            title = "修改章节";
        }
        const index = layui.layer.open({
            title: title,
            type: 2,
            content: "/cms/book/contents/edit.html",
            scrollbar: false,
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find(".uid").val(BigInt(edit.id).toString());
                    body.find(".userName").val(edit.userName);  //用户名
                    body.find(".loginName").val(edit.loginName);  //登录名
                    body.find(".userMobilePhone").val(edit.userMobilePhone);
                    body.find(".userNickName").val(edit.userNickName);
                    body.find(".userAddress").val(edit.userAddress);
                    body.find(".userEmail").val(edit.userEmail);  //邮箱
                    body.find(".userSex input[value=" + edit.userSex + "]").prop("checked", "checked");  //性别
                    body.find(".allowedNetworkType").val(edit.allowedNetworkType);
                    body.find(".userType").val(edit.userType);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDescription").text(edit.userDescription);    //用户简介
                    body.find(".remark").text(edit.remark);
                    form.render();
                }
                setTimeout(function () {
                    layui.layer.tips('点击此处返回分类列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        });
        layui.layer.full(index);
        window.sessionStorage.setItem("index", index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
})