define(['jquery'], function ($) {
    var Controller = {
        index: function () {
            var getColor = function (color) {
                if (!color || color === '—') return '#95a5a6';
                if (color.indexOf('红') !== -1) return '#e74c3c';
                if (color.indexOf('蓝') !== -1) return '#3498db';
                if (color.indexOf('绿') !== -1) return '#2ecc71';
                return '#95a5a6';
            };

            var ball = function (num, color) {
                return '<span class="num-ball-sm" style="background-color:' + getColor(color) + ';">' + num + '</span>';
            };

            function loadData() {
                var periods = parseInt($('#dash-periods').val()) || 30;
                if (periods < 10) periods = 10;
                if (periods > 100) periods = 100;
                $('#dash-content').html('<div class="text-center" style="padding:60px;"><i class="fa fa-spinner fa-spin fa-2x"></i><br><span class="text-muted">' + __('Loading') + '</span></div>');

                $.ajax({
                    url: 'history/dashboard',
                    type: 'GET',
                    data: {periods: periods},
                    dataType: 'json',
                    success: function (ret) {
                        if (ret.code != 1) {
                            $('#dash-content').html('<div class="alert alert-danger">' + (ret.msg || '加载失败') + '</div>');
                            return;
                        }
                        render(ret.data);
                    },
                    error: function () {
                        $('#dash-content').html('<div class="alert alert-danger">请求失败，请重试</div>');
                    }
                });
            }

            function render(data) {
                var hc = data.hotcold, cw = data.colorwave, zo = data.zodiac;
                var oe = data.oddeven, bs = data.bigsmall, sp = data.special, tn = data.tailnumbers;
                var zt = data.zonetransition;

                var html = '';

                // 热号：按次数降序取前5名
                var hotAll = hc.all.slice().sort(function (a, b) { return b.count - a.count; });
                var hotTop5 = hotAll.slice(0, 5);
                // 冷号：按次数升序取前5名，第5名有并列则全部包含
                var coldAll = hc.all.slice().sort(function (a, b) { return a.count - b.count; });
                var coldList = coldAll.slice(0, 5);
                if (coldList.length >= 5) {
                    var threshold = coldList[4].count;
                    var rest = coldAll.slice(5);
                    for (var k = 0; k < rest.length; k++) {
                        if (rest[k].count === threshold) coldList.push(rest[k]);
                        else break;
                    }
                }

                html += '<div class="dash-section"><h4>🔥❄️ 冷热号码</h4><div class="row">';
                html += '<div class="col-sm-6"><div class="dash-card" style="border-left:3px solid #e74c3c;"><div style="color:#e74c3c;font-weight:bold;margin-bottom:8px;">热号 Top 5</div>';
                html += '<div style="display:flex;flex-wrap:wrap;">';
                for (var i = 0; i < hotTop5.length; i++) {
                    html += '<span style="flex:0 0 20%;text-align:center;margin-bottom:4px;">' + ball(hotTop5[i].num, hotTop5[i].color) + '<br><span style="font-size:11px;color:#999;">' + hotTop5[i].count + '次</span></span>';
                }
                html += '</div></div></div>';
                html += '<div class="col-sm-6"><div class="dash-card" style="border-left:3px solid #3498db;"><div style="color:#3498db;font-weight:bold;margin-bottom:8px;">冷号</div>';
                html += '<div style="display:flex;flex-wrap:wrap;">';
                for (var i = 0; i < coldList.length; i++) {
                    html += '<span style="flex:0 0 20%;text-align:center;margin-bottom:4px;">' + ball(coldList[i].num, coldList[i].color) + '<br><span style="font-size:11px;color:#999;">' + coldList[i].count + '次</span></span>';
                }
                html += '</div></div></div></div></div>';

                html += '<div class="dash-section"><h4>📊 比例分析</h4><div class="row">';
                html += '<div class="col-sm-4"><div class="dash-card"><div id="colorwave-chart" style="width:100%;height:200px;"></div></div></div>';
                html += '<div class="col-sm-4"><div class="dash-card"><div id="oddeven-chart" style="width:100%;height:200px;"></div></div></div>';
                html += '<div class="col-sm-4"><div class="dash-card"><div id="bigsmall-chart" style="width:100%;height:200px;"></div></div></div>';
                html += '</div></div>';

                html += '<div class="dash-section"><h4>⭐ 生肖排名</h4><div id="zodiac-chart" style="width:100%;height:250px;"></div></div>';

                html += '<div class="dash-section"><h4>📈 特码走势</h4><div id="special-chart" style="width:100%;height:280px;"></div></div>';

                html += '<div class="dash-section"><h4>🔢 尾数频率</h4><div id="tail-chart" style="width:100%;height:220px;"></div></div>';

                // 区域转移概率 + 波色转移概率
                if (zt && zt.matrix && zt.matrix.length > 0) {
                    var cwt = data.colorwavetransition;
                    var zt2 = data.zodiactransition;
                    html += '<div class="dash-section"><h4>🔄 转移概率</h4><div class="row">';
                    // 左侧：区域转移
                    html += '<div class="col-sm-7"><div style="font-size:12px;color:#999;margin-bottom:8px;">区域转移（共 ' + zt.total_transitions + ' 次）</div>';
                    html += '<table class="table table-bordered table-condensed text-center" style="margin:0 auto;"><thead><tr><th style="width:80px;position:relative;"><div style="width:100%;height:35px;border-bottom:1px solid #e5e5e5;position:relative;"><span style="position:absolute;top:2px;right:5px;font-size:12px;">区域</span><span style="position:absolute;bottom:2px;left:5px;font-size:12px;">特码</span></div></th>';
                    for (var z = 0; z < zt.zones.length; z++) {
                        html += '<th>' + zt.zones[z] + '</th>';
                    }
                    html += '</tr></thead><tbody>';
                    for (var r = 0; r < 5; r++) {
                        html += '<tr><td style="font-weight:bold;">' + zt.zones[r] + '</td>';
                        for (var c = 0; c < 5; c++) {
                            var pct = zt.probabilities[r][c];
                            var cnt = zt.matrix[r][c];
                            var bg = pct > 30 ? '#e74c3c' : pct > 20 ? '#f39c12' : pct > 10 ? '#3498db' : pct > 0 ? '#95a5a6' : '#f5f5f5';
                            var txt = pct > 10 ? '#fff' : '#333';
                            html += '<td style="background-color:' + bg + ';color:' + txt + ';">' + cnt + '次<br>' + pct + '%</td>';
                        }
                        html += '</tr>';
                    }
                    html += '</tbody></table></div>';
                    // 右侧：波色转移
                    if (cwt && cwt.matrix && cwt.matrix.length > 0) {
                        html += '<div class="col-sm-5"><div style="font-size:12px;color:#999;margin-bottom:8px;">波色转移（共 ' + cwt.total_transitions + ' 次）</div>';
                        html += '<table class="table table-bordered table-condensed text-center" style="margin:0 auto;"><thead><tr><th style="width:70px;position:relative;"><div style="width:100%;height:35px;border-bottom:1px solid #e5e5e5;position:relative;"><span style="position:absolute;top:2px;right:5px;font-size:12px;">区域</span><span style="position:absolute;bottom:2px;left:5px;font-size:12px;">特码</span></div></th>';
                        for (var z = 0; z < cwt.colors.length; z++) {
                            html += '<th>' + cwt.colors[z] + '</th>';
                        }
                        html += '</tr></thead><tbody>';
                        var cwColors = ['#e74c3c', '#3498db', '#2ecc71'];
                        for (var r = 0; r < 3; r++) {
                            html += '<tr><td style="font-weight:bold;color:' + cwColors[r] + ';">' + cwt.colors[r] + '</td>';
                            for (var c = 0; c < 3; c++) {
                                var pct = cwt.probabilities[r][c];
                                var cnt = cwt.matrix[r][c];
                                var bg = pct > 40 ? cwColors[r] : pct > 20 ? cwColors[c] : pct > 0 ? '#95a5a6' : '#f5f5f5';
                                var txt = pct > 20 ? '#fff' : '#333';
                                html += '<td style="background-color:' + bg + ';color:' + txt + ';">' + cnt + '次<br>' + pct + '%</td>';
                            }
                            html += '</tr>';
                        }
                        html += '</tbody></table></div>';
                    }
                    html += '</div>';
                    // 下方：区域→区域转移矩阵，单元格内显示波色概率
                    var ztc = data.zonetocolortransition;
                    if (ztc && ztc.matrix && ztc.matrix.length > 0) {
                        html += '<div style="margin-top:20px;font-size:12px;color:#999;margin-bottom:8px;">区域→区域转移矩阵（单元格内为波色概率）</div>';
                        html += '<table class="table table-bordered table-condensed text-center" style="max-width:600px;margin:0 auto;"><thead><tr><th style="width:80px;position:relative;"><div style="width:100%;height:35px;border-bottom:1px solid #e5e5e5;position:relative;"><span style="position:absolute;top:2px;right:5px;font-size:12px;">区域</span><span style="position:absolute;bottom:2px;left:5px;font-size:12px;">特码</span></div></th>';
                        for (var z = 0; z < ztc.zones.length; z++) {
                            html += '<th>' + ztc.zones[z] + '</th>';
                        }
                        html += '</tr></thead><tbody>';
                        for (var r = 0; r < 5; r++) {
                            html += '<tr><td style="font-weight:bold;">' + ztc.zones[r] + '</td>';
                            for (var c = 0; c < 5; c++) {
                                var cnt = ztc.matrix[r][c];
                                var cp = ztc.color_probs[r][c];
                                var bg = cnt > 0 ? '#fafafa' : '#fff';
                                var txt = (cp.red > 40 || cp.blue > 40 || cp.green > 40) ? '#fff' : '#333';
                                html += '<td style="background-color:' + bg + ';color:' + txt + ';"><span style="color:#e74c3c;">红' + cp.red + '%</span><br><span style="color:#3498db;">蓝' + cp.blue + '%</span><br><span style="color:#2ecc71;">绿' + cp.green + '%</span><br><span style="font-size:11px;color:#000;">(' + cnt + '次)</span></td>';
                            }
                            html += '</tr>';
                        }
                        html += '</tbody></table>';
                    }
                    // 下方：生肖转移矩阵
                    if (zt2 && zt2.matrix && zt2.matrix.length > 0) {
                        html += '<div style="margin-top:20px;font-size:12px;color:#999;margin-bottom:8px;">生肖转移（共 ' + zt2.total_transitions + ' 次）</div>';
                        html += '<table class="table table-bordered table-condensed text-center" style="max-width:720px;margin:0 auto;"><thead><tr><th style="width:50px;">特码</th>';
                        for (var z = 0; z < zt2.animals.length; z++) {
                            html += '<th>' + zt2.animals[z] + '</th>';
                        }
                        html += '</tr></thead><tbody>';
                        var animalColors = ['#e74c3c', '#3498db', '#2ecc71', '#f39c12', '#9b59b6', '#1abc9c', '#e67e22', '#34495e', '#e74c3c', '#3498db', '#2ecc71', '#f39c12'];
                        for (var r = 0; r < zt2.animals.length; r++) {
                            html += '<tr><td style="font-weight:bold;color:' + animalColors[r] + ';">' + zt2.animals[r] + '</td>';
                            for (var c = 0; c < zt2.animals.length; c++) {
                                var pct = zt2.probabilities[r][c];
                                var cnt = zt2.matrix[r][c];
                                var bg = pct > 30 ? '#e74c3c' : pct > 20 ? '#f39c12' : pct > 10 ? '#3498db' : pct > 0 ? '#95a5a6' : '#f5f5f5';
                                var txt = pct > 10 ? '#fff' : '#333';
                                html += '<td style="background-color:' + bg + ';color:' + txt + ';">' + cnt + '次<br>' + pct + '%</td>';
                            }
                            html += '</tr>';
                        }
                        html += '</tbody></table>';
                    }

                    // 尾号转移矩阵
                    var tnt = data.tailnumbertransition;
                    if (tnt && tnt.matrix && tnt.matrix.length > 0) {
                        html += '<div style="margin-top:20px;font-size:12px;color:#999;margin-bottom:8px;">尾号转移（共 ' + tnt.total_transitions + ' 次）</div>';
                        html += '<table class="table table-bordered table-condensed text-center" style="max-width:600px;margin:0 auto;"><thead><tr><th style="width:50px;">特码</th>';
                        for (var z = 0; z < tnt.tails.length; z++) {
                            html += '<th>' + tnt.tails[z] + '</th>';
                        }
                        html += '</tr></thead><tbody>';
                        for (var r = 0; r < tnt.tails.length; r++) {
                            html += '<tr><td style="font-weight:bold;">' + tnt.tails[r] + '</td>';
                            for (var c = 0; c < tnt.tails.length; c++) {
                                var pct = tnt.probabilities[r][c];
                                var cnt = tnt.matrix[r][c];
                                var bg = pct > 20 ? '#e74c3c' : pct > 15 ? '#f39c12' : pct > 10 ? '#3498db' : pct > 0 ? '#95a5a6' : '#f5f5f5';
                                var txt = pct > 10 ? '#fff' : '#333';
                                html += '<td style="background-color:' + bg + ';color:' + txt + ';">' + cnt + '次<br>' + pct + '%</td>';
                            }
                            html += '</tr>';
                        }
                        html += '</tbody></table>';
                    }

                    // 首号转移矩阵
                    var hnt = data.headnumbertransition;
                    if (hnt && hnt.matrix && hnt.matrix.length > 0) {
                        html += '<div style="margin-top:20px;font-size:12px;color:#999;margin-bottom:8px;">首号转移（共 ' + hnt.total_transitions + ' 次）</div>';
                        html += '<table class="table table-bordered table-condensed text-center" style="max-width:400px;margin:0 auto;"><thead><tr><th style="width:60px;">特码</th>';
                        for (var z = 0; z < hnt.heads.length; z++) {
                            html += '<th>' + hnt.heads[z] + '</th>';
                        }
                        html += '</tr></thead><tbody>';
                        for (var r = 0; r < hnt.heads.length; r++) {
                            html += '<tr><td style="font-weight:bold;">' + hnt.heads[r] + '</td>';
                            for (var c = 0; c < hnt.heads.length; c++) {
                                var pct = hnt.probabilities[r][c];
                                var cnt = hnt.matrix[r][c];
                                var bg = pct > 30 ? '#e74c3c' : pct > 20 ? '#f39c12' : pct > 10 ? '#3498db' : pct > 0 ? '#95a5a6' : '#f5f5f5';
                                var txt = pct > 10 ? '#fff' : '#333';
                                html += '<td style="background-color:' + bg + ';color:' + txt + ';">' + cnt + '次<br>' + pct + '%</td>';
                            }
                            html += '</tr>';
                        }
                        html += '</tbody></table>';
                    }
                    html += '</div>';
                }

                // 热力图部分
                var hm = data.heatmap;
                html += '<div class="dash-section"><h4>🎨 特码热力图</h4><div style="font-size:12px;color:#999;margin-bottom:8px;">X轴：期号（从左往右，从远到近） | Y轴：号码1-49 | 颜色：号码波色</div><div id="heatmap-chart" style="width:100%;height:500px;"></div></div>';

                $('#dash-content').html(html);

                // 波色饼图
                if (typeof echarts !== 'undefined') {
                    var cwDom = document.getElementById('colorwave-chart');
                    if (cwDom) {
                        var cwChart = echarts.init(cwDom);
                        cwChart.setOption({
                            tooltip: {trigger: 'item', formatter: '{b}: {c} ({d}%)'},
                            series: [{
                                type: 'pie', radius: '60%',
                                data: [
                                    {value: cw.red, name: '红波', itemStyle: {color: '#e74c3c'}},
                                    {value: cw.blue, name: '蓝波', itemStyle: {color: '#3498db'}},
                                    {value: cw.green, name: '绿波', itemStyle: {color: '#2ecc71'}}
                                ],
                                label: {show: true, formatter: '{b}\n{d}%'},
                                emphasis: {itemStyle: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0,0,0,0.5)'}}
                            }],
                            grid: {top: 0, bottom: 0}
                        });
                        $(window).on('resize', function(){ cwChart.resize(); });
                    }

                    // 奇偶饼图
                    var oeDom = document.getElementById('oddeven-chart');
                    if (oeDom) {
                        var oeChart = echarts.init(oeDom);
                        oeChart.setOption({
                            tooltip: {trigger: 'item', formatter: '{b}: {c} ({d}%)'},
                            series: [{
                                type: 'pie', radius: '60%',
                                data: [
                                    {value: oe.odd, name: '奇数', itemStyle: {color: '#e74c3c'}},
                                    {value: oe.even, name: '偶数', itemStyle: {color: '#3498db'}}
                                ],
                                label: {show: true, formatter: '{b}\n{d}%'},
                                emphasis: {itemStyle: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0,0,0,0.5)'}}
                            }],
                            grid: {top: 0, bottom: 0}
                        });
                        $(window).on('resize', function(){ oeChart.resize(); });
                    }

                    // 大小饼图
                    var bsDom = document.getElementById('bigsmall-chart');
                    if (bsDom) {
                        var bsChart = echarts.init(bsDom);
                        bsChart.setOption({
                            tooltip: {trigger: 'item', formatter: '{b}: {c} ({d}%)'},
                            series: [{
                                type: 'pie', radius: '60%',
                                data: [
                                    {value: bs.big, name: '大数', itemStyle: {color: '#f39c12'}},
                                    {value: bs.small, name: '小数', itemStyle: {color: '#2ecc71'}}
                                ],
                                label: {show: true, formatter: '{b}\n{d}%'},
                                emphasis: {itemStyle: {shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0,0,0,0.5)'}}
                            }],
                            grid: {top: 0, bottom: 0}
                        });
                        $(window).on('resize', function(){ bsChart.resize(); });
                    }
                }

                // 生肖柱状图
                if (typeof echarts !== 'undefined' && zo.list && zo.list.length > 0) {
                    var zDom = document.getElementById('zodiac-chart');
                    if (zDom) {
                        var zChart = echarts.init(zDom);
                        zChart.setOption({
                            tooltip: {trigger: 'axis', axisPointer: {type: 'shadow'}},
                            grid: {left: 50, right: 20, bottom: 30, top: 10},
                            xAxis: {type: 'category', data: zo.list.map(function(z){return z.animal;})},
                            yAxis: {type: 'value', splitNumber: 3},
                            series: [{type: 'bar', data: zo.list.map(function(z){return z.count;}), itemStyle: {color: '#626c91'}, label: {show: true, position: 'top', fontSize: 12, color: '#333'}}]
                        });
                        $(window).on('resize', function(){ zChart.resize(); });
                    }
                }

                // 特码走势
                if (typeof echarts !== 'undefined' && sp.expects && sp.expects.length > 0) {
                    var chartDom = document.getElementById('special-chart');
                    if (chartDom) {
                        var chart = echarts.init(chartDom);
                        var colorMap = {'红': '#e74c3c', '蓝': '#3498db', '绿': '#2ecc71'};
                        var itemColors = sp.colors.map(function(c) {
                            if (c.indexOf('红') !== -1) return colorMap['红'];
                            if (c.indexOf('蓝') !== -1) return colorMap['蓝'];
                            if (c.indexOf('绿') !== -1) return colorMap['绿'];
                            return '#95a5a6';
                        });
                        chart.setOption({
                            tooltip: {trigger: 'axis', formatter: function(p) {
                                return '期号: ' + p[0].axisValue + '<br/>特码: <b style="color:' + itemColors[p[0].dataIndex] + '">' + p[0].data + '</b>';
                            }},
                            xAxis: {type: 'category', data: sp.expects, axisLabel: {rotate: 45, fontSize: 10}},
                            yAxis: {type: 'value', min: 0, max: 50},
                            series: [{type: 'line', data: sp.specials, smooth: true, symbolSize: 8, itemStyle: {color: function(p) { return itemColors[p.dataIndex]; }}, areaStyle: {color: 'rgba(52,152,219,0.05)'}}],
                            grid: {left: 50, right: 20, bottom: 50, top: 20}
                        });
                        $(window).on('resize', function () { chart.resize(); });
                    }
                }

                // 尾数柱状图
                if (typeof echarts !== 'undefined' && tn.all && tn.all.length > 0) {
                    var tDom = document.getElementById('tail-chart');
                    if (tDom) {
                        var tChart = echarts.init(tDom);
                        var sorted = tn.all.slice().sort(function(a,b){return a.tail - b.tail;});
                        tChart.setOption({
                            tooltip: {trigger: 'axis', axisPointer: {type: 'shadow'}},
                            grid: {left: 50, right: 20, bottom: 30, top: 10},
                            xAxis: {type: 'category', data: sorted.map(function(t){return t.tail;})},
                            yAxis: {type: 'value', splitNumber: 3},
                            series: [{type: 'bar', data: sorted.map(function(t){return t.count;}), itemStyle: {color: '#23b7e5'}, label: {show: true, position: 'top', fontSize: 12, color: '#333'}}]
                        });
                        $(window).on('resize', function(){ tChart.resize(); });
                    }
                }

                // 特码热力图
                if (typeof echarts !== 'undefined' && hm && hm.expects && hm.expects.length > 0) {
                    var hmDom = document.getElementById('heatmap-chart');
                    if (hmDom) {
                        var hmChart = echarts.init(hmDom);
                        // 构建完整的热力图数据矩阵（每期每号码）
                        var hmData = [];
                        for (var x = 0; x < hm.expects.length; x++) {
                            for (var y = 0; y < 49; y++) {
                                // 检查该期是否有该号码作为特码
                                var found = false;
                                for (var i = 0; i < hm.heatmap.length; i++) {
                                    if (hm.heatmap[i][0] === x && hm.heatmap[i][1] === y) {
                                        found = true;
                                        break;
                                    }
                                }
                                hmData.push([x, y, found ? 1 : 0]);
                            }
                        }
                        hmChart.setOption({
                            tooltip: {
                                position: 'top',
                                formatter: function(p) {
                                    var periodIdx = p.data[0];
                                    var num = p.data[1] + 1;
                                    var val = p.data[2];
                                    return '期号: ' + hm.expects[periodIdx] + '<br/>号码: ' + num + '<br/>状态: ' + (val === 1 ? '已开出' : '未开出');
                                }
                            },
                            grid: {left: 80, right: 30, bottom: 60, top: 30},
                            xAxis: {
                                type: 'category',
                                data: hm.expects,
                                axisLabel: {rotate: 45, fontSize: 10, interval: 0},
                                splitArea: {show: true}
                            },
                            yAxis: {
                                type: 'category',
                                data: hm.nums,
                                axisLabel: {fontSize: 10},
                                splitArea: {show: true}
                            },
                            visualMap: {
                                min: 0,
                                max: 1,
                                show: false,
                                inRange: {
                                    color: ['#f0f0f0', '#e74c3c']
                                }
                            },
                            series: [{
                                type: 'heatmap',
                                data: hmData,
                                label: {show: false},
                                itemStyle: {
                                    color: function(p) {
                                        // 未开出显示浅灰，开出显示该号码的波色
                                        if (p.data[2] === 0) return '#f5f5f5';
                                        var numIdx = p.data[1];
                                        return hm.colors[numIdx] || '#95a5a6';
                                    }
                                },
                                emphasis: {
                                    itemStyle: {shadowBlur: 10, shadowColor: 'rgba(0,0,0,0.5)'}
                                }
                            }]
                        });
                        $(window).on('resize', function(){ hmChart.resize(); });
                    }
                }
            }

            $('#btn-dash-refresh').on('click', loadData);
            loadData();
        }
    };

    return Controller;
});
