(function ($) {
    //当前页
    let currentPage = 1;
    // 配置常量
    const CONFIG = {
        grid: {
            pageNo: 1,
            pageRows: 5
        }
    }
    // 点击输入框时显示对应的自定义div
    $('.smart-dropdown-input').click(function () {
        $('.smart-dropdown-menu').css('display', 'block');
        initInfo();
    });
    //点击其它区域时关闭下拉框
    $(document).click(function (event) {
        if (!$(event.target).closest('.smart-dropdown').length) {
            $('.smart-dropdown-menu').css('display', 'none');
        }
    });
    //选择
    $('.smart-dropdown-content').on('click', '.smart-dropdown-item', function() {
        const value = $(this).data('value');
        const text = $(this).data('text');
        selectItem(value, text);
    });
    // 选择项
    function selectItem(value, text) {
        $('.smart-dropdown-input').val(text)
        $('#bookInfoId').val(value);
        $('.smart-search-input').val("");
        $('.smart-dropdown-menu').css('display', 'none');
    }
    //实时搜索
    $('.smart-search-input').on('input', function() {
        initInfo();
    });
    //上一页
    $(".smart-page-prev").click(function () {
        CONFIG.grid.pageNo = currentPage - 1;
        currentPage = currentPage - 1;
        initInfo();
    })
    //下一页
    $(".smart-page-next").click(function () {
        CONFIG.grid.pageNo = currentPage + 1;
        currentPage = currentPage + 1;
        initInfo();
    })

    function setInfo(data) {
        const items = data.dataList || [];
        const pageCount = data.pageCount || 1;
        const totalRows = data.totalRows || 0;
        const currentRows = items.length;


        // 渲染列表项
        let html = '';
        if (items.length > 0) {
            items.forEach(function (item) {
                let itemHtml = '';
                itemHtml = `<div class="smart-dropdown-item" 
                                          data-value="${item.isbn}" 
                                          data-text="${item.title}">${item.title}</div>`;
                html += itemHtml;
            });
        } else {
            html = '<div class="loading">暂无数据</div>';
        }
        $('.smart-dropdown-content').html(html);

        // 渲染分页信息
        let paginationText = '';
        const start = (currentPage - 1) * data.pageRows + 1;
        const end = start + currentRows - 1;
        paginationText = `显示 ${start}-${end} 条，共 ${totalRows} 条`;

        $('.smart-pagination-info').text(paginationText);

        // 更新分页按钮状态
        $('.smart-page-prev').prop('disabled', currentPage <= 1);
        $('.smart-page-next').prop('disabled', currentPage >= pageCount);
    }

    //初始化信息
    function initInfo() {
        var requestData = $.extend({
            infoName:$('.smart-search-input').val()
        }, CONFIG.grid);
        $.ajax({
            url: "/cms/book/info/queryPage",
            type: "POST",
            dataType: "json",
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify(requestData),
            success: function (res) {
                if (res.code === 200) {
                    setInfo(res.data);
                } else {
                    showError("加载失败", res.msg);
                }
            },
            error: function (xhr, status, error) {
                console.error("请求失败:", error);
                showError("请求失败", "网络错误或服务器异常");
            }
        });
    }

    // 显示错误信息
    function showError(title, msg) {
        layer.msg(msg || '操作失败', {icon: 2});
    }
})(jQuery);