define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.order/index',
        add_url: 'mall.order/add',
        edit_url: 'mall.order/edit',
        delete_url: 'mall.order/delete',
        export_url: 'mall.order/export',
        modify_url: 'mall.order/modify',
        recharge_url: 'mall.order/recharge',
        blockip_url: 'mall.order/blockip',
        recycle_url: 'mall.order/recycle',
        start_url: 'mall.order/start_url',
    };

    let checkTimer = null; // 定时器对象
    let autoCheckEnabled = true; // 默认启用
    let autoCheckId = 1; // 默认启用

    return {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: [
                    'refresh', 'delete', 'export', 'recycle'
                ],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID', searchOp: '='},
                    {field: 'rid', width: 100, title: '订单号'},
                    {field: 'money', width: 100, title: '金额'},
                    {field: 'ip', width: 100, title: 'IP'},
                    {field: 'url', width: 100, title: '支付链接', edit: true},
                    {field: 'status', title: '状态', width: 85, selectList: {0: '未分配', 1: '已分配', 2: '已支付'}},
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                    {
                        width: 500,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                class: 'layui-btn layui-btn-danger layui-btn-xs',
                                method: 'get',
                                auth: 'delete',
                                field: 'id',
                                text: '开始操作',
                                url: init.start_url,
                            }],
                            [{
                                class: 'layui-btn layui-btn-success layui-btn-xs',
                                method: 'open',
                                field: 'id',
                                auth: 'delete',
                                text: '编辑支付链接',
                                url: init.edit_url,
                                extend: 'data-width="90%" data-height="95%"',
                            }],
                            [{
                                class: 'layui-btn layui-btn-danger layui-btn-xs',
                                method: 'get',
                                field: 'id',
                                auth: 'delete',
                                text: '确认支付',
                                url: init.recharge_url,
                            }],
                            [{
                                class: 'layui-btn layui-btn-danger layui-btn-xs',
                                method: 'get',
                                field: 'id',
                                auth: 'delete',
                                text: 'IP拉黑',
                                url: init.blockip_url,
                            }],
                            'delete'
                        ]
                    }
                ]],
                done: (res) => {
                    $.each(res.data, function (idx, item) {

                        // ✅ 根据 type 修改“开始操作”按钮文字
                        const $row = $(`tr[data-index="${idx}"]`);
                        const $startBtn = $row.find('a.layui-btn:contains("开始操作")');
                        if (item.type === 1) {
                            $startBtn.text('取消操作')
                                .removeClass('layui-btn-danger')
                                .addClass('layui-btn-warm');
                        } else {
                            $startBtn.text('开始操作')
                                .removeClass('layui-btn-warm')
                                .addClass('layui-btn-danger');
                        }

                        // ✅ 根据状态高亮行
                        if (item.status === 1) {
                            $row.css({
                                'background': 'linear-gradient(to left, #77eb7c, #bbffbe, #ffffff, transparent)',
                                'border': 'none',
                            });
                        }
                    });
                }
            });
            // 🔁 定时检测逻辑
            checkTimer = setInterval(function () {
                ea.request.get({
                    url: ea.url('index/get_order'),
                }, function (res) {
                    if (res.code === 1 && res.data.count > 0) {
                        let audio = document.getElementById('order-audio');
                        if (!audio) {
                            audio = document.createElement('audio');
                            audio.id = 'order-audio';
                            audio.src = '/1.mp3'; // 你的提示音路径
                            audio.autoplay = true;
                            audio.volume = 1.0;
                            document.body.appendChild(audio);
                        }
                        if (autoCheckId !== res.data.rid) {
                            autoCheckId = res.data.rid;
                            layui.table.reload(init.table_render_id);
                        }
                        audio.play().catch(err => console.warn('播放失败', err));
                    }
                }, function (err) {
                    console.error('检查未分配订单失败', err);
                });
            }, 5000);
            // ✅ 每5分钟清理一次缓存并刷新页面
            setInterval(function () {
                try {
                    console.log('🧹 清理浏览器缓存并刷新页面...');
                    // 刷新当前页
                    window.location.reload();
                } catch (e) {
                    console.warn('清理缓存失败', e);
                }
            }, 10 * 60 * 1000);
// 5分钟 = 300000ms
            ea.listen();
        },

        edit: function () {
            ea.listen();
        },
    };

});
