@extends('base.base')

@section('base')
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="page-header">
                <h3 class="page-title">
                    <span class="page-title-icon bg-gradient-primary text-white mr-2">
                        <i class="mdi mdi-cookie"></i>
                    </span>
                    AccountCookie 分析
                </h3>
            </div>

            <div class="row mb-3">
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['total'] }}</h4>
                            <small>命中记录</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['unique_cookies'] }}</h4>
                            <small>唯一 Cookie</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['registered_users'] }}</h4>
                            <small>注册用户</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['paid_users'] }}</h4>
                            <small>付费用户</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['fbclid_count'] }}</h4>
                            <small>含 fbclid</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['unique_ips'] }}</h4>
                            <small>唯一 IP</small>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row mb-3">
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['unique_users'] }}</h4>
                            <small>唯一 UserID</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['fb_inapp_count'] }}</h4>
                            <small>Facebook UA</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['ig_inapp_count'] }}</h4>
                            <small>Instagram UA</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['duplicate_fbclid_groups'] }}</h4>
                            <small>重复 fbclid 组</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['duplicate_fbclid_rows'] }}</h4>
                            <small>重复 fbclid 行</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['duplicate_ff_groups'] }}</h4>
                            <small>重复 FF 组</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h4>{{ $stats['duplicate_ff_rows'] }}</h4>
                            <small>重复 FF 行</small>
                        </div>
                    </div>
                </div>
                <div class="col-md-2">
                    <div class="card border-danger">
                        <div class="card-body text-center">
                            <h4 class="text-danger">{{ $stats['fbclid_cookie_issues'] }}</h4>
                            <small>fbclid/Cookie 异常</small>
                        </div>
                    </div>
                </div>
            </div>

            <form class="form-inline mb-3" method="get" action="">
                <div class="form-group mr-2">
                    <label class="mr-1">UserID</label>
                    <input type="text" name="UserID" class="form-control" value="{{ $filters['user_id'] }}" style="width: 120px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">GameID</label>
                    <input type="text" name="GameID" class="form-control" value="{{ $filters['game_id'] }}" style="width: 120px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">UrlSign</label>
                    <input type="text" name="UrlSign" class="form-control" value="{{ implode(',', $filters['url_signs']) }}" placeholder="110,125" style="width: 140px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">账号渠道</label>
                    <input type="text" name="AccountChannel" class="form-control" value="{{ implode(',', $filters['account_channels']) }}" placeholder="110,125" style="width: 140px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">Platform</label>
                    <input type="text" name="Platform" class="form-control" value="{{ $filters['platform'] }}" style="width: 100px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">CreateStart</label>
                    <input type="date" name="date_start" class="form-control" value="{{ $filters['date_start_raw'] }}" style="width: 160px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">CreateEnd</label>
                    <input type="date" name="date_end" class="form-control" value="{{ $filters['date_end_raw'] }}" style="width: 160px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">RegStart</label>
                    <input type="date" name="register_start" class="form-control" value="{{ $filters['register_start_raw'] }}" style="width: 160px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">RegEnd</label>
                    <input type="date" name="register_end" class="form-control" value="{{ $filters['register_end_raw'] }}" style="width: 160px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">fbclid</label>
                    <select name="has_fbclid" class="form-control">
                        <option value="1" @if($filters['has_fbclid'] === 1) selected @endif>仅 fbclid</option>
                        <option value="0" @if($filters['has_fbclid'] === 0) selected @endif>全部</option>
                        <option value="2" @if($filters['has_fbclid'] === 2) selected @endif>排除 fbclid</option>
                    </select>
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">Origin</label>
                    <input type="text" name="Origin" class="form-control" value="{{ $filters['origin'] }}" style="width: 180px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">IP</label>
                    <input type="text" name="IP" class="form-control" value="{{ $filters['ip'] }}" style="width: 140px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">UA</label>
                    <input type="text" name="UA" class="form-control" value="{{ $filters['ua'] }}" style="width: 180px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">参数分类</label>
                    <select name="param_category" class="form-control">
                        <option value="" @if($filters['param_category'] === '') selected @endif>全部</option>
                        <option value="channel" @if($filters['param_category'] === 'channel') selected @endif>Channel</option>
                        <option value="utm" @if($filters['param_category'] === 'utm') selected @endif>UTM</option>
                        <option value="campaign" @if($filters['param_category'] === 'campaign') selected @endif>Campaign</option>
                        <option value="attribution" @if($filters['param_category'] === 'attribution') selected @endif>Attribution</option>
                        <option value="identifier" @if($filters['param_category'] === 'identifier') selected @endif>Identifier</option>
                    </select>
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">参数名</label>
                    <input type="text" name="param_key" class="form-control" value="{{ $filters['param_key'] }}" placeholder="utm_campaign" style="width: 140px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">参数值</label>
                    <input type="text" name="param_value" class="form-control" value="{{ $filters['param_value'] }}" placeholder="120242..." style="width: 180px;">
                </div>
                <div class="form-group mr-2">
                    <label class="mr-1">每页</label>
                    <select name="page_size" class="form-control">
                        <option value="100" @if($filters['page_size'] == 100) selected @endif>100</option>
                        <option value="200" @if($filters['page_size'] == 200) selected @endif>200</option>
                        <option value="500" @if($filters['page_size'] == 500) selected @endif>500</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">搜索</button>
                <a href="/admin/account_cookie/list" class="btn btn-warning ml-2">重置</a>
            </form>

            <div class="row mb-3">
                <div class="col-md-3">
                    <div class="card">
                        <div class="card-header">Platform 分布</div>
                        <div class="card-body" style="max-height: 220px; overflow-y: auto;">
                            <table class="table table-sm">
                                @foreach(array_slice($stats['platforms'], 0, 10, true) as $key => $count)
                                    <tr><td>{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                @endforeach
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card">
                        <div class="card-header">UrlSign 分布</div>
                        <div class="card-body" style="max-height: 220px; overflow-y: auto;">
                            <table class="table table-sm">
                                @foreach(array_slice($stats['url_signs'], 0, 10, true) as $key => $count)
                                    <tr><td>{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                @endforeach
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card">
                        <div class="card-header">utm_source 分布</div>
                        <div class="card-body" style="max-height: 220px; overflow-y: auto;">
                            <table class="table table-sm">
                                @foreach(array_slice($stats['utm_sources'], 0, 10, true) as $key => $count)
                                    <tr><td>{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                @endforeach
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card">
                        <div class="card-header">Origin 分布</div>
                        <div class="card-body" style="max-height: 220px; overflow-y: auto;">
                            <table class="table table-sm">
                                @foreach(array_slice($stats['origins'], 0, 10, true) as $key => $count)
                                    <tr><td style="word-break: break-all;">{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                @endforeach
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row mb-3">
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header">投放参数分类</div>
                        <div class="card-body" style="max-height: 240px; overflow-y: auto;">
                            <table class="table table-sm">
                                @forelse(array_slice($stats['param_categories'], 0, 12, true) as $key => $count)
                                    <tr><td>{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                @empty
                                    <tr><td colspan="2" class="text-center">暂无数据</td></tr>
                                @endforelse
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header">投放参数 Key Top</div>
                        <div class="card-body" style="max-height: 240px; overflow-y: auto;">
                            <table class="table table-sm">
                                @forelse(array_slice($stats['param_keys'], 0, 12, true) as $key => $count)
                                    <tr><td>{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                @empty
                                    <tr><td colspan="2" class="text-center">暂无数据</td></tr>
                                @endforelse
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header">识别重点字段</div>
                        <div class="card-body" style="max-height: 240px; overflow-y: auto; font-size: 13px;">
                            <div>支持动态提取 Params 中的全部字段</div>
                            <div>自动归类为 Channel / UTM / Campaign / Attribution / Identifier / Custom</div>
                            <div style="margin-top: 6px;">可按分类、参数名、参数值组合筛选</div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">重复 fbclid 分组</div>
                        <div class="card-body" style="max-height: 220px; overflow-y: auto;">
                            <table class="table table-sm">
                                <thead>
                                <tr>
                                    <th>组</th>
                                    <th>颜色</th>
                                    <th>数量</th>
                                    <th>fbclid</th>
                                </tr>
                                </thead>
                                <tbody>
                                @forelse(array_slice($fbclidGroups, 0, 20) as $group)
                                    <tr>
                                        <td>#{{ $group['index'] }}</td>
                                        <td>
                                            <span style="display:inline-block;width:18px;height:18px;border-radius:3px;border:1px solid #ccc;background:{{ $group['color'] }};"></span>
                                        </td>
                                        <td>{{ $group['count'] }}</td>
                                        <td style="word-break: break-all;">{{ $group['value'] }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-center">当前筛选结果没有重复 fbclid。</td>
                                    </tr>
                                @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">重复 FF 分组</div>
                        <div class="card-body" style="max-height: 220px; overflow-y: auto;">
                            <table class="table table-sm">
                                <thead>
                                <tr>
                                    <th>组</th>
                                    <th>颜色</th>
                                    <th>数量</th>
                                    <th>FF</th>
                                </tr>
                                </thead>
                                <tbody>
                                @forelse(array_slice($ffGroups, 0, 20) as $group)
                                    <tr>
                                        <td>#{{ $group['index'] }}</td>
                                        <td>
                                            <span style="display:inline-block;width:18px;height:18px;border-radius:3px;border:1px solid #ccc;background:{{ $group['color'] }};"></span>
                                        </td>
                                        <td>{{ $group['count'] }}</td>
                                        <td style="word-break: break-all;">{{ $group['value'] }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-center">当前筛选结果没有重复 FF。</td>
                                    </tr>
                                @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row mb-3">
                @forelse($stats['param_category_stats'] as $category => $categoryStats)
                    <div class="col-md-4">
                        <div class="card">
                            <div class="card-header">{{ $categoryStats['label'] }} 详情</div>
                            <div class="card-body" style="max-height: 260px; overflow-y: auto;">
                                <div class="mb-2"><strong>Top Keys</strong></div>
                                <table class="table table-sm mb-3">
                                    @foreach(array_slice($categoryStats['keys'], 0, 6, true) as $key => $count)
                                        <tr><td>{{ $key }}</td><td class="text-right">{{ $count }}</td></tr>
                                    @endforeach
                                </table>
                                <div class="mb-2"><strong>Top Values</strong></div>
                                <table class="table table-sm">
                                    @foreach(array_slice($categoryStats['values'], 0, 6, true) as $value => $count)
                                        <tr><td style="word-break: break-all;">{{ $value }}</td><td class="text-right">{{ $count }}</td></tr>
                                    @endforeach
                                </table>
                            </div>
                        </div>
                    </div>
                @empty
                    <div class="col-md-12">
                        <div class="alert alert-light border">当前筛选下没有可分析的投放参数。</div>
                    </div>
                @endforelse
            </div>

            <div class="card">
                <div class="card-header">注册列表 (共 {{ $list->total() }} 条)</div>
                <div class="card-body table-responsive">
                    @php
                        $formatTime = function ($value) {
                            if (empty($value)) {
                                return '-';
                            }
        
                            return preg_replace('/^\d{4}-\d{2}-/', '', preg_replace('/\.\d+$/', '', str_replace(' ', "\n", $value)));
                        };
                        $formatShort = function ($value, $length = 30) {
                            $value = (string)$value;
                            if ($value === '') {
                                return '-';
                            }
                            return mb_strlen($value) > $length ? mb_substr($value, 0, $length) . '...' : $value;
                        };
                        $paramLabels = [
                            'channel' => 'Channel',
                            'utm' => 'UTM',
                            'campaign' => 'Campaign',
                            'attribution' => 'Attribution',
                            'identifier' => 'Identifier',
                            'custom' => 'Custom',
                        ];
                    @endphp
                    <table class="table table-bordered table-sm">
                        <thead>
                        <tr>
                            <th>UserID</th>
                            <th>GameID</th>
                            <th>账号渠道</th>
                            <th>UrlSign</th>
                            <th>CreateTime</th>
                            <th>RegisterDate</th>
                            <th>Platform</th>
                            <th>付费</th>
                            <th>上报状态</th>
                            <th>UA分析</th>
                            <th>IP / Locale</th>
                            <th>Origin</th>
                            <th>投放参数</th>
                            <th>Cookie</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($list as $row)
                            @php
                                $paramDialogSections = [];
                                foreach (($row->ParamAnalysis['categories'] ?? []) as $category => $entries) {
                                    $lines = [($paramLabels[$category] ?? ucfirst($category)) . ':'];
                                    foreach ($entries as $entry) {
                                        $lines[] = $entry['key'] . '=' . $entry['value'];
                                    }
                                    $paramDialogSections[] = implode("\n", $lines);
                                }
                                $paramDialogText = implode("\n\n", $paramDialogSections);
                            @endphp
                            @php
                                $rowStyle = '';
                                if (!empty($row->FFGroup)) {
                                    $rowStyle = 'background-color: ' . $row->FFGroup['color'] . ';';
                                } elseif (!empty($row->FbclidGroup)) {
                                    $rowStyle = 'background-color: ' . $row->FbclidGroup['color'] . ';';
                                }
                                if (!empty($row->FbclidGroup)) {
                                    $rowStyle .= 'border-left: 4px solid #856404;';
                                }
                                if (!($row->FbclidCookieCheck['ok'] ?? true)) {
                                    $rowStyle .= 'border-right: 4px solid #dc3545;';
                                }
                            @endphp
                            <tr @if($rowStyle !== '') style="{{ $rowStyle }}" @endif>
                                <td>{{ $row->UserID }}</td>
                                <td>{{ $row->GameID }}</td>
                                <td>{{ $row->AccountChannel }}</td>
                                <td>{{ $row->UrlSign }}</td>
                                <td style="white-space: nowrap;">{{ $formatTime($row->CreateTime) }}</td>
                                <td style="white-space: nowrap;">{{ $formatTime($row->RegisterDate) }}</td>
                                <td>{{ $row->Platform }}</td>
                                <td>
                                    <div>单数: {{ $row->PayOrderCount }}</div>
                                    <div>金额: {{ $row->PayAmountDisplay }}</div>
                                    <div style="white-space: nowrap;">{{ $formatTime($row->LastPayAt) }}</div>
                                </td>
                                <td style="min-width: 150px;">
                                    <div>{{ $row->AdjustStatus }}</div>
                                    @if(!empty($row->AdjustLogs))
                                        <div style="margin-top: 6px; font-size: 12px; color: #666; word-break: break-all;">
                                            {{ $formatShort($row->AdjustLogs[0] ?? '', 24) }}
                                        </div>
                                        <a href="javascript:void(0);" onclick='showTextDialog("上报日志 - UserID {{ $row->UserID }}", @json(implode("\n\n", $row->AdjustLogs)))'>查看详情</a>
                                    @endif
                                </td>
                                <td>
                                    <div>{{ $row->UaApp }}</div>
                                    <div>{{ $row->UaOs }}</div>
                                    <div>{{ $row->UaDevice }}</div>
                                </td>
                                <td>
                                    <div>{{ $row->IP }}</div>
                                    <div>{{ $row->Locale }}</div>
                                    @if(!empty($row->IP))
                                        <a href="javascript:void(0);" onclick='lookupIpLocation(this, @json($row->IP))'>查地址</a>
                                        <div class="ip-location-result" style="margin-top: 4px; font-size: 12px; color: #666; word-break: break-all;"></div>
                                    @endif
                                </td>
                                <td style="max-width: 220px; word-break: break-all;">{{ $row->Origin }}</td>
                                <td style="min-width: 220px;">
                                    <div>fbclid: {{ $row->HasFbclid ? 'Y' : 'N' }}</div>
                                    @if(!empty($row->FbclidGroup))
                                        <div>
                                            <span class="badge badge-dark">组 #{{ $row->FbclidGroup['index'] }}</span>
                                            <span class="badge badge-light">{{ $row->FbclidGroup['count'] }} 条</span>
                                        </div>
                                        <div style="word-break: break-all;">{{ $formatShort($row->FbclidValue, 28) }}</div>
                                    @endif
                                    @if(!($row->FbclidCookieCheck['ok'] ?? true))
                                        <div style="margin-top: 4px;">
                                            <span class="badge badge-danger">{{ $row->FbclidCookieCheck['status'] }}</span>
                                        </div>
                                        <div class="text-danger" style="word-break: break-all;">
                                            {{ $row->FbclidCookieCheck['message'] }}
                                        </div>
                                    @endif
                                    @if(!empty($row->FFGroup))
                                        <div style="margin-top: 4px;">
                                            <span class="badge badge-success">FF组 #{{ $row->FFGroup['index'] }}</span>
                                            <span class="badge badge-light">{{ $row->FFGroup['count'] }} 条</span>
                                        </div>
                                        <div style="word-break: break-all;">FF: {{ $formatShort($row->FF, 28) }}</div>
                                    @endif
                                    <div>channel: {{ $row->ParamChannel ?: '-' }}</div>
                                    <div>campaign: {{ $formatShort($row->ParamCampaign ?: '-', 24) }}</div>
                                    <div>adgroup: {{ $formatShort($row->ParamAdgroup ?: '-', 24) }}</div>
                                    <div>creative: {{ $formatShort($row->ParamCreative ?: '-', 24) }}</div>
                                    <div>utm_source: {{ $row->UtmSource ?: '-' }}</div>
                                    <div>utm_medium: {{ $row->UtmMedium ?: '-' }}</div>
                                    <div>utm_campaign: {{ $formatShort($row->UtmCampaign ?: '-', 24) }}</div>
                                    <div>pixel: {{ $row->Pixel ?: '-' }}</div>
                                    <div>FPID: {{ $row->FPID ?: '-' }}</div>
                                    <div>FF: {{ $row->FF ?: '-' }}</div>
                                    @if($paramDialogText !== '')
                                        <div style="margin-top: 6px;">
                                            <a href="javascript:void(0);" onclick='showTextDialog("投放参数详情 - UserID {{ $row->UserID }}", @json($paramDialogText))'>查看参数分类</a>
                                        </div>
                                    @endif
                                </td>
                                <td style="min-width: 220px;">
                                    <div>fbp: <span style="word-break: break-all;">{{ $formatShort($row->Fbp, 24) }}</span></div>
                                    <div>fbc: <span style="word-break: break-all;">{{ $formatShort($row->Fbc, 24) }}</span></div>
                                    @if(!empty($row->FbclidCookieCheck['cookie_fbclid']))
                                        <div>cookie_fbclid: <span style="word-break: break-all;">{{ $formatShort($row->FbclidCookieCheck['cookie_fbclid'], 24) }}</span></div>
                                    @endif
                                    <a href="javascript:void(0);" onclick='showTextDialog("Cookie详情 - UserID {{ $row->UserID }}", @json("fbp: " . ($row->Fbp ?: "-") . "\n\nfbc: " . ($row->Fbc ?: "-") . "\n\nUA: " . ($row->ClickUA ?: $row->GameUA ?: "-")))'>查看详情</a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="14" class="text-center">暂无数据</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                    {!! $list->links() !!}
                </div>
            </div>
        </div>
    </div>
    <script>
        function showTextDialog(title, text) {
            layer.open({
                type: 1,
                title: title,
                shadeClose: true,
                shade: 0.4,
                area: ['720px', '420px'],
                content: '<div style="padding:16px;max-height:360px;overflow-y:auto;white-space:pre-wrap;word-break:break-all;">' + $('<div>').text(text).html() + '</div>'
            });
        }

        var ipLookupCache = {};

        function lookupIpLocation(element, ip) {
            var $trigger = $(element);
            var $result = $trigger.siblings('.ip-location-result');
            if (!ip) {
                return;
            }

            if (ipLookupCache[ip]) {
                $result.text(ipLookupCache[ip]);
                return;
            }

            $result.text('查询中...');
            $.getJSON('https://ipapi.co/' + encodeURIComponent(ip) + '/json/', function (res) {
                var text = '-';
                if (res && !res.error) {
                    text = [res.country_name, res.region, res.city, res.org].filter(Boolean).join(' / ');
                }
                ipLookupCache[ip] = text;
                $result.text(text);
            }).fail(function () {
                $result.text('查询失败');
            });
        }
    </script>
@endsection
